I need this program convert to interface method --------------------------------
ID: 3771020 • Letter: I
Question
I need this program convert to interface method
-------------------------------------------------------------------
package oop.assignment.pkg4;
import java.util.Scanner;
public class OOPAssignment4 {
public static void main(String[] args)
{
System.out.println(" ** Welcome to the Shapes calculation Menu ** ");
System.out.println(" Please choose one of the option below ");
System.out.println(" to get Area Surface and Volume of each shape ");
System.out.println(" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ");
Scanner input = new Scanner (System.in);
Shape [] shapes = new Shape[10];
shapes[0]= new Block(2.0,3.0,4.0);
shapes[1]= new Pyramid(2.0,3.0,4.0);
shapes[2]= new Sphere (3.0);
int numberOfShapes;
numberOfShapes = 0;
while (true)
{
System.out.println (" Enter 1 for Block ");
System.out.println (" Enter 2 for sphere ");
System.out.println (" Enter 3 for pyramid ");
System.out.println (" Enter 4 to display all data ");
System.out.println (" Enter 6 to Exit ");
System.out.println (" Enter : ");
int ch = input.nextInt();
switch (ch)
{
case 1:
double length;
double width;
double height;
double radius;
System.out.print ("Enter the length ");
length = input.nextDouble();
System.out.print ("Enter the width ");
width = input.nextDouble();
System.out.print ("Enter the height ");
height = input.nextDouble();
shapes[numberOfShapes] = new Block(length, width, height);
numberOfShapes++;
Block blockOne = new Block (length, width, height);
System.out.print("Here is your Area: ");
System.out.println (blockOne.getArea());
System.out.print("Here is your Volume: ");
System.out.println ( blockOne.getVolume());
System.out.println (" ");
System.out.println (" To countiue please choose other options ");
System.out.println (" ")
break;
case 2:
System.out.print ("Enter the radius ");
radius = input.nextDouble();
shapes[numberOfShapes] = new Sphere(radius);
numberOfShapes++;
Sphere sphereOne = new Sphere (radius);
System.out.print("Here is your Area: ");
System.out.println ( sphereOne.getArea());
System.out.print("Here is your Volume: ");
System.out.println ( sphereOne.getVolume());
System.out.println (" ");
System.out.println("------------------------------------------");
System.out.println (" Please choose other options ");
System.out.println (" "); break;
case 3:
System.out.print ("Enter the Base length ");
length = input.nextDouble();
System.out.print ("Enter the Base width ");
width = input.nextDouble();
System.out.print ("Enter the height ");
height = input.nextDouble();
shapes[numberOfShapes] = new Pyramid(length, width, height);
numberOfShapes++;
Pyramid pyramidOne = new Pyramid ( length, width, height);
System.out.print("Here is your Surface Area: ");
System.out.println ( pyramidOne.getArea());
System.out.print("Here is your Volume: ");
System.out.println ( pyramidOne.getVolume());
System.out.println (" ");
System.out.println (" Please choose other options ");
System.out.println (" ") break;
case 4:
System.out.println (" Here are all Data ");
System.out.println (" ------------------");
System.out.println (" ");
for ( int c = 0 ;c < numberOfShapes; c ++)
{
System.out.printf("Area of Shape %d: %f ", (c+1), shapes[c].getArea());
System.out.printf("Volume of shapes%d: %f ",(c+1), shapes[c].getVolume());
} case 5: break; default:
System.out.println( " It was our pleaure doing business with your ");
System.out.println( " Please come back again ");
return; } } } }
package oop.assignment.pkg4;
public class Block extends Shape
{
private double length =1.0;
private double width =1.0;
private double height =1.0;
// @Overide
public double Block()
{
return length * width* height;
}
// constructor
public Block ( double l, double w, double h)
{
super();
this.length = l;
this.width = w;
this.height = h;
}
// set length method
public void setLength (double l)
{
if (l>0)
{
this.length = l; //
}
else
{
this.length = 1.0;
}
}
public void setHeight (double h)
{
if (h > 0)
{
height = h;
}
else
{
height = 1.0;
}
}
public void setWidth (double w)
{
if (w > 0)
{
width = w;
}
else
{
width = 1.0;
}
}
@Override
public double getArea()
{
return 2*(this.length*this.width+ this.width*this.height+ this.length*this.height);
}
@Override
public double getVolume()
{
return (this.length*this.width* this.height);
}
public String getLength()
{
return String.format("%f", this.length);
}
public String getWidth()
{
return String.format("%f", this.width);
}
public String getHeight()
{
return String.format("%f", this.height);
}
@Override
public String toString()
{
return String.format("Length is %s. Width is %s. ", getLength(), getWidth(), getHeight());
} @Override
public void doubleMe()
{ length = 2.0;
width = 2.0;
height = 3.0; } }
package oop.assignment.pkg4;
public class Pyramid extends Shape
{
private double length =1.0;
private double width =1.0;
private double height =1.0;
// alternative constructor
public Pyramid()
{
// no need anything here
}
// constructor
public Pyramid ( double l, double w, double h)
{
super();
this.length = l;
this.width = w;
this.height = h;
}
// set length method
public void setLength (double l)
{
if (l>0)
{
this.length = l; //
}
else
{
this.length = 1.0;
}
}
public void setHeight (double h)
{
if (h > 0)
{
height = h;
}
else
{
height = 1.0;
}
}
public void setWidth (double w)
{
if (w > 0)
{
width = w;
}
else
{
width = 1.0;
}
}
@Override
public double getArea()
{
double sideLength = Math.sqrt(this.height * this.height+ this.length*this.width/4);
return 2 * this.width * sideLength;
}
@Override
public double getVolume()
{
return (this.length*this.width* this.height/3);
}
public String getLength()
{
return String.format("%f", this.length);
}
public String getWidth()
{
return String.format("%f", this.width);
}
public String getHeight()
{
return String.format("%f", this.height);
}
@Override
public String toString()
{
return String.format("Length is %s. Width is %s. ", getLength(), getWidth(), getHeight());
}
@Override
public void doubleMe()
{ length =4.0;
width = 4.0;
height = 5.0; } }
package oop.assignment.pkg4;
public class Sphere extends Shape
{
private double radius =1.0;
// alternative constructor
public Sphere()
{
// no need anything here
}
public Sphere ( double r)
{
super();
this.radius = r;
}
public void setRadius (double r)
{
if (r>0)
{
this.radius = r; //
}
else
{
this.radius = 1.0;
}
}
@Override
public double getArea()
{
return 4.0*(this.radius*this.radius*Math.PI);
}
public double getVolume()
{
return (4.0/3.0)*(this.radius*this.radius*Math.PI);
}
public String getRadius()
{
return String.format("%f", this.radius);
public String toString()
{
return String.format("radius is %s. ", getRadius());
}
public void doubleMe()
{ radius = 2.0; } }
package oop.assignment.pkg4;
// Super class ( abstract class with no property or method
public abstract class Shape
{ public abstract void doubleMe();
public abstract double getArea();
public abstract double getVolume(); }
Explanation / Answer
Here is the solution
----------------------------------------------------------OOPAssignment4.java-------------------------------------------------
package oop.assignment.pkg4;
import java.util.Scanner;
public class OOPAssignment4
{
public static void main(String[] args)
{
System.out.println(" ** Welcome to the Shapes calculation Menu ** ");
System.out.println(" Please choose one of the option below ");
System.out.println(" to get Area Surface and Volume of each shape ");
System.out.println(" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ");
Scanner input = new Scanner (System.in);
Shape [] shapes = new Shape[10];
shapes[0]= new Block(2.0,3.0,4.0);
shapes[1]= new Pyramid(2.0,3.0,4.0);
shapes[2]= new Sphere (3.0);
int numberOfShapes;
numberOfShapes = 0;
while (true)
{
System.out.println (" Enter 1 for Block ");
System.out.println (" Enter 2 for sphere ");
System.out.println (" Enter 3 for pyramid ");
System.out.println (" Enter 4 to display all data ");
System.out.println (" Enter 6 to Exit ");
System.out.println (" Enter : ");
int ch = input.nextInt();
switch (ch)
{
case 1:
double length;
double width;
double height;
double radius;
System.out.print ("Enter the length ");
length = input.nextDouble();
System.out.print ("Enter the width ");
width = input.nextDouble();
System.out.print ("Enter the height ");
height = input.nextDouble();
shapes[numberOfShapes] = new Block(length, width, height);
numberOfShapes++;
Block blockOne = new Block (length, width, height);
System.out.print("Here is your Area: ");
System.out.println (blockOne.getArea());
System.out.print("Here is your Volume: ");
System.out.println ( blockOne.getVolume());
System.out.println (" ");
System.out.println (" To countiue please choose other options ");
System.out.println (" ");
break;
case 2:
System.out.print ("Enter the radius ");
radius = input.nextDouble();
shapes[numberOfShapes] = new Sphere(radius);
numberOfShapes++;
Sphere sphereOne = new Sphere (radius);
System.out.print("Here is your Area: ");
System.out.println ( sphereOne.getArea());
System.out.print("Here is your Volume: ");
System.out.println ( sphereOne.getVolume());
System.out.println (" ");
System.out.println("------------------------------------------");
System.out.println (" Please choose other options ");
System.out.println (" "); break;
case 3:
System.out.print ("Enter the Base length ");
length = input.nextDouble();
System.out.print ("Enter the Base width ");
width = input.nextDouble();
System.out.print ("Enter the height ");
height = input.nextDouble();
shapes[numberOfShapes] = new Pyramid(length, width, height);
numberOfShapes++;
Pyramid pyramidOne = new Pyramid ( length, width, height);
System.out.print("Here is your Surface Area: ");
System.out.println ( pyramidOne.getArea());
System.out.print("Here is your Volume: ");
System.out.println ( pyramidOne.getVolume());
System.out.println (" ");
System.out.println (" Please choose other options ");
System.out.println (" ");
break;
case 4:
System.out.println (" Here are all Data ");
System.out.println (" ------------------");
System.out.println (" ");
for ( int c = 0 ;c < numberOfShapes; c ++)
{
System.out.printf("Area of Shape %d: %f ", (c+1), shapes[c].getArea());
System.out.printf("Volume of shapes%d: %f ",(c+1), shapes[c].getVolume());
} case 5: break; default:
System.out.println( " It was our pleaure doing business with your ");
System.out.println( " Please come back again ");
return;
}
}
}
}
---------------------------------------------------Pyramid.java--------------------------------------------------
package oop.assignment.pkg4;
public class Pyramid implements Shape
{
private double length =1.0;
private double width =1.0;
private double height =1.0;
// alternative constructor
public Pyramid()
{
// no need anything here
}
// constructor
public Pyramid ( double l, double w, double h)
{
super();
this.length = l;
this.width = w;
this.height = h;
}
// set length method
public void setLength (double l)
{
if (l>0)
{
this.length = l; //
}
else
{
this.length = 1.0;
}
}
public void setHeight (double h)
{
if (h > 0)
{
height = h;
}
else
{
height = 1.0;
}
}
public void setWidth (double w)
{
if (w > 0)
{
width = w;
}
else
{
width = 1.0;
}
}
@Override
public double getArea()
{
double sideLength = Math.sqrt(this.height * this.height+ this.length*this.width/4);
return 2 * this.width * sideLength;
}
@Override
public double getVolume()
{
return (this.length*this.width* this.height/3);
}
public String getLength()
{
return String.format("%f", this.length);
}
public String getWidth()
{
return String.format("%f", this.width);
}
public String getHeight()
{
return String.format("%f", this.height);
}
@Override
public String toString()
{
return String.format("Length is %s. Width is %s. ", getLength(), getWidth(), getHeight());
}
@Override
public void doubleMe()
{ length =4.0;
width = 4.0;
height = 5.0; } }
-----------------------------------------------------Sphere.java-----------------------------------------------------------
package oop.assignment.pkg4;
public class Sphere implements Shape
{
private double radius =1.0;
// alternative constructor
public Sphere()
{
// no need anything here
}
public Sphere ( double r)
{
super();
this.radius = r;
}
public void setRadius (double r)
{
if (r>0)
{
this.radius = r; //
}
else
{
this.radius = 1.0;
}
}
@Override
public double getArea()
{
return 4.0*(this.radius*this.radius*Math.PI);
}
@Override
public double getVolume()
{
return (4.0/3.0)*(this.radius*this.radius*Math.PI);
}
public String getRadius()
{
return String.format("%f", this.radius);
}
@Override
public String toString()
{
return String.format("radius is %s. ", getRadius());
}
@Override
public void doubleMe()
{
radius = 2.0;
}
}
--------------------------------------------------------Block.java-------------------------------------------------
package oop.assignment.pkg4;
public class Block implements Shape
{
private double length =1.0;
private double width =1.0;
private double height =1.0;
// @Overide
public double Block()
{
return length * width* height;
}
// constructor
public Block ( double l, double w, double h)
{
super();
this.length = l;
this.width = w;
this.height = h;
}
// set length method
public void setLength (double l)
{
if (l>0)
{
this.length = l; //
}
else
{
this.length = 1.0;
}
}
public void setHeight (double h)
{
if (h > 0)
{
height = h;
}
else
{
height = 1.0;
}
}
public void setWidth (double w)
{
if (w > 0)
{
width = w;
}
else
{
width = 1.0;
}
}
@Override
public double getArea()
{
return 2*(this.length*this.width+ this.width*this.height+ this.length*this.height);
}
@Override
public double getVolume()
{
return (this.length*this.width* this.height);
}
public String getLength()
{
return String.format("%f", this.length);
}
public String getWidth()
{
return String.format("%f", this.width);
}
public String getHeight()
{
return String.format("%f", this.height);
}
@Override
public String toString()
{
return String.format("Length is %s. Width is %s. ", getLength(), getWidth(), getHeight());
} @Override
public void doubleMe()
{ length = 2.0;
width = 2.0;
height = 3.0; } }
---------------------------------------------Shape.java-----Interface---------------------------------
package oop.assignment.pkg4;
public interface Shape
{
public abstract void doubleMe();
public abstract double getArea();
public abstract double getVolume();
}
-----------------------------------------------------------Program Output---------------------------------
run:
** Welcome to the Shapes calculation Menu **
Please choose one of the option below
to get Area Surface and Volume of each shape
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Enter 1 for Block
Enter 2 for sphere
Enter 3 for pyramid
Enter 4 to display all data
Enter 6 to Exit
Enter :
1
Enter the length 10
Enter the width 20
Enter the height 30
Here is your Area: 2200.0
Here is your Volume: 6000.0
To countiue please choose other options
Enter 1 for Block
Enter 2 for sphere
Enter 3 for pyramid
Enter 4 to display all data
Enter 6 to Exit
Enter :
4
Here are all Data
------------------
Area of Shape 1: 2200.000000
Volume of shapes1: 6000.000000
Enter 1 for Block
Enter 2 for sphere
Enter 3 for pyramid
Enter 4 to display all data
Enter 6 to Exit
Enter :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.