Question
Lab 4 Due Note that all work that is turned in must: Be properly documented using javadoc standards. Contain explicit acknowledgment if there was any
Lab 4 Due
Note that all work that is turned in must:
-
Be properly documented using javadoc standards.
-
Contain explicit acknowledgment if there was any collaboration
Deliverables:
Upload your code (Circle, Point, Sphere, CircleDriver.java files) The Driver should include a comment at the bottom that shows the execution of the program
Part 1:
You will implement a complete class called Point that describes a point as it may be
used in a graphics application. A point consists of an x and y coordinate and a color.
Here are the specifications:
The class has three instance variables. x: an int representing the x coordinate. y: an int representing the y coordinate. color: a String that represents the color associated with the point.
There is also a static variable called countPoint. It should be incremented whenever you make a new Point
Methods you must implement: Constructor(s) - no argument and 3 argument Accessor and mutator methods (get/set methods) translate: This method takes two int parameters, dx and dy. It then increments the x coordinate by dx, and increments the y coordinate by dy.
toString: Write the toString method associated with this class. The returned String has to be in the following form: (x,y) : [x and y values here], color: [color value here] ''.
equals Compares one point to another - if the x, y and color are the same return true
Start a driver to test this. Test constructors, setters, getters, equals and toString . In your driver print out the number of points you created using the static variable
Lab Part 2: Implement a Circle class which extends the Point class you have defined in Part 1. The x and y coordinates inherited from the Point class represent the center point of the circle. In addition to the instance variables of Point, Circle has the following instance variable:
radius: an int representing the radius of a circle.
Methods you must implement: Constructor(s)using the super command, accessor and mutator methods (get/set methods) for the radius.
In addition, code these methods in Circle clas calculateArea: This method should return the area of the circle. In this method use Math.PI. This is a constant representing PI. It requires nothing to be imported. ( Math.PI * radius*radius)
scale: This method takes a single int parameter, scaleFactor. It then scales radius by scalefactor (the new radius value should be old radius times the scale factor.) merge: This method takes a Circle type parameter. The method will create and return a new Circle object whose radius is the sum of the radius of the calling object and the radius of the parameter. The color of the new Circle object will be the color of the parameter object. The x and y coordinates of the new Circle object will be the x and y coordinates of the parameter object. A reference to the new Circle object should be returned. The calling object and the parameters object should not be modified.
public Circle merge(Circle c)
when you call the method in the main
Circle returnedCircle = c1.merge(c2);
System.out.println(returnedCircle);
toString: Write the toString method associated with this class. The returned String has to be in the following form(use super.toString()):
(x,y) : [x and y values here], color: [color value here] '`radius: [radius value here]'.
equals Write an equals method for Circle . Circles are equals if x, y and radius are the same. Can use Point equals if you want
Part3:
Implement a class called Sphere that extends Circle
It adds no instance variables to Circle.
You can write constructors for Sphere by using the super keyword. There is no need for setters/getters/toString or equals in this class.
Write a method called calculateArea that will override the method it inherits from Circle.
To calculate the area of a sphere use the equation
4*Math.PI*radius*radius
Part 4:
Write a driver class to test your Point and Circle and Sphere classes.
Create these objects
-
p1 using Point no argument constructor. Then use setX, setY, setColor to set these values to whatever you want. Print this point using toString to the output file.
-
Create another point p2 that passing 2 ints read in from keyboard and a String color
-
Print out p1 and p2. Use the translate method on one of the points and print out the new x and y
-
Create a Circle object c1 that uses the no argument constructor for Circle. Then use SetX and SetY , setColor and setRadius to make complete circle. Use toString to write to output.
-
Create a Circle c2 that uses p2 and the third integer (r2) read in from the keyboard to create Circle. Print to output file.
-
Call equals method on circles
-
Create an array of Circles( SIZE=10). Initialize the array with a for loop - set x equal to the array index, y= array index +5, Color=BLACK, radius equals array index *10. See below
-
circleArray[i]= new Circle (i, i+5, BLACK, i*10);
-
-
Call toString to print out the circleArray to the output file.
-
Call scalefactor on the array of circles. Print out the changes .
-
Call merge on c1 and c2. Print out the new resulting Circle.
-
Create a Sphere object s1 and calculate the area and print out
Sample output
p1 (x,y) : [x and y values here], color: [color value here]
p2 (x,y) : [x and y values here], color: [color value here]
c1 :(x,y) [x and y values here], color: [color value here] radius: [radius value here],
c2 (x,y) [x and y values here], color: [color value here] radius: [radius value here],
The area of c1 is [area] + the area of c2 is [area]
The circles are not equal
CircleArray[1] (x,y) : [x and y values here], color: [color value here] radius: [radius value here],
CircleArray [2] (x,y) : [x and y values here], color: [color value here] radius: [radius value here],
etc
After scalefactor
CircleArray[1] (x,y) : [x and y values here], color: [color value here] radius: [radius value here],
CircleArray [2] , (x,y) : [x and y values here], color: [color value here] radius: [radius value here]
.. etc
After merge c1 and c2
radius:(x,y) : [x and y values here], color: [color value here] radius: [radius value here],
Sphere 1
s1 (x,y) : [x and y values here], color: [color value here] radius: [radius value here],
The area of s1 is [area of s1]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started