CS案例之java案例Project Sample Scenario and Class Creat
当前位置:以往案例 > >CS案例之java案例Project Sample Scenario and Class Creat
2018-09-28

image.png


School of Business Vocational Education


Course Name: Apply Introductory Object Oriented Language Skills Course Code: COSC7374


Project Sample Scenario and Class Creation – Stage 1
This document provides an example scenario to show how the program classes can be generated
from the scenario description.

Sample Scenario Description You have been contracted to create a program that allows an educational institution to keep track (i.e. record and retrieve details) of the different staff members (including which type of staff member they are – e.g. administration, teaching, security, cleaning, etc. – and their name, staff number, age, etc.), and the students (also including type of student – e.g. full fee paying, international, etc. – and their name, student number, age, etc.) at the institution.

Develop the Required Classes For Stage 1 of the project we will concentrate on creating just two classes, which will be the most general class that we can derive from the scenario. As we progress we will begin to establish other classes that contain more specific detail related to each type of staff member and student.

The most general class that we could develop from the above scenario is a Person class and Educational Institution. That will be our basic class. Later we will later create a general Student class, and a general StaffMember class.
We would also need to test this class to make sure that we are able to create instances of the class (objects), and access, modify, and display their data. This is done in a tester class.

For the Person class we would include attributes such as the person's given name & family name, their age or date of birth, their address, etc. For the moment we'll just include their given name(s), family name, and their age.

For the Educational Institution class we will have attributes as

So what we have is: a Person class, with attributes: givenName, familyName, and age. We also need to include getter (accessor) and setter (mutator) methods, and at least one constructor (see Week 4 handout ) – we'll actually put two in as per the project description; and, PersonTester class.

Initial Class Diagram

image.png

Person






PersonTester


+ main (String[]): void

– age: int

– givenName: String

– familyName: String

– uni: EducationalInstitution


+ Person()

+ Person (String, String, int)

+ setGivenName (String): void

+ getGivenName(): String

+ setFamilyName (String): void

+ getFamilyName(): String

+ setAge (int): void

+ getAge(): int

+ setUni(EducationalInstitution): void

+ getUni():EducationalInstitution




Java Code (also present as a zipped file)
The Person Class


public class Person{
// Person attributes
private int age;
private String givenName;
private String familyName;
private EducationalInstitution uni;

// the constructors
Person () { // this one emulates the default constructor age = 0;
givenName = null; familyName = null; uni = null;
}
Person (String gName, String fName, int howOld, EducationalInstitution eI) { age = howOld;
givenName = gName; familyName = fName; uni = eI;
}

// the mutators
public void setGivenName (String gName) { givenName = gName;
}
public void setFamilyName (String fName) { familyName = fName;
}
public void setAge (int howOld) { age = howOld;
}
public void setUni(EducationalInstitution eI) {
this.uni = eI;
}

// the accessors
public String getGivenName () {
return givenName;
}
public String getFamilyName () {
return familyName;
}
public int getAge () {
return age;
}
public EducationalInstitution getUni() {
return uni;
}

// make attributes available for display
public String toString () {
return "\nFamily Name: " + familyName + "\nFirst Given Name: " + givenName + "\nAge: " + age + "\n"+uni.toString();
}

} // end Person class



Educational Institution class:

public class EducationalInstitution {
private String name;
private String city;

public EducationalInstitution(String n, String c) {
super(); name = n; city = c;
}
public String getName() {
return name;
}
public void setName(String n) { name = n;
}
public String getCity() {
return city;
}
public void setCity(String c) { city = c;
}
@Override
public String toString() {
return "Student of: " + name + "\nCity: " + city;
}
}

The PersonTester Class
public class PersonTester{
/**
* The program uses Person and Educational Institution objects, and gathers info
* about their given names, family names, ages and Institution details.
* It then displays that information.
*/
public static void main (String [] args) {
//create three phone objects EducationalInstitution e1 = new
EducationalInstitution("RMIT","Melbourne");
// create three Person objects
Person p1 = new Person("RMIT","Citizen",22,e1);
// assign attribute data p1.setGivenName ("John"); p1.setFamilyName ("Citizen"); p1.setAge (22);

// repeat the above for Person p2 & p3

// display the Person data
System.out.println ("Displaying the person information ...");
// When displaying the contents of an object using print() or println()
// Java automatically looks for a toString() method, and, if available
// uses it to display the object contents. System.out.println (p1.toString());

// repeat display for p2 & p3

} // end main()

} // end PersonTester class


在线提交订单