Unit 3 wrap-upPut it together

| Word | What it means | Where you use it |
|---|---|---|
| public | Any class can use it | The class, its constructors, methods clients call |
| private | Only code inside this class can use it | Every instance variable; helper methods |
| static | Belongs to the class: one shared copy | Counters, constants, utility methods |
| final | Can be assigned only once | Constants such as MAX_ITEMS |
| void | Returns nothing | Methods that only change state or print |
| return | Ends the method and sends a value back | Every path of a non-void method |
| new | Builds an object and runs a constructor | Client code, and constructors that copy |
| this | The object whose method is running | this.x = x; passing or returning the current object |
Three threads run through the whole unit. First, the author protects the object: private data, constructors that set everything, mutators that keep values valid. Second, references: an object variable is an arrow, so assignments and parameters make aliases, and only new makes a new object. Third, every name has a home: instance, static, parameter or local, and the home decides where it can be used and how long it lives.
?Wait, accessor or mutator: which is which?
I thought about it. Show me
The Class Design FRQ
Question 2 on the exam is always Class Design, worth 7 points. You get a description of a class and a table of example calls with their results, and you write the entire class: header, instance variables, constructor and methods. No starter code. The table is the specification, so read it row by row before you write anything.
| Statement | Returns | Explanation |
|---|---|---|
| ParkingMeter m = new ParkingMeter(120); | holds at most 120 minutes; starts at 0 | |
| m.isExpired() | true | no time on the meter |
| m.addQuarters(5) | 75 | 5 quarters is 75 minutes |
| m.passTime(30); | 45 minutes left | |
| m.isExpired() | false | |
| m.addQuarters(6) | 75 | 45 + 90 = 135, capped at 120: 75 added |
| m.passTime(200); | time never goes below 0 | |
| m.isExpired() | true |
1public class ParkingMeter12{3 private int maxMinutes;24 private int minutesLeft;256 public ParkingMeter(int maxMinutes)37 {8 this.maxMinutes = maxMinutes;39 minutesLeft = 0;310 }1112 public int addQuarters(int q)413 {14 int before = minutesLeft;515 minutesLeft += 15 * q;516 if (minutesLeft > maxMinutes)517 {518 minutesLeft = maxMinutes;519 }520 return minutesLeft - before;521 }2223 public void passTime(int minutes)424 {25 minutesLeft -= minutes;626 if (minutesLeft < 0)627 {628 minutesLeft = 0;629 }630 }3132 public boolean isExpired()433 {34 return minutesLeft == 0;735 }36}
- line 1 Point 1: public class header
- line 3-4 Point 2: private instance variables for what must be remembered
- line 6-9 Point 3: constructor sets every instance variable
- line 12-32 Point 4: public method headers with the right return types
- line 14-20 Point 5: add, cap, and return what was really added
- line 25-29 Point 6: subtract, never below 0
- line 34 Point 7: the boolean answer, from the state
- Header: public class ParkingMeter.
Why?
Point 1 is often just the header. Spell the name exactly as the problem does. - What must it remember between calls? The maximum and the minutes left. private int maxMinutes; private int minutesLeft;
Why?
Point 2. The number of quarters doesn't need to be stored: it's used once, inside addQuarters. - Constructor: public ParkingMeter(int maxMinutes) { this.maxMinutes = maxMinutes; minutesLeft = 0; }
Why?
Point 3 checks that every instance variable is set, including the one with no parameter. - Headers from the table: addQuarters returns a number, so public int addQuarters(int q). passTime returns nothing, so public void passTime(int minutes). isExpired returns true or false, so public boolean isExpired().
- addQuarters: remember the old time, add 15 * q, cap at maxMinutes, and return the new time minus the old.
Why?
The highlighted row: 45 + 90 = 135 is capped to 120, so only 75 minutes were added. Returning 15 * q (90) is the most common lost point. - passTime: subtract, then if minutesLeft < 0, set it to 0.
- isExpired: return minutesLeft == 0;
Why?
Return the boolean expression itself. No if needed.
Answer: See the complete class below. Running the table's calls prints true, 75, false, 75, true.
Stuck? Get a hint
- List what the meter must remember, then turn each table row into a method header.
- For addQuarters, save the old minutes before you change anything.
- The amount added is the new time minus the old time, after the cap.
Traps that cost points
Unit 3 code patterns
private int minutesLeft;private static int placed = 0;public static final int MAX = 5;public Meter(int max) { this.max = max; }public int getMax() { return max; }public void reset() { minutesLeft = 0; }bed = new Plot(p.getWidth(), p.getLength());c.addMember(this); return this;Order.isValid(7)FRQ strategy for Class Design
- Read the description and the whole example table first. Circle every value the object must remember from one call to the next: those are your instance variables.
- Write the class header and the private instance variables before anything else. They're quick points.
- Write the constructor next and set every instance variable, from a parameter or a fixed starting value.
- Turn each different method in the table into a header. Pick the return type from what the call gives back: a number, a String, true or false, or nothing.
- Write each method body, then check it against every table row, especially the strange ones: caps, resets, ties and empty cases.
- Return what the problem says to return, on every path. Don't print unless it says print.
- Use the problem's exact names. A misspelled method name can cost the point for that method.
ParkingMeter m = new ParkingMeter(60); m.addQuarters(3); m.passTime(10); System.out.println(m.addQuarters(2));
- A25
- B30
- C60
- D20
Show the answer
A. 3 quarters add 45 minutes (45 left). passTime(10) leaves 35. Two quarters would make 65, which is capped at 60, so only 25 minutes were added. The second choice is the top trap: 15 * 2 = 30 ignores the cap.
- Atitle = title; length = seconds;
- Bthis.title = title; length = seconds;
- Cthis.title = title; int length = seconds;
- Dtitle = this.title; this.length = seconds;
Show the answer
B. The parameter title hides the instance variable, so you need this.title = title. length has no name clash, so length = seconds works. The first choice assigns the parameter to itself, the third creates a local length, and the fourth copies the instance variable's default (null) into the parameter, which is backwards.
