THINK FIRST·CODE LATER

← All labs

Tax to the cent: the type cast

Problem

A cash register must turn a computed tax such as 11.853 into cents. TaxCents reads a price and a tax rate in percent, and prints:

  1. the tax exactly as the double computes it;
  2. the tax cut to cents: (int) (tax * 100) / 100.0;
  3. the tax rounded to cents: (int) (tax * 100 + 0.5) / 100.0;
  4. the total to pay (price + rounded tax);
  5. the price split into whole units and cents, using (int).

For the input

197.55 6

the program prints

Tax, as computed: 11.853000000000002
Tax cut to cents: 11.85
Tax rounded to cents: 11.85
Total to pay: 209.4
The price is 197 units and 55 cents

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l3-9-data-1.txt

197.55 6
What your program must print for l3-9-data-1.txt
Tax, as computed: 11.853000000000002
Tax cut to cents: 11.85
Tax rounded to cents: 11.85
Total to pay: 209.4
The price is 197 units and 55 cents

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l3-9-data-1.txt

Windows PowerShell does not understand <:

Get-Content l3-9-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.