| RHS => Allan => Basic Programming => Exercises => Person |
Updated:
02/02/2004 23:36 WebMaster: allanhn@rhs.dk |
Person
You are to develop a program, which can handle registrations of personal
data.
For a start we define a simple version of the class Person:
Datafields:
private String name;
private int yob;
(yob = year of birth)Constructors
public Person(String aName) { ...
public Person(String aName, int aYear) { ...Methods operating on the data fields
public void setName(String aName) { ...
public String getName() { ...
public void setYear(int aYear) { ...
public int getYear() {...
public int getAge(int thisYear) { ...
public String toString() { ...
Worker class
The class Person is to resemble the following template:
/* Fill in your comments
*
*/
public class Person {
private String name;
private int year;// constructor for creating objects
Person(String aName) {
name = aName;
year = 2003; //default value
}//Fill in your comments
Person(String aName, int aYear) {
name = aName;
year = aYear;
}//Fill in your comments
public String getName() {
return name;
}//Fill in your comments
public void setName(String aName) {
name = aName;
}//Fill in your comments
public int getYear() {
return year;
}//Fill in your comments
public void setYear(int aYear) {
year = aYear;
}//Fill in your comments
public int getAge(int thisYear ) {
return (thisYear - year);
}//Fill in your comments
public String toString() {
return ("Name: " + name + "\n Year of Birth: " + year);
}}// Person
Question 1 Worker class
Use TextPad to enter the above given Person class.
Save it in Person.java. Compile it.
This is your worker class.
Question 2 Application class
We are now to write an application using objects of the class Person.
Therefore
we introduce a new application class, PersonApp with the usual main method.
The PersonApp class must resemble the following template:
public class PersonApp {
public static void main(String[] args) {
Person teacher = new Person("Michael", 1961);
System.out.println(teacher.getName());
teacher.setName("Michael Claudius");
System.out.println(teacher.getName());
System.out.println(teacher.getYear());
System.out.println(teacher.getAge(2003));Person man = new Person("Li Tai", 1980);
Person woman = new Person("Christine Tai", 1970);System.out.println();
System.out.println(man.toString());
System.out.println(woman.toString());
}//main}//PersonApp
In TextPad open a new window and enter the above given PersonApp class.
Save it. Compile it. Run it.
Question 3 Student data
In main() in PersonApp declare
a new variable student (that's
you!) with your personal data and print these data just like we
did for the man and woman variables.
Question 4 Anniversary
In Person define a method public
int anniversary() which
returns the year when a Person will be 100 years
old.
Do your very best!
Tip: Very similar to the method getAge().
Call this method for man and student in the PersonApp class.
Question 5 How does it work?
In your study group very, very careful discuss why and how this program work
!
Go through each line and explain for each other what goes on.
Question 6 Read data
In main() in PersonApp use
showInputDialog() to read in the data: name and year for
a student.
Declare a new variable studentX with these data.
Use the getAge() method to find who is the elder
of the two students student and studentX and
print out the name of the eldest.
Question 7 Sex
Expand the Person class with a data field for the sex (male or female) of the Person.
Create a new constructor, which can initialise: name, year and sex.
Do not change the other constructors !!
In PersonApp use this constructor to create an object and print out the data
for this object.