Skip to main content

Section 3.13 Exercises

Subsection 3.13.1 Matching Vocabulary

Exercise 3.13.1. Matching Problem for Methods.

Exercise 3.13.2. Matching Problem for Inheritance.

Exercise 3.13.3. Matching Problem for Scope and Access.

Exercise 3.13.4. Matching Problem for Control Structures.

Subsection 3.13.2 Debugging Exercises

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

Exercise 3.13.5.

Debug the following program.

Exercise 3.13.6.

Debug the following program.

Exercise 3.13.7.

Debug the following program.

Exercise 3.13.8.

Debug the following program.

Subsection 3.13.3 Programming Exercises

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

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

  2. 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 if b2 is true, then print “one”; otherwise, print “two”.

    3. If b1 is false then if b2 is true, then print “one”; otherwise, print “two”, otherwise print “three”.

  3. 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");
      

  4. 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");
    }
    

  5. 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");
         }
    }
    

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

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

  8. 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”.

  9. 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 previous exercise. This one returns a String. That one was a void method.

  10. 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”.

  11. Define a void method named day1 that prints “a partridge in a pear tree”.

  12. 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 intro 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.

  13. 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”.

  14. Define a void method named permute, which 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.

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

  16. Define a class named Donor that has two instance variables, the donor's name and rating, both of which are String s. 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. Write a complete Java application program.

  17. 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.) Write a complete Java application program.

  18. 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. Write a complete Java application program.

  19. Suppose you have an Object A, with public methods a(), 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 those methods that would be considered polymorphic.

  20. 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;
             }
    }
    

You have attempted of activities on this page.