Thursday, July 23, 2009

BlueJ Example - People

Below is a a picture showing a BlueJ example entitled "People". This basically illustrates two simple relationships that can be visualized in BlueJ: "use" and "inherit" relationships. As you see, here "Database" class uses "Person" abstract class and the "Staff" and "Student" classes inherit from this "Person" class. Just take a look at the code snippets to have a complete view of how these relationships are translated into codes.


BlueJ example "People", code author: Michael Kolling:
abstract class Person
{
private String name;
private int yearOfBirth;

Person(String name, int yearOfBirth){
this.name = name;
this.yearOfBirth = yearOfBirth;
}

public void setName(String newName) { name = newName;}
public String getName() { return name; }
public void setYearOfBirth(int newYearOofBirth) {yearOfBirth = newYearOofBirth;}
public int getYearOfBirth(){ return yearOfBirth;}

public String toString() { // redefined from "Object"
return "Name: " + name + "\n" + "Year of birth: " + yearOfBirth + "\n";
}
}
class Staff extends Person
{
private String room;

Staff() {
super("(unknown name)", 0000);
room = "(unknown room)";
}

Staff(String name, int yearOfBirth, String roomNumber) {
super(name, yearOfBirth);
room = roomNumber;
}

public void setRoom(String newRoom) {room = newRoom;}
public String getRoom() { return room; }

public String toString() { // redefined from "Person"
return super.toString() + "Staff member\n" + "Room: " + room + "\n";
}
}
class Student extends Person
{
private String SID; // student ID number

Student() {
super("(unknown name)", 0000);
SID = "(unknown ID)";
}

Student(String name, int yearOfBirth, String studentID) {
super(name, yearOfBirth);
SID = studentID;
}

public String getStudentID() { return SID; }

public String toString() { // redefined from "Person"
return super.toString() + "Student\n" + "Student ID: " + SID + "\n";
}
}
import java.util.ArrayList;
import java.util.Iterator;

public class Database {

private ArrayList persons;

public Database() { persons = new ArrayList();}
public void addPerson(Person p) { persons.add(p);}

public void listAll ()
{
for (Iterator i = persons.iterator(); i.hasNext();) {
System.out.println(i.next());
}
}
}

No comments:

Post a Comment