Solutions to Self-Study Exercises


[Page 140]

Solution 3.1

A method declaration defines the method by specifying its name, qualifiers, return type, formal parameters, and algorithm, thereby associating a name with a segment of executable code. A method invocation calls or uses a defined method.

Solution 3.2

A formal parameter is a variable in the method declaration. Its purpose is to store a value while the method is running. An argument is a value passed to a method in place of a formal parameter.

Solution 3.3

The following code declares two instance variables for names of players and defines a setName() method:

private String nameOne = "Player One"; private String nameTwo = "Player Two"; public void setNames(String name1, String name2) {    nameOne = name1;      nameTwo = name2; } 


Of course, there are many other appropriate names for the variables and parameters and other initial assignments.

Solution 3.4

A method call that sets the names of the players of game1 is:

game1.setNames("Xena","Yogi"); 


Solution 3.5

A constructor cannot have a return type, such as void.

Solution 3.6

One definition for the method is:

public OneRowNim(int sticks) {    nSticks = sticks;      player = 2; } 


Solution 3.7

The following would be displayed on the screen:

1 20 false 


Solution 3.8

One definition for the method is:

public int getMoves() {   return nMoves; } 



[Page 141]
Solution 3.9

One definition for the method is:

public boolean playerOneIsNext() {   return (player == 1); } 


Solution 3.10

See Figure 3.21.

Figure 3.21. Flowchart of the if-else version of the getStatus() method.


Solution 3.11

if (isHeavy == true)      System.out.println("Heavy") ; else;  // Error (remove this semicolon)      System.out.println("Light"); if (isLong == true)      System.out.println("Long") else                                    // Error (end line above with semicolon)      System.out.println("Short"); 


Solution 3.12

public String getPlayerName() {    if (player == 1)          return "Ann";      else if (player == 2)          return "Bill";      else if (player == 3)          return "Cal";      else          return "Error"; } 



[Page 142]
Solution 3.13

When passing an argument for a primitive type, a copy of the argument's value is passed. The actual argument cannot be changed inside the method. When passing a reference to an object, the object can be changed within the method.

Solution 3.14

public int sumCubes(int min, int max) {     int num = min;     int sum = 0;     while (num <=  max) {          // While num <= max         sum = sum + num*num*num;   // Add cube of num to sum         num = num + 1;             // Add 1 to num     }                              // while     return sum;                    // Return the sum } 





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