1. Define a class Turkey in which each object has two parameters: the weight of the turkey, and the price (both integers). Do not forget to include a constructor method, accessor methods, mutator methods, and a method that computes an (integer-valued) price per pound.
Solution:
public class Turkey{
protected int weight;
protected int price;
public Turkey(int aweight, int aprice)
{weight = aweight; price = aprice;}
public int getWeight(){return weight;}
public int getPrice(){return price;}
public void setWeight(int newWeight){weight = newWeight;}
public void setPrice(int newPrice){price = newPrice;}
public int pricePerPound(){return price / weight;}
}
2. Considering the class Turkey (defined in Problem 1)
as a superclass, define a subclass
ThanksgivingTurkey in which each object has an additional field: the
weight of stuffing. Overwrite the original price-per-pound method: it
should now divide the cost by the total weight of the turkey and of
the stuffing (i.e., by the sum of the two weights).
Solution:
public class ThanksgivingTurkey extends Turkey{
private int weightOfStuffing;
public ThanksgivingTurkey(int aweight, int aprice, int aweightOfStuffing)
{super(aweight, aprice); weightOfStuffing = aweightOfStuffing;}
public int getWeightOfStuffing(){return weightOfStuffing;}
public void setWeightOfStuffing(int newWeightOfStuffing)
{weightOfStuffing = newWeightOfStuffing;}
public int pricePerPound()
{return price / (weight + weightOfStuffing);}
}
3. In the main method, define a new object of type
ThanksgivingTurkey (as defined in Problem 2).
This object should describe a turkey
that weighs 20 pounds, has 5 pounds of stuffing,
and costs $26. Use the price-per-pound method to compute and print the
pre-pound cost of this turkey.
Solution: ThanksgivingTurkey myTurkey = new ThanksgivingTurkey(20, 26, 5); int result = myTurkey.pricePerPound(); 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 |
| ------------ |
| | 26 | |
| ------------ |
| aprice |
| ------------ |
| | 5 | |
| ------------ |
| aweightPerPound |
|-----------------|
| |
| |
| |
| |
| |
| |
| |
| |
-----------------
ThanksgivingTurkey
this method, in its turn, calls a constructor method of the superclass:
-----------------
| ------------- |
| | 20 | |
| ------------- |
| aweight |
| ------------ |
| | 26 | |
| ------------ |
| aprice |
|-----------------|
| |
| |
| |
| |
| |
| |
| |
| |
-----------------
Turkey
after the first line, we get a new object
-----------------
| ------------- |
| | 20 | |
| ------------- |
| weight |
| ------------ |
| | 26 | |
| ------------ |
| price |
| ------------ |
| | 5 | |
| ------------ |
| weightPerPound |
|-----------------|
| |
| |
| |
| |
| |
| |
| |
| |
-----------------
myTurkey
after the second line, we get
----------
| 1 |
----------
result
5. Modify the price-per-pound method from Problem 2 as follows:
use try/catch block to produce
a meaningful error message when the total weight is 0.
Solution:
public int pricePerPound()
{try{
return price / (weight + weightOfStuffing);
}
catch(ArithmeticException a){
System.out.println("Total weight is 0, so price-per-pound is undefined.");
}
}
6. Write a method that, given an array
h of integers,
computes the smallest element of this array. Test your method
step-by-step on a numerical example.
Solution:
public int smallest(int[] h){
int smallestSoFar = h[0];
for(int i = 1; i < h.length; i++)
{if (h[i] < smallestSoFar) smallestSoFar = h[i];}
return smallestSoFar;
}
Testing:
in the main method:
int[] z = {2, 4, 0};
int small = smallest(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 smallestSoFar and assign
it a value h[0] = 2:
---------------------
| 2 |
---------------------
smallestSoFar
Then, we start a loop by defining a new variable i and assigning it
a value 1:
--------------------- ---------
| 2 | | 1 |
--------------------- ---------
smallestSoFar i
Since 1 = i < h.length = 3, we go inside the loop. Since i = 1, the
condition 4 = h[1] < smallestSoFar = 2 is not satisfied, so we do not
do anything inside the loop. Next, we increase i by 1:
--------------------- ---------
| 2 | | 2 |
--------------------- ---------
smallestSoFar i
Since 2 = i < h.length = 3, we go inside the loop. Since i = 2, the
condition 0 = h[2] < smallestSoFar = 2 is satisfied, so change the
value of smallestSoFar to h[2] = 0:
--------------------- ---------
| 0 | | 2 |
--------------------- ---------
smallestSoFar i
Next, we increase i by 1:
--------------------- ---------
| 0 | | 3 |
--------------------- ---------
smallestSoFar i
Now, 3 = i is not smaller than h.length = 3, so we exit the loop, and
return the value smallestSoFar = 0 to the line
int small = smallest(z);
according to which the value 0 is assigned to the new variable small:
-------------
| 0 |
-------------
small