I need this interface program changed to Graphical user ( GUI ) ----------------
ID: 3771761 • Letter: I
Question
I need this interface program changed to Graphical user ( GUI )
--------------------------------------------------------------------------------
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();
}
Explanation / Answer
Answer:
note: A class is added to make it as GUI
controlPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Scanner;
public class ControlPanel extends JPanel
{
private String[] shapeNames1 = {"None", "Sphere",
"Block", "Pyramid"};
private JLabel createLabel1;
private JComboBox createBox1;
private JButton computeButton1;
private JButton quitButton1;
private ArrayList shapes1;
public ControlPanel()
{
shapes1 = new ArrayList();
createLabel1 = new JLabel("Create Shape");
add(createLabel1);
createBox1 = new JComboBox(shapeNames1);
createBox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(createBox1.getSelectedItem().equals(shapeNames1[1
]))
{
String str1 =JOptionPane.showInputDialog("Enter
");
double tmp1 = Double.parseDouble(str1);
shapes.add(new Sphere(tmp1));
}
else if (createBox1.getSelectedItem().equals(shapeNames1[2]))
{
String str1 = JOptionPane.showInputDialog("Enter ");
Scanner stok1 = new Scanner(str1);
shapes1.add(new Block(stok1.nextDouble(),stok1.nextDouble(),stok1.nextDouble()));
}
else if (createBox1.getSelectedItem().equals(shapeNames1[3]))
{
String str1 = JOptionPane.showInputDialog("Enter");
Scanner stok1 = new Scanner(str1);
shapes1.add(new Block(stok1.nextDouble(), stok1.nextDouble(),stok1.nextDouble()));
}
}
});
add(createBox1);
computeButton1 = new JButton("Compute areas");
computeButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
String result1 = new String();
for(Shape s1 : shapes)
{
s1.getArea();
result1 += s1;
}
JTextArea textArea1 = new JTextArea();
textArea1.setEditable(false);
textArea1.setText(result1);
JScrollPane scrollPane1 = new JScrollPane(textArea1);
scrollPane1.setPreferredSize(new Dimension(200,200));
JOptionPane.showMessageDialog(null, scrollPane, "Areas",
JOptionPane.PLAIN_MESSAGE);
}
});
add(computeButton1);
quitButton1= new JButton("Quit");
quitButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
add(quitButton1);
} }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.