콘텐츠로 이동

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 as private and 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, and int pages instance 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, create new BookRecord("Wonder", "R. J. Palacio", 315) and print the result of getLabel().

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 eventName and int count instance variables.
  • The constructor receives the event name and initializes count to 0.
  • The add(int people) method increases count by people.
  • The getCount() method returns the current attendance count.
  • In Main, create a counter for Science Fair, call add(12) and add(8), then print the event name and count.

Sample Output

Science Fair: 20

Check Your Work

  • Is add declared with the return type void because it changes state?
  • Is getCount declared with the return type int because 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 StudentScore class with String name and int score fields.
  • Its constructor initializes the name and score.
  • Write boolean scoredHigherThan(StudentScore other). It returns true only when the current object's score is higher than other's score.
  • In Main, create Mina(92) and Jisoo(87), then print mina.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 name for each member.
  • Declare one shared static int memberCount for all members.
  • Increase memberCount by 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 memberCount declared static so 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 owner and int balance instance variables.
  • Give the constructor parameters the same names, owner and balance, then use this.owner and this.balance to initialize the fields.
  • The charge(int amount) method adds amount to the balance.
  • The payFare(int fare) method subtracts the fare and returns true only when the balance is sufficient. If not, it leaves the balance unchanged and returns false.
  • In Main, create new TransitCard("Alex", 1200), call charge(500) and payFare(1400), then print the remaining balance.

Sample Output

true
300

Check Your Work

  • Does the constructor use this to distinguish fields from parameters?
  • Does payFare return false without changing the balance when funds are insufficient?

Reference: Runestone Academy, CSAwesome2 Unit 3: Class Creation