I am writing a program out from my Java textbook as practice for writing our own similar program. I have it written exactly as it appears in our book (Starting Out With Java, From Control Structures Through Data Structures, 3rd Edition, Page 833) (quadruple checked!) and every time, it throws 8 "cannot find symbol" errors on the same lines, and I cannot figure out what I am doing wrong.
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* The OrderCalculatorGUI class creates the GUI for the Brandi's Bagel House application
*/
public class OrderCalculatorGUI extends JFrame
{
private BagelPanel bagels;
private ToppingPanel toppings;
private CofeePanel coffee;
private GreetingPanel banner;
private JPanel buttonPanel;
private JButton calcButton;
private JButton exitButton;
private final double TAX_RATE = 0.06;
/**
* Constructor
*/
public OrderCalculatorGUI()
{
//Display a title.
setTitle("Order Calculator");
//Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create a orderLayout manager.
setLayout(new BorderLayout());
// Create the custom panels.
banner = new GreetingPanel();
bagels = new BagelPanel();
toppings = new ToppingPanel();
coffee = new CoffeePanel();
// Create the button panel.
buildButtonPanel();
//Add the components to the content pane.
add(banner, BorderLayout.NORTH);
add(bagels, BorderLayout.WEST);
add(toppings, BorderLayout.CENTER);
add(coffee, BorderLayout.EAST);
add(buttonPanel, BorderLayout.SOUTH);
//Pack the contents of the window and display it.
pack();
setVisible(true);
}
/**
* The buildButtonPanel method builds the button panel.
*/
private void buildButtonPanel()
{
//Create a panel for the buttons.
buttonPanel = new JPanel();
//Create the buttons.
calcButton = new JButton("Calculate");
exitButton = new JButton("Exit");
//Register the action listeners.
calcButton.addActionListener(new CalcButtonListener());
exitButton.addActionListener(new ExitButtonListener());
//Add the buttons to the button panel.
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
}
/**
* Private inner class that handles the event when
* the user clicks the Calculate button.
*/
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Variables to hold the subtotal, tax, and total
double subtotal, tax, total;
//Calculate the subtotal.
subtotal = bagels.getBagelCost() +
toppings.getToppingCost() +
coffee.getCoffeeCost();
//Calculate the sales tax.
tax = subtotal * TAX_RATE;
//Calculate the total.
total = subtotal * TAX_RATE;
//Calculate the total.
total = subtotal + tax;
//Display the charges.
JOptionPane.showMessageDialog(null,
String.format("Subtotal: $%,.2f\n" +
"Tax: $%,.2f\n" +
"Total: $%,.2f",
subtotal, tax, total));
}
}
/**
* Private inner class that handles the event when
* the user clicks the Exit button.
*/
private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
/**
* main method
*/
public static void main(String[] args)
{
new OrderCalculatorGUI();
}
}
And here is the console output in the cmd prompt after running "javac OrderCalculatorGUI.java"
OrderCalculatorGUI.java:11: error: cannot find symbol
private BagelPanel bagels;
^
symbol: class BagelPanel
location: class OrderCalculatorGUI
OrderCalculatorGUI.java:12: error: cannot find symbol
private ToppingPanel toppings;
^
symbol: class ToppingPanel
location: class OrderCalculatorGUI
OrderCalculatorGUI.java:13: error: cannot find symbol
private CofeePanel coffee;
^
symbol: class CofeePanel
location: class OrderCalculatorGUI
OrderCalculatorGUI.java:14: error: cannot find symbol
private GreetingPanel banner;
^
symbol: class GreetingPanel
location: class OrderCalculatorGUI
OrderCalculatorGUI.java:36: error: cannot find symbol
banner = new GreetingPanel();
^
symbol: class GreetingPanel
location: class OrderCalculatorGUI
OrderCalculatorGUI.java:37: error: cannot find symbol
bagels = new BagelPanel();
^
symbol: class BagelPanel
location: class OrderCalculatorGUI
OrderCalculatorGUI.java:38: error: cannot find symbol
toppings = new ToppingPanel();
^
symbol: class ToppingPanel
location: class OrderCalculatorGUI
OrderCalculatorGUI.java:39: error: cannot find symbol
coffee = new CoffeePanel();
^
symbol: class CoffeePanel
location: class OrderCalculatorGUI
8 errors
At this point I am a bit stumped. I am not sure what went wrong, what silly mistake I'm making, or other weird anomaly is happening. I appreciate any assitance.
Thanks!
As per your error, it says that it is unable to find the symbol that means you have not defined the class Bangel panel, Greeting panel, etc. so in order to use this, you must first define these classes or import the respective package. I suggest you to please review your course material and find the suitable code then try again or if you are unable to find then attach the picture of the book`s code here. Thank you, Hope this will help you. Happy coding/