I_twoD.java : /* This is an interface file to compute the area and the perimeter
ID: 3585129 • Letter: I
Question
I_twoD.java :
/*
This is an interface file to compute the area and the perimeter of a 2D
shape object.
*/
package assignment3;
public interface I_twoD {
float computeArea();
float computePerimeter();
}
I_threeD.java :
/*
This is an interface file to compute volume of a 3D shape object.
*/
package assignment3;
public interface I_threeD {
float computeVolume();
}
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
Note: As iam not able to write different program in different files i wrote all program in a single file and executed .Please note that all th java file should be saved in one folder so that there will be no need of using packages other wise use should import .
Another thing is when u spilt them into files please import import java.awt.*,import javax.swing.* in all the files becuase your using Color class
if you want compile the below program save it as Assigment3.java and compile it it will give out then u change it according to your requirement.
//Shape.java
import java.awt.*;
import javax.swing.*;
abstract class Shape
{
Color mColor;
String mName;
Shape(Color mColor,String mName)
{
this.mColor=mColor;
this.mName=mName;
}
Shape()
{
super();
}
abstract void setColor(Color mColor);
abstract void setName(String mName);
abstract String getName();
abstract Color getColor();
abstract void print();
}
interface I_threeD
{
public float computeVolume();
}
interface I_twoD
{
public float computeArea();
public float computePerimeter();
}
//Circle.java
class Circle extends Shape implements I_twoD
{
float mRadius;
Circle(Color mColor,String mName,float value)
{
setColor(mColor);
setName(mName);
this.mRadius=value;
}
public void setColor(Color mColor)
{
this.mColor=mColor;
}
public void setName(String mName)
{
this.mName=mName;
}
public String getName()
{
return mName;
}
public Color getColor()
{
return mColor;
}
public void print()
{
System.out.println("color is"+ getColor());
System.out.println("Name is "+ getName());
}
public float computeArea()
{
return 22*mRadius*mRadius;
}
public float computePerimeter()
{
return 2*22*mRadius;
}
}
//Square.java
class Square extends Shape implements I_twoD
{
float mside;
Square(Color mColor,String mName,float value)
{
setColor(mColor);
setName(mName);
this.mside=value;
}
Square()
{
super();
}
public void setColor(Color mColor)
{
this.mColor=mColor;
}
public void setName(String mName)
{
this.mName=mName;
}
public String getName()
{
return mName;
}
public Color getColor()
{
return mColor;
}
public void print()
{
System.out.println("color is"+ getColor());
System.out.println("Name is "+ getName());
}
public float computeArea()
{
return mside*mside;
}
public float computePerimeter()
{
return 4*mside;
}
}
//Cube.java
class Cube extends Square implements I_threeD
{
Square s;
Cube(Color mColor,String mName, float value)
{
s= new Square(mColor,mName,value);
}
public void print()
{
s.print();
}
public float computeVolume()
{
return s.mside*s.mside*s.mside;
}
}
//Assigment3.java
public class Assignment3
{
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());
}
}
output:
color isjava.awt.Color[r=255,g=0,b=0]
Name is myCircle
Area : 88.0
Perimeter : 88.0
color isjava.awt.Color[r=0,g=0,b=255]
Name is mySquare
Area : 12.25
Perimeter : 14.0
color isjava.awt.Color[r=0,g=255,b=255]
Name is myCube
Volume : 12.167
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.