Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2022E

Section 3.12 Chapter Summary

Subsection 3.12.1 Technical Terms

accessor method loop structure repetition structure
class scope method overloading scope
formal parameter method signature selection
if statement mutator method side effect
if/else statement multiway selection while statement
inherit override while structure
local scope polymorphism

Subsection 3.12.2 Important Points

  • A formal parameter is a variable in a method declaration. It always consists of a type followed by a variable identifier. An argument is a value that is passed to a method via a formal parameter when the method is invoked. A method’s parameters constrain the type of information that can be passed to a method.
  • When an argument of primitive type is passed to a method, it cannot be modified within the method. When an argument of reference type is passed to a method, the object it refers to can be modified within the method.
  • Except for void methods, a method invocation or method call is an expression which has a value of a certain type. For example, nim.getSticks() returns a int value.
  • The signature of a method consists of its name, and the number, types, and order of its formal parameters. A class may not contain more than one method with the same signature.
  • A constructor is a method that is invoked when an object is created. If a class does not contain a constructor method, the Java compiler supplies a default constructor.
  • Restricting access to certain portions of a class is a form of information hiding. Generally, instance variables are hidden by declaring them private. The class’s public methods make up its interface.
  • The if statement executes a statement only if its boolean condition is true. The if-else statement executes one or the other of its statements depending on the value of its boolean condition. Multiway selection allows one and only one of several choices to be selected depending on the value of its boolean condition.
  • The while statement is used for coding loop structures that repeatedly execute a block of code while a boolean condition is satisfied.

Solutions 3.12.3 Solutions to Self-Study Exercises

3.2 Passing Information to an Object
3.2.8 Self-Study Exercises

3.2.8.1. Fill-In Scope.
Solution.
A parameter variable has local scope.
3.2.8.2. Matching Method Vocabulary.
Solution.
  • method declaration: defining a method by specifying its name, its parameters and result, and associating it with a segment of code.
  • method invocation: calling or using a defined method.
  • formal parameter: a variable in the method declaration, whose purpose is to store a value while the method is running.
  • argument: a value that is passed to a method during a method call.
3.2.8.3. setNames().
Solution.
The following code declares two instance variables for names of players and defines a setName() method. Of course, there are many other appropriate names for the variables and parameters and other initial assignments.
private String nameOne = "Player One";
private String nameTwo = "Player Two";
public void setNames(String name1, String name2)
{    nameOne = name1;
     nameTwo = name2;
}
A method call that sets the names of the players of game1 is:
game1.setNames("Xena","Yogi");

3.3 Constructors
3.3.1 Self-Study Exercises

3.3.1.1. Constructor Definition Bug.
Solution.
A constructor cannot have a return type, such as void.
3.3.1.2. OneRowNim Constructors.
Solution.
public OneRowNim(int sticks)
{    
     nSticks = sticks;
     player = 2;
}
public OneRowNim(int sticks, int initPlayer)
{    
     nSticks = sticks;
     player = initPlayer;
}

3.4 Retrieving Information from an Object
3.4.2 An Expanded OneRowNimClass

Self-Study Exercises
3.4.2.1. Determine Output.
Solution.
The following would be displayed on the screen:
1
20
false
3.4.2.2. OneRowNim get Method.
Solution.
public int getMoves()
{   
     return nMoves;
}

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

3.6 Flow of Control: Control Structures
3.6.5 The Nested if/else Multiway Selection Structure

Self-Study Exercises
3.6.5.1. Flow Chart.
Solution.
Flowchart of the if-else version of the getStatus() method:
3.6.5.2. If Debug.
Solution.
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");
3.6.5.3. getPlayerName().
Solution.
public String getPlayerName()
{    if (player == 1)
         return "Ann";
     else if (player == 2)
         return "Bill";
     else if (player == 3)
         return "Cal";
     else
         return "Error";
}

3.6.6 The While Structure

While Loop Self-Study Exercises
3.6.6.1. sumCubes().
Solution.
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
}
You have attempted of activities on this page.