Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am asking to fix this code by adding function of compute block, pyramid 1) ent

ID: 3772600 • Letter: I

Question

I am asking to fix this code by adding function of compute block, pyramid

1) enter lenght

2) enter wight

3) enter height

then asking for redius for the Sphere

-----------------------------------------------------------------------

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

{

    // Knows

    private String[] shapeNames = {"None", "Sphere", "Block", "Pyramid"};

    private JLabel createLabel;

    private JComboBox createBox;

    private JButton computeButton;

    private JButton quitButton;

    private ArrayList shapes;

    // Does

    // Constructor

    public ControlPanel()

    {

        // Create shapes ArrayList

        shapes = new ArrayList();

        // Shape creation label and combo box

        createLabel = new JLabel("Create Shape");

        add(createLabel);

        createBox = new JComboBox(shapeNames);

        createBox.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e)

            {

                // Sphere selected

                if (createBox.getSelectedItem().equals(shapeNames[1]))

                {

                    String str = JOptionPane.showInputDialog("Enter ");

                    double tmp1 = Double.parseDouble(str);

                    shapes.add(new Sphere(tmp1));

                }

                // Block selected

                else if (createBox.getSelectedItem().equals(shapeNames[2]))

                {

                    String str = JOptionPane.showInputDialog("Enter ");

                    Scanner stok = new Scanner(str);

                    shapes.add(new Block(stok.nextDouble(),stok.nextDouble(),stok.nextDouble()));

                   

                }

                // Pyramid selected

                else if (createBox.getSelectedItem().equals(shapeNames[3]))

                {

                    String str = JOptionPane.showInputDialog("Enter

");

                    Scanner stok = new Scanner(str);

                    shapes.add(new Block(stok.nextDouble(), stok.nextDouble(),stok.nextDouble()));

                }

               

            }

        });

        add(createBox);

        // Compute areas button

        computeButton = new JButton("Compute areas");

        computeButton.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e)

            {

                // Compute areas

                String result = new String();

                for(Shape s : shapes )

                {

                    // Compute area

                    s.getArea();

                    //s.getVolume();

                    // Add result to string

                    result += s;

                }

                // Create scroll pane and text area to print result

                JTextArea textArea = new JTextArea();

                textArea.setEditable(false);

                textArea.setText(result);

                //JTextVolume textVolume = new JTextVolume();

                //textVolume.setEditable(false);

                //textVolume.setText(result);

                JScrollPane scrollPane = new JScrollPane(textArea);

                scrollPane.setPreferredSize(new Dimension(200,200));

                JOptionPane.showMessageDialog(null, scrollPane, "Areas",

                        JOptionPane.PLAIN_MESSAGE);

            }

        });

        add(computeButton);

        // Quit button

        quitButton = new JButton("Quit");

        quitButton.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e)

            {

                System.exit(0);

            }

        });

        add(quitButton);

    }

}

---------------------------------------------------------------------------------------

Shape Areas Create Shape None Compute areas Quit

Explanation / Answer

Complete Program:

// File: Shape.java
public class Shape
{
protected double area;
protected String name;

public Shape()
{
  area = 0;
  name = "";
}

public double getArea()
{
  return area;
}
  
public String getName()
{
  return name;
}

public String toString()
{
  return "Area of " + name + ": " + area + " ";
}
}


// File: Sphere.java
public class Sphere extends Shape
{
private double radius;

public Sphere(double temp)
{  
  radius = temp;
  name = "Sphere";
  
  computerArea();
}

public void computerArea()
{
  area = 4.0 * Math.PI * radius * radius;
}
}


// File: Block.java
public class Block extends Shape
{
private double length;
private double width;
private double height;

public Block(double tmp1, double tmp2, double tmp3)
{  
  length = tmp1;
  width = tmp2;
  height = tmp3;
  name = "Block";
  
  computerArea();
}

public void computerArea()
{
  area = 2 * (length * width + width * height + height * length);
}
}


// File: Pyramid.java
public class Pyramid extends Shape
{
private double length;
private double width;
private double height;

public Pyramid(double tmp1, double tmp2, double tmp3)
{  
  length = tmp1;
  width = tmp2;
  height = tmp3;
  name = "Pyramid";
  
  computerArea();
}

public void computerArea()
{
  area = length * width + length * Math.sqrt(Math.pow(width / 2.0, 2.0) + Math.pow(height, 2.0)) + width * Math.sqrt(Math.pow(length / 2.0, 2.0) + Math.pow(height, 2.0));
}
}


// File: ControlPanel.java
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class ControlPanel extends JFrame
{
private String[] shapeNames = {"None", "Sphere", "Block", "Pyramid"};
private JLabel createLabel;
private JComboBox<String> createBox;
private JButton computeButton;
private JButton quitButton;
private ArrayList<Shape> shapes;

public ControlPanel()
{
  setLayout(new FlowLayout());
  shapes = new ArrayList<Shape>();  
  createLabel = new JLabel("Create Shape");
  add(createLabel);
  createBox = new JComboBox<String>(shapeNames);
  
  createBox.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    if(createBox.getSelectedItem().equals(shapeNames[1]))
    {
     String str = JOptionPane.showInputDialog("Enter <radius>");
     double tmp1 = Double.parseDouble(str);
     
     shapes.add(new Sphere(tmp1));
    }
    else if(createBox.getSelectedItem().equals(shapeNames[2]))
    {
     String str = JOptionPane.showInputDialog("Enter <length>");
     double tmp1 = Double.parseDouble(str);
     
     str = JOptionPane.showInputDialog("Enter <width>");
     double tmp2 = Double.parseDouble(str);
     
     str = JOptionPane.showInputDialog("Enter <height>");
     double tmp3 = Double.parseDouble(str);
     shapes.add(new Block(tmp1, tmp2, tmp3));
    }
    else if(createBox.getSelectedItem().equals(shapeNames[3]))
    {
     String str = JOptionPane.showInputDialog("Enter <length>");
     double tmp1 = Double.parseDouble(str);
     
     str = JOptionPane.showInputDialog("Enter <width>");
     double tmp2 = Double.parseDouble(str);
     
     str = JOptionPane.showInputDialog("Enter <height>");
     double tmp3 = Double.parseDouble(str);
     shapes.add(new Pyramid(tmp1, tmp2, tmp3));
    }
   }
  });
  add(createBox);
  
  computeButton = new JButton("Compute areas");
  computeButton.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    String result = new String();
    for(Shape s : shapes)
    {
     result += s;
    }
    
    JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    textArea.setText(result);
    
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setPreferredSize(new Dimension(200, 200));
    JOptionPane.showMessageDialog(null, scrollPane, "Areas", JOptionPane.PLAIN_MESSAGE);
   }
  });
  add(computeButton);
  
  quitButton = new JButton("Quit");
  quitButton.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    System.exit(0);
   }
  });
  add(quitButton);
}

public static void main(String[] args)
{
  ControlPanel frame = new ControlPanel();
  frame.setTitle("Shape Areas");
  frame.setSize(400, 80);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}