DAY 5 | Class Creation
Unit 3 | 10–18% of exam | FRQ 1 always requires writing a class
Your Plan for Today
apcsexamprep.com/pages/ap-csa-unit-3-class-creation-study-guide
Focus: constructors, instance variables, accessor/mutator methods, toString.
apcsexamprep.com/pages/ap-csa-unit-3-practice-exam
Do 8 questions. Unit 3 is lighter on MCQ -- more weight is on FRQ.
-> Score 6-8: move to Step 3.
-> Score 0-5: reread the class anatomy section, then do Step 3 anyway.
| Your Score |
What to Do |
| 0–3 pts |
Reread class anatomy section. Write the Counter example below from scratch, then try 2024 FRQ 1. |
| 4–6 pts |
Review rubric and check private vs. public, then move to Day 6. |
| 7–9 pts |
Strong. Move to Day 6. |
Concept Review
Anatomy of a Class
- Instance variables: private, declared at the top of the class, outside all methods
- Constructor: same name as class, no return type — sets all instance variables
- Accessor (getter):
public, returns ONE instance variable
- Mutator (setter):
public void, takes a parameter, updates an instance variable
-
toString(): public String — called automatically when you print an object
-
this: refers to the current object — required when parameter and field share the same name
public class Product
{
// Instance variables -- private, declared at the top
private String name;
private double price;
// Constructor -- no return type, same name as the class
public Product(String name, double price)
{
this.name = name; // 'this' is required here
this.price = price; // parameter and field share the same name
}
// Accessor
public String getName()
{
return name;
}
// Mutator
public void setPrice(double p)
{
price = p;
}
// toString
public String toString()
{
return name + ": $" + price;
}
}
Practice Questions
Q21. [SPOT THE ERROR]
The class below has one bug. What is it?
public class Item
{
private String name;
public Item(String n)
{
name = n;
}
public String getName()
{
return n; // BUG: n does not exist here
}
}
- (A) The constructor parameter should be named name, not n
- (B) getName() returns n, which no longer exists after the constructor finishes
- (C) Instance variables must be public, not private
- (D) The constructor needs a return type of void
▶ REVEAL ANSWER
Answer: B
n is the constructor parameter -- it only exists inside the constructor body. After the constructor finishes, n is gone. Fix: return name;
Q22. [SPOT THE ERROR]
A class has a private int field named count. Which reset() implementation is correct?
- (A) public void reset() { count = 0; }
- (B) public void reset() { int count = 0; }
- (C) public int reset() { return 0; }
- (D) public reset() { this.count = 0; }
▶ REVEAL ANSWER
Answer: A
A assigns 0 to the instance variable. B declares a LOCAL variable that shadows the field -- field unchanged. C returns 0 but never changes the field. D is missing the return type (void) -- compile error.
Q23. [I/II/III]
Which are TRUE about instance variables vs. local variables?
I. Instance variables are accessible from any method in the same class.
II. Local variables are automatically initialized to 0 or false.
III. Instance variables declared private cannot be accessed from outside the class.
- (A) I only
- (B) I and III only
- (C) II and III only
- (D) I, II, and III
▶ REVEAL ANSWER
Answer: B
I TRUE. II FALSE -- local variables are NOT auto-initialized; using one unassigned is a compile error. III TRUE. Only I and III are correct.
Q24. [TRACE]
What is printed?
Box b1 = new Box(10);
Box b2 = new Box(20);
System.out.println(b1.getValue() + " " + b2.getValue());
- (A) 10 10
- (B) 10 20
- (C) 20 20
- (D) 20 10
▶ REVEAL ANSWER
Answer: B
Two independent Box objects. b1 stores 10, b2 stores 20. Changing one does not affect the other. Output: 10 20.
Answer Key (for printing)
Q21: B Q22: A Q23: B Q24: B