1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Could someone please tell me why my assignment didn't work.

Discussion in 'Java' started by carolynhesse, Oct 31, 2011.

  1. carolynhesse

    carolynhesse New Member

    Joined:
    Oct 31, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi all. I'm taking my first course in Java and am terrible at it. I failed the last assignment we did and I was wondering if someone could tell me how to fix it so I don't run into the same problems on the next lab. We were instructed to create a working tip calculator. Thank you so much for your help in advance!


    Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    
    public class Lab5 extends JApplet implements ActionListener
    {    
        
        
    public void actionPerformed(ActionEvent e);
    {    
        //determines what will occur when the buttons are pressed
        if(e.getSource()==submit)
            performCalculations();
        else
            ClearGUI();
    }    
    
    public void performCalculations()
    {
        double billAmountEntered = Double.parseDouble(totalBill.getText().trim());
        int numCustomers = Integer.parseInt(people.getText().trim());
        string country = (string)countries.getSelectedItem();
        string typeOfTip = (string)tip.getSelectedIndex();
        pic = getImage(getCodeBase(),"Images/"+country+".png");
        double percentOfTip;
        double taxPercentUsed;
        double totalCalculation;
        double amountPerPersonCalculation;
        double tipAmount;
        double taxAmount;
        
    
        
        //calculates the tip according to the user input
        if(country==0)
            ;
            else if(country==1)
                typeOfTip = tip.getSelectedIndex();
                taxPercentUsed = .08;
                billAmount.setText(billAmountEntered);
                taxAmountDisplay.setText(taxAmount);
                totalBill.setText(totalCalculation);
                personAmount.setText(amountPerPersonCalculation);
                if(typeOfTip==0)
                    tip=0.0;
                    else if(typeOfTip==1)
                        tip=.13;
                        else if(typeOfTip==2)
                            tip=.15;
                            else
                                tip=.20;
                percentOfTip = tip;
                tipAmount = billAmountEntered * percentOfTip;
                taxAmount = billAmountEntered * taxPercentUsed;
                totalCalculation = billAmountEntered + tipAmount + taxAmount;
            else if(country==2)
                typeOfTip = tip.getSelectedIndex();
                taxPercentUsed = .05;
                billAmount.setText(billAmountEntered);
                taxAmountDisplay.setText(taxAmount);
                totalBill.setText(totalCalculation);
                personAmount.setText(amountPerPersonCalculation);
                if(typeOfTip==0)
                    tip=0.0;
                    else if(typeOfTip==1)
                        tip=.13;
                        else if(typeOfTip==2)
                            tip=.15;
                            else if(typeOfTip==3)
                                tip=.20;
                percentOfTip = tip;
                tipAmount = billAmountEntered * percentOfTip;
                taxAmount = billAmountEntered * taxPercentUsed;
                totalCalculation = billAmountEntered + tipAmount + taxAmount;
            else if(country==3)
                typeOfTip = tip.getSelectedIndex();
                taxPercentUsed = .08;
                billAmount.setText(billAmountEntered);
                taxAmountDisplay.setText(taxAmount);
                totalBill.setText(totalCalculation);
                personAmount.setText(amountPerPersonCalculation);
                if(typeOfTip==0)
                    tip=0.0;
                    else if(typeOfTip==1)
                        tip=.10;
                        else if(typeOfTip==2)
                            tip=.12;
                            else if(typeOfTip==3)
                                tip=.15;
                percentOfTip = tip;
                tipAmount = billAmountEntered * percentOfTip;
                taxAmount = billAmountEntered * taxPercentUsed;
                totalCalculation = billAmountEntered + tipAmount + taxAmount;
    
    }
    
    public void ClearGui()
    {   
        //sets the applet to its original state
        tip.setSelectedIndex(0);
        countries.setSelectedIndex(0);
        includeTax.setSelected(true);
        
        validate();
    }
    
    //the layout code
    public void init()
    {
        setLayout(new BorderLayout());
        
        JLabel title = new JLabel("Multi Country Tip Calculator");
        JComboBox countries = new JComboBox();
        JLabel checkAmount = new JLabel("Check Amount:");
        JTextField billAmount = new JTextField(8);
        JLabel tax = new JLabel("Tax:");
        JTextField taxAmountDisplay = new JTextField(8);
        JCheckBox includeTax = new JCheckBox("Include Tax");
        DefaultListModel myModel = new DefaultListModel();
        JList tip = new JList(myModel);
        JLabel tipPercentUsed = new JLabel("Tip % Used:");
        JTextField tipPercentSelection = new JTextField(8);
        JLabel total = new JLabel("Total:");
        JTextField totalBill = new JTextField(8);
        JLabel numberOfPeople = new JLabel("Number of People:");
        JTextField people = new JTextField(3);
        JLabel amountPerPerson = new JLabel("Amount Per Person:");
        JTextField personAmount = new JTextField(8);
        JButton submit = new JButton("Submit");
        JButton clear = new JButton("Clear");
        
        //adds a JPanel and its components to the north portion of the border layout
        JPanel north = new JPanel(new FlowLayout());
        add(north, BorderLayout.NORTH);
        north.add(title);
        
        //adds a JPanel and its components to the west portion of the border layout
        JPanel west = new JPanel(new FlowLayout());
        add(west, BorderLayout.WEST);
        west.add(countries);
        
        //adds a JPanel and its components to the east portion of the border layout
        JPanel east = new JPanel(new GridLayout(2,1));
        add(east, BorderLayout.EAST);
        east.add(tip);
        east.add(includeTax);
        
        //adds a JPanel and its components to the center portion of the border layout
        JPanel center = new JPanel(new GridLayout(5,2));
        add(center, BorderLayout.CENTER);
        center.add(tipPercentUsed);
        center.add(tipPercentSelection);
        center.add(checkAmount);
        center.add(billAmount);
        center.add(tax);
        center.add(taxAmountDisplay);
        center.add(numberOfPeople);
        center.add(people);
        
        //adds a JPanel and its components to the south portion of the border layout
        JPanel south = new JPanel(new GridLayout(4,4));
        add(south, BorderLayout.SOUTH);
        south.add(total);
        south.add(totalBill);
        south.add(amountPerPerson);
        south.add(personAmount);
        south.add(submit);
        south.add(clear);
        
        
        //sets the title's font, color, size, and location
        title.setForeground(Color.blue);
        title.setFont(new Font("Serif",Font.BOLD,40));
        title.setLocation(200,200);
                
        //adds items to the combo box
        countries.addItem("Please Select A Country");
        countries.addItem("United States");
        countries.addItem("Italy");
        countries.addItem("France");
                
        //adds items to the list
        myModel.addElement("No tip");
        myModel.addElement("Minimum tip");
        myModel.addElement("Normal tip");
        myModel.addElement("High tip");
        
        //changes the background color
        north.setBackground(Color.ORANGE);
        south.setBackground(Color.ORANGE);
        west.setBackground(Color.ORANGE);
        center.setBackground(Color.ORANGE);
        east.setBackground(Color.ORANGE);
        
        //sets all the JLabels' colors 
        total.setForeground(Color.blue);
        amountPerPerson.setForeground(Color.blue);
        checkAmount.setForeground(Color.blue);
        tax.setForeground(Color.blue);
        tipPercentUsed.setForeground(Color.blue);
        numberOfPeople.setForeground(Color.blue);
        
        //sets the size of "Total" and "Amount Per Person"
        total.setFont(new Font("Serif",Font.BOLD,20));
        amountPerPerson.setFont(new Font("Serif",Font.BOLD,20));
        
        //sets the color of the JButtons
        submit.setBackground(Color.red);
        submit.setForeground(Color.blue);
        clear.setBackground(Color.red);
        clear.setForeground(Color.blue);
        
        //sets the colors in the JList
        tip.setBackground(Color.white);
        tip.setForeground(Color.blue);
        
        //sets the colors of the JCheckBox
        includeTax.setForeground(Color.blue);
        includeTax.setBackground(Color.white);
        
        //sets the colors of the JComboBox
        countries.setForeground(Color.blue);
        countries.setBackground(Color.white);
        
    }
     
    Last edited by a moderator: Nov 1, 2011
  2. ewaldhorn

    ewaldhorn New Member

    Joined:
    Feb 16, 2010
    Messages:
    36
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    Cape Town, South Africa
    Home Page:
    Hi.

    Your code doesn't compile - there are basic syntax errors that show that this code was never tested. Perhaps you should consider developing in smaller increments, so you can test sections of your code as you go along.

    A handy tip is to always use brackets {} with if and else statements, in fact, always group related bits of code with these brackets, that way you will never run into situations where you are not sure where a piece of code belongs.

    If you can fix the syntax errors, like the ; after actionPerformed(ActionEvent e) - you might get the code to compile. Debugging is an important skill, and the only way of learning it is to work through code and find errors, one at a time.

    What IDE / Editor are you using?
     
    shabbir likes this.

Share This Page