Core Java · Chapter 10 of 48

Classes and objects

By the end of this chapter you can bundle values into an object, give it methods, and tell two names for one object from two separate objects. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Objects
  • 6 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

At the SBC counter the boys write orders on paper slips, and half the slips get lost in the 6 am rush. Ravi wants every order to live in the program: two masala chai at ₹40, one bun maska, each with its item, price and quantity kept together, and a tray of orders that adds itself up.

With what you know so far, you would keep three arrays side by side (items, prices, quantities) and pass all three to every method. Remove one order and forget one of the arrays, and every price after it belongs to the wrong item. Nothing in the code says these three values are one thing.

What you build: 6 tasks

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

  1. Task 1

    Order.java: what one order costs

    In Order.java, make total() return what this one order costs: its price times its quantity, in rupees. An order with quantity 0 costs 0.

    For example, 3 cutting chai at 25 returns 75.

  2. Task 2

    Order.java: add cups to one order

    In Order.java, make addCups(cups) add that many cups to this order's quantity. It returns nothing. Only the order it is called on changes: another order with the same values stays as it was. Calling it again adds again.

    For example, an order of 1 masala chai has quantity 3 after addCups(2), and 4 after addCups(1) more.

  3. Task 3

    Order.java: an order that prints as a line

    In Order.java, make toString() return the order as one line: the quantity, then " x ", the item, then " = ₹" and the total.

    For example, 2 masala chai at 40 prints as 2 x Masala chai = ₹80. Joining an order to a string uses the same line, so "Order: " plus 3 cutting chai at 25 is Order: 3 x Cutting chai = ₹75.

  4. Task 4

    Order.java: copy an order into a new object

    In Order.java, make copy() return a new Order object, never this same one, with the same item, price and quantity. Changing the copy afterwards must not change the original. It never returns null.

    For example, the copy of 2 x Masala chai at 40 is another 2 x Masala chai at 40; setting the copy's quantity to 5 leaves the original at 2.

  5. Task 5

    Tray.java: add up a tray with empty slots

    In Tray.java, make total() return the sum of the totals of the orders in the tray's three slots. A slot with no order in it is null: skip it. An empty tray returns 0.

    For example, 2 x Masala chai at 40 and 1 x Bun maska at 50 with the middle slot empty returns 130.

  6. Task 6 · interview question

    Interview: Tray.java, predict what two references print

    In Tray.java, make predictOutput() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (a number like 7, or true or false in lower case).

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

What you will learn

  • Classes and fields: a class describes what every object holds, and new builds one.
  • Instance methods: methods without static, which work on one object's own fields.
  • References: a variable holds the way to an object, not the object, so two variables can share one object, and null means no object at all.
  • toString: how an object prints.

More in Objects

Build "Classes and objects" 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.