Home FRQ 2 - Writing Classes
Post
Cancel

FRQ 2 - Writing Classes

What to Know

for more info on classes go here: csawesome_resource

  • Missing public qualifier on class or constructor header will not incur a penalty.

  • classes are the blueprints for making objects. When you create objects, you create new instances of that class and what you can do with those instances is determined by what methods are defined in the class.

  • for the ap test just use your classes to store your methods if they ask you to write classes.

  • Realistically the only things you need to worry about is remembering to make instance variables and constructors if they ask to write one and to actually put everything within the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Example 
{
    // instance variables
    private String var1;
    private int var2;

    // Constructor
    public Example(String initVar1, int initVar2)
    {
        var1 = initVar1;
        var2 = initVar2;
    }

    // Method to print instance variable values, again NOT 
    public void print()
    {
        System.out.println("Var1: " + var1);
        System.out.println("Var2: " + var2);
    }

    // Main method for testing, NOT NEEDED FOR TEST USUALLY
    public static void main(String[] args)
    {
        // Creating instances of Example class
        Example e1 = new Example("Value1", 10);
        Example e2 = new Example("Value2", 20);

        // Calling print method for each instance
        e1.print();
        e2.print();
    }
}

Example Time

The CB question usually provides you with setters and getters but no additional parameters or a constructor. Here is what the given class for SingleTable looks like when all the methods are written.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class SingleTable {
    
    int seats;
    int height;
    double viewQuality;

    public SingleTable(int seats, int height, double viewQuality) {
        this.seats = seats;
        this.height = height;
        this.viewQuality = viewQuality;
    }

    //returns number of seats at this table. value is always greater than or equal to 4
    public int getNumSeats() {
        return this.seats;
    }

    //returns the height of table in cm
    public int getHeight() {
        return this.height;
    }

    //returns the quality of the view from this table
    public double getViewQuality() {
        return this.viewQuality;
    }

    //sets the quality of the view from this table to [value]
    public void setViewQuality(double newQuality) {
        this.viewQuality = newQuality;
    }

    public static void main(String[] args) {
        //single table values that were given in the question!
        SingleTable t1 = new SingleTable(4, 74, 60.0);
        SingleTable t2 = new SingleTable(8, 74, 70.0);
        SingleTable t3 = new SingleTable(12, 76, 75.0);
    }

}

This question asks that you write a new class called CombinedTable which uses the methods of the SingleTable class to determine the “desirability” of the combined tables.

Here are the requirements for the class:

  • A CombinedTable can seat a number of customers that is two fewer than the total number of seats in its two SingleTable objects (to account for seats lost when the tables are pushed together).

  • A CombinedTable has a desirability that depends on the views and heights of the two single tables. If the two single tables of a CombinedTable object are the same height, the desirability of the CombinedTable object is the average of the view qualities of the two single tables.

  • If the two single tables of a CombinedTable object are not the same height, the desirability of the CombinedTable object is 10 units less than the average of the view qualities of the two single tables.

Tip

The first thing you want to do is make the instructions simpler. Right now its a huge chunk of paragraph that may hurt your head long term. So after doing a skim quickly write down what the objectives of this class are. Here is what I’ve gathered:

  • when combining two single tables, subtract two seats to get combined table seat count
  • if the height of combined table is level, desirability is the avg of the single tables
  • if the height of combined table isn’t level, desirability is the avg of the single tables - 10

Looking to the net chunk of info CB gives, they literally hand over what methods you need to make frq2lesson

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class CombinedTable {

    private SingleTable table1;
    private SingleTable table2;

    //constructor
    //uses t1 and t2 from SingleTable class
    public CombinedTable(SingleTable t1, SingleTable t2) {
        table1 = t1;
        table2 = t2;
    }

    public boolean canSeat(int n) {

        int cap = table1.getNumSeats() + table2.getNumSeats() - 2;

        if (n <= cap) {
            return true;
        }
        else {
            return false;
        }
    }

    public double getDesirability(int height, double quality){
        double desirability;
        if (table1.getHeight() == table2.getHeight()) {
            desirability = (table1.getViewQuality() + table2.getViewQuality()) / 2;
        }
        else {
            desirability = ((table1.getViewQuality() + table2.getViewQuality()) / 2) - 10;
        } 
        return desirability; 
    }
    
}

Conclusion

  1. class header (1 point)
  2. (private) instance variables (1 point)
  3. Constructor (1 point)
  4. Methods (1+ point)
This post is licensed under CC BY 4.0 by the author.

Workshop 1 (FRQ1) Hacks

Blog on Sorting