CS 1401, Quiz 9

Date: Monday, October 29, 2007
Name (please type legibly, ideally in block letters): ______________________________________________________________________
Class section (9:30 am or 11:30 am): _______________________________

1. Mark what new material is covered and what is not covered in Chapter 9:

2. Define a class Pizza whose elements are (surprise!) pizzas. Each pizza is characterized by its size in inches, its list of toppings, its price, and whether its crust is thin or not. Your class should contain a constructor method, accessor methods, mutator methods, and a method for computing the area of the pizza. Reminder: the radius is half its size, and the area is pi (= 3.141592...) times the square of the radius.


Solution:

public class Pizza{
  private double size;
  private String toppings;
  private double price;
  private boolean thin;

  public Pizza(double s, String t, double p, boolean th){
    size = s, toppings = t; price = p; thin = th;}

  public double getSize(){return size;}

  public String getToppings(){return toppings;}

  public double getPrice(){return price;}

  public boolean getThin(){return thin;}

  public void setSize(double news){size = news;}

  public void setToppings(String newt){toppings = newt;}

  public void setPrice(double newp){price = newp;}

  public void setThin(boolean newth){thin = newth;}

  public double area(){
    double radius = size/2.0;
    return Math.PI * radius * radius;}
}
3. Use your class in the main method to define a new 5-inch pizza with the toppings of your choice. Then, suppose that you got hungry and decided to increase the pizza's size to 10 inches. Show how to increase this size. Compute and print the area of this pizza.


Solution:

Pizza myPizza = new Pizza(5, "anchovies and peperoni", 8.0, true);
myPizza.setSize(10);
double a = myPizza.area();
System.out.println(a);

4. Trace your program step-by-step.

Solution (in brief):

on the first line, we call a constructor method:

 --------------
|  ----------  |
| |    5     | |
|  ----------  |
|    s         |
|  ----------  |
| | anchovies| |
|  ----------  |
|      t       |
|  ----------  |
| |   8.0    | |
|  ----------  |
|      p       |
|  ----------  |
| |  true    | |
|  ----------  |
|     th       |
 --------------
|              |
|              |
|              |
|              |
|              |
|              |
|              |
|              |
 --------------
    Pizza

after the first line, we get a new object

  --------------
|  ----------  |
| |    5     | |
|  ----------  |
|    size      |
|  ----------  |
| | anchovies| |
|  ----------  |
|   toppings   |
|  ----------  |
| |   8.0    | |
|  ----------  |
|    price     |
|  ----------  |
| |  true    | |
|  ----------  |
|    thin      |
 --------------
    myPizza

after the second line, we get

  --------------
|  ----------  |
| |   10     | |
|  ----------  |
|    size      |
|  ----------  |
| | anchovies| |
|  ----------  |
|   toppings   |
|  ----------  |
| |   8.0    | |
|  ----------  |
|    price     |
|  ----------  |
| |  true    | |
|  ----------  |
|    thin      |
 --------------
    myPizza

on the third line, we call the area method:

 --------------
|              |
|              |
 --------------
|  ----------  |
| |    5     | |
|  ----------  |
|    radius    |
 --------------
    area

and then return and print

 -------------
| 78.5398...  |
 -------------
      a