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 Fill-In Scope.
A parameter variable has local scope. Matching Method Vocabulary.
  • 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. setNames().
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:

3.3 Constructors
3.3.1 Self-Study Exercises Constructor Definition Bug.
A constructor cannot have a return type, such as void. OneRowNim Constructors.
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 Determine Output.
The following would be displayed on the screen:
false OneRowNim get Method.
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 Flow Chart.
Flowchart of the if-else version of the getStatus() method: If Debug.
if (isHeavy == true)
     System.out.println("Heavy") ;
else ;  // Error (remove this semicolon)
if (isLong == true)
else   // Error (end line above with semicolon)
     System.out.println("Short"); getPlayerName().
public String getPlayerName()
{    if (player == 1)
         return "Ann";
     else if (player == 2)
         return "Bill";
     else if (player == 3)
         return "Cal";
         return "Error";

3.6.6 The While Structure

While Loop Self-Study Exercises sumCubes().
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.