One-page version suitable for printing.
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;
}
}
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''
}
}
Write a Point class to represent a point in two-dimensional Cartesian space. The point class should support the following methods.
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''
}
}
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.
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%''
}
}