Difference between revisions of "Tip Calculator"

From CompSciWiki
Jump to: navigation, search
m (no more underscore in link)
m
Line 51: Line 51:
  
 
You should now have a program that calculates tips. For the entire code solution, see below.
 
You should now have a program that calculates tips. For the entire code solution, see below.
|SolutionCode=<pre>
+
|SolutionCode=
 
/* Class Tip
 
/* Class Tip
 
  * Created by Brendan Curran-Johnson
 
  * Created by Brendan Curran-Johnson
Line 93: Line 93:
 
}
 
}
 
}
 
}
</pre>
 
  
  
 
}}
 
}}

Revision as of 10:44, 8 April 2010

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, and will 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: 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.

Code

Solution Code

Back to the Program-A-Day homepage