Shape class is the super abstract class in the hierarchy. And we have 2 interfac
ID: 3585342 • Letter: S
Question
Shape class is the super abstract class in the hierarchy. And we have 2 interfaces, I_twoD and I_threeD. Cube class is a 3-dimensional object, hence it implements the I_threeD interface, in contrast Square and Circle classes are 2-dimensional objects, and they implement the I_twoD interface.
In addition, Cube is an extension of the Square class.
As an overall, you are going to implement 4 classes Shape, Cube, Square and Circle in 4 files; Shape.java, Cube.java, Square.java, and Circle.java respectively and also main test driver class in the file Assignment3.java.
In the print function, please print the type, the name, and the quantitative parameter of the shape (either radius or side).
Please use the following main function in your implementation:
public static void main(String[] args) {
// TODO code application logic here
Circle circle = new Circle(Color.RED, "myCircle", 2.f);
circle.print();
System.out.println("Area : "+circle.computeArea());
System.out.println("Perimeter : "+circle.computePerimeter());
Square square = new Square(Color.BLUE, "mySquare", 3.5f);
square.print();
System.out.println("Area : "+square.computeArea());
System.out.println("Perimeter : "+square.computePerimeter());
Cube cube = new Cube(Color.CYAN, "myCube", 2.3f);
cube.print();
System.out.println("Volume : "+cube.computeVolume());
}
Explanation / Answer
Assignment3.java class is as below
import java.awt.Color;
public class Assignment3
{
public static void main(String[] args)
{
Circle circle = new Circle(Color.RED, "myCircle", 2.f);
circle.print();
System.out.println("Area : "+circle.computeArea());
System.out.println("Perimeter : "+circle.computePerimeter());
Square square = new Square(Color.BLUE, "mySquare", 3.5f);
square.print();
System.out.println("Area : "+square.computeArea());
System.out.println("Perimeter : "+square.computePerimeter());
Cube cube = new Cube(Color.CYAN, "myCube", 2.3f);
cube.print();
System.out.println("Volume : "+cube.computeVolume());
}
}
Shape.java class
import java.awt.Color;
abstract class Shape
{
public Color mColor;
public String mName;
Shape(Color mColor, String mName)
{
this.mColor = mColor;
this.mName = mName;
}
public String getName()
{
return mName;
}
public Color getColor()
{
return mColor;
}
public void setName(String name)
{
mName = name;
}
public void setColor(Color color)
{
mColor = color;
}
public abstract void print();
}
Cube.java class
import java.awt.Color;
public class Cube extends Square
{
Cube(Color c, String n, float s)
{
super(c,n,s);
}
public float computeVolume()
{
return mSide*mSide*mSide;
}
public void print()
{
System.out.println("Type of the Shape is Cube");
//System.out.println("Name of the Shape is: "+getName());
System.out.println("Name of the Shape: "+ mName);
System.out.println("The parameter of the Shape is side ");
}
}
Square.java class
import java.awt.Color;
public class Square extends Shape
{
public float mSide;
public Square(Color c, String n, float s)
{
super(c,n);
mSide = s;
}
public float computeArea()
{
return mSide*mSide;
}
public float computePerimeter()
{
return 4 * mSide;
}
public void print()
{
System.out.println("Type of the Shape is Square");
//System.out.println("Name of the Shape is: "+getName());
System.out.println("Name of the Shape: "+ mName);
System.out.println("The parameter of the Shape is side ");
}
}
Circle.java class
import java.awt.Color;
import java.lang.Math;
public class Circle extends Shape
{
public float mRadius;
public Circle(Color c, String n, float r)
{
super(c,n);
mRadius = r;
}
public double computeArea()
{
return Math.PI*mRadius*mRadius;
}
public double computePerimeter()
{
return 2*Math.PI*mRadius;
}
public void print()
{
System.out.println("Type of the Shape is Circle");
//System.out.println("Name of the Shape is: "+getName());
System.out.println("Name of the Shape: "+ mName);
System.out.println("The parameter of the Shape is radius ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.