Exercises


[Page 142 (continued)]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 3.1

Fill in the blanks in each of the following sentences:

  1. When two different methods have the same name, this is an example of _____.

  2. Methods with the same name are distinguished by their _____.

  3. A method invoked when an object is created is known as _____ a method.

  4. A method whose purpose is to provide access to an object's instance variables is known as an _____ method.

  5. A boolean value is an example of a _____ type.

  6. A OneRowNim variable is an example of a _____ type.

  7. A method's parameters have _____ scope.

  8. A class's instance variables have _____ scope.

  9. Generally, a class's instance variables should have _____ access.

  10. The methods that make up an object's interface should have _____ access.

  11. A method that returns no value should be declared _____.

  12. Java's if statement and if-else statement are both examples of _____ control structures.

  13. An expression that evaluates to either TRue or false is known as a _____.

  14. In an if-else statement, an else clause matches _____.

  15. The ability to use a superclass method in a subclass is due to Java's _____ mechanism.

  16. The process of redefining a superclass method in a subclass is known as _____ the method.


[Page 143]
Exercise 3.2

Explain the difference between the following pairs of concepts:

  1. Parameter and argument.

  2. Method definition and method invocation.

  3. Local scope and class scope.

  4. Primitive type and reference type.

  5. Access method and constructor method.

Exercise 3.3

Translate each of the following into Java code:

  1. If b1 is true, then print "one"; otherwise, print "two."

  2. If b1 is false and b2 is true, then print "one"; otherwise, print "two."

  3. If b1 is false and b2 is true, then print "one"; otherwise, print "two," or print "three."

Exercise 3.4

Identify and fix the syntax errors in each of the following:

  1. if (isWalking == true) ;     System.out.println("Walking"); else     System.out.println("Not walking"); 

  2. if (isWalking)     System.out.println("Walking") else     System.out.println("Not walking"); 

  3. if (isWalking)     System.out.println("Walking"); else     System.out.println("Not walking") 

  4. if (isWalking = false)     System.out.println("Walking"); else     System.out.println("Not walking"); 


[Page 144]
Exercise 3.5

For each of the following, suppose that isWalking is true and isTalking is false (first draw a flowchart for each statement and then determine what would be printed by each statement):

  1. if (isWalking == false)     System.out.println("One");     System.out.println("Two"); 

  2. if (isWalking == true)     System.out.println("One");     System.out.println("Two"); 

  3. if (isWalking == false) {     System.out.println("One");     System.out.println("Two"); } 

  4. if (isWalking == false)     if (isTalking == true)         System.out.println("One");     else         System.out.println("Two"); else     System.out.println("Three"); 

Exercise 3.6

Show what the output would be if the following version of main() were executed:

public static void main(String argv[]) {     System.out.println("main() is starting");     OneRowNim game1;     game1  = new OneRowNim(21);     OneRowNim game2;     game2 = new OneRowNim(8);     game1.takeSticks(3);     game2.takeSticks(2);     game1.takeSticks(1);     game1.report();     game2.report();     System.out.println("main() is finished"); } 



[Page 145]
Exercise 3.7

Determine the output of the following program:

public class Mystery {     public String myMethod(String s)     {         return("Hello" + s);     }     public static void main(String argv[])     {         Mystery mystery = new Mystery();         System.out.println( mystery.myMethod(" dolly");     } } 


Exercise 3.8

Write a boolean methoda method that returns a booleanthat takes an int parameter and converts the integers 0 and 1 into false and true, respectively.

Exercise 3.9

Define an int method that takes a boolean parameter. If the parameter's value is false, the method should return 0; otherwise, it should return 1.

Exercise 3.10

Define a void method named hello that takes a single boolean parameter. The method should print "Hello" if its parameter is true; otherwise, it should print "Goodbye."

Exercise 3.11

Define a method named hello that takes a single boolean parameter. The method should return "Hello" if its parameter is true; otherwise it should return "Goodbye." Note the difference between this method and the one in the preceding exercise. This one returns a String. That one was a void method.

Exercise 3.12

Write a method named hello that takes a single String parameter. The method should return a String that consists of the word "Hello" concatenated with the value of its parameter. For example, if you call this method with the expression hello("dolly"), it should return "hello dolly." If you call it with hello("young lovers wherever you are"), it should return "hello young lovers wherever you are."

Exercise 3.13

Define a void method named day1 that prints "a partridge in a pear tree."

Exercise 3.14

Write a Java application program called TwelveDays that prints the Christmas carol "Twelve Days of Christmas." For this version, write a void method named intro() that takes a single String parameter that gives the day of the verse and prints the introduction to the song. For example, intro("first") should print, "On the first day of Christmas my true love gave to me." Then write methods day1(), day2(), and so on, each of which prints its version of the verse. Then write a main() method that calls the other methods to print the whole song.

Exercise 3.15

Define a void method named verse that takes two String parameters and returns a verse of the Christmas carol "Twelve Days of Christmas." For example, if you call this method with verse("first", "a partridge in a pear tree"), it should return, "On the first day of Christmas my true love gave to me, a partridge in a pear tree."


[Page 146]
Exercise 3.16

Define a void method named permute that takes three String parameters and prints out all possible arrangements of the three strings. For example, if you called permute("a", "b", "c"), it would produce the following output: abc, acb, bac, bca, cab, cba, with each permutation on a separate line.

Exercise 3.17

Design a method that can produce limericks given a bunch of rhyming words. That is, create a limerick template that will take any five words or phrases and produce a limerick. For example, if you call

limerick("Jones","stones","rained","pained","bones"); 


your method might print (something better than)

There once a person named Jones Who had a great liking for stones, But whenever it rained, Jones' expression was pained, Because stones weren't good for the bones. 


For each of the following exercises, write a complete Java application program:

Exercise 3.18

Define a class named Donor that has two instance variables, the donor's name and rating, both of which are Strings. The name can be any string, but the rating should be one of the following values: "high," "medium," or "none." Write the following methods for this class: a constructor, Donor(String,String), that allows you to set both the donor's name and rating; and access methods to set and get both the name and rating of a donor.

Exercise 3.19

Challenge. Define a CopyMonitor class that solves the following problem. A company needs a monitor program to keep track of when a particular copy machine needs service. The device has two important (boolean) variables: its toner level (too low or not) and whether it has printed more than 100,000 pages since its last servicing (it either has or has not). The servicing rule that the company uses is that service is needed when either 100,000 pages have been printed or the toner is too low. Your program should contain a method that reports either "service needed" or "service not needed" based on the machine's state. (Pretend that the machine has other methods that keep track of toner level and page count.)

Exercise 3.20

Challenge. Design and write an OldMacdonald class that sings several verses of "Old MacDonald Had a Farm." Use methods to generalize the verses. For example, write a method named eieio() to "sing" the "E I E I O" part of the verse. Write another method with the signature hadAnX(String s), which sings the "had a duck" part of the verse, and a method withA(String sound) to sing the "with a quack quack here" part of the verse. Test your class by writing a main() method.


[Page 147]

Additional Exercises

Exercise 3.21

Suppose you have an Object A, with public methods a() and b(),and private method c(). And suppose you have a subclass of A named B with methods named b(), c(), and d(). Draw a UML diagram showing the relationship between these two classes. Explain the inheritance relationships between them and identify the methods that would be considered polymorphic.

Exercise 3.22

Consider the definition of the class C. Define a subclass of C named B that overrides method m1() so that it returns the difference between m and n instead of their sum.

public class C {     private int m;     private int n;     public C(int mIn, int nIn) {         m = mIn;         n = nIn;     }     public int m1() {         return m+n;     } } 





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net