# Unit 3 Projects

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

*These projects were created by Leigh Anne Fitz and Lisa Ferran to provide engaging, standards-aligned learning experiences for AP Computer Science A students. The autograding tests were designed by Kate McDonnell.*

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.

## Class Structure Review

A class is a blueprint for creating objects. It defines the data an object has and the actions it can perform.

**Basic Class Structure:**

```java
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;
    }
}
```

**The Main Parts of a Class**

| Part | Example | Description |
| --- | --- | --- |
| Class declaration | `public class Student {` | Creates a class named Student. |
| Instance variables | `private String name;`<br>`private int grade;` | Store information about each Student object. |
| Constructor | `public Student(String n, int g) {`<br>`name = n;`<br>`grade = g;`<br>`}` | 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() {`<br>`return "My name is " + name`<br>`+ " and I am in grade " + grade;`<br>`}` | Methods define what an object can do. |

> Remember -  A useful way to think about a class is:
>
> > **Class** = **What it has** + **What it can do**

Example:

- Class: Dog
- What it Has: name, breed, age
- What it Can Do: bark(), run(), eat()

## 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.

**LibraryBook Methods**

| Statement | Return Value (blank if no value) | Explanation |
| --- | --- | --- |
| 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. |

### Exercise 1

Write the complete LibraryBook class using the description above.

> Open the `01-unit3-project-1` folder and edit `Main.java`. Press **Run** to try it; **Run Tests** checks it (10 tests). Name the class, then rename the file to match.

## 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:
  - *Beginner* if the average score is less than 50.
  - *Intermediate* if the average score is at least 50 but less than 100.
  - *Advanced* if the average score is 100 or higher.
    - Note: This method must call the method for averaging the score correctly to receive full points
- 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>
  - This method must call the appropriate methods for the average and the rank to receive full points

The following table shows examples of calls to methods in the GamePlayer class.

**GamePlayer Methods**

| Statement | Return Value (blank if no value) | Explanation |
| --- | --- | --- |
| 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" | Returns a String with the player's current information. |

### Exercise 2

Write the complete GamePlayer class using the description above.

> Open the `02-unit3-project2-gameplayer` folder and edit `Main.java`. Press **Run** to try it; **Run Tests** checks it (10 tests). Name the class, then rename the file to match.
