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%''
}
}
public class Pizza {
private boolean is_vegetarian;
public Pizza() {
is_vegetarian = true;
}
public void addTopping(Topping what) {
is_vegetarian = is_vegetarian && what.isVegetarian();
}
public boolean isVegetarian() {
return is_vegetarian;
}
}
public class Point {
private double my_x;
private double my_y;
public Point() {
my_x = 0.0;
my_y = 0.0;
}
public Point(double x, double y) {
my_x = x;
my_y = y;
}
public double distanceTo(Point other) {
return Math.sqrt(Math.pow(my_x - other.my_x, 2.0)
+ Math.pow(my_y - other.my_y, 2.0));
}
public void translate(double dx, double dy) {
my_x += dx;
my_y += dy;
}
}
public class Student {
private static final int QUIZ_POSSIBLE = 30;
private int total_score;
private int total_possible;
private boolean has_quiz;
private int min_quiz;
public Student() {
total_score = 0;
total_possible = 0;
has_quiz = false;
min_quiz = 0;
}
public double getTotal() {
return (double) total_score / total_possible;
}
public void addRegularScore(int score, int possible) {
total_score += score;
total_possible += possible;
}
public void addQuizScore(int score) {
if(has_quiz) {
if(score < min_quiz) { // we have a new minimum
total_score += min_quiz;
total_possible += QUIZ_POSSIBLE;
min_quiz = score;
} else { // this quiz score counts
total_score += score;
total_possible += QUIZ_POSSIBLE;
}
} else {
min_quiz = score;
has_quiz = true;
}
}
}