辅导案例-S120

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
Semester 1, 2020
COSC6219C_Stage1_Java_Sample_Scenario_S120 Page 1 of 4


Purpose


School of Business Vocational Education
Course Name: Apply Introductory Object-Oriented Language Skills
Course Code: COSC6219C
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. You must create your own scenario however, do not use this one!
Sample Scenario Description
You have been contracted to create a program that allows an entomologist to keep track (i.e. record and
retrieve details) of different insects. Data fields will include the category (e.g. Beetle, Moth, Dragonfly,
Bee, etc.) and their species, colour and lifespan. The Insect's typical habitat and the areas where they can
be found, will also need to be recorded.
Develop the Required Classes
For Stage 1 of the project you will concentrate on creating two classes, which will be the most
general classes that can be derived from the scenario. In the next stage, you will establish other classes
that contain more specific details relating to particular groups of insects.
In this example, the most general class that can be developed from the above scenario is an Insect class.
This focuses on the attributes of the insect itself. To provide locational information, an Environment
class is also included. These will be the basic classes. Later you will create two specific insect classes to
collect data relevant to these separate insect groups, along with a user interface for easier data collection.
You would also need to test this class to make sure that you are able to create instances of the class
(objects), and access, modify, and display their data. This is done with a driver class (aka test class).
Therefore, you will need to create:
• An Insect class, with attributes: category, species, colour and lifespan.
• An Environment class with attributes: habitat and region.
• An InsectDriver class where Insect objects are created.
The Insect and Environment classes will need to include getter (accessor) and setter (mutator) methods,
and at least one constructor (see Week 4 handout) – although we'll actually use two in the Insect class to
demonstrate the multiple constructor option.
Initial Class Diagram
Environment
- habitat: String
- region: String
+ Environment()
+ Environment(String, String)
+ getHabitat (): String
+ setHabitat(String): void
+ getRegion(): String
+ setRegion(String): void
+ toString(): String
InsectDriver
+ main (String[]): void
Insect
- species: String
- size: int
- colour: String
- lifespan: int
- home: Environment
+ Insect()
+ Insect(String, int, String, int)
+ getSpecies(): String
+ setSpecies(String): void
+ getSize(): int
+ setSize(int): void
+ getColour(): String
+ setColour(String): void
+ getLifespan(): int
+ setLifespan(int): void
+ getHome(): Environment
+ setHome(Environment): void
+ toString(): String
Semester 1, 2020
COSC6219C_Stage1_Java_Sample_Scenario_S120 Page 2 of 4



Java Code

The Insect Class:

public class Insect {
// Insect attributes
private String species;
private int size;
private String colour;
private int lifespan;
private Environment home;
// the constructors
Insect() { // this one emulates the default constructor
species = null;
size = 0;

** add remaining attribute initialisations here **

}

Insect(String iSpecies, int iSize, String iColour, int iLifespan, Environment iHome){
species = iSpecies;

** add remaining attribute initialisations here **

}

// the accessors
public String getSpecies() {
return species;
}
** add remaining accessors here **


// the mutators
public void setSpecies(String iSpecies) {
species = iSpecies;
}
** add remaining mutators here **

// make attributes available for display
public String toString() {
return "\nSpecies: " + species + "\nSize (mm): " + size + "\nColour: " + colour +
"\nLifespan (days): " + lifespan + "\n" + home.toString();
}
} // end Insect class
Semester 1, 2020
COSC6219C_Stage1_Java_Sample_Scenario_S120 Page 3 of 4



The Environment class:

public class Environment {
// Environment attributes
public String habitat;
public String region;

// Constructor
public Environment(String eHabitat, String eRegion) {
super();
habitat = eHabitat;
region = eRegion;
}

// accessors
public String getHabitat() {
return habitat;
}

public String getRegion() {
return region;
}

// mutators
public void setHabitat(String eHabitat) {
habitat = eHabitat;
}

public void setRegion(String eRegion) {
region = eRegion;
}

// display attributes
@Override
public String toString() {
return "\n Insect typically found in " + habitat + " in " + region + "\n \n ------------
------";
}
} // end Environment class

-------------------------------------------------------------------------------------------------------
The InsectDriver Class:

public class InsectDriver {
// The program uses Insect and Environment objects, and gathers data about category, species,
// colour, lifespan, habitat and region and then displays that information.

public static void main(String[] args) {
// create three environment objects
Environment e1 = new Environment("Rainforest", "Northern Queensland");

** create remaining two environment objects (e2, e3) here **

// create three insect objects
Insect i1 = new Insect("Red Lacewing Butterfly", 26, "Red", 10, e1);

** create remaining two insect objects (i2, i3) here **
System.out.println("Displaying the insect information...");
// display insect data
System.out.println(i1.toString());

** add remaining two output statements here **
}

} // end InsectDriver class
Semester 1, 2020
COSC6219C_Stage1_Java_Sample_Scenario_S120 Page 4 of 4



Example program output:

Displaying the insect information...

Species: Red Lacewing Butterfly
Size (mm): 26
Colour: Red
Lifespan (days): 10
Insect typically found in Rainforest in Northern Queensland

------------------

Species: Transverse Ladybird
Size (mm): 6
Colour: Orange-Red
Lifespan (days): 365
Insect typically found in Grassland/Forest in Australian mainland

------------------

Species: Fiddler Beetle
Size (mm): 20
Colour: Yellow-Black
Lifespan (days): 120
Insect typically found in Eucalypt woodland in Eastern Australia

------------------
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468