Chapter 7 1. Consider the following classes. class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weight){ this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators . . . } class Breed { private String name; private double averageWgt; //in lbs. public Breed(String name, double averageWgt){ this.name = name; this.averageWgt = averageWgt; } public double getWeight( ) { return averageWgt; } //other accessors and mutators . . . } Identify the invalid statements in the following main class. For each invalid statement, state why it is invalid. class Q1Main { public static void main( String[] args ) { Breed persian = new Breed("Persian", 10.0); Cat chacha = new Cat("Cha Cha", persian, 12.0); Cat bombom = new Cat("Bom Bom", "mix", 10.0); 1 Cat puffpuff = new Cat("Puff Puff", chacha, 9.0); 2 double diff = chacha.getWeight() - persian.getWeight(); System.out.println( puffpuff.getBreed().getWeight()); } } 2. Given the Cat and Breed classes from Exercise 1, what will be the output from the following code? class Q2Main { public static void main( String[] args ) { Cat myCat = new Cat("winky", new Breed("mix", 10.5), 9.5); System.out.println(myCat.getWeight()); System.out.println(myCat.getBreed().getWeight()); } } 3. Given the Fraction class from Section 7.8, draw the state-of-memory diagram at the point immediately after the last statement is executed. Fraction f1, f2, f3; f1 = new Fraction(3, 8); f2 = new Fraction(2, 3); f3 = f1.add(f2); 4. Consider the following class. class Dog { . . . private double weight; . . . public boolean isBiggerThan(Dog buddy) { return this.getWeight() > buddy.getWeight(); } public double getWeight() { return weight; } . . . } For each of the following code fragments, complete the state-of-memory diagram by filling in the arrows for this and buddy. a. Dog tuffy = new Dog(...); Dog puffy = new Dog(...); puffy.isBiggerThan(tuffy); b. Dog tuffy = new Dog(...); Dog puffy = new Dog(...); tuffy.isBiggerThan(puffy); 5. Complete the following constructor. class Student { private String name; private int age; private Address address; public Student(String name, int age, Address address){ //assign passed values to the data members } 6. Which of the following groups of overloaded constructors are valid? a. public Cat(int age) { ... } b. c. d. public public public public public public public public Cat(double Dog(String Dog(String Dog(String Dog(double Cat(String Cat(String Cat(double wgt) { ... } name, double weight) name, double height) name, double weight) weight, String name) name) { ... } name, double weight) weight) { ... } { { { { ... ... ... ... } } } } { ... } 7. Which of the following groups of overloaded methods are valid? a. public void compute(int num) { ... } b. c. d. public public public public public public public public int compute(double num) { ... } void move(double length) { ... } void move( ) { ... } int adjust(double amount) { ... } void adjust(double amount, double charge) { ... } void doWork( ) { ... } void doWork(String name) { ... } int doWork(double num) { ... } 8. Complete the first four constructors of the following class. Each of the four constructors calls the fifth one by using the reserved word this. class Cat { private private private private private private static final String DEFAULT_NAME = "No name"; static final int DEFAULT_HGT = 6; static final double DEFAULT_WGT = 10.0; String name; int height; double weight; public Cat( ) { //assign defaults to all data members } public Cat(String name) { //assign the passed name to the data member //use defaults for height and weight } public Cat(String name, int height) { //assign passed values to name and height //use default for weight } public Cat(String name, double weight) { //assign passed values to name and weight //use default for height } public Cat(String name, int height, double weight){ this.name = name; this.height = height; this.weight = weight; } . . . } 9. Define a class method (static) named compare to the Fraction class. The compare method accepts two Fraction objects f1 and f2. The method returns -1 if f1 is less than f2 0 if f1 is equal to f2 +1 if f1 is greater than f2 10. Rewrite the compare method from Exercise 9 by changing it to an instance method. This method accepts a Fraction object and compares it to the receiving object. The method is declared as follows: public int compare(Fraction frac) { //compare the Fraction objects this and frac //return the result of comparison } 11. Discuss the pros and cons of the compare methods from Exercise 8 and Exercise 9. 12. Consider the following class. class Modifier { public static change(int x, int y){ x = x - 10; y = y + 10; } } What will be an output from the following code? int x = 40; int y = 20; Modifier.change(x,y); System.out.println("x = " + x); System.out.println("y = " + y); Level 1 Programming Exercises 13. Modify the following class to make it a part of the package named myutil. In addition to adjusting the source file, what are the steps you need to take so that the class becomes usable/accessible from other classes that are outside of this myutil package? class Person { private String name; public Person( ) { name = "Unknown"; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 14. Write a program that tests the Person class defined in Exercise 13. Place this test program outside of the myutil package and make sure you can call the constructor and the methods of the Person class correctly. Level 2 Programming Exercises 15. Design a class that keeps track of a student’s food purchases at the campus cafeteria. A meal card is assigned to an individual student. When a meal card is first issued, the balance is set to the number of points. If the student does not specify the number of points, then the initial balance is set to 100 points. Points assigned to each food item are a whole number. A student can purchase additional points at any time during a semester. Every time food items are bought, points are deducted from the balance. If the balance becomes negative, the purchase of food items is not allowed. There is obviously more than one way to implement the MealCard class. Any design that supports the key functionalities is acceptable. Put this class in the myutil package. 16. Write a program that tests the MealCard class defined in Exercise 15. Define this test program outside the myutil package. Create one or two MealCard objects in the test program and verify that all methods defined in the MealCard class operate correctly. Development Exercises For the following exercises, use the incremental development methodology to implement the program. For each exercise, identify the program tasks, create a design document with class descriptions, and draw the program diagram. Map out the development steps at the start. Present any design alternatives and justify your selection. Be sure to perform adequate testing at the end of each development step. 17. Write an application that plays the game of Fermi. Generate three distinct random digits between 0 and 9. These digits are assigned to positions 1, 2, and 3. The goal of the game is for the player to guess the digits in three positions correctly in the least number of tries. For each guess, the player provides three digits for positions 1, 2, and 3. The program replies with a hint consisting of Fermi, Pico, or Nano. If the digit guessed for a given position is correct, then the reply is Fermi. If the digit guessed for a given position is in a different position, the reply is Pico. If the digit guessed for a given position does not match any of the three digits, then the reply is Nano. Here are sample replies for the three secret digits 6, 5, and 8 at positions 1, 2, and 3, respectively: Notice that if the hints like the above are given, the player can tell which number did not match. For example, given the hint for the second guess, we can tell that 3 is not one of the secret numbers. To avoid this, provide hints in a random order or in alphabetical order (e.g., it will be Fermi Nano Pico instead of Pico Fermi Nano for the second reply). Play games repeatedly until the player wants to quit. After each game, display the number of guesses made. Use javadoc comments to document the classes you design for this application. 18. Write an application that teaches children fraction arithmetic. For each training session, randomly generate 10 questions involving addition, subtraction, division, and multiplication of two fractions. At the beginning of each session, the user has the option of specifying the time limit for answering the questions. If the time limit is not specified, then use 30 s as a default time limit. After you pose a question, wait until the user answers the question. Award points based on the following rules: After one session is over, use the console output to display the grade distribution and the total points in the following manner: After one session is over, give the user the option to play another session.