OOP Test Option 1
Construct an algorithm in pseudocode for listSpecies(Specimen[] animals), which will generate a list of the different species in the zoo
//ANIMALS is a unique array containing Specimen objects //UNIQUE is a collection, initially empty //SPECIES is a Species object loop BEAST from 0 to the number of elements in ANIMALS NEW_SPECIES = true UNIQUE.resetNext() loop while UNIQUE.hasNext() if UNIQUE.getNext() = species of ANIMALS[BEAST] then NEW_SPECIES = false end if end loop UNIQUE.resetNext() loop while UNIQUE.getNext() SPECIES = UNIQUE.getNext() output SPECIES.toString() end loop
Outline one advantage and one disadvantage of having the specimen object as a sub-class of the species object
Advantage - Specimen objects would inherit all the attributes of the species object - would allow code in specimen object to access species-related methods directly Disadvantage - Logically inconsistent - data in the species class may not be consistent across the associated Specimens
Construct code for the genus object including a constructor, accessor methods and a toString() method
public class Genus{ private String genusName; public Genus(String g){ setGenusName(g);} public void setGenusName(String g){ genusName = g;} public String getGenusName(){ return genusName;} public String toString(){ return "Genus: " + genusName;} }
UML diagram for species object
species String: speciesName setSpeciesName(String s) String getSpeciesName() String toString() boolean equals(Species s)
State relationship between the genus and species objects
the species class inherits from the genus class
Outline why calling the toString() method in this code does not cause an error
the two methods are related to the specific class declared the compiler selects the correct method
Construct a method countSpecimens(Specimen[] animals, Species s) that will output the number of specimens of the given species in the zoo
void countSpecimens(Specimen[] animals, Species s){ int sCount = 0; int i; for(i = 0; i < animals.length; i++){ if (s.equals(animals[i].getTOA()){ sCount++;} } System.out.println(sCount); }
identify an instance variable in the specimen class
private String name
Outline the changes that would be needed in order to add a description of each animal's individual markings to the program
markings are a characteristic of each individual animal description of markings should be an instance variable within the specimen class there should be accessor (get/set) methods for the markings the toString() method should include the description of the markings
Identify the term for this property
polymorphism or overriding
Identity an accessor method in the specimen class
private String getName()
Outline two benefits provided by encapsulation
Ease of testing - by putting all the structure of the data in a single class, other classes which make use of that data can be easily tested by simply providing then with data values even before the "real" data class is available for testing Ease of maintenance - if there are changes to the data or how it is stored or accessed, only the one class needs to be changed and the others will not
Define the term encapsulation
Encapsulation refers to the practice of hiding the structure and representation of data within a class and making it accessible outside the class via accessor functions
State the relationship between the species and specimen objects
The specimen class has a member variable that is a species object Species and specimen are associated They "have a relationship"(no inheritance)
Outline two ways in which the programming team can benefit from the way the relationships between the three objects, specimen, species, and genus, have been represented in the code
avoids duplicate code- can call the same object multiple times if code is needed. Data is inherited faster development - programmers can work on different parts of the code and different object at the same time without having to rewrite the entire code Simplifies Testing - functionality inherited from a parent class does not need to be re-tested in the new class
