CS 1401, Exam #4

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

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.

















































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).
























































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.

























































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


























































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.

























































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.