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

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class J

ID: 3539051 • Letter: I

Question

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JPizza extends JFrame implements ItemListener { private int pizza = 0; FlowLayout flow = new FlowLayout(); double toppings = 0; double sizes; double totalprice = toppings + sizes; JLabel label = new JLabel("Please choose the size of pizza you would like and the topping : )"); JLabel label2 = new JLabel("Your total price is : " + totalprice); final int FRAME_WIDTH = 600; final int FRAME_HEIGHT = 400; JCheckBox pepperoni= new JCheckBox("pepperoni"); JCheckBox sausage= new JCheckBox("cheese"); JCheckBox mushrooms= new JCheckBox("mushrooms"); JCheckBox peppers= new JCheckBox("peppers"); JCheckBox pineapples= new JCheckBox("pineapples"); String[] pizzasize = { "small", "medium", "large", "xlarge"}; JComboBox size = new JComboBox(pizzasize); public JPizza() { super("Pizzeria Menu and Prices"); size.addItemListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout (new FlowLayout()); getContentPane().setBackground(Color.black); label.setFont(new Font("Arial", Font.BOLD, 18)); label.setForeground(Color.white); label2.setFont(new Font("Arial", Font.BOLD, 18)); label2.setForeground(Color.white); size.setFont(new Font("Arial", Font.BOLD, 22)); size.setForeground(Color.white); size.setBackground(Color.black); pepperoni.addItemListener(this); mushrooms.addItemListener(this); sausage.addItemListener(this); peppers.addItemListener(this); pineapples.addItemListener(this); pepperoni.setForeground(Color.white); sausage.setForeground(Color.white); mushrooms.setForeground(Color.white); peppers.setForeground(Color.white); pineapples.setForeground(Color.white); pepperoni.setBackground(Color.black); sausage.setBackground(Color.black); mushrooms.setBackground(Color.black); peppers.setBackground(Color.black); pineapples.setBackground(Color.black); add(pepperoni); add(sausage); add(mushrooms); add(peppers); add(pineapples); add(label); add(size); setSize(FRAME_WIDTH, FRAME_HEIGHT); add(label2); } public void itemStateChanged2(ItemEvent e) { if (pepperoni.isSelected ()) { toppings ++; } else { toppings --; } if (sausage.isSelected ()) { toppings ++; } else { toppings --; } if (mushrooms.isSelected ()) { toppings ++; } else { toppings --; } if (peppers.isSelected ()) { toppings ++; } else { toppings --; } if (pineapples.isSelected ()) { toppings ++; } else { toppings --; } } public void itemStateChanged(ItemEvent e) { if (size.getSelectedItem().equals("small")) { sizes =7; } if (size.getSelectedItem().equals("medium")) { sizes =9; } if (size.getSelectedItem().equals("large")) { sizes =11; } if (size.getSelectedItem().equals("xlarge")) { sizes =14; } } public static void main(String[] args) { JPizza frame = new JPizza(); frame.setVisible(true); } }

Explanation / Answer

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

public class JPizza extends JFrame implements ItemListener {
    private int pizza = 0;
    FlowLayout flow = new FlowLayout();
    double toppings = 0;
    double sizes;
    double totalprice = toppings + sizes;
    JLabel label = new JLabel(
            "Please choose the size of pizza you would like and the topping : )");
    JLabel label2 = new JLabel("Your total price is : " + totalprice);
    final int FRAME_WIDTH = 600;
    final int FRAME_HEIGHT = 400;
    JCheckBox pepperoni = new JCheckBox("pepperoni");
    JCheckBox sausage = new JCheckBox("cheese");
    JCheckBox mushrooms = new JCheckBox("mushrooms");
    JCheckBox peppers = new JCheckBox("peppers");
    JCheckBox pineapples = new JCheckBox("pineapples");
    String[] pizzasize = { "small", "medium", "large", "xlarge" };
    JComboBox size = new JComboBox(pizzasize);

    public JPizza() {
        super("Pizzeria Menu and Prices");
        size.addItemListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        getContentPane().setBackground(Color.black);
        label.setFont(new Font("Arial", Font.BOLD, 18));
        label.setForeground(Color.white);
        label2.setFont(new Font("Arial", Font.BOLD, 18));
        label2.setForeground(Color.white);
        size.setFont(new Font("Arial", Font.BOLD, 22));
        size.setForeground(Color.white);
        size.setBackground(Color.black);
        pepperoni.addItemListener(this);
        mushrooms.addItemListener(this);
        sausage.addItemListener(this);
        peppers.addItemListener(this);
        pineapples.addItemListener(this);
        pepperoni.setForeground(Color.white);
        sausage.setForeground(Color.white);
        mushrooms.setForeground(Color.white);
        peppers.setForeground(Color.white);
        pineapples.setForeground(Color.white);
        pepperoni.setBackground(Color.black);
        sausage.setBackground(Color.black);
        mushrooms.setBackground(Color.black);
        peppers.setBackground(Color.black);
        pineapples.setBackground(Color.black);
        add(pepperoni);
        add(sausage);
        add(mushrooms);
        add(peppers);
        add(pineapples);
        add(label);
        add(size);
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        add(label2);
    }

    public void itemStateChanged2(ItemEvent e) {
        totalprice -= toppings;
        if (pepperoni.isSelected()) {
            toppings++;
        } else {
            toppings--;
        }
        if (sausage.isSelected()) {
            toppings++;
        } else {
            toppings--;
        }
        if (mushrooms.isSelected()) {
            toppings++;
        } else {
            toppings--;
        }
        if (peppers.isSelected()) {
            toppings++;
        } else {
            toppings--;
        }
        if (pineapples.isSelected()) {
            toppings++;
        } else {
            toppings--;
        }
        totalprice += toppings;
    label2.setText("Your total price is : " + totalprice);
    }

    public void itemStateChanged(ItemEvent e) {
       totalprice -= sizes;
        if (size.getSelectedItem().equals("small")) {
            sizes = 7;
        }
        if (size.getSelectedItem().equals("medium")) {
            sizes = 9;
        }
        if (size.getSelectedItem().equals("large")) {
            sizes = 11;
        }
        if (size.getSelectedItem().equals("xlarge")) {
            sizes = 14;
        }
       totalprice += sizes;
       label2.setText("Your total price is : " + totalprice);
    }

    public static void main(String[] args) {
        JPizza frame = new JPizza();
        frame.setVisible(true);
    }
}