Directions: Write Java code for each question. Each question is independent β you may earn points on any question even if another is incomplete. Use only Java concepts from Unit 1. No if statements or loops are required. Assume code is in the appropriate class unless the prompt says Main.
Put your name at the top. Your work saves automatically in this browser as you type. When you finish β and any time you want to save progress to your teacher β click Turn in to teacher. You can edit and turn in again as many times as you like. Download is a backup copy for yourself.
AP CSA Java Quick Reference β open here or in a new tab
Open the Quick Reference in a new tab β
10 points
Question 1 β Class Design: GameCard
A GameCard stores a player name and the number of tickets on the card. Write the COMPLETE GameCard class with:
β’ two instance variables: String player and int tickets
β’ a constructor that initializes both fields
β’ public void addTickets(int amount), which permanently adds amount to tickets
β’ public int ticketsAfterBonus(int bonus), which RETURNS tickets + bonus without changing tickets
10 points
Question 2 β Method Writing: TemperatureReading
A TemperatureReading object already has this field:
double celsius;
Write BOTH methods below.
A. public double fahrenheit() β returns the Fahrenheit equivalent using: celsius * 9.0 / 5.0 + 32. The field must not change.
B. public double distanceFromFreezing() β returns the positive distance between celsius and 0 using Math.abs.
10 points
Question 3 β Objects, References, and Strings
Assume the following class already exists:
public class Team {
String name;
public Team(String name) { this.name = name; }
public String shortName() { return name.substring(0, 3); }
}
In Main, write Java statements that:
β’ create a Team reference named first for a new Team whose name is "Panthers"
β’ create a Team reference named alias that refers to the SAME object as first
β’ create a second, DISTINCT Team object referenced by second whose name is also "Panthers"
β’ store first.shortName() in a String variable named tag
β’ print whether first and alias refer to the same object
β’ print whether first.name and second.name contain the same text using the correct String-content comparison
10 points
Question 4 β Arithmetic, Math, and Returned Values
Write the requested Java statements. These are independent.
A. Given int total = 17 and int groups = 5, create a double named average that stores the DECIMAL result of total / groups.
B. Create an int named remainder that stores the remainder when total is divided by groups.
C. Create an int named die that stores a random integer from 1 through 6 inclusive using Math.random().
D. A BankAccount reference named account already exists and getBalance() returns int. Store the returned balance in int saved, then print saved + 20.