CS 1401, Exam #4

Date: Wednesday, November 22, 2006, 11:30 am
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. Define a class Pie in which each object has two parameters: weight of the pie (in ounces), and the number of people served (both integers). Do not forget to include a constructor method, accessor methods, mutator methods, and a method that computes the (integer-valued) amount per person served.


Solution:

public class Pie{
  protected int weight;
  protected int number;
  public Pie(int aweight, int anumber)
    {weight = aweight; number = anumber;}
  public int getWeight(){return weight;}
  public int getNumber(){return number;}
  public void setWeight(int newWeight){weight = newWeight;}
  public void setNumber(int newNumber){number = newNumber;}
  public int amountPerPerson(){return weight / number;}
}

2. Considering the class Pie (defined in Problem 1) as a superclass, define a subclass PumpkinPie in which each object has an additional field: the weight of the frosting. Overwrite the original amount-per-person method: it should now divide the total weight of the pie and of the frosting by the number of persons served.


Solution:

public class PumpkinPie extends Pie{
  private int frosting;
  public PumpkinPie(int aweight, int anumber, int afrosting)
    {super(aweight, anumber); frosting = afrosting;}
  public int getFrosting(){return frosting;}
  public void setFrosting(int newFrosting)
    {frosting = newFrosting;}
  public int amountPerPerson()
    {return (weight + frosting) / number;}
}

3. In the main method, define a new object of type PumpkinPie (as defined in Problem 2). This object should describe a pumpkin pie that weighs 20 ounces, has 5 ounces of frosting, and serves 5 persons. Use the amount-per-person method to compute and print the amount per person served.


PumpkinPie myPie = new PumpkinPie(20, 5, 5);
int result = myPie.amountPerPerson();
System.out.println(result);

4. Trace your code from Problem 3 step-by-step.


Tracing:

on the first line, we call a constructor method:

 -----------------
|  -------------  |
| |    20       | |
|  -------------  |
|    aweight      |
|  ------------   |
| |    5       |  |
|  ------------   |
|    anumber      |
|  ------------   |
| |    5       |  |
|  ------------   |
|    afrosting    |
|-----------------|
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
 -----------------
   PumpkinPie

this method, in its turn, calls a constructor method of the superclass:

 -----------------
|  -------------  |
| |    20       | |
|  -------------  |
|    aweight      |
|  ------------   |
| |    5       |  |
|  ------------   |
|    anumber      |
|-----------------|
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
 -----------------
      Pie

after the first line, we get a new object

 -----------------
|  -------------  |
| |    20       | |
|  -------------  |
|     weight      |
|  ------------   |
| |    5       |  |
|  ------------   |
|     number      |
|  ------------   |
| |    5       |  |
|  ------------   |
|    frosting     |
|-----------------|
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
 -----------------
     myPie

after the second line, we get

 ----------
|   5     |
 ----------
   result

5. Modify the amount-per-person method from Problem 2 as follows: use try/catch block to produce a meaningful error message when the number of persons served is 0.


Solution:

  public int amountPerPerson()
    {try{
      return (weight + frosting) / number;
     }
    catch(ArithmeticException a){
      System.out.println("Number of persons served is 0, so amount-per-person is undefined.");
    }
  }

6. Write a method that, given an array h of integers, computes the largest element of this array. Test your method step-by-step on a numerical example.


Solution:

public int largest(int[] h){
  int largestSoFar = h[0];
  for(int i = 1; i < h.length; i++)
    {if (h[i] > largestSoFar) largestSoFar = h[i];}
  return largestSoFar;
}

Testing:

in the main method:

int[] z = {2, 4, 0};
int large = largest(z);

tracing:

First, we define an array

 ----
|  \|
 ---\
  z  \
      \|
      - -------
       |   2  |  z[0]
        -------
       |   4  |  z[1]
        -------
       |   0  |  z[2]
        -------

Then, we call a method:

 ---------------------------
|    ----                   |
|   |  \|                   |
|    ---\                   |
|     h  \                  |
|         \|                |
|         - -------         |
|          |   2  |  h[0]   |
|           -------         |
|          |   4  |  h[1]   |
|           -------         |
|          |   0  |  h[2]   |
|           -------         |
|---------------------------|
|                           |
|          ...              |

Within this method, we define a new variable largestSoFar and assign
it a value h[0] = 2:

 ---------------------
|        2            |
 ---------------------
    largestSoFar

Then, we start a loop by defining a new variable i and assigning it
a value 1:

 ---------------------      ---------
|        2            |    |    1    |
 ---------------------      ---------
    largestSoFar               i

Since 1 = i < h.length = 3, we go inside the loop. Since i = 1, the
condition 4 = h[1] > largestSoFar = 2 is satisfied, so we change the
value of largestSoFar to h[2] = 4:

 ---------------------      ---------
|        4            |    |    1    |
 ---------------------      ---------
    largestSoFar               i

Next, we increase i by 1:

 ---------------------      ---------
|        0            |    |    2    |
 ---------------------      ---------
    largestSoFar               i


Since 2 = i < h.length = 3, we go inside the loop. Since i = 2, the
condition 0 = h[0] > largestSoFar = 2 is not satisfied, so we do not
do anything. Next, we increase i by 1:

 ---------------------      ---------
|        4            |    |    3    |
 ---------------------      ---------
    largestSoFar               i

Now, 3 = i is not smaller than h.length = 3, so we exit the loop, and
return the value largestSoFar = 4 to the line

int large = largest(z);

according to which the value 4 is assigned to the new variable large:

 -------------
|     4       |
 -------------
    large