A method’s name, number, type, and order of parameters
constructor
A method that is invoked when an object is created
accessor (get) method
A method whose purpose is to provide access to an object’s instance variables
mutator (set) method
A method whose purpose is to change an object’s instance variables
void
The return type for a method that returns no value
Exercise3.13.2.Matching Problem for Inheritance.
Match the vocabulary with their definitions.
Review the chapter vocabulary in the Summary.
inheritance
Allows the use of a superclass method in a subclass
method overidding
Redefining a superclass method in a subclass
method overloading
Giving the same name (but different number or types of parameters) to two or more methods
Exercise3.13.3.Matching Problem for Scope and Access.
Match the vocabulary with their definitions.
Review the chapter summary.
local scope
A method’s parameters have this scope
class scope
A class’s instance variables have this scope
private
A class’s instance variables should usually have this type of access.
public
The methods that make up an object’s interface should have this type of access.
Exercise3.13.4.Matching Problem for Control Structures.
Match the vocabulary with their definitions.
Review the chapter summary.
selection statements
if statements and if-else statements
boolean
An expression that evaluates to either true or false
the closest unmatched if in the same block of statements
an else clause matches with
Subsection3.13.2Debugging Exercises
Identify and fix the syntax errors in each of the following:
Exercise3.13.5.
Debug the following program.
Exercise3.13.6.
Debug the following program.
Exercise3.13.7.
Debug the following program.
Exercise3.13.8.
Debug the following program.
Subsection3.13.3Programming 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:
Exercise3.13.9.
Parameter and argument.
Exercise3.13.10.
Method definition and method invocation.
Exercise3.13.11.
Local scope and class scope.
Exercise3.13.12.
Primitive type and reference type.
Exercise3.13.13.
Access method and constructor method.
Translate each of the following into Java code:
Exercise3.13.14.
If b1 is true, then print “one”; otherwise, print “two”.
Exercise3.13.15.
If b1 is false and if b2 is true, then print “one”; otherwise, print “two”.
Exercise3.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):
Exercise3.13.17.
if (isWalking == false)
System.out.println("One");
System.out.println("Two");
Exercise3.13.18.
if (isWalking == true)
System.out.println("One");
System.out.println("Two");
Exercise3.13.19.
if (isWalking == false)
{
System.out.println("One");
System.out.println("Two");
}
Exercise3.13.20.
if (isWalking == false)
if (isTalking == true)
System.out.println("One");
else
System.out.println("Two");
else
System.out.println("Three");
Exercise3.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");
}
Exercise3.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");
}
}
Exercise3.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.
Exercise3.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.
Exercise3.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”.
Exercise3.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.
Exercise3.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”.
Exercise3.13.28.
Define a void method named day1 that prints “a partridge in a pear tree”.
Exercise3.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.
Exercise3.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”.
Exercise3.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.
Exercise3.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
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.
Exercise3.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.
Exercise3.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.
Exercise3.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.
Exercise3.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.
Exercise3.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;
}
}