In this assignment you are going to implement the following design using the int
ID: 3586506 • Letter: I
Question
In this assignment you are going to implement the following design using the interface files l twoD a and I threeD ava Shape + Color mColor + String mName Shape(Color, String) +getName) String +getColor() Color +setName(String) +setColor(Color) + print() I twoD I threeD + computeArea) +computePerimeter() +computeVolume() Cube Square Circle + float mSide + float mRadius Cube(Color, String, float) Square(Color, String, float) Circle(Color, String, float) 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 l 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.Explanation / Answer
I_twoD.java
public interface I_twoD {
float computeArea();
float computePerimeter();
}
_______________
I_threeD.java
public interface I_threeD {
float computeVolume();
}
_______________
Shape.java
import java.awt.Color;
public abstract class Shape {
public Color mColor;
public String mName;
public Shape(Color red, String mName) {
this.mColor = red;
this.mName = mName;
}
public Color getColor() {
return mColor;
}
public void setColor(Color mColor) {
this.mColor = mColor;
}
public String getName() {
return mName;
}
public void setName(String mName) {
this.mName = mName;
}
abstract public void print();
}
_________________
Circle.java
import java.awt.Color;
public class Circle extends Shape implements I_twoD {
public float mRadius;
/*
public Circle(Color mColor, String mName,float mRadius) {
super(mColor, mName);
this.mRadius=mRadius;
}
*/
public Circle(Color red, String mName, float mRadius) {
super(red, mName);
this.mRadius = mRadius;
}
@Override
public float computeArea() {
return Math.PI * mRadius * mRadius;
}
@Override
public float computePerimeter() {
return 2 * Math.PI * mRadius;
}
public float getRadius() {
return mRadius;
}
public void setRadius(float mRadius) {
this.mRadius = mRadius;
}
@Override
public void print() {
System.out.println("Circle (" + getName() + ") side " + getRadius() + " and color :" + getColor());
}
}
___________________
Square.java
import java.awt.Color;
public class Square extends Shape implements I_twoD {
public float mSide;
public Square(Color mColor, String mName, float mSide) {
super(mColor, mName);
this.mSide = mSide;
}
@Override
public float computeArea() {
return mSide * mSide;
}
@Override
public float computePerimeter() {
return 4 * mSide;
}
public float getSide() {
return mSide;
}
public void setSide(float mSide) {
this.mSide = mSide;
}
@Override
public void print() {
System.out.println("Square (" + getName() + ") side " + getSide() + " and color :" + getColor());
}
}
_________________
Cube.java
import java.awt.Color;
public class Cube extends Square implements I_threeD {
public Cube(Color mColor, String mName, float mSide) {
super(mColor, mName, mSide);
}
@Override
public float computeVolume() {
return mSide * mSide * mSide;
}
@Override
public void print() {
System.out.println("Cube (" + getName() + ") side " + getSide() + " and color :" + getColor());
}
}
__________________
Test.java
import java.awt.Color;
public class Test {
public static void main(String[] args) {
Circle circle = new Circle(Color.RED, "myCircle", 2. f);
circle.print();
Square square = new Square(Color.BLUE, "mySquare", 3.5 f);
square.print();
System.out.println("Area :" + square.computeArea());
System.out.println("Perimeter :" + square.computePerimeter());
Cube cube = new Cube(Color.CYAN, "myCube", 2.3 f);
cube.print();
System.out.printf("Volume :%.3f", cube.computeVolume());
}
}
__________________
Output:
Circle (myCircle) side 2.0 and color :java.awt.Color[r=255,g=0,b=0]
Square (mySquare) side 3.5 and color :java.awt.Color[r=0,g=0,b=255]
Area :12.25
Perimeter :14.0
Cube (myCube) side 2.3 and color :java.awt.Color[r=0,g=255,b=255]
Volume :12.167
_____________Could you rate me well.Plz .Thank You