EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
bartleby

Concept explainers

bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 14, Problem 1CP

Explanation of Solution

Program:

File name: “JCarlysCaterring.java

//Import necessary header files

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

//Define a class named JCarlysCatering

public class JCarlysCatering extends JFrame implements ItemListener, ActionListener

{

    //Construct a new FlowLayout object

    FlowLayout flow = new FlowLayout();

//Create a JTextField component for capturing number of //guests

    JTextField guestsField = new JTextField(8);

    //Create a label with the text "Please enter number of //guests"

JLabel guestsLabel = new JLabel("Please enter number of guests");

    //Define CheckBoxes to capture user input for main course,

    //side, items, and desert.

    JCheckBox beefBox = new JCheckBox("Beef", false);

    JCheckBox chickenBox = new JCheckBox("Chicken", false);

    JCheckBox fishBox = new JCheckBox("Fish", false);

    JCheckBox pastaBox = new JCheckBox("Pasta", false);

    JCheckBox saladBox = new JCheckBox("Salad", false);

    JCheckBox vegBox = new JCheckBox("Mixed vegetables", false);

    JCheckBox potBox = new JCheckBox("Baked potato", false);

    JCheckBox breadBox = new JCheckBox("Garlic bread", false);

    JCheckBox cakeBox = new JCheckBox("Chocolate cake", false);

    JCheckBox pieBox = new JCheckBox("Apple pie", false);

JCheckBox pudBox = new JCheckBox("Butterscotch pudding", false);

    //Create a label with the text "Carly's Catering"

    JLabel mainLabel = new JLabel("Carly's Catering");

    //Set the font style of mainLabel contents

    Font font = new  Font("Ariel",Font.ITALIC, 30);

    //Create a label with the text "Total"

    JLabel label2 = new JLabel("Total");

    //Create a label with the text "Select options"

    JLabel label1 = new JLabel("Select options");

    JLabel totPrice = new JLabel();

    //Initialize the required variables

    double price = 0;

    String entreeString = "";

    String sideString = "";

    String dessertString = "";

    String output;

    int numSelected = 0;

    //Define a default constructor

    public JCarlysCatering()

    {

        //Set the title of the JFrame container

        super("Menu options");

        //Set the close and layout of the JFrame container

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(flow);

        //Define a ButtonGroup object entreeGroup

        ButtonGroup entreeGroup = new ButtonGroup();

  //Add the check boxes for the main menu items

        //to the entreeGroup

        entreeGroup.add(beefBox);

        entreeGroup.add(chickenBox);

        entreeGroup.add(fishBox);

        entreeGroup.add(pastaBox);

        //Define a ButtonGroup object dessertGroup

        ButtonGroup dessertGroup = new ButtonGroup();

        //Add the check boxes for the dessert menu items

        //to the dessertGroup

        dessertGroup.add(cakeBox);

        dessertGroup.add(pieBox);

        dessertGroup.add(pudBox);

        //Add respective components in an

        //order to the container

        add(mainLabel);

        add(guestsLabel);

        add(guestsField);

        add(label1);

        add(beefBox);

        add(chickenBox);

        add(fishBox);

        add(pastaBox);

        add(saladBox);

        add(vegBox);

        add(potBox);

        add(breadBox);

        add(cakeBox);

        add(pieBox);

        add(pudBox);

        mainLabel.setFont(font);

        add(label2);

        add(totPrice);

        totPrice.setText("$0");

        //Add the item listener to the buttons

        guestsField.addActionListener(this);

        beefBox.addItemListener(this);

        chickenBox.addItemListener(this);

        fishBox.addItemListener(this);

        pastaBox.addItemListener(this);

        saladBox.addItemListener(this);

        vegBox.addItemListener(this);

        potBox.addItemListener(this);

        breadBox.addItemListener(this);

        cakeBox.addItemListener(this);

        pieBox.addItemListener(this);

        pudBox.addItemListener(this);

    }

    //Override method

    @Override

    public void actionPerformed(ActionEvent e)

    {

        //Get the source

        Object source = e.getSource();

        final int PRICE_PER_GUEST = 35;

        //Check whether the text filed

        if(source == guestsField)

        {

            //Try block

            try

            {

                //Calculate the price

price = Integer.parseInt(guestsField.getText()) * PRICE_PER_GUEST;

            }

            //Catch exception

            catch(Exception exc)

            {

                //Set the number of quests to zero

                //when non-numeric value is entered

                price = 0;

            }

            //Set the output string

output = "$" + price + " Menu includes " + entreeString +

            sideString + dessertString;

            totPrice...

Blurred answer
Students have asked these similar questions
In previous chapters, you have created a number of programs for Carly's Catering. Now, create an interactive GUI program that allows the user to enter the number of guests for an event into a text field; if the value entered is not numeric, set the event price to 0. Also allow the user to choose one entree from a group of at least four choices, up to two side dishes from a group of at least four choices, and one dessert from a group of at least three choices. Display the cost of the event as $35 per person; as the user continues to make selection changes, display a list of the current items chosen. If a user attempts to choose more than two side dishes, remove all the current side dish selections so that the user can start over. Save the program as JCarlysCatering.java.
MFC  In the second part of your project, you need to develop a mathematical worksheet graphical interface for elementary school students. The worksheet consists of 10 questions of operations between two random numbers. The operations are defined randomly from the set {+, -, *, /}. The GUI uses a text field to enter the maximum value of random numbers. A button with the caption "Check Answers" should be added to the GUI, and when clicked, a correct icon () should appear in front of the correct answer and an incorrect icon
In previous chapters, you have created a number of programs for Sammy's Seashore Rentals. Now, create an interactive GUI program that allows the user to enter a rental time in hours into a text field; if the value entered is not numeric, set the rental price to 0. Also allow the user to choose one equipment type to rent from a group of seven choices. The rental fee is $40 per hour for a personal watercraft or pontoon boat; $20 per hour for a rowboat, canoe, or kayak; and $7 per hour for a beach chair or umbrella. Let the user add an equipment lesson for an extra $5. Display a message that indicates all the details for the rental, including the total price. Save the program as JSammysSeashore.java.

Chapter 14 Solutions

EBK JAVA PROGRAMMING

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Time Complexity Analysis - How To Calculate Running Time | InterviewBit; Author: InterviewBit;https://www.youtube.com/watch?v=--oxG4Q1PA0;License: Standard YouTube License, CC-BY