Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

6. List outcomes of the adaptation process.

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago