Java traps · Chapter 9 of 16

Floating-point money

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Money
  • 3 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Java traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Repro.java: a ₹90.30 bill whose GST is a paisa short

    Write reproduce() in Repro.java. Ask Gst.gst for the GST on a bill of "90.30" and return what it gives. 5% of 90.30 is exactly 4.515, which rounds half up to 4.52.

    The check runs your reproduce() twice: beside the original Gst.java it must return "4.51" (a paisa short), and beside a fixed Gst.java it must return "4.52". So it must really ask gst. No real waiting and no reading input: reproduce() must finish within 3 seconds.

  2. Task 2

    Gst.java: GST and totals to the paisa

    Fix gst, receipt and total in Gst.java so that every amount is exact to the paisa. gst(amount) is 5% of the amount rounded half up to the paisa ("90.30" gives "4.52", "70.10" gives "3.51", "40.00" gives "2.00"); receipt(billId, amount) gives the amount plus its GST ("80.30" gives "84.32"); total(paid) gives the exact sum of the amounts (ten "0.10" give "1.00", no bills give "0.00"). Every result is text with exactly two decimals.

    Keep the log lines as they are: receipt's (bill <id>: <amount> + GST <gst>, pay <total>) and total's (day total: <n> bills, <total>). The check uses its own bills: every amount from 0.01 to 500.00, many with a 5% that ends in exactly half a paisa, and a day of 300 bills.

  3. Task 3

    GstTest.java: a test that would have caught it

    Write a JUnit test class, GstTest.java, that would have caught this bug before the GST return was filed. The check runs your file three times: beside the original Gst.java, where at least one test must fail; beside the fixed Gst.java, where every test must pass; and beside a wrong fix that builds its BigDecimals from doubles, where at least one test must fail.

    So test both sides: amounts whose 5% ends in exactly half a paisa get it rounded up, "90.30" (4.52) and also "70.10" (3.51, where the wrong fix gives 3.50), and the total of many small amounts is exact (ten "0.10" are "1.00"). Compare the text, two decimals and all. No real waiting: each test must finish within 3 seconds.

What you will learn

  • Repro.java: a ₹90.30 bill whose GST is a paisa short
  • Gst.java: GST and totals to the paisa
  • GstTest.java: a test that would have caught it

More in Money

Build "Floating-point money" yourself

Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Java traps are free. This one comes with the all labs plan.