Skip to main content
Logo image

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

Section 2.8 Chapter Summary

Subsection 2.8.1 Technical Terms

access modifier method call and return
class-level variable null pointer
default value null pointer exception
delimiter pointer
empty string reference
flow of control reference variable
interface static modifier
local variable user interface

Subsection 2.8.2 Important Points

  • Dot notation is used to refer to an object’s public elements.
  • Designing a class is a matter of deciding what role it will play and what information and actions it will have.
  • Writing a Java program is a matter of defining one or more classes. A class definition serves as a template for creating instance of the class.
  • Classes typically contain two kinds of elements, variables and methods. An object’s state is defined by its instance variables.
  • Class elements that are declared public can be accessed by other objects. Elements that are declared private are hidden from other objects.
  • A class’s instance variables are usually declared private so they cannot be accessed directly by other objects.
  • An object’s public instance methods can be called by other objects. Thus, they make up the object’s interface with other objects.
  • Object instantiation is the process of creating an object, using the new operator in conjunction with a constructor method.
  • A class definition consists of a header and a body. The header gives the class a name, specifies its accessibility (public), and its place in the Java class hierarchy (extends Object). The class body contains declarations of the class’s variables and definitions of its methods.
  • By default, a newly defined class is consider a subclass of Object.
  • Class elements that are declared static, such as the main() method, are associated with the class(not with its instances).
  • A Java application program must contain a main() method, which is where it begins execution.
  • Methods that are used solely for the internal operations of the class should be declared private.
  • An instance variable declaration reserves memory for the instance variable within the object, associates a name and a type with the location, and specifies its accessibility.
  • A method definition consists of two parts: a header, which names the method and provides other general information about it, and a body, which contains its executable statements.
  • Declaring a variable creates a name for an object but does not create the object itself. An object is created by using the new operator and a constructor method.

Solutions 2.8.3 Solutions to Self-Study Exercises

2.4 Class Definition
2.4.5 Define, Create, Use

Self-Study Exercises
2.4.5.1. Class Name.
Solution.
The name of the class is Riddle.
2.4.5.2. Riddle Instance Variables.
Solution.
The names of two instance variables: question, answer.
2.4.5.3. Riddle Methods.
Solution.
The names of three methods: Riddle(), getQuestion(), getAnswer().
2.4.5.4. Riddle Instances.
Solution.
The names of two Riddle instances: riddle1, riddle2.
2.4.5.5. Riddle Method Calls.
Solution.
All six method calls of the Riddle objects in the program:
Riddle("What is black and white and red all over?", "An embarrassed zebra.")
Riddle("What is black and white and read all over?","A newspaper.")
riddle1.getQuestion()
riddle1.getAnswer()
riddle2.getQuestion()
riddle2.getAnswer()
2.4.5.6. Riddle Qualified Names.
Solution.
Qualified names: riddle1.getQuestion() and riddle1.getAnswer().

2.5 CASE STUDY: Simulating a Two-Person Game
2.5.3 Testing the OneRowNim Class

Self-Study Exercises
2.5.3.1. Add Hint to Riddle Class.
Solution.
  1. Definition of new instance variable in the Riddle class:
    private String hint;
    
  2. The getHint() method of the Riddle class, which should be a public method, is:
    public String getHint()
    {
      return hint;
    }
    
  3. The setHint() method of the Riddle class, with the result type void. is:
    public void setHint(String aHint)
    {
      hint = aHint;
    }
    
2.5.3.2. Student Class.
Solution.
A possible definition of the Student class is given below.
public class Student
{  private String firstName;
   private String lastName;
   private int studentID;
   public void setStudent(String fName, String lName,int anID)
   {
     firstName = fName;
     lastName = lName;
     studentID = anID;
   }
   public int getStudentID() { return studentID; }
   public String getStudentName() ( 
    return firstName + " " + lastName; 
  } 
}

2.7 From the Java Library: java.util.Scanner
2.7.3 Exceptions

Self-Study Exercise
2.7.3.1. TestScanner for Decimals.
Solution.
A main method that reads and squares a real number is given below.
public static void main(String[] args)
{               // Create Scanner object
  Scanner sc = Scanner.create(System.in);
  System.out.print("Input a real number:");    // Prompt
  double realNum= sc.nextDouble();      // Read a double
  System.out.println(num + " squared = " + realNum*realNum);
} //main()
You have attempted of activities on this page.