It is intended that you complete this lab assignment during the lab session on Thursday, February 2. You will be assigned a different pair programming partner for this lab, so be sure to finish the exercises from last week's lab with the same partner you had last week before starting this lab. When you have finished, demonstrate your programs to the instructor to receive credit for this lab.
If you do not finish during the Feb. 2 lab session, you may be checked off at the start of the Feb. 9 lab session instead.
The following programming exercises are to be completed with a different programming partner from the person you worked with last week. Remember to trade roles every 5 to 10 minutes. When your team finishes a programming exercise, you will both receive credit for that exercise.
Pair Math: Do Programming Exercise P4.4 (page 145). Note that you should create a method for each of the values that your program is to calculate — getSum(), getDifference(), getProduct(), etc. — and have your driver program, PairTest, construct a Pair object, then call each method defined in class Pair, printing each result.
Min and Max: Do Programming Exercise P4.6 (page 145). Note that, although the problem as stated specifies that a method named "addValue" should be provided, this method doesn't really add a value to anything — it only checks whether its parameter is a new largest or smallest value (using Math.min and Math.max, as specified in the problem description). The method might be better named "acceptValue" or "inspectValue".
Test your program with each of the following sequences of integers:
1 2 3 4 4 3 2 1 5 8 3 9 2 6
Circle and Sphere: Do Programming Exercise P4.8 (page 146). Note that you should create separate classes Circle and Sphere, where Circle has methods area() and circumference() and Sphere has methods area() and volume(). You will also need to write a driver that prompts the user for a radius, creates a Circle object and a Sphere object with that radius, then calls the methods defined for each object and prints the results.
Hint: The formula for the volume of a sphere with radius r is (4/3) π r3. The formula for the surface area of a sphere with radius r is 4 π r2. Remember that the value of π is available as Math.PI.
If you test your program with a radius of 5, you should get the following values as results:
| area | 78.53981633974483 |
| circumference | 31.41592653589793 |
| volume | 523.5987755982989 |
| surface area | 314.1592653589793 |
Draw a House: Do Programming Exercises P5.8 and P5.9 (page 186). Note that Exercise P5.9 has been corrected in the textbook errata to read:
Extend Exercise P5.8 by supplying a House constructor for specifying the position and size. Then populate your screen with a few houses of different sizes.
Pig Latin: Write a Java program that translates English words into pig Latin. Pig Latin is a simple encoding of English words that children sometimes use for their amusement. In its simplest form, each English word is encoded in pig Latin by moving the first letter of the English word to the end of that word and adding "ay". For example, "computer" would become "omputercay" and "book" would become "ookbay". (Pig Latin actually has special rules for words beginning with a vowel or with a multi-character phoneme, such as the "th" of "the", but we will use only the simplest rule in order to make it easy to write this program.)
You may assume that the English word entered by the user will contain no punctuation marks and that the word will have two or more letters. Use a method pigLatinWord that accepts a single English word as its String parameter and returns its pig Latin equivalent as a String. Use the Scanner class in a main method to read the word typed by the user, then print the result returned by the method pigLatinWord.
Each program in this set of lab exercises will be graded on a "mastery" basis — you will get full credit for the program if it works correctly, or no points if it is not completed. The first three programs will be worth 2 points each and the last two, 3 points each, for a total of 12 points for this lab assignment.
Copyright © 2006 Jonathan Mohr