Question

Java code for the following inheritance hierarchy figure..

1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point.

Provide constructor for initialising two instance variables.

Provide set and get methods for each instance variable,

Provide toString method to return formatted string for a point coordinates.

2. Create class Circle, its inheritance from Point.

Provide a integer private radius instance variable.

Provide constructor to initialise the center coordinates and radius for a circle,.In the constructor it must call its superclass constructor by using super(x,y);

Provide set and get for the radius instance variable.

Provide two methods for calculating its area and perimeter.

Provide a toString method to return a formatted string for circle features. In the process, it must call its superclass toString method by using super.toString().

3. Similar to the above class Circle definition, Design Rectangle class.

4. Similar to the above class Rectangle definition, Design Cube class,

It has a hight instance variable.

Calculate the surface area and volume for a Cube.

5. Design a test-class to test the above four classes functionality

It creates four objects for these classes and calls the corresponding methods to test complete functionality for each class.

y for each class for each class. Point Rectangle Circle Cube Figure: Shape inheritance hierarchy


0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Point
{
private int x;
private int y;
public Point()
{
this.x = 0;
this.y = 0;
}
public Point(int x,int y)// constructor
{
this.x = x;
this.y = y;
}
// set and get methods
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public String toString()
{
return "Point("+getX() +","+getY()+")";
}
};
class Circle extends Point
{
  
private int radius;

public Circle()
{
super(0,0);
radius = 0;
  
}
public Circle(int x,int y,int radius)// constructor
{
super(x,y);
this.radius = radius ;
}

// set and get methods
public int getRadius()
{
return radius;
}
  
  
public double getArea() //compute the area of the circle
{
double area;
area = 3.14*radius*radius;
return area;
}
public double getPerimeter() //compute the perimeter of circle
{
return 2*3.14*radius;
}
public String toString()
{
return "Circle : Center :"+ getX() +","+ getY() +" radius :"+getRadius() ;
}
}
  
  
class Rectangle extends Point
{

private int length,width;

public Rectangle()
{
super(0,0);
length = 0;
width = 0;
}
public Rectangle(int x,int y,int length,int width)
{
super(x,y);
this.length = length;
this.width = width;
}
public int getLength()
{
return length;
}
public int getWidth()
{
   return width;
}
  
public void setLength(int length)
{
this.length = length;
}
public double getArea() //compute the area of the square.
{
double area;
area = length * width;
return area;
}

public String toString()
{
return "Rectangle : Top left Point :"+ getX() +","+ getY() +" length :"+getLength() +" width : "+getWidth();
}
}
  
class Cube extends Point
   {
private Point p;
private int depth;

public Cube()
{
super(0,0);
depth = 0;
  
}
public Cube(int x,int y,int depth)// constructor
{
super(x,y);
this.depth = depth;
}

// set and get methods
public int getDepth()
{
return depth;
}
  
public Point getPoint()
{
return this.p;
}
public double getArea() //compute the area of the cube
{
double area;
area = 6*getDepth()*getDepth();
return area;
}
public double getVolume() //compute the volume of the cube
{
double vol;
vol = getDepth() * getDepth() * getDepth();
return vol;
}
public String toString()
{
return "Cube : Top left Point :"+ getX() +","+ getY() +" depth :"+getDepth() ;
}
}

class GeometricTest
{
public static void main (String[] args)
{
  
  
Circle circle = new Circle(3,-1,6);
System.out.println(circle.toString());
System.out.println("Area of Circle = "+circle.getArea());
  

Rectangle r = new Rectangle(1,2,4,5);
System.out.println(r.toString());
System.out.println("Area of Rectangle = "+r.getArea());

  
Cube c = new Cube(4,5,2);

System.out.println(c.toString());
System.out.println("Area of cube = "+c.getArea());
System.out.println("Volume of cube = "+c.getVolume());

  
  
}

}

Output:

Success #stdin #stdout 0.05s 2184192KB

Circle : Center :3,-1 radius :6
Area of Circle = 113.03999999999999
Rectangle : Top left Point :1,2 length :4 width : 5
Area of Rectangle = 20.0
Cube : Top left Point :4,5  depth :2
Area of cube = 24.0
Volume of cube = 8.0

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

  • JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use...

    JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • IN JAVA Design and code a class hierarchy that demonstrates your understanding of inheritance and polymorphism....

    IN JAVA Design and code a class hierarchy that demonstrates your understanding of inheritance and polymorphism. Your hierarchy should contain at least 5 classes and one driver program that instantiates each type of object and runs multiple methods on those objects. Inheritance and Polymorphism must be apparent in the project. Please keep in mind that polymorphism was in chapter 37, so you will need to have read both chapters to understand what goes into this project. Bonus if you add...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • 6. define a C++ class having the following members: (1) 2 instance methods  the prototype...

    6. define a C++ class having the following members: (1) 2 instance methods  the prototype of the first method is double funA(int r). The parameter r represents the radius of a circle. The funA will calculate the area of a circle and the value of its radius is from the parameter r. It will return the area of the circle.  the prototype of the second method is double funB(int w, int h). The parameters w and h represent...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT