# Tip calculator

Page 1 of 1

## 12 Replies - 4805 Views - Last Post: 05 December 2010 - 02:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=203749&amp;s=351e92bb00ff2123bec0cd2986096d6c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Anderson2300

Reputation: 0
• Posts: 10
• Joined: 08-November 10

# Tip calculator

Posted 05 December 2010 - 08:06 AM

Hello all. I am having some difficulty finishing my code. I am trying to write a ez tip calculator. Where you enter the amount and the tip you want to leave. Then the total will be what the amount and tip are combined. I have it almost done but I can not figure out how to get the tip and the amount to work. Here is my code and thank you in advanced.
```import javax.swing.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Font;
import java.text.DecimalFormat;

import java.awt.event.*;

public class EzTip extends JFrame implements ActionListener {

JLabel moneyOutput = new JLabel("Please enter Total bill amount ");
JTextField moneyInput = new JTextField("0.0");
JLabel tipOutput = new JLabel ("Please enter Tip amount ");
JTextField tipInput = new JTextField("0");
JButton Calculate = new JButton("Calculate Total");
JLabel totalOutput = new JLabel("Bill amount");
JLabel totalInput = new JLabel("0.0");
JLabel Output = new JLabel("");

public static void main(String[] args) {
new EzTip();
} // end main

public EzTip() {
super("EZ Tip Calculator");
this.setDefaultCloseOperation(EzTip.EXIT_ON_CLOSE);
Container surface = this.getContentPane();
surface.setLayout(new GridLayout(0,1));
moneyOutput.setBackground(Color.CYAN);
moneyInput.setBackground(Color.CYAN);
tipOutput.setBackground(Color.CYAN);
tipInput.setBackground(Color.CYAN);
totalOutput.setBackground(Color.CYAN);
totalInput.setBackground(Color.CYAN);
Output.setBackground(Color.CYAN);
Font myFont = new Font("Serif", Font.BOLD, 16);
moneyOutput.setFont(myFont);
tipOutput.setFont(myFont);
totalOutput.setFont(myFont);
Output.setFont(myFont);
Calculate.setFont(myFont);
this.pack();
this.setVisible(true);

} // end constructor

class EventHandler implements ActionListener{
public void actionPerformed(ActionEvent e){

String money = moneyInput.getText();
String tip = tipInput.getText();
double money1 = Float.parseFloat(money);
double tip2 = Float.parseFloat(tip);
float total = float ((money * 100)/(tip * 100));

}

}

```

Is This A Good Question/Topic? 0

## Replies To: Tip calculator

### #2 [email protected]

Reputation: 1003
• Posts: 975
• Joined: 30-September 10

## Re: Tip calculator

Posted 05 December 2010 - 08:24 AM

Hello there,

What exactly are the problems you are encountering? If I was to change one thing looking at the code in the ActionPerformed method, it would be to stop mixing and matching floats and doubles. I would generally use double over float, as floats have limited accuracy and are generally not recommended unless storage space is a real issue. Mixing and matching will eventually lead to losses in accuracy. Try changing the code in the ActionPerformed method to this:

```public void actionPerformed(ActionEvent e){

double money1 = Double.parseDouble(moneyInput.getText());
double tip2 = Double.parseDouble(tipInput.getText());
double total = (money1 * 100)/(tip2 * 100);
}

```

You were using the string values (instead of the numeric values you parsed) in the multiplication rather than the numeric values. Strings cannot be multiplied or divided! Also, something weird was going on in the last line All you needed to do on this line was perform the calculation, as all values have been parsed already.

This post has been edited by [email protected]: 05 December 2010 - 08:26 AM

### #3 Anderson2300

Reputation: 0
• Posts: 10
• Joined: 08-November 10

## Re: Tip calculator

Posted 05 December 2010 - 08:39 AM

Thank you [email protected] for your help. That make sense not to mix the double and float. The problem that keeps occuring is that the program runs but whenever I put the amount for the bill in and the tip amount in and try to calculate it, it does not work. An error message always comes up with: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The type EzTip must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

More error message are stated but I just did not post them.

### #4 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Tip calculator

Posted 05 December 2010 - 08:51 AM

That means that your EzTip class implements ActionListener, but you didn't provide that actionPerformed code directly in that class.

### #5 Anderson2300

Reputation: 0
• Posts: 10
• Joined: 08-November 10

## Re: Tip calculator

Posted 05 December 2010 - 09:07 AM

Thank you Dogstopper for you help but what do you mean? I did not properly write the code for that section.

### #6 [email protected]

Reputation: 1003
• Posts: 975
• Joined: 30-September 10

## Re: Tip calculator

Posted 05 December 2010 - 09:18 AM

He means that you haven't provided an actionPerformed method in the EzTip class. Both the EventHandler class and the EzTip class implement the ActionListener interface, therefore, both have to have an actionPerformed method. Currently, you don't provide that method in the EzTip class. You only provide it in the EventHandler class.

This post has been edited by [email protected]: 05 December 2010 - 09:21 AM

### #7 Anderson2300

Reputation: 0
• Posts: 10
• Joined: 08-November 10

## Re: Tip calculator

Posted 05 December 2010 - 09:29 AM

Ok I think I get it. Thank you

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Tip calculator

Posted 05 December 2010 - 11:48 AM

and put the actionPerformed() method directly in your EzTip class

### #9 Anderson2300

Reputation: 0
• Posts: 10
• Joined: 08-November 10

## Re: Tip calculator

Posted 05 December 2010 - 12:58 PM

Thank you all for your help. But I have a new problem. I don't get any error messages but when I click the calculate button nothing happens. It doesn't show the answer. Here is the update code.

```	import javax.swing.*;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.event.*;

public class EzTip extends JFrame implements ActionListener {

JLabel moneyOutput = new JLabel("Please enter Total bill amount ");
JTextField moneyInput = new JTextField("0");
JLabel tipOutput = new JLabel ("Please enter Tip amount ");
JTextField tipInput = new JTextField("0");
JButton Calculate = new JButton("Calculate Total");
JLabel totalOutput = new JLabel("Bill amount");
JLabel totalInput = new JLabel("0");
JLabel Output = new JLabel("");

public static void main(String[] args) {
new EzTip();
} // end main

public EzTip() {
super("EZ Tip Calculator");
this.setDefaultCloseOperation(EzTip.EXIT_ON_CLOSE);
Container surface = this.getContentPane();
surface.setLayout(new GridLayout(0,1));
moneyOutput.setBackground(Color.CYAN);
moneyInput.setBackground(Color.CYAN);
tipOutput.setBackground(Color.CYAN);
tipInput.setBackground(Color.CYAN);
totalOutput.setBackground(Color.CYAN);
totalInput.setBackground(Color.CYAN);
Output.setBackground(Color.CYAN);
Font myFont = new Font("Serif", Font.BOLD, 16);
moneyOutput.setFont(myFont);
tipOutput.setFont(myFont);
totalOutput.setFont(myFont);
Output.setFont(myFont);
Calculate.setFont(myFont);
this.pack();
this.setVisible(true);

} // end constructor

public void actionPerformed(ActionEvent e){
String money = moneyInput.getText();
String tip = tipInput.getText();

double money1= Double.parseDouble(money);
double tip1 = Double.parseDouble(tip)/100;
double total = (money1 * 100)/(tip1);
int totalOutput = (int)(total);

}

}
```

I know I am doing(or not doing) something wrong in the actionPerformed section but I do not know

### #10 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Tip calculator

Posted 05 December 2010 - 01:01 PM

int totalOutput = (int)(total);

this totalOutput you will have to put it as a String in a JLabel or JTextfield

### #11 Anderson2300

Reputation: 0
• Posts: 10
• Joined: 08-November 10

## Re: Tip calculator

Posted 05 December 2010 - 01:43 PM

Thank you pbl for your help with this. But when you mean string totalOutput do you mean like this: String total = totalOutput.getText();

### #12 [email protected]

Reputation: 1003
• Posts: 975
• Joined: 30-September 10

## Re: Tip calculator

Posted 05 December 2010 - 02:01 PM

Currently, 'totalOutput' is an int. You need to convert that to a string. Then you can put that string into a Label, textbox...whatever you want.

In Java, you have to wrap the int (a primitive type with no methods) in an Integer object (an object that has methods, fileds etc) before you can call ToString() (either explicitly or by auto boxing).

See below:

```Integer i = (int)total; //note use of Integer rather than int.This method doesn't explicitly create new Integer object using (new Integer(total)). This technique is called auto boxing.

String totalOutput = i.toString(); //now the int is wrapped in an object, you can call toString();

```

Now that you have a string, you can set the text of JLabels etc using their setText method:

```nameOfJLabel.setText(totalOutput);

```

The code you provided was to get text back from a component.

The technique to wrap the int in an Integer in the first snippet is called autoboxing. All that it is doing is this:

```Integer i = new Integer((int)total); //notice explicit creation of Interger object here.

String totalOutput = i.toString();

```

Notice how here I have used 'new' to explicitly create an object. I then pass to it the value of the 'total' variable. This is called wrapping 'total' in an Integer and is required to get access to toString(). You don't have to explicitly create a new object (as I didn't in the first snippet). However, I would generally recommend that you use the 'new Integer()' explicit technique rather than the auto boxing technique just so it is clear and explicit what you are doing .

This post has been edited by [email protected]: 05 December 2010 - 02:21 PM

### #13 Anderson2300

Reputation: 0
• Posts: 10
• Joined: 08-November 10

## Re: Tip calculator

Posted 05 December 2010 - 02:07 PM

Thank you [email protected] for your help. I think I get it now.