Java Reference
In-Depth Information
Exercise 2.70 Correct the error in this method:
public void getAge()
{
return age;
}
Exercise 2.71 Write an accessor method called getName that returns the value of a field
called name , whose type is String .
Exercise 2.72 Write a mutator method called setAge that takes a single parameter of type
int and sets the value of a field called age .
Exercise 2.73 Write a method called printDetails for a class that has a field of type
String called name . The printDetails method should print out a string of the form “The
name of this person is”, followed by the value of the name field. For instance, if the value of the
name field is “Helen”, then printDetails would print:
The name of this person is Helen
If you have managed to complete most or all of these exercises, then you might like to try
creating a new project in BlueJ and making up your own class definition for a Person . The
class could have fields to record the name and age of a person, for instance. If you were
unsure how to complete any of the previous exercises, look back over earlier sections in this
chapter and the source code of TicketMachine to revise what you were unclear about. In
the next section, we provide some further review material.
2.20
Reviewing a familiar example
By this point in the chapter, you have met a lot of new concepts. To help reinforce them, we
shall now revisit a few in a different but familiar context. Along the way, though, watch out
for one or two further new concepts that we will then cover in more detail in later chapters!
Open the lab-classes project that we introduced in Chapter 1, and then examine the Student
class in the editor (Code 2.9).
Code 2.9
The Student class
/**
* The Student class represents a student in a
* student administration system.
* It holds the student details relevant in our context.
*
* @author Michael Kölling and David Barnes
* @version 2011.07.31
*/
public class Student
{
// the student's full name
 
 
Search WWH ::




Custom Search