Unit 1 wrap-upPut it together

| Topic | The one thing to remember |
|---|---|
| Errors (1.1) | Compiling proves only that the rules were followed |
| Types (1.2) | int, double, boolean hold values; String and objects hold references |
| Expressions (1.3) | int / int drops the decimals; a String makes + glue |
| Casting (1.5) | (int) chops toward zero; int overflows silently |
| Methods (1.7-1.11) | Match the signature; use what non-void methods return |
| Objects (1.12-1.14) | new makes an object; = copies the arrow, not the object |
| Strings (1.15) | Indexes 0 to length() - 1; substring stops before the end index |
?Wait, what's the difference between a class method and an instance method again?
Three threads run through this unit. First, types decide behavior: int / int truncates, a String turns + into glue, and a method's return type decides where its call can go. Second, evaluation is mechanical: Java works through every expression in a fixed order, so you can always trace it step by step. Third, variables of class types hold arrows: new makes an object, = copies arrows, and Strings never change, they get replaced.
I thought about it. Show me
Traps that cost points
Unit 1 syntax sheet
7 / 2 7 % 2 7 / 2.0(double) a / b(int) (x + 0.5)x += 3; x++;Integer.MAX_VALUEMath.abs(x) Math.pow(b, e) Math.sqrt(x)(int) (Math.random() * (max - min + 1)) + minBottle b = new Bottle(500);b.add(5); int n = b.getMl();s.length() s.indexOf(t)s.substring(a, b) s.substring(a)s.equals(t) s.compareTo(t) < 0FRQ strategy for Unit 1
- Unit 1 lives mostly in Question 1, Methods and Control Structures (7 points). Part B usually works with Strings, so the String methods have to be automatic.
- Read the method header first: its return type, its parameters, and whether it's static. Return that type on every path, and return it, don't print it.
- When the question says to call a given method, call it with the right arguments and use what it returns. Rewriting its logic yourself loses that point.
- Check every division: do you want an int or a double? Check every substring against the shortest String the preconditions allow.
- Use only Quick Reference methods. Never hard-code an index that depends on the data: find it with indexOf or length().
- On multiple choice, trace with a pencil: number String letters from 0, keep a trace table for variables, and draw arrows for objects.
- The first letter of first is first.substring(0, 1). The first four letters of last are last.substring(0, 4).
Why?
substring(0, 4) takes indexes 0 to 3: four characters. The precondition promises last is long enough, so no check is needed. - The last two digits of year are year % 100: 27 for 2027, but 9 for 2009. Concatenating 9 would give "KAdam9", one digit short.
Why?
This is the trap the examples are warning you about. Always test the example that looks unusual. - Trick: add 100 first. year % 100 + 100 is between 100 and 199, so it always has three digits: 127 or 109. Turn it into a String and drop the 1 with substring(1): "27" or "09".
Why?
"" + number makes a String from a number. substring(1) skips index 0, the leading 1. - Glue the three pieces and return the result (don't print it).
Answer:
public static String username(String first, String last, int year)
{
String yy = "" + (year % 100 + 100);
return first.substring(0, 1) + last.substring(0, 4)
+ yy.substring(1);
}username("Maria", "Lopez", 2027) returns "MLope27" and username("Ken", "Adams", 2009) returns "KAdam09" (checked under Java 17).
Stuck? Get a hint
- Build the answer in three pieces: one letter, four letters, two digits.
- year % 100 gives the last two digits, but as a number. What goes wrong for 2009?
- Try year % 100 + 100, then turn it into a String and take substring(1).
String s = "Toledo"; System.out.println(s.substring(0, 1) + (s.length() - 1));
- AT5
- BTod5
- CT4
- DTdo5
Show the answer
A. s.length() is 6, and the parentheses make s.length() - 1 happen first: 5. substring(0, 1) is "T", and "T" + 5 is "T5". Without the parentheses, "T" + 6 would be glued first, and subtracting 1 from a String doesn't compile. Choice C forgets that "Toledo" has 6 letters.
Bottle x = new Bottle(10); Bottle y = x; y.add(5); System.out.println(x.getMl() + " " + y.fractionFull());
- A5 0.5
- B5 0.0
- C0 0.5
- D10 1.0
Show the answer
A. One new, so x and y are aliases of the same object, which starts at 0 ml. y.add(5) changes that object to 5 ml, so x.getMl() is 5. fractionFull casts ml to double before dividing: 5.0 / 10 is 0.5. Choice C treats y as a separate bottle, and choice B forgets the cast inside fractionFull.
