What is AP Computer Science A unit 3?
Unit 3 is where AP CSA shifts from using existing Java classes to building your own. You define the attributes and behaviors of objects, control what outside code can see, and write the constructors and methods that make objects work. This unit carries 10-18% of the exam weight and appears directly in free-response questions that ask you to write or complete a class.
Unit 3 is about creating Java classes. You declare private instance variables, write public constructors to initialize them, and define methods that expose or modify object state. You also learn when to use static members, how scope determines which variable a name refers to, and how this references the current object.
Abstraction drives class design
Data abstraction gives a data type a name without exposing how it is stored. Procedural abstraction gives a process a name so callers do not need to know the implementation. Together they let you design a class by listing attributes and behaviors before writing a single line of code.
Encapsulation protects object state
Declaring instance variables private and providing public accessor and mutator methods keeps implementation details hidden. Outside classes interact with your object only through the public interface, so you can change internal representation without breaking other code.
Static members belong to the class, not objects
A static variable is shared by every object of the class. A static method runs without a specific object and cannot access instance variables unless passed an object. Access static members using the class name and the dot operator, such as ClassName.field.
Classes model real-world entities and extend Java's powerEvery class you write is a new data type. By combining private data, a public constructor, and well-named methods, you create objects that accurately represent ideas from a bank account to a student record. This capability is what allows programs to have broad impacts on society, economies, and cultures, and it is the foundation for Unit 4's data collections.
Unit 3 review notes
3.1
Abstraction and Program Design
Abstraction reduces complexity by focusing on the main idea and hiding irrelevant details. Before writing code, you can design a class by listing its attributes (data it stores) and behaviors (actions it can perform). UML class diagrams and natural-language descriptions are both valid design tools on the AP exam.
- Data abstraction: Gives a data type a name without revealing how it is stored internally. An instance variable is one form: it has a name and type but hides its memory representation.
- Procedural abstraction: Gives a process a name through a method so callers use the method without knowing its implementation. Changing the method body does not affect callers as long as the signature stays the same.
- Attribute: A piece of data defined in a class outside any method. Instance variables are attributes whose values are unique to each object.
- Behavior: What an object can do, defined by its methods. Listing behaviors during design maps directly to the methods you will write.
- Method decomposition: Breaking a complex task into smaller helper methods, each with a single clear purpose, to improve readability and reuse.
Can you list the attributes and behaviors for a class like BankAccount or Student before writing any Java code?
| Abstraction type | What it names | What it hides |
|---|
| Data abstraction | A variable or data type | How the data is stored in memory |
| Procedural abstraction | A method or process | The implementation steps inside the method |
3.2
Impact of Program Design
Writing a class is not just a technical act. Programs affect society, the economy, and culture in both intended and unintended ways. Reliability and legal compliance are responsibilities every programmer carries.
- System reliability: A program's ability to perform its tasks as expected under stated conditions without failure. Testing with a variety of inputs, including edge cases, improves reliability.
- Unintended harmful effects: Programs built to solve one problem can cause harm beyond their intended use. Algorithmic bias and privacy violations are common examples.
- Open source: Code published and freely available for use without requiring permission or payment. Reusing code that is not open source requires obtaining permission and often purchasing a license.
- Intellectual property: Legal rights protecting original software code. Incorporating unlicensed code without permission is a legal violation.
What is the difference between open-source code and proprietary code in terms of what a programmer is allowed to do with it?
| Code type | Permission needed? | Cost possible? |
|---|
| Open source | No | No |
| Proprietary / not open source | Yes | Often yes |
3.3
Class Anatomy and Constructors
A Java class has a public class header, private instance variables, and public constructors. The constructor sets the initial state of an object by assigning values to every instance variable. When new is called, memory is allocated and a reference to the new object is returned.
- Data encapsulation: Keeping implementation details hidden by declaring instance variables private and exposing state only through public methods.
- Constructor: A special block with the class name and no return type that initializes instance variables when an object is created with new.
- Default values: When no constructor is provided, Java initializes int and double to 0, boolean to false, and object references to null.
- Mutable parameter copy: When a constructor receives a mutable object as a parameter, it should store a copy, not the original reference, to prevent outside code from altering the object's state.
- Constructor header: The declaration line specifying the access modifier, the class name as the constructor name, and the parameter list. No return type appears here.
Write the constructor header and instance variable declarations for a class called Movie with a String title and an int year.
| Member | Access modifier | Purpose |
|---|
| Instance variable | private | Stores per-object state |
| Constructor | public | Initializes instance variables when object is created |
| Accessor method | public | Returns a copy of an instance variable's value |
| Mutator method | public | Updates an instance variable's value |
3.5
Writing Methods and Passing References
Methods define what objects can do. Void methods perform actions without returning a value. Non-void methods evaluate a return expression and hand back a single value. Primitives are passed by value, so the original argument never changes. Object references are also passed by value, but because the parameter holds a copy of the reference, the method can still modify the original object's state through that shared reference.
- Return by value: A non-void method evaluates its return expression and sends the result back to the caller. Code after a return statement in the same block never executes.
- Accessor method: A public non-void method that returns a copy of an instance variable's value, allowing outside classes to read state without direct access to the private field.
- Mutator method: A public void method that updates an instance variable, giving outside classes a controlled way to change object state.
- Object reference parameter: When an object is passed to a method, the parameter receives a copy of the reference. The method can call methods on the object and change its state, but reassigning the parameter does not affect the original variable.
- Side effect: An action a method performs beyond returning a value, such as printing output or modifying an instance variable. Mutator methods intentionally produce side effects.
If a method receives an int parameter and doubles it, does the original int variable in the caller change? What if the parameter is an ArrayList?
| Parameter type | What is copied | Can method change original? |
|---|
| Primitive (int, double, boolean) | The value itself | No |
| Object reference | The reference (memory address) | Yes, through the shared reference |
3.7
Static Variables and Methods
Static members belong to the class itself, not to any individual object. A static variable has one shared copy across all objects. A static method runs without a specific object instance and cannot access instance variables or call instance methods unless an object is passed in as a parameter.
- Class variable (static field): Declared with the static keyword. All objects share one copy. Accessed outside the class as ClassName.variableName.
- Class method (static method): Declared with static. Can access class variables and call other static methods, but cannot use this or access instance variables directly.
- final keyword: When a variable is declared final, its value cannot be changed after initialization. Combined with static, it creates a class-level constant such as static final double PI = 3.14159.
- Dot operator with class name: Public static members are accessed using ClassName.member rather than through an object reference, because they belong to the class.
Why can a static method not call an instance method directly, and how would you work around that restriction?
| Member type | Keyword | One copy per | Can access instance variables? |
|---|
| Instance variable | (none) | Object | Yes |
| Class variable | static | Class | No (it is one itself) |
| Instance method | (none) | Called on object | Yes |
| Class method | static | Called on class | No, unless passed an object |
3.8
Scope, Access, and the this Keyword
Scope determines where a variable name can be used. Local variables and parameters exist only inside the block where they are declared. When a local variable or parameter shares a name with an instance variable, the local name takes priority inside that block. The this keyword resolves the conflict by explicitly referring to the current object's instance variable.
- Local variable: Declared inside a method, constructor, or block. Only accessible within that block. Cannot be declared public or private.
- Variable shadowing: When a parameter or local variable has the same name as an instance variable, the local name is used inside that scope, hiding the instance variable.
- this keyword: Inside an instance method or constructor, this holds a reference to the current object. Use this.fieldName to access an instance variable when a parameter shadows it.
- this as argument: You can pass this as an argument in a method call to give another method a reference to the current object.
- No this in static methods: Static methods have no associated object, so the this keyword is not available inside them.
In a constructor with a parameter named title that shadows an instance variable also named title, what does this.title = title accomplish?
| Variable type | Declared where | Scope | Access modifiers allowed? |
|---|
| Instance variable | Class body, outside methods | Entire class | Yes (public/private) |
| Local variable | Inside a method or block | That block only | No |
| Parameter | Method or constructor header | That method or constructor only | No |
Practice AP Computer Science A unit 3 questions
Try AP-style multiple-choice questions and written prompts after you review the notes.
QuestionA WeatherStation class processes an array of daily temperatures. It needs to identify the longest streak of consecutive days above freezing, calculate the average temperature during that specific streak, and format a final report string. Which procedural abstraction design best manages complexity and promotes code reuse?
A public method that calls private helper methods to find the streak, calculate the average, and format the final temperature report.
A single public method that sequentially finds the streak, calculates the average, and formats the final temperature report directly.
A private method that calls public helper methods to find the streak, calculate the average, and format the final temperature report.
Three separate public methods that outside classes must call sequentially to find the streak, calculate the average, and format reports.
QuestionA researcher is investigating a telehealth platform's automated video system for accessibility barriers. They want to determine if the system disproportionately drops connections for patients residing in rural zip codes compared to urban zip codes. Each patient and session has a unique ID. The following datasets are available:
- Dataset 1: Patient ID, Age Group, Primary Language Spoken
- Dataset 2: Patient ID, Zip Code, Internet Service Provider
- Dataset 3: Zip Code, Geographic Classification (Urban/Rural)
- Dataset 4: Session ID, Patient ID, Connection Status (Completed/Dropped)
Which combination of datasets enables the researcher to draw a conclusion?
Datasets 2, 3, and 4, to connect the dropped sessions with the patients and rural zip codes.
Datasets 1, 2, and 4, to connect the dropped sessions with the patients and spoken languages.
Datasets 1, 3, and 4, to connect the dropped sessions with the languages and rural zip codes.
Datasets 1, 2, and 3, to connect the spoken languages with the patients and rural zip codes.
2. This question involves the PayCalculator class, which represents a payroll calculation system for an employee. You will write the complete PayCalculator class, which contains a constructor and two methods. PayCalculator objects are created by calls to a constructor with two parameters.
PayCalculator objects are created by calls to a constructor with two parameters.
The first parameter is a double that represents the hourly pay rate in dollars. (Precondition: Assume that this value will be greater than 0.0.)
The second parameter is a double that represents the tax rate as a decimal (e.g., 0.15 for 15%). (Precondition: Assume that this value will be between 0.0 and 1.0 inclusive.)
The PayCalculator class contains a calculateGrossPay method, which calculates the total pay before taxes based on the number of hours worked.
The method takes a single double parameter representing the number of hours worked. Assume this value is greater than or equal to 0.0.
Employees are paid at the hourly pay rate for the first 40 hours worked.
Any hours worked beyond 40 hours are considered overtime and are paid at 1.5 times the hourly pay rate.
The method returns the calculated gross pay as a double.
Returns the calculated gross pay as a double.
The PayCalculator class contains a getPayStub method, which generates a summary string of the pay and taxes.
The method takes a single double parameter representing the number of hours worked.
The method calculates the gross pay based on the hours worked.
The method calculates the tax amount by multiplying the gross pay by the tax rate.
The method calculates the net pay by subtracting the tax amount from the gross pay.
The method returns a formatted String containing the gross pay and net pay.
The returned string must be in the format "Gross: $G, Net: $N", where G is the gross pay and N is the net pay.
Returns a formatted String "Gross: $G, Net: $N".
Statement | Return Value (blank if no value) | Explanation |
|---|
PayCalculator p1 = new PayCalculator(20.0, 0.1); | | PayCalculator p1 is constructed with a rate of $20.00/hr and a 10% tax rate. |
double g1 = p1.calculateGrossPay(30.0); | 600.0 | 30 hours * 20.0/hr=600.0. |
String s1 = p1.getPayStub(30.0); | "Gross: $600.0, Net: $540.0" | Gross is $600.0. Tax is $600.0 * 0.1 = $60.0. Net is $540.0. |
double g2 = p1.calculateGrossPay(50.0); | 1100.0 | 40 hours * 20.0=800.0. 10 overtime hours * (20.0∗1.5)=300.0. Total = $1100.0. |
String s2 = p1.getPayStub(50.0); | "Gross: $1100.0, Net: $990.0" | Gross is $1100.0. Tax is $1100.0 * 0.1 = $110.0. Net is $990.0. |
PayCalculator p2 = new PayCalculator(15.0, 0.2); | | PayCalculator p2 is constructed with a rate of $15.00/hr and a 20% tax rate. |
String s3 = p2.getPayStub(40.0); | "Gross: $600.0, Net: $480.0" | 40 hours * 15.0=600.0. Tax is 600.0∗0.2=120.0. Net is $480.0. |
Write the complete PayCalculator class. Your implementation must meet all specifications and conform to the examples shown in the table.