I need help with this program please. Below is how I am supposed tosetup each cl
ID: 3614531 • Letter: I
Question
I need help with this program please. Below is how I am supposed tosetup each class in an inheritance hierarchy. Included is only asmall part of the assignment but if someone could help me with thisI should be able to do the rest of my assignment. Please help.class Point2d
A point2d represents points where z always equals 0. A point2d canchange the values
for x and y, but it still isn't allowed to change values for z. Allnumbers are double
precision.
class Shape2d
A shape2d is a special shape that represents a flat shape. It has acenter, which is a
point2d. Since a shape can be moved, you can change the shape2d'scenter whenever
you want. A shape2d also computes a default perimeter which alwaysreturns 0.
class Circle
A circle is a shape2d which always 1 side and is always named“circle”. It also has a
radius which can changed and which is used to compute its area andperimeter.
class TestShape
Create a test class TestShape. It should have a main() which doesthe following:
Create a circle at (10.1, 5.5) with a radius of 2.5. After creationchange the
circle's center to (0, 0.5).
For each shape2d, print its perimeter as well.
Here is my code so far:
I'm not sure what to do from there and this is only a small part ofmy assignment.
//class Circle
package shape;
public class Circle extends Shape2d
{
private double radius;
public Circle ()
{
System.out.println("test");
}
/**
* @return the radius
*/
public double getRadius()
{
return radius;
}
/**
* @param radius the radius to set
*/
public void setRadius(double radius)
{
this.radius =radius;
}
@Override
public String toString()
{
returnString.format("temp");
}
}
//class Shape2d
package shape;
import point.Point2d;
public class Shape2d extends Point2d
{
//emtpy
}
//class Point2d
package point;
public class Point2d
{
public Point2d()
{
//empty
}
}
//class TestShape
package shape;
public class TestShape
{
public static void main(String[] args)
{
Circle c = newCircle();
c.setRadius(2.5);
}
}
Explanation / Answer
1- Complete your Point2d class first. I don't think yourinstructor would want you to extend the Point class (especiallysince the Java Point class has its x and y fields as ints, and yourrubric specifies doubles). Write your instance variables first. Youwill have three--an x, y and z. You will have to write the code ina way that allows the x and y values to be changed. The z valuehowever should always be set to 0. Think about this in terms ofvisibility modifiers (public, protected, private) as well as themethods you write to access the variables. Again, make sure youdeclare x, y and z as doubles.
2-After your Point2d class is done, move to your Shape2dclass. I personally would not extend Point2d here. Declare yourinstance variables for this class. From your rubric, I gather therewill only be one--a Point2d object that will represent the centerof your shape. There will also be a method to change value of thePoint2d object. This should be straightforward towrite--accept parameters that will reflect the new center and usethe Point2d methods to change the values of x and y accordingly.Your rubric also says a perimeter method should be in thisclass.
3-Next, move on to your Circle class, which looks to be themost complete of all of your classes. Just follow your rubric--youneed a method that will compute the area using the radius. You willalso need to be able to compute the perimeter. There should be aperimeter method already in your Shape2d class. Circle shouldoverride the Shape2d perimeter method.
4-Lastly, you need to add some stuff to your TestShapeclass--which contains your main. In your main method, you doeverything the rubric asks except setting the center of the circleobject you are creating. Remember, if you followed my steps, youshould have a method in your Shape2d class that allows you to setthe center of the shape (by changing the values of the Point2dobject). Just call this method in your main...you can do this usingthe variable c--just like you called your setRadius method. c (andthe dot operator, of course) will access any method in the classCircle AND the class Shape2d (that's the beauty of inheritance andpolymorphism). To change the center again, just call the methodagain.
I hope this helps! Good luck!