AP Computer Science A, Unit 3
Exam words: Class Creation
The 24 words a written answer in this unit needs. For each one: what it means, a wording that loses the mark and why, and a sentence that uses it the way a full answer would.
Study these words as a deck
Flashcards, Learn, Test and Match in Cards, with your Still learning marks kept, and the booklet to print on A4 or US Letter. Sign in to open it.
The words
abstraction
Keeping only the details a program needs and hiding how the rest works.
Loses the mark: “a good class stores every detail of the thing it models”. A class keeps only what the program uses; storing more adds work and risk with no benefit.
Example: Through abstraction, the Larchmont library's `Patron` class stores a name, a card number and loans, and leaves out height and hair color.
field
A variable declared in a class, outside any method, that each object keeps.
Loses the mark: “every object of a class shares one copy of each field”. Each object has its own copy of every instance field; only a static field is shared.
Example: The `Plant` class in the Greenleaf app has the field `private int water;`, so the fern and the ivy each keep their own water level.
state
The values an object's fields hold at one moment.
Loses the mark: “an object's state is its list of methods”. The methods are its behavior; the state is the values in its fields.
Example: After `fern.addWater(3);`, the state of the Greenleaf `fern` object changes: its `water` field goes from 2 to 5.
encapsulation
Keeping a class's fields private so other code reaches them only through its methods.
Loses the mark: “private keeps the data secret from users and attackers”. Private limits which code can touch a field; it is a design rule, not a security barrier.
Example: With encapsulation, the Northgate `Locker` class keeps `code` private, and other classes can only use `open(String guess)`.
private
Access that lets only code inside the same class use a field or method.
Loses the mark: “a private field cannot be used even by its own class's methods”. Every method of the class can read and change its private fields.
Example: Because `water` is private, the Greenleaf `Plant` class's own `addWater` method changes it, but `fern.water = 9;` in another class does not compile.
public
Access that lets code in any class use a class, constructor or method.
Loses the mark: “a field must be public for other classes to use its value”. A public accessor such as `getPrice()` lets other classes read a private field.
Example: The kiosk class calls `ticket.getPrice()`, which is public, while the Lantern `Ticket` class's `price` field stays private.
access modifier
A keyword such as public or private that sets which classes may use a member.
Loses the mark: “a class's design matters only to the programmers”. What a class stores and shows decides what its users can do and what can go wrong for them.
Example: In the Harbor clinic's `Patient` class, the access modifier `private` on `history` means only `Patient`'s own methods read it.
constructor
Code with the class's name and no return type that sets a new object's fields.
Loses the mark: “a constructor needs a return type such as void”. A constructor has no return type; adding `void` turns it into an ordinary method.
Example: The constructor `public Tile(int size, String stone)` in the Flagstone app sets both fields, so `new Tile(30, "slate")` starts with size 30.
default constructor
The constructor with no parameters that Java supplies only when a class declares none.
Loses the mark: “every class always has a constructor with no parameters”. Once a class declares any constructor, Java adds no default constructor, so `new Stall()` may not compile.
Example: Because the Market Row `Stall` class declares `Stall(String name)`, it has no default constructor, and `new Stall()` does not compile.
default value
A field's value if never set: 0, 0.0, false or null, by its type.
Loses the mark: “every field the constructor leaves unset holds null”. Only reference fields start at null; an `int` field starts at 0 and a `boolean` at false.
Example: The Glowworm `Lamp` constructor never sets `lit`, so the field keeps its default value, false, and `brightness` stays 0.
accessor method
A method that returns a field's value, or something computed from it, without changing it.
Loses the mark: “an accessor prints the field's value”. An accessor returns the value to the caller; printing is the caller's choice.
Example: `getCoins()` is an accessor method in the Cloverdale `Jar` class: it returns `coins` and leaves the field unchanged.
mutator method
A method that changes one or more of an object's fields, usually returning nothing.
Loses the mark: “a method that returns a new value has changed the field”. `withBonus(10)` can return coins + 10 without storing it; only an assignment to the field changes it.
Example: `addCoins(int n)` is a mutator method in the Cloverdale `Jar` class: `coins += n;` changes the field, and it returns nothing.
void method
A method with return type void, which does its work and returns no value.
Loses the mark: “a void method gives back the value it changes”. A void method returns no value, so its call cannot be stored or printed.
Example: `addSongs(int n)` in the Tempo playlist class is a void method, so `int k = list.addSongs(3);` does not compile.
return statement
A statement that ends a method at once, sending back a value unless void.
Loses the mark: “statements after a return still run”. A return ends the method immediately, so the lines after it in that path never run.
Example: The return statement `return "gold";` in the Medalist app ends `medal(int score)` at once, so the lines below it do not run for a score of 95.
toString
The method giving an object's text form, used when it is printed or joined.
Loses the mark: “printing an object shows its fields without a toString”. Without its own `toString`, printing an object shows its class name and a code, not its fields.
Example: With `toString` written to return `"Route " + number`, `System.out.println(r);` in the Cityline bus app prints "Route 14".
alias
Another variable holding a reference to the same object, so changes show through both.
Loses the mark: “t2 = t1 gives t2 its own copy that starts the same”. `t2 = t1;` copies the reference, so t1 and t2 are aliases of one object.
Example: After `Tally t2 = t1;`, t2 is an alias of t1 in the Northfield gym app, so `t2.add(5);` raises the count that `t1.getCount()` returns.
pass by value
Java copies each argument's value into the parameter; for an object, it copies the reference.
Loses the mark: “a method gets its own copy of an object passed to it”. Only the reference is copied; the method's parameter points at the caller's object.
Example: Because Java uses pass by value, `judge(Entry e)` in the Blue Ribbon fair app gets a copy of the reference, so `e.addPoints(5)` changes the caller's entry.
mutable
Able to change after it is made, as an object with mutator methods is.
Loses the mark: “a method that returns an object always returns a separate copy”. It returns a reference; if the object is mutable, changes through it show in the original.
Example: A `Drone` in the Skyward app is mutable, so after `Spot s = d.getSpot(); s.shift(2);` the drone's own spot has moved.
garbage collection
Java freeing the memory of objects that no variable refers to any longer.
Loses the mark: “an object is deleted as soon as one variable stops pointing at it”. It is freed only when no reference to it remains anywhere.
Example: After `seat = new Seat(30);`, if nothing else refers to the old Seat 12 in the Starlight Hall app, garbage collection can free it.
static variable
A field declared static: one copy belongs to the class and every object shares it.
Loses the mark: “each object gets its own copy of a static field”. There is one copy for the whole class, so every object sees the same value.
Example: The Visitor class's static variable `count` goes up in each constructor, so after three visitors in the Fairground app `Visitor.getCount()` is 3.
constant
A variable declared final, whose value cannot change after it is first set.
Loses the mark: “final and static mean the same thing”. `final` means the value cannot change; `static` means the class shares it.
Example: The Seaview ferry class declares the constant `public static final int MAX_SEATS = 120;`, so `MAX_SEATS = 130;` does not compile.
local variable
A variable declared inside a method or block, existing only until that block ends.
Loses the mark: “a local variable with a field's name is the field”. Declaring `int sales` in a method makes a new local variable; the field is untouched.
Example: In the Juniper till's `ring` method, `int calls = 0;` is a local variable, so it starts at 0 on every call and is gone when the method ends.
scope
The part of a program where a variable's name can be used.
Loses the mark: “a variable declared inside a block can be used after it”. A variable declared in a block is out of scope after the closing brace.
Example: `best` declared inside the if block of the Willow scoring app is out of scope at the `return best;` below the block, so the method does not compile.
this
The keyword naming the object a method or constructor is running on.
Loses the mark: “name = name; copies the parameter into the field”. Both names mean the parameter, so it copies the parameter into itself; `this.name = name;` sets the field.
Example: The Honeycomb jar constructor writes `this.jars = jars;`, using this so the parameter's value lands in the field.