Tip Calculator

From CompSciWiki
Revision as of 23:06, 8 December 2011 by ChrisV (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Create a program that will allow the user to enter the price of a bill and the desired tip percentage, then output the size of the tip. The price will be an integer value representing the number of cents, the tip will be an integer value representing the percentage. Assume valid inputs. The output should be given in proper currency format ($XX.XX)

To solve this problem, you will need to understand:

 Sample Input: 999, 14
Sample Output: $1.39

Sample Input: 45678,0
Sample Output: $0.00 

NOTE: Why use pennies? Pennies (or ints) don't have the same lack-of-precision problem that doubles can have. Some programmers never use the double data-type to store money.

 

Primitive Data Types

Wiki chars03.jpg

Solution

There are four distinct steps in this program:

  • Reading in input from the user.
  • Calculating the tip.
  • Converting the tip to currency format.
  • Printing out the result.

The first step is to read in the input. For each of the two inputs, you are going to need to use an input dialog box.

 String input;

// Get cost
input = JOptionPane.showInputDialog(null, "Enter the cost (in cents)"); 

Don't forget your import statement.

 import javax.swing.*; //needed for JOptionPane 

The inputs you receive is going to be a string. You are going to need to convert that input into an integer before using it.

 int cost = Integer.parseInt(input); 

Do this for both sets of input. The next step is calculating the tip amount in cents. This can be calculated by multiplying the cost by the tip amount, and then dividing by 100 (because the tip is a percentage). Note: you should multiply before dividing, because this calculation will be using integer division, and if you divide first, any tip percentage less than 100% will result in a $0.00 tip (this may save you money, but it won't make you popular).

 int tipAmount = (cost*tip)/100; 

With the tip calculated, it is time to convert it to dollars and cents. The amount of dollars is the number of times 100 goes into the tip amount.

 int dollars = tipAmount/100; 

The number of cents, is the remainder of that calculation. Remainder (or modulo division, as it is properly called) is a basic operation in Java that can be done using the '%' character

 int cents = tipAmount%100; 

With the tip calculated and converted to dollars and cents, all that is left is to output the result. This should be done using System.out

 System.out.println("You need to give $" + dollars + "." + cents); 

You should now have a program that calculates tips. For the entire code solution, see below.

 /* Class Tip
 * Created by Brendan Curran-Johnson
 * Made in COMP3040
 * Calculates tips
 */

import javax.swing.*; //needed for JOptionPane

public class Tip 
{

	public static void main (String args[]) 
	{
		// These variables are used for input
		String input;
		int cost;
		int tip;
		
		// Get cost
		input = JOptionPane.showInputDialog(null, "Enter the cost (in cents)");

		// Convert string to int
		cost = Integer.parseInt(input);	

		// Get tip
		input = JOptionPane.showInputDialog(null, "Please enter the tip percentage");

		// Convert string to int
		tip = Integer.parseInt(input);		

		// Calculate tip in cents
		int tipAmount = (cost*tip)/100;

		// Convert tip to dollars and cents
		int dollars = tipAmount/100;
		int cents = tipAmount%100;

		//output solution
		System.out.println("You need to give $" + dollars + "." + cents);
	}
} 

You'll notice though, that this program has a slight issue! It doesn't work, for instance, if the tipAmount is $11.04. If you run your program in this case, you'll get an answer of $11.4. The problem is that if the variable cents only has one digit, then only one digit is displayed. There are two solutions for this.

  1. You can use an if statement, which we will learn later in the course.
  2. We can add to our program to store the tens digit of our output in a separate variable. To do this, we use the expression
 tens = (tipAmount / 10 ) % 10; 

If tipAmount were 1104, then tipAmount / 10 is 110 and so tens would be 0. To make this work, we also need to make cents equal to the last digit, by using the line

 cents = tipAmount%10; 

Code

Solution Code

Back to the Program-A-Day homepage