Core Java · Chapter 8 of 48
Methods
By the end of this chapter you can write methods that take what they need and return an answer, overload them, accept any number of values, say exactly what a call can change, and write a method that calls itself. 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
The counter code has grown: the same lines for a total sit in four places, and Ravi found a bug in only three of them. He also wants to add a delivery to every counter's stock in one call, and Meera wants a countdown on the screen before the first chai of the day.
Copied code means every fix must be made everywhere, and one is always missed. A method gives the work one name and one place. But you have to know what a call can and cannot change in the caller's variables, or the delivery lands nowhere.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Orders.java: overload total for one cup, many cups and a named item
In Orders.java, write the three overloads of total. total(price) returns what one cup at that price costs. total(price, qty) returns price times qty, and 0 cups cost 0. total(item, qty) looks the item's price up with priceOf, which is already written, and returns what qty of it cost. All three return rupees as an int.
For example, total(40) returns 40, total(40, 3) returns 120, total(25, 0) returns 0, total("bun maska", 2) returns 100 and total("cutting chai", 3) returns 75.
- Task 2
Orders.java: add up any number of amounts
In Orders.java, make sum(amounts) return all the amounts added up. It takes any number of int amounts, or an int array, and no amounts at all returns 0.
For example, sum(40, 25, 50) returns 115, sum(40) returns 40, sum() returns 0 and sum of an array holding 40 and 50 returns 90.
- Task 3
Orders.java: restock every counter
In Orders.java, make addToAll(stock, cups) add cups to every slot of the stock array it is given. It returns nothing: the caller's own array must show the change. An empty array is fine and must not throw.
For example, with stock 10, 0, 35, after addToAll(stock, 50) the caller's stock is 60, 50, 85.
- Task 4
Orders.java: count down to chai with recursion
In Orders.java, make countdown(n) return a countdown from n, which is 0 or more, to Chai!, with the numbers and the word separated by single spaces. Write it with recursion, with countdown(0) as the base case.
For example, countdown(0) returns Chai!, countdown(1) returns 1 Chai!, countdown(3) returns 3 2 1 Chai! and countdown(10) returns 10 9 8 7 6 5 4 3 2 1 Chai!.
- Task 5 · interview question
Interview: Orders.java, predict pass by value or by reference
In Orders.java, make predictCalls() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them.
The check runs the same code and compares your list with what Java really printed.
What you will learn
- Overloading: several methods with one name and different parameters.
- Varargs: a parameter that takes any number of values, as an array.
- Pass by value: Java copies every argument, and for an array the copy is a reference.
- Recursion: a method that calls itself, down to a base case.
More in First steps
Build "Methods" 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.