I need this JAVA program commented on for my research please. The comments shoul
ID: 3589977 • Letter: I
Question
I need this JAVA program commented on for my research please. The comments should give me a better understanding of the source code. Please help me better understand my studies.
Shape.java
------------
public interface Shape {
public abstract double getArea();
}
TwoDimensionalShape.java
--------------------------
interface TwoDimensionalShape extends Shape {
}
ThreeDimensionalShape.java
---------------------------
public interface ThreeDimensionalShape extends Shape{
public abstract double getVolume();
}
Circle.java
------------
public class Circle implements TwoDimensionalShape {
public Circle(int radius) {
super();
this.radius = radius;
}
private int radius;
public void setRadius(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
@Override
public double getArea() {
return Math.PI*radius*radius;
}
}
Square.java
-----------
public class Square implements TwoDimensionalShape{
public Square(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public double getArea() {
return Math.pow(size,2);
}
}
Triangle.java
--------------
public class Triangle implements TwoDimensionalShape{
public Triangle(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public double getArea() {
return (Math.sqrt(3)*Math.pow(size, 2))/4;
}
}
Cube.java
---------
public class Cube implements ThreeDimensionalShape{
public Cube(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public double getArea() {
return 6*Math.pow(size, 2);
}
@Override
public double getVolume() {
return Math.pow(size, 3);
}
}
Sphere.java
------------
public class Sphere implements ThreeDimensionalShape{
public Sphere(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public double getArea() {
return 4*Math.PI*Math.pow(size, 2);
}
@Override
public double getVolume() {
return 4/3*Math.PI*Math.pow(size, 3);
}
}
Tetrahedron.java
-----------------
public class Tetrahedron implements ThreeDimensionalShape{
public Tetrahedron(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public double getArea() {
return Math.sqrt(3)*Math.pow(size, 2);
}
@Override
public double getVolume() {
return Math.pow(size, 3)/(6*Math.sqrt(2));
}
}
TestApp.java
------------
public class TestApp {
public static void main(String[] args) {
Shape[] shapeArr=new Shape[6];
shapeArr[0]=new Circle(1);
shapeArr[1]=new Square(2);
shapeArr[2]=new Triangle(3);
shapeArr[3]=new Sphere(4);
shapeArr[4]=new Cube(5);
shapeArr[5]=new Tetrahedron(6);
print(shapeArr);
}
private static void print(Shape[] shapeArr) {
System.out.println("Shape Array:");
System.out.println("-----------");
for (int i = 0; i < shapeArr.length; i++) {
Shape r=shapeArr[i];
if(r instanceof Circle){
System.out.println("Circle Radius:::"+((Circle)r).getRadius());
System.out.println("Circle Area:::"+((Circle)r).getArea());
}else if(r instanceof Square){
System.out.println("Square Side:::"+((Square)r).getSize());
System.out.println("Square Area:::"+((Square)r).getArea());
}else if(r instanceof Triangle){
System.out.println("Traingle Side:::"+((Triangle)r).getSize());
System.out.println("Traingle Area:::"+((Triangle)r).getArea());
}else if(r instanceof Sphere){
System.out.println("Sphere Side:::"+((Sphere)r).getSize());
System.out.println("Sphere Area:::"+((Sphere)r).getArea());
System.out.println("Sphere Volume:::"+((Sphere)r).getVolume());
}else if(r instanceof Cube){
System.out.println("Cube Side:::"+((Cube)r).getSize());
System.out.println("Cube Area:::"+((Cube)r).getArea());
System.out.println("Cube Volume:::"+((Cube)r).getVolume());
}else if(r instanceof Tetrahedron){
System.out.println("Tetrahedron Side:::"+((Tetrahedron)r).getSize());
System.out.println("Tetrahedron Area:::"+((Tetrahedron)r).getArea());
System.out.println("Tetrahedron Volume:::"+((Tetrahedron)r).getVolume());
}
System.out.println();
}
}
}
Explanation / Answer
/*
Shape is a base interface which contains a method to get the area..
The method is declared as abstract so that whiever concrete class implements
the interface, will define it in its own way
*/
Shape.java
------------
public interface Shape {
public abstract double getArea();
}
/*
TwoDimensionalShape is also an interface which is child interface from Shape..
It means, it contains methods from Shape interface, as well as it can define its own also.
*/
TwoDimensionalShape.java
--------------------------
interface TwoDimensionalShape extends Shape {
}
/*
This also is extended from Shape interface and it defines one method for getting
the volume also.. So 3-D shapes have volume as well as area both
*/
ThreeDimensionalShape.java
---------------------------
public interface ThreeDimensionalShape extends Shape{
public abstract double getVolume();
}
/*
Circle is a 2-D object, hence it implements the TwoDimensionalShape interface..
It also need to define the getArea method as suitable
*/
Circle.java
------------
public class Circle implements TwoDimensionalShape {
public Circle(int radius) {
super();
this.radius = radius;
}
// instance member of circle class
private int radius;
// setter
public void setRadius(int radius) {
this.radius = radius;
}
// getter
public int getRadius() {
return radius;
}
// method that has been implemented for base interface
@Override
public double getArea() {
return Math.PI*radius*radius;
}
}
/*
This class also implements TwoDimensionalShape class and defines its own
method for getting area.. volumes are not applicable for TwoDimensionalShape
interface objects.
*/
Square.java
-----------
public class Square implements TwoDimensionalShape{
public Square(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
// implement base interface method
@Override
public double getArea() {
return Math.pow(size,2);
}
}
/*
Another 2d class
*/
Triangle.java
--------------
public class Triangle implements TwoDimensionalShape{
public Triangle(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
@Override
public double getArea() {
return (Math.sqrt(3)*Math.pow(size, 2))/4;
}
}
/*
This is a 3-D object, hence it need to extend ThreeDimensionalShape interface
and need to define both getArea as well as getVolume methods
*/
Cube.java
---------
public class Cube implements ThreeDimensionalShape{
public Cube(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
// implements method from Shape interface
@Override
public double getArea() {
return 6*Math.pow(size, 2);
}
// implements method from ThreeDimensionalShape interface
@Override
public double getVolume() {
return Math.pow(size, 3);
}
}
/*
ThreeDimensionalShape implemented class
*/
Sphere.java
------------
public class Sphere implements ThreeDimensionalShape{
public Sphere(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
// overrides both getArea and getVolume methods
@Override
public double getArea() {
return 4*Math.PI*Math.pow(size, 2);
}
@Override
public double getVolume() {
return 4/3*Math.PI*Math.pow(size, 3);
}
}
/*
ThreeDimensionalShape implemented class
*/
Tetrahedron.java
-----------------
public class Tetrahedron implements ThreeDimensionalShape{
public Tetrahedron(int size) {
super();
this.size = size;
}
private int size;
public void setSize(int size) {
this.size = size;
}
public int getSize() {
return size;
}
// overrides both getArea and getVolume methods
@Override
public double getArea() {
return Math.sqrt(3)*Math.pow(size, 2);
}
@Override
public double getVolume() {
return Math.pow(size, 3)/(6*Math.sqrt(2));
}
}
/*
below class is created for testing the above classes and their hierarichy
In this class, there is a static void main method, from which the compiler
starts the execution
*/
TestApp.java
------------
public class TestApp {
public static void main(String[] args) {
// Create 6 different shapes.. Note we are referencing every kind of shape in a shape object only
// We are not creating TwoDimensionalShape and ThreeDimensionalShape seperately..
// because all shapes are basically of type Shape only.
Shape[] shapeArr=new Shape[6];
shapeArr[0]=new Circle(1);
shapeArr[1]=new Square(2);
shapeArr[2]=new Triangle(3);
shapeArr[3]=new Sphere(4);
shapeArr[4]=new Cube(5);
shapeArr[5]=new Tetrahedron(6);
print(shapeArr);
}
// print shapes
private static void print(Shape[] shapeArr) {
System.out.println("Shape Array:");
System.out.println("-----------");
for (int i = 0; i < shapeArr.length; i++) {
Shape r=shapeArr[i];
// if the current shape is of type circle
if(r instanceof Circle){
// ((Circle)r) typecasts the shape object into a circle object
// once typecasted, we can perform circle class functions on
// a shape object
System.out.println("Circle Radius:::"+((Circle)r).getRadius());
System.out.println("Circle Area:::"+((Circle)r).getArea());
} else if(r instanceof Square){
// if current shape is of square, we need to print the size and area
System.out.println("Square Side:::"+((Square)r).getSize());
System.out.println("Square Area:::"+((Square)r).getArea());
}else if(r instanceof Triangle){
System.out.println("Traingle Side:::"+((Triangle)r).getSize());
System.out.println("Traingle Area:::"+((Triangle)r).getArea());
}else if(r instanceof Sphere){
System.out.println("Sphere Side:::"+((Sphere)r).getSize());
System.out.println("Sphere Area:::"+((Sphere)r).getArea());
System.out.println("Sphere Volume:::"+((Sphere)r).getVolume());
}else if(r instanceof Cube){
System.out.println("Cube Side:::"+((Cube)r).getSize());
System.out.println("Cube Area:::"+((Cube)r).getArea());
System.out.println("Cube Volume:::"+((Cube)r).getVolume());
}else if(r instanceof Tetrahedron){
// for tetrahedron, the size, area and volume, all are applicable
System.out.println("Tetrahedron Side:::"+((Tetrahedron)r).getSize());
System.out.println("Tetrahedron Area:::"+((Tetrahedron)r).getArea());
System.out.println("Tetrahedron Volume:::"+((Tetrahedron)r).getVolume());
}
System.out.println();
}
}
}
============
Please find the comments attached in the code itself.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.