Skip to main content
Contents Index
Search Book
Search Results:
No results.
Readability settings Prev Up Next Scratch ActiveCode Profile
title here
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
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
Solution .
The name of the class is
Riddle.
Solution .
The names of two instance variables:
question,
answer.
Solution .
The names of three methods:
Riddle(),
getQuestion(),
getAnswer().
Solution .
The names of two
Riddle instances:
riddle1,
riddle2.
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()
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
Solution .
Definition of new instance variable in the Riddle class:
The getHint() method of the Riddle class, which should be a public method, is: public String getHint()
{
return hint;
}
The setHint() method of the Riddle class, with the result type void. is: public void setHint(String aHint)
{
hint = aHint;
}
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
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.