Unit 3 Practice: Java Class Creation¶
Name: ________
Unit: CS Awesome Unit 3 - Class Creation
Write every solution in Java. Run each problem in a separate file with
public class Main. Declare class fields asprivateand provide behavior through methods when needed.
Problem 1. Book Record Class¶
Write a BookRecord class that represents one book you have read.
Requirements
- Declare
String title,String author, andint pagesinstance variables. - Write a constructor that receives all three values.
- Write a
getLabel()method that returns a string in this format:title - author (pagesp). - In
Main, createnew BookRecord("Wonder", "R. J. Palacio", 315)and print the result ofgetLabel().
Sample Output
Wonder - R. J. Palacio (315p)
Check Your Work
- Do the class name and constructor name both use
BookRecord? - Are the fields used inside an instance method instead of being printed directly from
Main?
Problem 2. Change an Object's State¶
Write an EventCounter class that tracks attendance at a school event.
Requirements
- Declare
String eventNameandint countinstance variables. - The constructor receives the event name and initializes
countto0. - The
add(int people)method increasescountbypeople. - The
getCount()method returns the current attendance count. - In
Main, create a counter forScience Fair, calladd(12)andadd(8), then print the event name and count.
Sample Output
Science Fair: 20
Check Your Work
- Is
adddeclared with the return typevoidbecause it changes state? - Is
getCountdeclared with the return typeintbecause it returns a value?
Problem 3. Send a Score to Another Object¶
Write a program that compares the quiz scores of two students.
Requirements
- Create a
StudentScoreclass withString nameandint scorefields. - Its constructor initializes the name and score.
- Write
boolean scoredHigherThan(StudentScore other). It returnstrueonly when the current object's score is higher thanother's score. - In
Main, createMina(92)andJisoo(87), then printmina.scoredHigherThan(jisoo).
Sample Output
true
Check Your Work
- Does the method parameter have the type
StudentScore? - Did you correctly use an instance variable from the parameter object, such as
other.score?
Problem 4. Shared Library Member Count¶
Write a LibraryMember class that represents a library member.
Requirements
- Store a
String namefor each member. - Declare one shared
static int memberCountfor all members. - Increase
memberCountby 1 every time the constructor is called. - Write
static int getMemberCount()to return the total number of members. - In
Main, create three members, then print the total number of members.
Sample Output
3
Check Your Work
- Is
memberCountdeclaredstaticso it is not duplicated for every object? - Is the static method called as
LibraryMember.getMemberCount()?
Problem 5. Manage a Card Balance with this¶
Write a TransitCard class for a simple prepaid transit card.
Requirements
- Declare
String ownerandint balanceinstance variables. - Give the constructor parameters the same names,
ownerandbalance, then usethis.ownerandthis.balanceto initialize the fields. - The
charge(int amount)method addsamountto the balance. - The
payFare(int fare)method subtracts the fare and returnstrueonly when the balance is sufficient. If not, it leaves the balance unchanged and returnsfalse. - In
Main, createnew TransitCard("Alex", 1200), callcharge(500)andpayFare(1400), then print the remaining balance.
Sample Output
true
300
Check Your Work
- Does the constructor use
thisto distinguish fields from parameters? - Does
payFarereturnfalsewithout changing the balance when funds are insufficient?
Reference: Runestone Academy, CSAwesome2 Unit 3: Class Creation