FRQ 2 Practice with Classes

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period3 exercises19 graded tests

Files

README.mdDownload

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

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

For teachers

FRQ 2 Practice with Classes

Students read CSAwesome2's “FRQ 2 Practice with Classes” in CS Room and work through its 3 Java coding exercises.

  • Time: 1 class period
  • Tests: 19 in 3 graded exercises, set up for you when you import the lesson

Goals

  • Practice for Unit 3: Class Creation.

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–03, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-classTreeTree.javaAutograded7
02-classBookBook.javaAutograded6
03-classPresentPresent.javaAutograded6

Teachers can download a reference solution after signing in.