Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code should be written in Java Write a program called InheritanceTest1.java

ID: 3756654 • Letter: T

Question

This code should be written in Java

Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private data of Point should be the x-y coordinates, the private data of Square should be the sideLength, and the private data of Cube should be depth. Provide applicable accessor methods, mutator methods, toString() methods, area() method, and volume() method to all classes. Write a program that instantiates objects of your classes, tests all instance methods, and outputs each object’s area and volume when appropriate.

Please show code runs.

Explanation / Answer

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 Cube extends Point
{
    private Point p;
    private double depth;
   

    public Cube()
    {
     p.setX(0);
     p.setY(0);
     depth = 0;
    
    }
    public Cube(Point p,double depth)// constructor
    {
        this.p = p;
        this.depth = depth;
    }
   
    // set and get methods
    public double 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 :"+ getPoint() +"depth :"+getDepth() ;
    }
    }

  
  

    class Square extends Point
    {
    private Point p;
    private double sideLength;


    public Square()
    {
     p.setX(0);
     p.setY(0);
     sideLength = 0;

    }
    public Square(Point p,double sideLength)
    {
        this.p = p;
        this.sideLength = sideLength;
    }

    public double getSideLength()
    {

       return sideLength;
    }
    public Point getPoint()
    {
        return this.p;
    }
    public void setLength(double sideLength)
    {
        this.sideLength = sideLength;
    }
    public double getArea()   //compute the area of the square.
    {
        double area;
        area = getSideLength() * getSideLength();
        return area;
    }
   
    public String toString()
    {
        return "Square : Top left Point :"+ getPoint() +" length :"+getSideLength() ;
    }
    }


class GeometricTest
{
    public static void main (String[] args)
    {
      
        Point ps = new Point(3,-1);
        Square s = new Square(ps,4.0);

        System.out.println(s.toString());
        System.out.println("Area of Square = "+s.getArea());


        Point pc = new Point(5,8);
        Cube c = new Cube(ps,2.0);

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


        Point p1 = c.getPoint();
        Point p2 = s.getPoint();

      
    }


}

Output:

Square : Top left Point :Point(3,-1) length :4.0
Area of Square = 16.0
Cube : Top left Point :Point(3,-1)depth :2.0
Area of cube = 24.0
Volume of cube = 8.0

Do ask if any doubt. Please upvote.