Thursday, October 21, 2010

Sample solution for last question of Exam #2


package iamhungry;

/**
* This class models a college student.
*/
public class CollegeStudent {

private String name;
private int favoritePizzaTopping;
private int pizzasBaked;

/**
* This constructor creates a college student with a given name
* and a favorite pizza topping. The student has not yet baked
* any pizzas.
*/
public CollegeStudent (String name, int favoritePizzaTopping) {
this.name = name;
this.favoritePizzaTopping = favoritePizzaTopping;
this.pizzasBaked = 0;
}

/**
* This method commands the student to bake the pizza being
* passed in.
*/
public void bakePizza (FrozenPizza fp) {
fp.bake(425, 10);
pizzasBaked = pizzasBaked + 1;
}

/**
* This method checks if the given pizza has the tuype of topping
* that is the student's favorite.
*/
public boolean checkForPreferredPizza (FrozenPizza fp) {
return fp.topping() == favoritePizzaTopping;
}

/**
* This method classifies the student according to the number of
* pizzas they have baked. Less than 10 is a "taster" 10 - 20 is
* an enthusiast. Over 20 is a "connoisseur".
*/
public String classification () {
if (pizzasBaked < 10) {
return "taster";
}
else if (pizzasBaked <= 20) {
return "enthusiast";
}
else {
return "connoisseur";
}
}
}

No comments:

Post a Comment