Core Java · Chapter 3 of 48

Operators and casting

By the end of this chapter you can predict what Java's arithmetic does with whole numbers and decimals, and convert between them on purpose. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First steps
  • 5 tasks
  • Java
  • In the all labs plan

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

Why you need this

Anjali has two complaints from the SBC counter. A group of students split a bill, and the screen could not say where two rupees went. And Ravi says the GST on some receipts is one rupee short, which the railways' auditors will not accept.

Both come from arithmetic you already write. / on two int values throws the decimals away, a double turned into an int is cut, not rounded, and a value moved into a smaller type can come out as a different number. Without these rules you cannot tell a correct bill from a wrong one.

What you build: 5 tasks

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

  1. Task 1

    Bill.java: split a bill in whole rupees

    In Bill.java, make two methods. share(total, people) returns what each person pays in whole rupees when the bill is split evenly, with the part that does not divide evenly dropped. leftover(total, people) returns the whole rupees left over after that split. When the bill divides evenly the leftover is 0, and when there are more people than rupees each share is 0 and the leftover is the whole bill.

    For example, ₹130 between 4 gives share 32 and leftover 2; ₹120 between 3 gives 40 and 0; ₹25 between 30 gives 0 and 25.

  2. Task 2

    Bill.java: an average price that keeps its decimals

    In Bill.java, make averagePrice(total, items) return the average price of one item, total divided by items, as a double that keeps its decimal part.

    For example, averagePrice(115, 2) returns 57.5, averagePrice(130, 4) returns 32.5 and averagePrice(120, 3) returns 40.0.

  3. Task 3

    Bill.java: round 5% GST to the nearest rupee

    In Bill.java, make gst(amount) return 5% of the amount, rounded to the nearest whole rupee, as an int. Halves go up. Nothing sold gives 0.

    For example, gst(130) returns 7 (5% is 6.5), gst(50) returns 3 (2.5), gst(25) returns 1 (1.25), gst(40) returns 2 and gst(0) returns 0.

  4. Task 4

    Bill.java: predict what casts and double sums print

    In Bill.java, make predictCasts() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (a whole number like 7, a double with its decimal point like 7.0, every digit Java shows).

    The check runs the same code and compares your list with what Java really printed.

  5. Task 5 · interview question

    Interview: Bill.java, predict what ++ before and after give

    In Bill.java, make predictTokens() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (whole numbers like 7).

    The check runs the same code and compares your list with what Java really printed.

What you will learn

  • / and %: whole-number division and the remainder it leaves.
  • Casts: widening that Java does for you, and narrowing you must ask for.
  • Rounding and doubles: Math.round, and why some decimals are never exact.
  • ++ before and after: what x++ and ++x give back.

More in First steps

Build "Operators and casting" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

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