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

Program: import javax.swing.*; import javax.swing.border.*; import java.awt.*; i

ID: 3542438 • Letter: P

Question

Program:


import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class PizzaCalculator extends JFrame implements ActionListener
{
    JPanel westPanel = new JPanel();
    JPanel eastPanel = new JPanel();
    JPanel centerPanel = new JPanel();
    JPanel northPanel = new JPanel();
    JPanel southPanel = new JPanel();
    
    //pizza size options
    JRadioButton Small = new JRadioButton("Small");
    JRadioButton Medium = new JRadioButton("Medium");
    JRadioButton Large = new JRadioButton("Large");
    JRadioButton Party = new JRadioButton("Party");
    
    //pizza crust choices
    JRadioButton Thin = new JRadioButton("Thin");
    JRadioButton Thick = new JRadioButton("Thick");
            
    //pizza topping selections
    JCheckBox Cheese = new JCheckBox("Cheese");
    JCheckBox Pepperoni = new JCheckBox("Pepperoni");
    JCheckBox Sausage = new JCheckBox("Sausage");
    JCheckBox Ham = new JCheckBox("Ham");
    JCheckBox Pinapple = new JCheckBox("Pinapple");
    JCheckBox BlackOlives = new JCheckBox("Black Olives");
    
    //calculate, clear, and total buttons
    JButton Calculate = new JButton("Calculate");
    JButton Clear = new JButton("Clear");
    JLabel Total = new JLabel("Total: ");
    JTextField TotalResult = new JTextField(8);
    
    //variables used to calculate total
    int TopQty = 0;
    double Size = 0;
    double TopCost = 0;
    double CrustCost = 0;
    double TotalCost = 0;
    
    public PizzaCalculator()
    {
        //layout north portion of frame
        northPanel.setLayout(new GridLayout(2,3));
        northPanel.setBorder(new TitledBorder("Pizza Size and Crust Type:"));
        
        //register listeners with buttons
        Small.addActionListener(this);
        Medium.addActionListener(this);
        Large.addActionListener(this);
        Party.addActionListener(this);
        
        ButtonGroup pizzaSize = new ButtonGroup();
        pizzaSize.add(Small);
        pizzaSize.add(Medium);
        pizzaSize.add(Large);
        pizzaSize.add(Party);
        
        ButtonGroup pizzaCrust = new ButtonGroup();
        pizzaCrust.add(Thin);
        pizzaCrust.add(Thick);
        
        //add buttons to north panel
        northPanel.add(new JTextField("Pizza Total Calculator"));
        northPanel.add(Small);
        northPanel.add(Medium);
        northPanel.add(Large);
        northPanel.add(Party);
        northPanel.add(Thin);
        northPanel.add(Thick);
        
        centerPanel.setLayout(new GridLayout(5,5));
        centerPanel.setBorder(new TitledBorder("Toppings:"));
        
        centerPanel.add(Cheese);
        centerPanel.add(Pepperoni);
        centerPanel.add(Sausage);
        centerPanel.add(Ham);
        centerPanel.add(Pinapple);
        centerPanel.add(BlackOlives);
        
        southPanel.add(Calculate);
        southPanel.add(Clear);
        southPanel.add(Total);
        southPanel.add(TotalResult);
        
        //add all portions of panel
        setLayout(new BorderLayout(5,5));
        add(northPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
    }
    
    //handle action event for each pizza option
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == Small)
            setSizeS();
        else if(e.getSource() == Medium)
            setSizeM();
        else if(e.getSource() == Large)
            setSizeL();
        else if(e.getSource() == Party)
            setSizeP();
        
        if(e.getSource() == Thin)
            setCrustThin();
        else if(e.getSource() == Thick)
            setCrustThick();
        
         if(e.getSource() == Cheese)
            TopQty++;
        else if(e.getSource() == Pepperoni)
            TopQty++;
        else if(e.getSource() == Sausage)
            TopQty++;
        else if(e.getSource() == Ham)
            TopQty++;
        else if(e.getSource() == Pinapple)
            TopQty++;
        else if(e.getSource() == BlackOlives)
            TopQty++;
         
         TopCost(TopQty);
         
         if(e.getSource() == Calculate)
             calculate(Size, TopCost, CrustCost);
    }
    
    public double setSizeS()
    {
        return Size = 6.00;
    }
    
    public double setSizeM()
    {
        return Size = 8.00;
    }
    
    public double setSizeL()
    {
        return Size = 10.00;
    }
    
    public double setSizeP()
    {
        return Size = 12.00;
    }
    
    public double setCrustThin()
    {
        return CrustCost = 0.00;
    }
    
    public double setCrustThick()
    {
        return CrustCost = 1.00;
    }
    
    public double TopCost(int TopQty)
    {
        if(e.getSource() == Small)
            if(TopQty == 1)
                TopCost = 0.75;
            else if(TopQty == 2)
                TopCost = 2*(0.75);
            else if(TopQty == 3)
                TopCost = 3*(0.75);
            else if(TopQty == 4)
                TopCost = 4*(0.75);
            else if(TopQty == 5)
                TopCost = 5*(0.75);
            else if(TopQty == 6)
                TopCost = 6*(0.75);
        else if(e.getSource() == Medium)
            if(TopQty == 1)
                TopCost = 1.25;
            else if(TopQty == 2)
                TopCost = 2*(1.25);
            else if(TopQty == 3)
                TopCost = 3*(1.25);
            else if(TopQty == 4)
                TopCost = 4*(1.25);
            else if(TopQty == 5)
                TopCost = 5*(1.25);
            else if(TopQty == 6)
                TopCost = 6*(1.25);
        else if(e.getSource() == Large)
            if(TopQty == 1)
                TopCost = 1.75;
            else if(TopQty == 2)
                TopCost = 2*(1.75);
            else if(TopQty == 3)
                TopCost = 3*(1.75);
            else if(TopQty == 4)
                TopCost = 4*(1.75);
            else if(TopQty == 5)
                TopCost = 5*(1.75);
            else if(TopQty == 6)
                TopCost = 6*(1.75);
        else if(e.getSource() == Party)
            if(TopQty == 1)
                TopCost = 2.25;
            else if(TopQty == 2)
                TopCost = 2*(2.25);
            else if(TopQty == 3)
                TopCost = 3*(2.25);
            else if(TopQty == 4)
                TopCost = 4*(2.25);
            else if(TopQty == 5)
                TopCost = 5*(2.25);
            else if(TopQty == 6)
                TopCost = 6*(2.25);
        
        return TopCost;

    }
    
    public void calculate(double Size, double TopCost, double CrustCost )
    {
        TotalCost = Size + TopCost + CrustCost;
        TotalResult.setText(String.valueOf(TotalCost));
    }
    
    public static void main(String[] args)
    {
        PizzaCalculator PizzaCalc = new PizzaCalculator();
        //set window title
        PizzaCalc.setTitle("Pizza Cost Calculator");
        //set window location
        PizzaCalc.setLocationRelativeTo(null);
        //specify window location
        PizzaCalc.setDefaultCloseOperation(JFrame.EXIT_ON_ClOSE );
        //set window size
        PizzaCalc.setSize(800,250);
        //display window
        PizzaCalc.setVisible(true);
    }
}

Error:



----jGRASP exec: javac -g PizzaCalculator.java

PizzaCalculator.java:166: error: cannot find symbol
        if(e.getSource() == Small)
           ^
  symbol:   variable e
  location: class PizzaCalculator
PizzaCalculator.java:179: error: cannot find symbol
        else if(e.getSource() == Medium)
                ^
  symbol:   variable e
  location: class PizzaCalculator
PizzaCalculator.java:192: error: cannot find symbol
        else if(e.getSource() == Large)
                ^
  symbol:   variable e
  location: class PizzaCalculator
PizzaCalculator.java:205: error: cannot find symbol
        else if(e.getSource() == Party)
                ^
  symbol:   variable e
  location: class PizzaCalculator
PizzaCalculator.java:237: error: cannot find symbol
        PizzaCalc.setDefaultCloseOperation(JFrame.EXIT_ON_ClOSE );
                                                 ^
  symbol:   variable EXIT_ON_ClOSE
  location: class JFrame
5 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Explanation / Answer

instead of == try .equals()