A method’s contract says what inputs mean and what result to provide. This task uses a simple booking rule to practise parameters, branches and returns. It is not asking for keyboard input, arrays or objects. main can supply example values.
The brief
A session costs 200 cents plus 50 cents for each requested block. A member gets 100 cents off the total, but the result must never become negative. Inputs for blocks are whole numbers from 0 through 100. Write price(int blocks, boolean member) returning cents. Keep printing outside the calculation method.
Use the practice cases as examples, not as a lookup table. Zero blocks and no membership costs 200, two blocks with membership costs 200, and three blocks without membership costs 350. The general arithmetic and membership rule must also work for other allowed values. There is no floating-point money in this exercise: cents are whole numbers.
Plan before typing
Name the subtotal, apply the membership discount if needed, then enforce the minimum. With today’s 100-cent discount and nonnegative block count, the price will not actually fall below zero. Keep the minimum rule because it is part of the brief; the larger-discount experiment at the end will exercise that branch. Draw those steps in order. Returning before applying a later rule would skip that rule. A temporary variable can make the steps clearer than a long expression, and it is local to each invocation.
public class Main {
public static void main(String[] args) {
int bill = charge(3);
System.out.println("Cents: " + bill);
}
static int charge(int count) {
return 100 + count * 20;
}
}Check your prediction
Cents: 160 This is a different price rule, so copying its constants would fail the checkpoint. The useful pattern is a method returning an amount for main to describe.
public class Main {
public static void main(String[] args) {
int result = floorAtZero(50 - 100);
System.out.println(result);
}
static int floorAtZero(int value) {
if (value < 0) {
return 0;
}
return value;
}
}Check your prediction
0 The intermediate subtraction may be negative even if the final contract forbids a negative price. Distinguish the steps instead of assuming all intermediate values already meet the final rule.
Your turn
Implement the booking rule
Write price(blocks, member) as specified. Its ordinary main should print 200 for two blocks with membership. The practice cases also check (0,false)→200, (0,true)→100 and (3,false)→350. Add (3,true)→250 to your own reasoning.
public class Main {
public static void main(String[] args) {
System.out.println(price(2, true));
}
static int price(int blocks, boolean member) {
return 0; // Replace the placeholder.
}
}Hint 1: the idea
Build the price in stages. Membership is a separate boolean decision after the per-block calculation.
Hint 2: a first step
Use a local cents variable initialized from the base and blocks. Return only after all applicable rules have been applied.
Show one solution
public class Main {
public static void main(String[] args) {
System.out.println(price(2, true));
}
static int price(int blocks, boolean member) {
int cents = 200 + blocks * 50;
if (member) {
cents -= 100;
}
if (cents < 0) {
return 0;
}
return cents;
}
}200 Change it and explain
Explain why a method that prints the right price but returns zero fails the practice cases. Then change the membership discount to 250 for an experiment: which zero-block case now needs the floor? Restore the stated rule afterward.
Write a brief contract for a different method: remaining pages in a book, never below zero. Implement it from that contract without opening today’s earlier solution. Before the next day, be comfortable calling a method and using its returned value in a comparison.
Completion is saved in this browser only. It does not measure mastery.