AP Computer Science A, Unit 1
Exam words: Using Objects and Methods
The 61 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
algorithm
An exact, finite list of steps that carries out a task when followed in order.
Loses the mark: “an algorithm is any piece of Java code”. An algorithm is the plan of steps; it can be written in Java, in another language or in plain words.
Example: The algorithm for the Lakeside bus app's fare total is: start at 0, add each ride's fare, then print the total; the Java method follows those steps in order.
source code
A program's text as a programmer writes it, before the compiler translates it.
Loses the mark: “the computer runs the source code directly”. Java source code is first compiled into bytecode, and the Java Virtual Machine runs that.
Example: The source code for the fictional Ridgeview scoreboard is the text in `Scoreboard.java`, which must compile before any of it can run.
compiler
The tool that checks source code against Java's rules and translates it into runnable bytecode.
Loses the mark: “the compiler checks that the program gives the right answer”. The compiler checks only that the code follows Java's rules; a program can compile and still compute the wrong value.
Example: The compiler rejected `PlantLog.java` because line 6 was missing a semicolon, so no line of the program ran.
compile-time error
A break in Java's rules that the compiler reports, so the program never runs.
Loses the mark: “the lines before the compile-time error still run”. A program with a compile-time error is never run at all, so not even the earlier lines print.
Example: Writing `int seats = "forty";` is a compile-time error: a String cannot be stored in an `int`, so the class does not compile.
run-time error
An error that stops a compiled program while it runs, usually by throwing an exception.
Loses the mark: “a run-time error means nothing is printed”. Every statement before the failing one has already run, so its output is already printed.
Example: The Corbin lap timer printed its first two lines and then hit a run-time error at `total / laps`, because `laps` was 0.
exception
An object Java throws when a statement cannot be carried out, stopping the program.
Loses the mark: “an exception is thrown whenever the answer is wrong”. Java throws an exception only when a statement cannot be done, such as dividing an int by zero; a wrong answer throws nothing.
Example: Dividing an `int` by zero throws an exception, an `ArithmeticException`, so the Maple Street pool app stops at that line.
logic error
A mistake that lets a program compile and run but produce the wrong result.
Loses the mark: “a program that compiles and runs has no errors”. The compiler cannot tell what the program was meant to do, so a wrong result can still come out.
Example: The Glenwood rectangle method has a logic error: it returns `width + height` for the area, so a 3 by 4 rectangle gives 7 instead of 12.
debugging
Finding and fixing a program's errors, often by testing inputs with known answers.
Loses the mark: “if the program runs without a message it does not need testing”. A clean run says nothing about the result; testing with a known answer is what finds a logic error.
Example: While debugging the Fernhill tip calculator, Rosa tested a $20 bill with a known answer of $3 and saw it print 0.
variable
A named storage place that holds one value of its declared type at a time.
Loses the mark: “a variable takes the type of whatever is stored in it”. A Java variable keeps the type it was declared with for its whole life.
Example: The variable `seatsLeft` in the Harbor Theater app holds one `int` at a time, so `seatsLeft = 12;` replaces whatever it held before.
data type
The kind of value a variable holds, which decides what can be done with it.
Loses the mark: “quotes make no difference to the type”. `"true"` in quotes is a String, not a boolean, so it cannot go in a boolean variable.
Example: The data type of `price` in the Pinecrest cafe app is `double`, so it can hold 4.75, while `cups` is an `int` and holds only whole numbers.
primitive type
A built-in type, such as int, double or boolean, whose variable holds the value itself.
Loses the mark: “a declared variable starts at 0”. A local variable has no value until it is assigned, and the code that reads it first does not compile.
Example: Because `int` is a primitive type, `int visits = 40;` stores the number 40 in the variable itself.
int
Java's primitive type for whole numbers, positive, negative or zero, within a fixed range.
Loses the mark: “an int accepts a decimal and rounds it”. Assigning 3.7 to an `int` does not compile; a decimal must be cast first, and a cast drops the fraction.
Example: The Larchmont library app counts books in an `int`, since a count of 312 books is always a whole number.
double
Java's primitive type for numbers with a decimal part, such as 2.5 or 12.0.
Loses the mark: “a double holding a whole number prints like an int”. A `double` always prints with a decimal point, so 12 stored in a double prints as 12.0.
Example: The Spokewise bike-share app stores each ride's distance as a `double`, so `double miles = 12;` prints as 12.0.
reference type
A class type, such as String, whose variable holds a reference to an object.
Loses the mark: “a String variable contains its text the way an int contains its number”. A String variable holds a reference that points to a String object; the text lives in the object.
Example: `String` is a reference type, so `String venue = "Hall";` stores in `venue` a reference to a String object holding the text.
declaration
A statement that names a variable and gives its type, optionally with a first value.
Loses the mark: “changing a variable means declaring it again”. A variable is declared once; giving it a new value is an assignment, and declaring the name again does not compile.
Example: The declaration `int tickets = 0;` in the Rivertown fair app creates `tickets`; later lines change it with `tickets = 5;`, not by declaring it again.
expression
A combination of values, variables, operators and method calls that Java evaluates to one value.
Loses the mark: “Java works out an expression strictly from left to right”. Java applies operator precedence first: `*`, `/` and `%` before `+` and `-`.
Example: In the Kestrel gym app, the expression `4 + 6 * 2` evaluates to 16, because the multiplication is done first.
integer division
Dividing one int by another: the result is an int, with the remainder dropped.
Loses the mark: “integer division rounds to the nearest whole number”. Java drops the fractional part; it never rounds, so `19 / 5` is 3, not 4.
Example: Because of integer division, `int teams = 23 / 4;` in the Brookside league app gives 5 teams, and the 3 leftover players are dropped.
remainder operator
The % operator: the whole-number remainder left after dividing one int by another.
Loses the mark: “the % operator gives the quotient”. `19 % 5` is the remainder, 4; the quotient, 3, comes from `19 / 5`.
Example: The Glen Park parking app uses the remainder operator: `minutes % 60` gives the minutes past the hour, so 135 minutes leaves 15.
operator precedence
The rules for which operator Java applies first when an expression has several.
Loses the mark: “addition always comes first because it is written first”. Multiplication, division and remainder take precedence over addition and subtraction whatever their order.
Example: By operator precedence, `total = 10 + 3 * 2;` in the Ashford snack bar app stores 16, because `3 * 2` is worked out before the addition.
compound assignment operator
An operator such as += that combines arithmetic with assignment: x += 3 means x = x + 3.
Loses the mark: “price += 2 puts 2 into price”. `price += 2` adds 2 to the value already in `price`.
Example: The compound assignment operator in `points += 5;` adds 5 to the Ridgeline quiz score, so 12 becomes 17.
increment operator
The ++ operator, which adds 1 to a variable; -- is the decrement operator, which subtracts 1.
Loses the mark: “count++ makes count equal to 1”. `count++` adds 1 to whatever `count` holds, so 4 becomes 5.
Example: Each time a visitor enters, the Hilltop museum counter runs `visitors++;`, the increment operator, so 41 becomes 42.
println
System.out.println prints its value and then ends the line; System.out.print does not end it.
Loses the mark: “print and println both end the line”. Only `println` ends the line; text printed with `print` is followed on the same line by the next output.
Example: In the Tidewater menu app, `System.out.print("Soup ");` then `System.out.println("Salad");` shows "Soup Salad" on one line, and the next output starts a new line.
assignment statement
A statement that stores the value on the right of = in the left variable.
Loses the mark: “assignment links two variables”. `best = score;` copies the value in `score` now; a later change to `score` does not change `best`.
Example: After the assignment statement `best = score;` in the Oakmont bowling app, `best` holds a copy of 180, and adding to `score` later leaves `best` at 180.
assignment operator
The single = sign, which assigns a value to a variable; it does not test equality.
Loses the mark: “the = sign states that two things are equal”. `=` assigns; `stock = stock - 3;` takes 3 from stock, and `==` is the operator that compares.
Example: In `stock = stock - 3;` the assignment operator stores 17 in `stock` when the Bayview cafe sells 3 of its 20 mugs.
initialization
Giving a variable its first value, often in the same statement that declares it.
Loses the mark: “a variable that is never initialized holds 0”. A local variable with no first value cannot be read; the code does not compile.
Example: The initialization `int total = 0;` gives the Westbrook step counter a starting value before the loop adds each day's steps.
input
Data a program reads while it runs, from a user, a file or another program.
Loses the mark: “digits typed as input are a number however they are read”. Digits read as a String are text: "12" + 5 joins to "125"; they must be read or parsed as an int to add.
Example: The Cedar Hills survey program reads each answer as input with a `Scanner`, then stores it in a variable before the next line runs.
casting
Converting a value to another type by writing that type in parentheses before it.
Loses the mark: “a cast after an int calculation repairs it”. `(double) (7 / 2)` is 3.0, because the integer division happens before the cast.
Example: Casting one operand, `(double) points / games`, makes the Northgate team's average 3.5 rather than the 3 that integer division gives.
truncation
Dropping the decimal part of a number, as a cast from double to int does.
Loses the mark: “casting to int rounds to the nearest whole number”. `(int) 7.8` is 7: the cast cuts off the decimal part and never rounds.
Example: Because of truncation, `(int) 7.8` gives 7, so the Clearwater pizza app adds 0.5 first, `(int) (7.8 + 0.5)`, to round to 8.
integer overflow
An int result past the type's range, which wraps around to the other end.
Loses the mark: “going past the largest int causes an error”. Java throws nothing; the result silently wraps to a large negative number.
Example: Doubling 1,500,000,000 in an `int` causes integer overflow, so the Starward game's score prints as a negative number with no error.
Integer.MAX_VALUE
The largest value an int can hold, 2,147,483,647; Integer.MIN_VALUE is the smallest.
Loses the mark: “Integer.MAX_VALUE + 1 is a bigger int”. Adding 1 to `Integer.MAX_VALUE` overflows and gives `Integer.MIN_VALUE`.
Example: Adding 1 to `Integer.MAX_VALUE` in the Quarry Road counter gives `Integer.MIN_VALUE`, -2,147,483,648, because the `int` range has run out.
API
Application programming interface: the documented classes and methods a program can use from a library.
Loses the mark: “using a method requires reading its code”. The API documentation says what a method takes and returns, so it can be used without seeing its code.
Example: The `Math` class's API says `Math.sqrt` takes a `double` and returns its square root, so the Elmwood geometry app calls it without reading its code.
library
A collection of ready-made classes and methods that programs use instead of writing their own.
Loses the mark: “a library class must be copied into the program before it can be used”. A program uses a library class by name, importing it where needed; its code is not copied in.
Example: The Seaside weather app uses the Java library's `Math` and `ArrayList` classes rather than writing its own square root or list.
documentation
A written description of what a method does, what it needs and what it returns.
Loses the mark: “documentation describes how the method's code works line by line”. Documentation says what a method does and how to call it, not how its insides work.
Example: The documentation for the Pinewood `Booking` class says `cancel()` returns `true` if a booking was removed, so a caller can use it without reading the method.
comment
Text in source code that the compiler ignores, written for people reading the program.
Loses the mark: “when a comment and the code disagree, the comment wins”. The compiler ignores comments, so the code does what the code says.
Example: The comment `// add a 10 point bonus` above `score += 5;` in the Fairview quiz app is wrong, and the program adds 5, as the code says.
precondition
What must be true before a method is called for it to work as described.
Loses the mark: “a method checks its own precondition”. A precondition is the caller's job; the method may assume it and is not required to check it.
Example: The precondition of `boardsNeeded(int lengthCm)` in the Cedar deck planner is that `lengthCm` is greater than 0, so a caller must not pass 0.
postcondition
A condition a method promises is true when it finishes, if its precondition held.
Loses the mark: “a postcondition holds whatever the arguments”. The promise holds only when the precondition was met; with bad arguments nothing is promised.
Example: The postcondition of `sortPrices` in the Marigold store app is that the array is in increasing order when the method returns.
method
A named block of code that does one job and runs when it is called.
Loses the mark: “a method runs as soon as the class is compiled”. A method's body runs only when some statement calls it.
Example: The Brightwater pool app has a method `lapsToMeters(int laps)`; its body runs each time a statement calls it.
method signature
A method's name together with its list of parameter types, which tells calls apart.
Loses the mark: “two methods with the same name always clash”. Methods may share a name if their parameter lists differ; Java picks one by the arguments' types and number.
Example: The method signature `fee(int, double)` differs from `fee(int)` in the Ellis Park rink class, so both methods can exist.
parameter
A variable in a method's header that receives a value when the method is called.
Loses the mark: “changing a parameter changes the caller's variable”. A parameter holds a copy of the argument's value, so assigning to it leaves the caller's variable alone.
Example: In `tip(double bill)` from the Harbor Grill app, the parameter `bill` receives a copy of 40.0 when the call is `tip(40.0)`.
argument
A value passed to a method in a call, copied into the matching parameter.
Loses the mark: “the argument and the parameter are the same variable”. The argument's value is copied into the parameter; they are separate variables.
Example: In the call `area(3, 4)`, the arguments 3 and 4 are copied into the parameters `width` and `height` in that order.
return type
The type before a method's name, saying what kind of value it returns, or void.
Loses the mark: “Math.pow gives an int for int arguments”. `Math.pow` has return type `double`, so `Math.pow(2, 4)` is 16.0.
Example: The return type of `public static double average(int a, int b)` in the Elm Street grades app is `double`, so a call to it gives a decimal value.
return value
The value a method sends back to the statement that called it.
Loses the mark: “printing a value and returning it are the same”. A returned value goes back to the caller; nothing appears on screen unless some code prints it.
Example: The return value of `Math.abs(-25)` is 25, which the Summit hiking app stores in `drop` without printing anything.
static method
A method that belongs to the class, called with the class name, not an object.
Loses the mark: “every method must be called on an object”. A static method is called through its class, as in `Math.sqrt(9.0)`, with no object made.
Example: `Math.sqrt` is a static method, so the Riverside geometry app calls `Math.sqrt(49.0)` with the class name and no `Math` object.
method overloading
Writing several methods with one name but different parameter lists in the same class.
Loses the mark: “Java picks an overloaded method by its name alone”. The name is shared, so Java picks by the number and types of the arguments.
Example: Through method overloading, the Lantern ticket class has `price(int age)` and `price(int age, boolean member)`, and the call's arguments pick one.
Math class
Java's class of static math methods, such as Math.abs, Math.pow and Math.sqrt.
Loses the mark: “Math.sqrt of an int returns an int”. `Math.sqrt` returns a `double`, so `Math.sqrt(16)` is 4.0.
Example: The Math class gives the Lindell surveying app `Math.sqrt(x * x + y * y)`, a `double`, for the straight-line distance.
Math.random
A static method returning a random double from 0.0 up to, but not including, 1.0.
Loses the mark: “Math.random() can return 1.0”. The result is always less than 1.0, so `(int) (Math.random() * 6)` is never 6.
Example: The Twin Oaks dice game rolls with `(int) (Math.random() * 6) + 1`, since `Math.random()` is below 1.0 and the cast drops the decimal.
class
A blueprint that defines the fields and methods every object of that type will have.
Loses the mark: “the class is itself one object that every variable shares”. A class describes objects; each object made from it has its own copy of the instance fields.
Example: The class `Tally` in the fictional Northfield gym app defines a count field and a `click()` method, and each `Tally` object made from it keeps its own count.
object
One instance of a class, with its own field values and the class's methods.
Loses the mark: “every object of a class shares the same field values”. Each object has its own instance fields, so two `Tally` objects can hold different counts.
Example: `new Tally("Lobby")` makes an object of the class `Tally`, and clicking it leaves every other `Tally` object's count unchanged.
new
The keyword that creates an object through a constructor and returns a reference to it.
Loses the mark: “a constructor is called like any other method”. A constructor runs only through `new`; `Tally("Lobby", 12)` on its own does not compile.
Example: `Tally door = new Tally("Door");` uses new to create the object and stores a reference to it in `door`.
object reference
The value an object variable holds: where the object is, not the object itself.
Loses the mark: “assigning one object variable to another copies the object”. `same = door;` copies the reference, so both variables name the one object.
Example: After `Tally same = door;`, both variables hold the same object reference, so `same.click()` also raises the count `door` shows.
null
The value of a reference variable that refers to no object at all.
Loses the mark: “a null reference is an empty object”. `null` means there is no object, so there are no fields to read and no methods to call.
Example: In the Pebble Creek locker app, `Locker spare = null;` refers to no object, so `spare.open()` cannot run.
NullPointerException
The exception thrown when code calls a method or reads a field through null.
Loses the mark: “calling a method on null returns null”. Java cannot call a method on no object; the code throws a `NullPointerException`.
Example: Calling `spare.open()` while `spare` is `null` throws a `NullPointerException` in the Pebble Creek locker app.
instance method
A method called on one object, which can use and change that object's fields.
Loses the mark: “an instance method can be called on the class name”. An instance method needs an object: `door.click()`, not `Tally.click()`.
Example: `click()` is an instance method of `Tally`, so the Northfield gym app calls `door.click()` on one object and only that object's count goes up.
String
Java's class for text: an object holding a sequence of characters, positions from 0.
Loses the mark: “the first character is at position 1”. Positions start at 0, so the first character of a String is at index 0.
Example: In the Larkspur map app, the String `"maps"` has the character `m` at position 0 and `s` at position 3.
string concatenation
Joining strings end to end with +; a number joined to a string becomes text.
Loses the mark: “numbers next to a string are added before they are joined”. Java works left to right: `"Tickets: " + 2 + 3` is "Tickets: 23"; parentheses are needed to add first.
Example: By string concatenation, `"Row " + 4 + 2` in the Cobalt cinema app gives "Row 42", while `"Row " + (4 + 2)` gives "Row 6".
substring
Part of a string; substring(a, b) runs from position a up to, not including, b.
Loses the mark: “substring(1, 4) includes the character at position 4”. The end position is not included, so `"plant".substring(1, 4)` is "lan".
Example: In the Tumbleweed garden app, `"plant".substring(1, 4)` is the substring "lan": positions 1, 2 and 3, stopping before 4.
indexOf
A String method giving where a smaller string first appears, or -1 if absent.
Loses the mark: “indexOf reports every match”. `indexOf` gives only the first position, so `"cocoa".indexOf("co")` is 0.
Example: `"cocoa".indexOf("oa")` is 3 and `"cocoa".indexOf("z")` is -1, so the Hollis recipe app checks indexOf for -1 before using the position.
length
The String method giving a string's number of characters; the last is at length() - 1.
Loses the mark: “length() is the position of the last character”. Positions start at 0, so the last character is at `length() - 1`.
Example: `"K9X".length()` is 3 in the Fairmont parking app, so its last character is at position 2, which is `length() - 1`.
equals
The method that tests whether two strings hold the same characters, case included.
Loses the mark: “== tells whether two strings hold the same text”. `==` compares references, so two separate String objects holding "deck" can give false; `equals` compares the text.
Example: The Copperfield club app checks `typed.equals(code)` rather than `typed == code`, because equals compares the characters, not the references.
compareTo
A String method giving a negative, zero or positive int by two strings' dictionary order.
Loses the mark: “compareTo returns true or false”. `compareTo` returns an `int`: negative, zero or positive.
Example: In the Hartwell roster app, `"Avery".compareTo("Blake")` is negative, so Avery is listed before Blake.
immutable
Unable to change after it is made; String methods return new strings instead.
Loses the mark: “a String method changes the string it is called on”. After `path.substring(0, 4)`, `path` is unchanged; the method returns a new String.
Example: Strings are immutable, so in the Wendell map app `path.substring(0, 4);` alone leaves `path` unchanged; it must be `path = path.substring(0, 4);`.