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.
Explain the difference between the following pairs of concepts:
Translate each of the following into Java code:

Exercise 3.13.14.

If b1 is true, then print โ€œoneโ€; otherwise, print โ€œtwoโ€.

Exercise 3.13.15.

If b1 is false and if b2 is true, then print โ€œoneโ€; otherwise, print โ€œtwoโ€.

Exercise 3.13.16.

If b1 is false then if b2 is true, then print โ€œoneโ€; otherwise, print โ€œtwoโ€, otherwise print โ€œthreeโ€.
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):

Exercise 3.13.17.

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

Exercise 3.13.18.

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

Exercise 3.13.19.

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

Exercise 3.13.20.

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

Exercise 3.13.21.

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

Exercise 3.13.22.

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

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.

Exercise 3.13.24.

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

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

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.

Exercise 3.13.27.

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

Define a void method named day1 that prints โ€œa partridge in a pear treeโ€.

Exercise 3.13.29.

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.

Exercise 3.13.30.

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โ€.

Exercise 3.13.31.

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.

Exercise 3.13.32.

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.

Exercise 3.13.33.

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 get both the name and rating of a donor. Write a complete Java application program.

Exercise 3.13.34.

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.

Exercise 3.13.35.

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.

Exercise 3.13.36.

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.

Exercise 3.13.37.

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.