Exercise Set 0

One-page version suitable for printing.

Question 0-1

Suppose we already have the following Topping class to represent a type of food that might go on top of a pizza.

public class Topping {
    public static final Topping PEPPERONI = new Topping(true);
    public static final Topping SAUSAGE = new Topping(true);
    public static final Topping ONION = new Topping(false);
    public static final Topping GREEN_PEPPER = new Topping(false);
    public static final Topping BLACK_OLIVE = new Topping(false);

    private boolean is_meat;

    private Topping(boolean in_is_meat) {
        is_meat = in_is_meat;
    }

    public boolean isVegetarian() {
        return !is_meat;
    }
}

Write a Pizza class that represents a pizza with up to 7 toppings. Your class should contain the following methods.

Pizza()
Constructs a pizza with no toppings.

void addTopping(Topping what)
Adds the topping what onto the pizza.

boolean isVegetarian()
Returns true if all the toppings on the pizza are vegetarian.

Though it may appear that you need an array to store all the toppings, in fact there's a way to do this without an array.

To test your Pizza class, you can use the following program.

import csbsju.cs160.*;

public class PizzaTest {
    public static void main(String[] args) {
        Pizza p = new Pizza();
        p.addTopping(Topping.ONION);
        IO.println(p.isVegetarian()); // prints ``true''

        p.addTopping(Topping.GREEN_PEPPER);
        IO.println(p.isVegetarian()); // prints ``true''

        p.addTopping(Topping.PEPPERONI);
        IO.println(p.isVegetarian()); // prints ``false''

        p.addTopping(Topping.BLACK_OLIVE);
        IO.println(p.isVegetarian()); // prints ``false''
    }
}

Solution

Question 0-2

Write a Point class to represent a point in two-dimensional Cartesian space. The point class should support the following methods.

Point()
Constructs a point at location (0,0) in the Cartesian plane.

Point(double x, double y)
Constructs a point at location (x,y) in the Cartesian plane.

void translate(double dx, double dy)
Moves the point by (dx, dy). In traditional Cartesian coordinates, this would move it dx units to the right and dy units up.

double distanceTo(Point other)
Computes the distance to other from the point asked. For example, if x is the point (0,0) and y is the point (3,4), then x.distanceTo(y) should return ((0-3)2 + (0-4)2)0.5 = 5.

You can use the following class to test your point.

import csbsju.cs160.*;

public class PointTest {
    public static void main(String[] args) {
        Point origin = new Point();
        Point pt = new Point(3, 4);

        IO.println(pt.distanceTo(origin)); // prints ``5.0''

        pt.translate(2, 8);
        IO.println(pt.distanceTo(origin)); // prints ``13.0''
    }
}

Solution

Question 0-3

In CSCI 160, there are two types of grades: quiz grades (out of 30) and regular grades. The lowest quiz grade is dropped in computing a student's total score. For example, if a student scored 30/35 and 24/35 on two labs and 20/30, 15/30, and 30/30 on three quizzes, the student's total score would be (30 + 24 + 20 + 30) / (35 + 35 + 30 + 30) = 104 / 130 = 80%, since the lowest score is dropped.

Write a class Student to accumulate a single student's grade. This class should have three methods.

double getTotal()
Returns the fraction of the points the student has scored so far, neglecting the quiz score.

void addQuizScore(int score)
Inserts a score of score points of the possible 30 for a quiz.

void addRegularScore(int score, int possible)
Inserts a score of score points of the possible possible into the student's total.

I should be able to execute the following class to test your Student class.

import csbsju.cs160.*;

public class StudentTest {
    public static void main(String[] args) {
        Student stud = new Student();
        stud.addRegularScore(30, 35);
        stud.addRegularScore(24, 35);
        stud.addQuizScore(20);
        stud.addQuizScore(15);
        stud.addQuizScore(30);
        IO.println(100.0 * stud.getTotal() + "%"); // prints ``80.0%''
    }
}

Solution