Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By SurfMan

Thread: Please tell me why this won't work.

  1. #1
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Please tell me why this won't work.

    Also, please don't try to assume that I know what I am doing.
    This is my first multi class homework assignment.
    The object GratCalc at the bottom isn't working. Keeps saying error: { expected.

    Here is the entire program:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    
    
    public class GratuityCalculator extends JFrame
    {
    
    //declarations
    
    // color objects
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    	Color light_gray = new Color(192, 192, 192);
    
    //components
    
    DecimalFormat currency = new DecimalFormat("$#0.00");
    
    JButton enterJButton;
    JButton clearJButton;
    JButton closeJButton;
    JTextField billAmountJTextField;
    JTextField gratuityAmountJTextField;
    JLabel billAmountJLabel;
    JLabel gratuityAmountJLabel;
    JLabel checkJLabel;
    String check;
    CalcGrat calcGrat;
    double firstValue;
    double secondValue;
    
    //variables
    double billAmount;
    double gratuityAmount;
    
    GratuityCalculator gratuityCalculator; // object class declaration
    
    
       	public GratuityCalculator()
       	{
          		createUserInterface();
       	}
    
       	public void createUserInterface()
       	{
    		Container contentPane = getContentPane();
    		contentPane.setBackground(Color.white);
    		contentPane.setLayout(null);
    
    // Initalize Components
          billAmountJLabel = new JLabel();
    		billAmountJLabel.setBounds(50, 50, 120, 20);
    		billAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		billAmountJLabel.setText("Enter Amount of Bill:");
    		billAmountJLabel.setForeground(black);
    		billAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
    		contentPane.add(billAmountJLabel);
    
    		billAmountJTextField = new JTextField();
    		billAmountJTextField.setBounds(225, 50, 50, 20);
    		billAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		billAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    		billAmountJTextField.setForeground(black);
    		billAmountJTextField.setBackground(white);
    		billAmountJTextField.setEditable(true);
    		contentPane.add(billAmountJTextField);
    
          gratuityAmountJLabel = new JLabel();
    		gratuityAmountJLabel.setBounds(50, 80, 150, 20);
    		gratuityAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		gratuityAmountJLabel.setText("Amount of Gratuity:");
    		gratuityAmountJLabel.setForeground(black);
    		gratuityAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
    		contentPane.add(gratuityAmountJLabel);
    
    		gratuityAmountJTextField = new JTextField();
    		gratuityAmountJTextField.setBounds(225, 80, 50, 20);
    		gratuityAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		gratuityAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    		gratuityAmountJTextField.setForeground(black);
    		gratuityAmountJTextField.setBackground(white);
    		gratuityAmountJTextField.setEditable(false);
    		contentPane.add(gratuityAmountJTextField);
    
    
          enterJButton = new JButton();
    		enterJButton.setBounds(20, 300, 100, 20);
    		enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		enterJButton.setText("Enter");
    		enterJButton.setForeground(black);
    		enterJButton.setBackground(white);
    		contentPane.add(enterJButton);
    		enterJButton.addActionListener(
    
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					enterJButtonActionPerformed(event);
    				}
    			}
    		);
    
    		clearJButton = new JButton();
    		clearJButton.setBounds(130, 300, 100, 20);
    		clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		clearJButton.setText("Clear");
    		clearJButton.setForeground(black);
    		clearJButton.setBackground(white);
    		contentPane.add(clearJButton);
    		clearJButton.addActionListener(
    
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					clearJButtonActionPerformed(event);
    				}
    			}
    		);
    
    		closeJButton = new JButton();
    		closeJButton.setBounds(240, 300, 100, 20);
    		closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		closeJButton.setText("Close");
    		closeJButton.setForeground(black);
    		closeJButton.setBackground(white);
    		contentPane.add(closeJButton);
    		closeJButton.addActionListener(
    
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					closeJButtonActionPerformed(event);
    				}
    			}
    		);
    
    		setTitle("GratuityCalculator");
    		setSize(400, 400);
    		setVisible(true);
       	}
    
    
    //main method
    // main method
       	public static void main(String[] args)
       	{
          		GratuityCalculator application = new GratuityCalculator();
          		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       	}
    
    
    
    public void clearJButtonActionPerformed(ActionEvent event)
    	{
    		billAmountJTextField.setText("");
            billAmountJTextField.requestFocusInWindow();
            gratuityAmountJTextField.setText("");
            
    	}
    
    	   public void closeJButtonActionPerformed(ActionEvent event)
    	{
    		   GratuityCalculator.this.dispose();
    	}
          public void enterJButtonActionPerformed(ActionEvent event)
    	
      
       {
       try
       {
    	billAmount = Double.parseDouble(billAmountJTextField.getText());
       	gratuityAmount = billAmount * 0.15;
       gratuityAmountJTextField.setText("" + currency.format(gratuityAmount));
       
       }
    		catch(NumberFormatException exception)
    		{
    		      	JOptionPane.showMessageDialog(this,
    		       	"Please enter amount of bill!",
    		        "Number Format Error", JOptionPane.ERROR_MESSAGE );
    		        billAmountJTextField.setText("");
    		        billAmountJTextField.requestFocusInWindow();
    
    
        }
        calcGrat = new CalcGrat(billAmount, gratuityAmount);
    		displayResults();
    	}
    
    	public void displayResults()
    	{
    		/* call object class method */
    		resultNumber = calculateIntegers.getResults();
    
    		resultsJTextField.setText("" + resultNumber);
    	}
    
     
    
    
    }
    
    
    class CalcGrat;
       {
       
    double billAmount;
    double gratuityAmount;
    
    public CalcGrat(double firstValue, double secondValue)
    {
    
    billAmount = firstValue;
    gratuityAmount = secondValue;
    }
    
    public double getResults()
    {
    return firstNumber * 0.15;
    }
    }


    GratuityCalculator.java:210: error: '{' expected
    class CalcGrat;
    ^
    1 error

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Please tell me why this won't work.

    It told you where the error was. The semi-colon should not be there.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this won't work.

    There should not be a ; at the end of line 210.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this won't work.

    Okay so now it works but it is not properly multiplying the input number by .15. What did I do wrong? Every time I enter a number it produces 0.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this won't work.

    it produces 0.
    What line(s) of code are doing that? Are you sure the input is correct? 0 times anything is still 0
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this won't work.

    double billAmount;
    double gratuityAmount;
    double firstNumber;
    double secondNumber;

    public CalcGrat(double firstNumber, double secondNumber)
    {

    billAmount = firstNumber;
    gratuityAmount = secondNumber;
    }

    public double getResults()
    {
    return billAmount * 0.15;
    }
    }

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Please tell me why this won't work.

    Are you certain it compiled? Even after fixing the semi-colon you still have errors.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this won't work.

    Quote Originally Posted by jim829 View Post
    Are you certain it compiled? Even after fixing the semi-colon you still have errors.

    Regards,
    Jim
    It compiled. Prior to adding the 2nd class, it worked. Before the 2nd class I have:

    }
    calcGrat = new CalcGrat(billAmount, gratuityAmount);
    displayResults();

  9. #9
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this won't work.

    I think what I am not grasping is how the 2nd class is to produce the results on JTextField GRatuityAmount.

    For example, prior I had it like, double.parseDouble etc
    with gratuityAmount= billAmount * 0.15

    And it was set to display on JTextField gratuityAmount the results + currency.format

    Now, it's kind of different because I am using the 2nd class to perform the same task, and instead I am using
    public double getResults()
    {
    return billAmount * 0.15;
    }
    }

    I am not familiar how to use this. I have never used a 2nd class. I have never used a return. I have never done it like this and it's highly confusing.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Please tell me why this won't work.

    You said it compiled? Are you changing the code? If so, it is hard to help you.

    Some of the values in this method are undefined (form the POV of the method).

    Java Code:
    public void displayResults() {
          /* call object class method */
          resultNumber = calculateIntegers.getResults();
          resultsJTextField.setText("" + resultNumber);
       }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this won't work.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    
    
    public class GratuityCalculator extends JFrame
    {
    
    //declarations
    
    // color objects
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    	Color light_gray = new Color(192, 192, 192);
    
    //components
    
    DecimalFormat currency = new DecimalFormat("$#0.00");
    
    JButton enterJButton;
    JButton clearJButton;
    JButton closeJButton;
    JTextField billAmountJTextField;
    JTextField gratuityAmountJTextField;
    JLabel billAmountJLabel;
    JLabel gratuityAmountJLabel;
    JLabel checkJLabel;
    String check;
    CalcGrat calcGrat;
    double firstNumber;
    double secondNumber;
    
    //variables
    double billAmount;
    double gratuityAmount;
    
    GratuityCalculator gratuityCalculator; // object class declaration
    
    
       	public GratuityCalculator()
       	{
          		createUserInterface();
       	}
    
       	public void createUserInterface()
       	{
    		Container contentPane = getContentPane();
    		contentPane.setBackground(Color.white);
    		contentPane.setLayout(null);
    
    // Initalize Components
          billAmountJLabel = new JLabel();
    		billAmountJLabel.setBounds(50, 50, 120, 20);
    		billAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		billAmountJLabel.setText("Enter Amount of Bill:");
    		billAmountJLabel.setForeground(black);
    		billAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
    		contentPane.add(billAmountJLabel);
    
    		billAmountJTextField = new JTextField();
    		billAmountJTextField.setBounds(225, 50, 50, 20);
    		billAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		billAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    		billAmountJTextField.setForeground(black);
    		billAmountJTextField.setBackground(white);
    		billAmountJTextField.setEditable(true);
    		contentPane.add(billAmountJTextField);
    
          gratuityAmountJLabel = new JLabel();
    		gratuityAmountJLabel.setBounds(50, 80, 150, 20);
    		gratuityAmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		gratuityAmountJLabel.setText("Amount of Gratuity:");
    		gratuityAmountJLabel.setForeground(black);
    		gratuityAmountJLabel.setHorizontalAlignment(JLabel.LEFT);
    		contentPane.add(gratuityAmountJLabel);
    
    		gratuityAmountJTextField = new JTextField();
    		gratuityAmountJTextField.setBounds(225, 80, 50, 20);
    		gratuityAmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		gratuityAmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    		gratuityAmountJTextField.setForeground(black);
    		gratuityAmountJTextField.setBackground(white);
    		gratuityAmountJTextField.setEditable(false);
    		contentPane.add(gratuityAmountJTextField);
    
    
          enterJButton = new JButton();
    		enterJButton.setBounds(20, 300, 100, 20);
    		enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		enterJButton.setText("Enter");
    		enterJButton.setForeground(black);
    		enterJButton.setBackground(white);
    		contentPane.add(enterJButton);
    		enterJButton.addActionListener(
    
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					enterJButtonActionPerformed(event);
    				}
    			}
    		);
    
    		clearJButton = new JButton();
    		clearJButton.setBounds(130, 300, 100, 20);
    		clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		clearJButton.setText("Clear");
    		clearJButton.setForeground(black);
    		clearJButton.setBackground(white);
    		contentPane.add(clearJButton);
    		clearJButton.addActionListener(
    
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					clearJButtonActionPerformed(event);
    				}
    			}
    		);
    
    		closeJButton = new JButton();
    		closeJButton.setBounds(240, 300, 100, 20);
    		closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		closeJButton.setText("Close");
    		closeJButton.setForeground(black);
    		closeJButton.setBackground(white);
    		contentPane.add(closeJButton);
    		closeJButton.addActionListener(
    
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					closeJButtonActionPerformed(event);
    				}
    			}
    		);
    
    		setTitle("GratuityCalculator");
    		setSize(400, 400);
    		setVisible(true);
       	}
    
    
    //main method
    // main method
       	public static void main(String[] args)
       	{
          		GratuityCalculator application = new GratuityCalculator();
          		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       	}
    
    
    
    public void clearJButtonActionPerformed(ActionEvent event)
    	{
    		billAmountJTextField.setText("");
            billAmountJTextField.requestFocusInWindow();
            gratuityAmountJTextField.setText("");
            
    	}
    
    	   public void closeJButtonActionPerformed(ActionEvent event)
    	{
    		   GratuityCalculator.this.dispose();
    	}
          public void enterJButtonActionPerformed(ActionEvent event)
    	
      
       {
       try
       {
    	   
       }
    		catch(NumberFormatException exception)
    		{
    		      	JOptionPane.showMessageDialog(this,
    		       	"Please enter amount of bill!",
    		        "Number Format Error", JOptionPane.ERROR_MESSAGE );
    		        billAmountJTextField.setText("");
    		        billAmountJTextField.requestFocusInWindow();
    
    
        }
        calcGrat = new CalcGrat(billAmount, gratuityAmount);
    		displayResults();
    	}
    
    	public void displayResults()
    	{
    		/* call object class method */
    		gratuityAmount = calcGrat.getResults();
    
    		gratuityAmountJTextField.setText("" + gratuityAmount);
    	}
    
     
    
    
    }
    
    
    class CalcGrat
       {
       
    double billAmount;
    double gratuityAmount;
    double firstNumber;
    double secondNumber;
    
    public CalcGrat(double billAmount, double gratuityAmount)
    {
    
    billAmount = firstNumber;
    gratuityAmount = secondNumber;
    }
    
    public double getResults()
    {
    return billAmount * 0.15;
    }
    }

    This is the program as it stands now in its modified state.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Please tell me why this won't work.

    OK, you need to sprinkle some print statements thru out your program to print various values.
    I think the billAmount is not getting passed correctly to the calc class.

    This may help you track down the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    Lawlington is offline Member
    Join Date
    Nov 2016
    Posts
    19
    Rep Power
    0

    Default Re: Please tell me why this won't work.

    Quote Originally Posted by jim829 View Post
    OK, you need to sprinkle some print statements thru out your program to print various values.
    I think the billAmount is not getting passed correctly to the calc class.

    This may help you track down the problem.

    Regards,
    Jim
    Heh.

    I appreciate the time you've spent on this, but that honestly makes no sense to me.

    I've been trying super hard to get through this online class.

    I know nothing about the principals or ANYTHING. I do not learn from books very well, or reading in general.

    Big mistake to take this class online.

    I even went to tutoring and the TWO people who tried to help me educated me more on how good they are at programming whilst solving no problem or educating me at all.

    All I needed was a simple input dialog string.

    What I'm trying to say is that your words make sense, but I haven't a clue as to WHY or HOW. I have an hour to solve this I've been working on this stupid thing for a week and can't get it to work.


    LITERALLY the only thing I really understand right now is copy pasting code and modifying the declaration names.

    That's how dire this is.
    Last edited by Lawlington; 11-21-2016 at 04:39 AM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Please tell me why this won't work.

    Here's what Jim was suggesting. Also the code has been formatted:
    Java Code:
    class CalcGrat
       {
           
       double billAmount;
       double gratuityAmount;
       double firstNumber;
       double secondNumber;
        
       public CalcGrat(double billAmount, double gratuityAmount)
       {
          System.out.println("CalcGrat cnstr billAmount="+billAmount);  // show billAmount
          this.billAmount = billAmount;
          this.gratuityAmount = gratuityAmount;
       }
        
       public double getResults()
       {
          System.out.println("CalcGrat gerResults billAmount="+billAmount);  // show billAmount
          return billAmount * 0.15;
       }
    }
    The print statements will show you if billAmount has a valid value when the code is executed.
    Last edited by Norm; 11-21-2016 at 02:01 PM. Reason: Fixed terrible error as per SurfMan
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Please tell me why this won't work.

    The constructor parameters are messed up. It looks like firstNumber and secondNumber are not necessary. Then the assignment to the fields needs some work. If you don't prefix the billAmount with "this.", you would effectively assign the value of the parameter to the parameter itself. My example is pretty much a defacto standard way of assigning parameters to fields.
    Java Code:
    class CalcGrat {
       double billAmount;
       double gratuityAmount;
         
       public CalcGrat(double billAmount, double gratuityAmount)
       {
          System.out.println("CalcGrat cnstr billAmount="+billAmount);  // shoe billAmount
          this.billAmount = billAmount;
          this.gratuityAmount = gratuityAmount;
       }
         
       public double getResults()
       {
          System.out.println("CalcGrat gerResults billAmount="+billAmount);  // shoe billAmount
          return billAmount * 0.15;
       }
    }
    Norm likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Please tell me why this won't work.

    Ok, here are the problems. First, lines 171 thru 191 are incorrect. Here is how it should
    be organized:

    Java Code:
    public void enterJButtonActionPerformed(ActionEvent event) {
       try  {
            calcGrat = new CalcGrat(billAmount, gratuityAmount);
            displayResults();
       }  catch(NumberFormatException exception)  {
                    JOptionPane.showMessageDialog(this,
                    "Please enter amount of bill!",
                    "Number Format Error", JOptionPane.ERROR_MESSAGE );
                    billAmountJTextField.setText("");
                    billAmountJTextField.requestFocusInWindow();
      }
    // calcGrat = new CalcGrat(billAmount, gratuityAmount);
    // displayResults();
    }
    Notice I put the last two lines (now commented out) within the try block above. The whole purpose of a try
    catch block is to catch exceptions and handle them. And therein lies the problem. You are not doing anything
    which would potentially generate an exception. Clearly, this is supposed to catch a number format exception. That means
    that if you can't convert entered text (aka the billAmount) into a number you throw an exception and ask the user
    to enter again. But you never try to do the convert. This means your billAmount was always zero and so was the gratuity.
    So you need to grab the text from the billTextField and convert to a double value using Double.valueOf().

    Once you do that you will have another problem. The gratuity won't fit into the gratuity field. So you will either have to round
    it up/down to the nearest appropriate currency value (US dollars and cents I presume). or reformat it using a format string.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Why does this work?
    By Icandoit in forum New To Java
    Replies: 1
    Last Post: 03-18-2014, 01:44 AM
  2. Replies: 2
    Last Post: 11-25-2012, 02:51 AM
  3. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  4. Getting SWT app to work on a different OS
    By Ravioli in forum SWT / JFace
    Replies: 0
    Last Post: 02-20-2011, 09:35 AM
  5. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •