Java Reference
In-Depth Information
10.
(You must complete Self-Check Problem 7 before answering this question.)
Add two new methods to the
Name
class:
public String getNormalOrder()
Returns the person's name in normal order, with the first name followed by the middle initial and last name. For exam-
ple, if the first name is
"John"
, the middle initial is
"Q"
and the last name is
"Public"
, returns
"John Q. Public"
.
public String getReverseOrder()
Returns the person's name in reverse order, with the last name preceding the first name and middle initial. For example,
if the first name is
"John"
, the middle initial is
"Q"
, and the last name is
"Public"
, returns
"Public, John Q."
.
11.
How do you write a class whose objects can easily be printed on the console?
12.
The
Point
class in the
java.awt
package has a
toString
method that returns a
String
in the following format:
java.awt.Point[x=7,y=2]
Write a modified version of the
toString
method on our
Point
class that returns a result in this format.
13.
(You must complete Self-Check Problem 7 before answering this question.)
Write a
toString
method for the
Name
class that returns a
String
such as
"John Q. Public"
.
Section 8.3: Object Initialization: Constructors
14.
What is a constructor? How is a constructor different from other methods?
15.
What are two major problems with the following constructor?
public void Point(int initialX, int initialY) {
int x = initialX;
int y = initialY;
}
16.
(You must complete Self-Check Problem 7 before answering this question.)
Add a constructor to the
Name
class that accepts a first name, middle initial, and last name as parameters and
initializes the
Name
object's state with those values.
17.
What is the meaning of the keyword
this
? Describe three ways that the keyword can be used.
18.
Add a constructor to the
Point
class that accepts another
Point
as a parameter and initializes this new
Point
to
have the same (
x, y
) values. Use the keyword
this
in your solution.
Section 8.4: Encapsulation
19.
What is abstraction? How do objects provide abstraction?
20.
What is the difference between the
public
and
private
keywords? What items should be declared
private
?
21.
When fields are made private, client programs cannot see them directly. How do you allow classes access to read these
fields' values, without letting the client break the object's encapsulation?
22.
Add methods named
setX
and
setY
to the
Point
class that allow clients to change a
Point
object's
x
- and
y
-coordinates, respectively.
23.
(You must complete Self-Check Problem 7 before answering this question.)
Encapsulate the
Name
class. Make its fields private and add appropriate accessor methods to the class.
Search WWH ::
Custom Search