2

I have a problem with my java applet assignment. For some reason that I cannot figure out, whenever I check a checkbox all of my if statements fire at once. The program is supposed to update the Total Price and notify the user each time a box is pressed. We must use checkbox and not radio buttons or JCheckBox and the program must be written as an applet. Here is a link to my code.

// Classes
import java.awt.*;                                                                                              
import java.applet.*;                                                                                           
import java.awt.event.*;                                                                                         


@SuppressWarnings("serial")                                                                                     
public class ReasonableComputersChristiansen extends Applet implements ItemListener                             
{
// Variables
        Image logo;                                                                                              
        double basePrice;                                                                                       

        Label companyLabel = new Label("Reasonable Computers");                                                 
        Label instructionLabel = new Label("Please select your desired peripherals from the list below");       
        Label outputLabel = new Label("TOTAL PRICE: ");                                                         

        Checkbox printerBox = new Checkbox("Ink Waster 9000");                                                  
        Checkbox monitorBox = new Checkbox("EvilCorp Monitor");                                                 
        Checkbox keyboardBox = new Checkbox("Clickity-Clackity Keyboard");                                      
        Checkbox mouseBox = new Checkbox("Worst-Aim Laser Gaming Mouse");                                       
        Checkbox joystickBox = new Checkbox("Crash and Burn X-2 Joystick");                                     
        Checkbox webcamBox = new Checkbox("Not-So-Anonymous Webcam");                                           


// Components
    public void init()                                                                                          
        {
        add(companyLabel);                                                                                      
        add(instructionLabel);                                                                                  
        add(printerBox);                                                                                        
        printerBox.addItemListener(this);                                                                       
        add(monitorBox);                                                                                        
        monitorBox.addItemListener(this);                                                                       
        add(keyboardBox);                                                                                       
        keyboardBox.addItemListener(this);                                                                      
        add(mouseBox);                                                                                          
        mouseBox.addItemListener(this);                                                                         
        add(joystickBox);                                                                                       
        joystickBox.addItemListener(this);                                                                      
        add(webcamBox);                                                                                         
        webcamBox.addItemListener(this);                                                                        
        add(outputLabel);                                                                                       
        logo = getImage(getDocumentBase(), "logo.gif");                                                         
        }

// Actions
    public void itemStateChanged(ItemEvent e)                                                                   
        {
        basePrice = 575.00;                                                                                     

        if (printerBox.getState());                                                                             
            {
            basePrice += 69.99;                                                                                 
            System.out.println("Printer added!");                                                               
            }
        if (monitorBox.getState());                                                                             
            {
            basePrice += 125.49;                                                                                
            System.out.println("Monitor added!");                                                               
            }
        if (keyboardBox.getState());                                                                            
            {
            basePrice += 55.59;                                                                                 
            System.out.println("Keyboard added!");                                                              
            }
        if (mouseBox.getState());                                                                               
            {
            basePrice += 19.99;                                                                                 
            System.out.println("Mouse added!");                                                                 
            }
        if (joystickBox.getState());                                                                            
            {
            basePrice += 224.97;                                                                                
            System.out.println("Joystick added!");                                                              
            }
        if (webcamBox.getState());                                                                              
            {
            basePrice += 99.19;                                                                                 
            System.out.println("Webcam added!");                                                                
            }
        }
// Output
    public void output()                                                                                        
        {
            outputLabel.setText("TOTAL PRICE: $" + (basePrice));                                                
    }
}

1 Answers1

1

You should not have semicolons after your if statements.

Corrected Code:

public void itemStateChanged(ItemEvent e)                                                                   
    {
    basePrice = 575.00;                                                                                     

    if (printerBox.getState())                                                                            
        {
        basePrice += 69.99;                                                                                 
        System.out.println("Printer added!");                                                               
        }
    if (monitorBox.getState())                                                                            
        {
        basePrice += 125.49;                                                                                
        System.out.println("Monitor added!");                                                               
        }
    if (keyboardBox.getState())                                                                         
        {
        basePrice += 55.59;                                                                                 
        System.out.println("Keyboard added!");                                                              
        }
    if (mouseBox.getState())                                                                            
        {
        basePrice += 19.99;                                                                                
        System.out.println("Mouse added!");                                                                 
        }
    if (joystickBox.getState())                                                                         
        {
        basePrice += 224.97;                                                                                
        System.out.println("Joystick added!");                                                              
        }
    if (webcamBox.getState())                                                                            
        {
        basePrice += 99.19;                                                                                 
        System.out.println("Webcam added!");                                                                
        }
    }

The semicolons end your if statements so they don't actually affect what code runs.

Hold on, but wouldn't the curly braces result in syntax errors, then?

Nope. As far as Java's concerned, you can use curly braces just for organization and they don't throw syntax errors. In most cases should you do that? No. But can you do it? Technically, yes.

(the caveat to "in most cases" is when you need to intentionally put variables in a specific scope, as Andy Turner mentioned)

Kyle Martin
  • 568
  • 2
  • 19