Numbers

Home

Tip Calculator

Now that we know (somewhat) how to get user input, lets try to make a simple tip calculator.
Take a look at what our goal is here.

Exercise 18: Setup the "form" that you see in the example. No need to do the actual logic behind the form right now.

Strings to Numbers

In our previous user input example, we were working only with Strings. In this example, we really want to get numbers from the user. however, anything we get from the TextFields are going to come out as Strings. Luckily, there is a way to convert the String to a number, but we have to know what kind of number we need.


Types of Numbers

In math, we have various sets of numbers (Real Numbers, Integers, Natural Numbers, etc.). The same is true about numbers in Java, as well as programming languages in general. Let's examine some of the different types of numbers.

Exercise 19: Which type of number do we want to use for the check amount? What about tip percent?

Strings to Numbers

There are actually several ways to convert from a String to a specific type of number. The easiest place to find these are the API, so we will take a look at the API. Let's look for something that deals with Integers.

Java provides classes to allow for more powerful use of their primitive data types. As a matter of fact, there is one for all of the Java primitives have what are called wrapper classes for them. Scroll through the list of availible methods. You should find the following method.

The above method takes as a parameter a String, and returns an int. Also, we notice that it is a static method. Therefore, to use parseInt, we do the following:

	String s = 23;
	int x = Integer.parseInt(s);
      

Exercise 20: Is there a similar method for double?

Working with Numbers

How does one calculate tip? First, you convert the percentage into a decimal (double). Then, check amount times tip decimal tells us how much tip to give. We add the result of that to the check amount to calculate the total. How do we go about this in Java?

Java provides operators for us to use. These operators are + - * / %. These are often referred to as binary operators, since they operate on two parameters. The result of multiplication and division is the least precise of the two operands.

Exercise 21: What errors do you get when running the following programs? Is the output what you expect? Can you explain why?
	public class test1{
	     public static void main(String[] args){
	          int x = 1 + 2.5;
	          System.out.println(x);
	     }
	}
      

	public class test2{
	     public static void main(String[] args){
	          double x = 1 + 2.5;
	          System.out.println(x);
	     }
	}
      

	public class test3{
	     public static void main(String[] args){
	          double x = 7/2;
	          System.out.println(x);
	     }
	}
      

	public class test4{
	     public static void main(String[] args){
	          double x = 7.0/2;
	          System.out.println(x);
	     }
	}
      
Exercise 22: Add the proper code to calculate the tip to the tip calculator.

Formatting Numbers for Output

Take a look at what you (most likely) have right now here. What happens when you try to calculate the tip? Is the result in a moneytary format? Obviously, we would like our output to be formatted. Once again, Java provides a very easy way to do this, using the NumberFormat class. It will actually format the number as a currency based of of the locale. We first have to get the NumberFormat we need.

	...
	//Get the NumberFormat instance for Currency
	NumberFormat fmt = NumberFormat.getCurrencyInstance();
	...
	//Use the formatter on output
	g.drawString("Tip Amount: " + fmt.format(tipAmount), 200, 50);
	g.drawString("Total Bill: " + fmt.format(checkAmount+tipAmount),
	             200, 75);
      
Alternatively, we could also combine the command to get the instance, and the code that performs the formatting. It results in the correct, but somewhat clunky code below.
	//Use the formatter on output
	g.drawString("Tip Amount: " + NumberFormat.getCurrencyInstance().format(tipAmount), 200, 50);
	g.drawString("Total Bill: " + NumberFormat.getCurrencyInstance().format(checkAmount+tipAmount),
	             200, 75);
      
In general, it is better use the first method when you are going to do the same type of formatting multiple times. The second method is only useful if you need to do it only once.
Exercise 23: Modify your tip calculator to perform number formatting.


Midweek Assignment

Use what you have learned thus far to write a grade averager. You need to keep a running total of the grades coming in, and the number of grades in order to calculate the average.