Tonga Institute of Higher Education

IT151: Introduction to Programming Java

Homework 4

Task 1

Create a class, call it Person.

It’s instance variables should include the following:

  • Name of the person
  • The person’s age
  • Place where the person lives
  • Person’s gender

It’s class variables, include:

  • A counting variable that should be able to count how many people have been declared and used in your program.

Please note, you have to carefully select the most appropriate data type for each variable. And MUST keep them private.

It’s methods, include

  • Default constructor

Sets the name to your first name

Set age to 0

Leave the place blank

Set the gender to your own gender

Increment the class variable

  • Overloaded constructor with name, age, place and gender, all passed as parameters

Sets the instance variables to the parameters given

Increment the class variable

  • A static method to display the value of the counting variable
  • Create Accessor methods for each instance variables

Example: getName, setName, getAge, setAge, etc …

  • A method to display the details of a person. This should printout the name of the person, how old he is, where he lives and his gender.

Please note, you have to carefully select the return type of these methods. Some of these methods will require parameters, you have to figure that out!

After you have coded your class, use the file attached on the second page to test your class. The output from my program is pasted on the third page.

You should try to write your class, so that the output from your program is like the output from my program.

GOOD LUCK!
import javax.swing.JOptionPane;

class PersonTest {

public static void main(String[] args)

{

JOptionPane.showMessageDialog(null,"Welcome to this test program",

"Welome", JOptionPane.INFORMATION_MESSAGE);

Person Josh = new Person();

Person Tana = new Person("Tana Umaga", 33, "Hurricanes", 'm');

Person Richie = new Person ("Richiard McCaw", 25, "Crusaders", 'm');

Tana.displayDetails();

Josh.displayDetails();

Richie.displayDetails();

Josh.setAge(28);

Tana.setPlace("Wellington");

Richie.setName("Richie McCaw");

Tana.displayDetails();

Josh.displayDetails();

Richie.displayDetails();

System.out.println("-----> GO HURRICANES <-----");

}

}

Make sure that you save your files.

When to hand-in

CS1

This homework is due: 5pm, Thursday 25th May 2006

CS2

This homework is due: 5pm, Friday 26th May 2006

How to hand-in your assignment

When you finish your assignment, email me the source code only.

Email to: