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 declaredprivate
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 themain()
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.2. Riddle Instance Variables.
2.4.5.3. Riddle Methods.
2.4.5.4. Riddle Instances.
2.4.5.5. Riddle Method Calls.
Solution.
2.4.5.6. Riddle Qualified Names.
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.
- Definition of new instance variable in the
Riddle
class:private String hint;
- The
getHint()
method of theRiddle
class, which should be apublic
method, is:public String getHint() { return hint; }
- The
setHint()
method of theRiddle
class, with the result typevoid
. 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 1 of 1 activities on this page.