联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2018-08-18 02:47


School of Business Vocational Education

Course Name: Apply Introductory Object Oriented Language Skills

Course Code: COSC7374

Project Sample Scenario and Class Creation – Stage 1

Purpose

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

PersonTester

+ main (String[]): void

Person

+ 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

- age: int

- givenName: String

- familyName: String

- uni: EducationalInstitution

EducationalInstitution

+EducationalInstitution(String,

String)

+ setName (String): void

+ getName(): String

+ seCity (String): void

+ getCity(): String

+ toString(): String

- name: String

- city: String

Semester 2, 2018

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

Semester 2, 2018

COSC7374C_Stage1_Java_Sample_Scenario_S118.docx Page 3 of 3

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


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp