Starting from:

$30

CSCI301-Lab 9 Classes and Methods Solved

This lab introduces you to fundamental concepts of Object Oriented Programming (OOP), arguably the dominant programming paradigm in use today. In the paradigm, a program consists of component parts (objects) that are independent of each other and that interact in order to achieve a desired result. At a very basic level, an object-oriented program consists of the following types of things. 

               Classes:      The template or blueprint from which objects are created. 

Objects:  The instantiation of a class. Once a class (the template) has been defined, many instances of it can be created.  

Methods:  The named procedures or functions defined for an object. A method accepts input arguments and contains statements that are executed when the method is invoked. Some methods return values. 

Instance 

Variables: Variables that are initialized when an object is created. Each instance of a class can have distinct values for these variables.

 

In this lab, you will practice working with these fundamentals. A skeleton of code has been written for you defining the basic class and method structure that you are to use. You will then add code to the existing skeleton methods so that the classes function as intended. You are then to use a testing program illustrate how the component parts of what you have created interact. 

Lab Objectives
By the end of the lab, you should be able to create classes utilizing:

•        access modifiers;

•        instance variables (also called fields);

•        methods which return values and void methods (which do not return any value);

•        methods calling other methods;

•        accessor and mutator methods; • The equals() method.

 

Instructions
 

The file Circle.java contains a partial definition for a class representing a circle. Save it to your computer and study it to see what methods it contains. Then complete the Circle class as described below. Note that you won't be able to test your methods until you complete the test client class called CircleTester.java.

 

1.      You must include a comment stating your name, the date, program purpose, and containing a statement of academic honesty.  When writing, you must also abide by the Java formatting and naming conventions.

2.      Complete the Circle class following the instructions detailed below:

a.      Declare four private instance variables: name, x, y and radius. The instance variables x and y represent the coordinates of the center of the circle. Note that you should not declare any other instance variables other than these. 

b.     Fill in the code for method toString, which should return a “string” containing values for the name of the circle, the center of the circle, x and y, and the radius of the circle. Note:

this method does not print the string – it returns the string.  The returned string value should be formatted as:

name: name center: (x,y) radius: r

c.      Fill in the code for the accessor (getter) methods: getName, getX, getY and getRadius properly. Note that accessors are also called “getter” methods. 

d.     Fill in the code for the mutator (setter) methods: setName, setX and setY properly. Mutators are also called “setter” methods. 

e.      Fill in the code for the mutator(setter) method:  setRadius properly. Note that:

i.       If the new radius value passed as a parameter to the method is not valid, than the method should leave the original radius unchanged.

ii.     A radius is valid only when it is greater than or equal to 0. 

f.       Fill in the code for the method area that returns the area of the circle, computed by 

PI * radius2 //Use the value of PI provided by the constant Math.PI.

g.      Fill in the code of the method perimeter that returns the perimeter of the circle computed by

 2 * PI * radius
h.     Fill in the code of the method diameter that returns the diameter of the circle.

i.       Fill in the code of the method isUnitCircle that returns true if the radius of the circle is 1 and is centered at the origin, i.e., the coordinates of the circle’s center are (0, 0). Otherwise, this method returns false.  Note: since you’re comparing doubles, you should not use ==.  Remember how to properly compare doubles from lecture. 

3.      The CircleTester.java file contains a driver program to try and test out your Circle class.

Using the comments in the CircleTester.java file, finish the file so that it does the following:

a.      Display the name, center and radius of circle1.

b.     Set the radius of circle2 to 5.3.

c.      Display the name, center and radius of circle2.

d.     Display the diameter, area and perimeter of circle1.

e.      Display the diameter, area and perimeter of circle2.

f.       Display a message that indicates whether or not circle1 is a unit circle.

g.      Display a message that indicates whether or not circle2 is a unit circle.

4.      Compile and test your Circle and CircleTester files, and verify your code is working properly. Then, set breakpoints in your methods, and the first line of the CircleTester class, and observe the flow of control within an Object-Oriented program.

5.      Add to your Circle class the following methods:

a. public boolean equals(Circle anotherCircle)  
This method returns true when the radius and centers of both circles are the same; otherwise, it returns false. This method can be implemented in one line.  Remember not to compare doubles using ==.

 

b. public double distance(Circle anotherCircle) 
This method returns the distance between the centers of the circle executing the method and anotherCircle. Let (x, y) and (xa, ya) be the centers of the circle executing the method and anotherCircle respectively, the distance between their centers is computed by 



(𝑥𝑥−𝑥𝑥𝑎𝑎)2+(𝑦𝑦−𝑦𝑦𝑎𝑎)2

The Math class contains methods for computing both square roots and powers.  Below are the definitions of the methods. 

//Returns a double value containing the square root of a double number. double sqrt(double number) 

               

//Returns a double value of the first argument raised to the power of the second argument.

double pow(double base, double exponent)

 

c. public boolean isSmaller(Circle anotherCircle)
 The method isSmaller returns true when the circle executing the method (calling object) is  smaller than the parameter (anotherCircle).  Otherwise, it returns false.  To see which is larger,  use the diameter of each circle.  You can use the diameter method written earlier to get this  value from each object.  Do this instead of using radius to practice calling methods within  methods.

d. public int compareTo(Circle anotherCircle)
 Works similar to the compareTo method in the String class. If the calling object is larger, the  method returns a positive 1.  If the calling object is smaller than anotherCircle, it returns -1.   Otherwise, it returns 0.  You may want to use the isSmaller method in your implementation.

e. public boolean intersects(Circle anotherCircle) 
The method intersects returns true when the circle executing the method and anotherCircle have an intersecting area (one or more points enclosed by both circles); otherwise, it returns false. 

Two circles intersect (by this lab’s definition) if the distance between the centers of the two circles is less than the sum of their radius. Your method must call the method distance to obtain the distance between the two circles.

6.      After you’ve added these methods, add at least three different tests for each method to your CircleTester class, and verify you have successfully written the above methods. Remember, your methods must work properly with any input we can provide, so test rigorously.  For each test, you should output pass/fail instead of dumping values to the screen.  An example can be found in the test file.

Check your method signatures using CircleMethodCheck.java (found on the course website).  If you pass the tests, it means you are using the proper method signatures.  Passing these tests does not guarantee that you have implemented the methods correctly but it helps avoid you missing points due to invalid headers (signatures).  To use this class, simply put it into the src folder of your project and then run CircleMethodCheck.java

More products