# FRQ 2 Practice with Classes

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.

Many of the FRQs on the AP exam contain a table which shows output or return values from methods in a class. In these practice exercises, you will be given a table that shows method calls and their output. You will need to write a class with the described methods that will produce the output shown in the table. Thank you to Sam Procopio from Bishop Blanchet High School in Seattle, WA, for these exercises.

## Tree Class

Write a class called Tree that has the appropriate instance variables and methods to produce the output described in the table below.

|  | Method Calls | Output |
| --- | --- | --- |
|  | Tree birch = new Tree(10, “soft”); |  |
|  | System.out.println(birch.getHeight()); | 10 |
|  | birch.water(5); |  |
|  | System.out.println(birch.getHeight()); | 15 |
|  | birch.cut(); |  |
|  | System.out.println(birch.getHeight()); | 7 |
|  | System.out.println(birch.woodType()); | soft wood |
|  | Tree oak = new Tree(20, “hard”); |  |
|  | System.out.println(oak.getHeight()); | 20 |
|  | System.out.println(oak.woodType()); | hard wood |
|  | oak.water(2); |  |
|  | System.out.println(oak.getHeight()); | 22 |
|  | oak.cut(); |  |
|  | System.out.println(oak.getHeight()); | 14 |

### Exercise 1

Write the class Tree with the appropriate instance variables, constructor, and methods described in main method and in the table above.

> Open the `01-classTree` folder and edit `Tree.java`. Press **Run** to try it; **Run Tests** checks it (7 tests).

## Book Class

Based on the table below, write a complete class called Book that has the appropriate instance variables and methods to produce the output described in the table.

|  | Method Calls | Output |
| --- | --- | --- |
|  | Book hp = new Book(“Harry Potter”, 150, true); |  |
|  | System.out.println(hp.getNumPages()); | 150 |
|  | System.out.println(hp.getName()); | Harry Potter |
|  | System.out.println(hp.isHardCover()); | true |
|  | System.out.println(hp.getDamages()); | 0 |
|  | System.out.println(hp.repairBook()); | false |
|  | hp.addDamages(49); |  |
|  | System.out.println(hp.getDamages()); | 49 |
|  | System.out.println(hp.repairBook()); | false |
|  | hp.addDamages(1); |  |
|  | System.out.println(hp.getDamages()); | 50 |
|  | System.out.println(hp.repairBook()); | true |

### Exercise 2

Write the class Book with the appropriate instance variables, constructor, and methods described in main method and in the table above.

> Open the `02-classBook` folder and edit `Book.java`. Press **Run** to try it; **Run Tests** checks it (6 tests).

## Present Class

Write a class called Present that describes a gift for an occasion. Use the method calls in the main method below to create the appropriate instance variables and methods for the Present class.

```java
public static void main(String[] args)
    {
        //creates a Present object
        Present gift = new Present("Birthday", 29.99, "Legos");

        //prints out the gift name and cost
        System.out.println("The gift is " + gift.getName()
                  + ", and it costs $" + gift.getPrice() );

        // prints out the reaction "Wow!"
        System.out.println("The reaction when opening the gift was "
                + gift.reaction() );
        // Prints out "Purchased at local store"
        gift.purchasedAt("local store");
    }
}
```

### Exercise 3

Write the class Present with the appropriate instance variables, constructor, and methods described in main method.

> Open the `03-classPresent` folder and edit `Present.java`. Press **Run** to try it; **Run Tests** checks it (6 tests).
