Skip to main content
Contents Index
Search Book
Search Results:
No results.
Read aloud
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 3.21 Unit 3 Projects
These projects were created by Leigh Anne Fitz to provide engaging, standards-aligned learning experiences for AP Computer Science A students.
Now that youβve mastered the fundamentals of Java programming, itβs time to begin building your own classes. In these projects, youβll learn how to design classes that model real-world objects by creating instance variables, writing constructors, and implementing methods that define an objectβs behavior.
Each project is designed to reinforce object-oriented programming through authentic programming tasks. As you complete them, focus on designing classes with clear responsibilities, choosing meaningful names for variables and methods, and testing your classes thoroughly. Well-designed classes are the foundation of organized, reusable, and maintainable programs.
Subsection 3.21.1 Class Structure Review
A class is a blueprint for creating objects. It defines the data an object has and the actions it can perform.
public class Student {
// Instance variables
private String name;
private int grade;
// Constructor
public Student(String n, int g) {
name = n;
grade = g;
}
// Method
public String introduce() {
return "My name is " + name
+ " and I am in grade " + grade;
}
}
Table 3.21.1. The Main Parts of a Class
Class declaration
Creates a class named Student.
Instance variables
private String name;
private int grade;
Store information about each Student object.
Constructor
public Student(String n, int g) {
name = n;
grade = g;
}
A constructor is used to initialize an object when it is created. It has the same name as the class and does not have a return type.
Methods
public String introduce() {
return "My name is " + name
+ " and I am in grade " + grade;
}
Methods define what an object can do.
What it Has: name, breed, age
What it Can Do: bark(), run(), eat()
Subsection 3.21.2 Unit 3 Project 1 - Library Book
A library wants to create a program to keep track of books that students can borrow.
A LibraryBook object represents one book in the library. Each book has a title, an author, and the number of days the book may be borrowed.
The class should provide methods that allow a program to:
Create a new LibraryBook object.
Get the title of a book.
Get the number of days a book may be borrowed.
Extend the number of days a book may be borrowed.
Get a description of the book.
The following table shows examples of calls to methods in the LibraryBook class.
Table 3.21.3. LibraryBook Methods
LibraryBook book1 = new LibraryBook("The Giver", "Lois Lowry", 14);
Β
Creates a LibraryBook object with the title "The Giver", author "Lois Lowry", and 14 borrow days.
book1.getTitle( )
"The Giver"
Returns the title of book1.
book1.getBorrowDays( )
14
Returns the current number of days book1 may be borrowed.
book1.extendBorrow(7)
Β
Increases the borrow period from 14 days to 21 days.
book1.getBorrowDays()
21
Returns the updated number of borrow days.
book1.getDescription( )
"The Giver by Lois Lowry (21 days)"
Returns a String containing the title, author, and current borrow period.
LibraryBook book2 = new LibraryBook("Holes", "Louis Sachar", 10);
Β
Creates a second LibraryBook object with a borrow period of 10 days.
book2.getDescription( )
"Holes by Louis Sachar (10 days)"
Returns a description of book2.
book2.extendBorrow(5)
Β
Increases the borrow period for book2 from 10 days to 15 days.
book2.getBorrowDays( )
Β
Returns the updated borrow period for book2.
Activity 3.21.1 .
Write the complete LibraryBook class using the description above.
Subsection 3.21.3 Unit 3 Project 2 - Game Player
An online game tracks information about each playerβs performance. A GamePlayer object stores a playerβs username, the number of games played, the total number of points earned, and whether the player is currently active.
The class should provide methods that allow a program to:
Create a new GamePlayer object
Complete a game where it adds the given number of points to totalPoints, increases gamesPlayed by 1, and if the player has completed 20 or more games, set active to false.
Change the playerβs status to active
Get the playerβs average score (if the player has not completed any games, return 0.0)
Get the rank of the player:
Get the status of the player (active or not)
Prints the player object in the form: <username>: Games Played = <gamesPlayed>, Average Score = <average>, Rank = <rank>
The following table shows examples of calls to methods in the GamePlayer class.
Table 3.21.4. GamePlayer Methods
GamePlayer p1 = new GamePlayer("DragonMaster");
Creates a player with a username of βDragonMasterβ, 0 games played, 0 points, and an active status.
p1.getAverageScore();
0
No games have been completed yet.
p1.completeGame(75);
The player has completed 1 game and has 75 total points.
p1.completeGame(125);
The player has completed 2 games and has 200 total points.
p1.getAverageScore();
100
The average score is 200 points and 2 games have been played.
p1.getRank();
"Advanced"
The average score is at least 100.
p1.isActive();
true
The player has completed fewer than 20 games.
p1.toString();
"DragonMaster: Games Played = 2, Average Score = 100.0, Rank = Advanced"
Displays the playerβs current information.
Activity 3.21.2 .
Write the complete GamePlayer class using the description above.
You have attempted
of
activities on this page.