A UFO has Aliens!
This is an example of containment.
Here are UML diagrams describing the Alien and Ufo classes.
Note the position of the diamond - think of it as “has” : the Ufo - HAS - Aliens.
Design the full Alien class. Put a comment above each method noting if it a constructor, accessor or mutator method.
Also, as you are properties and methods into the same class (a great strategy for managing data in a computer program), make sure you use the data hiding strategy.
You will need to use any of the following modifiers thoughtfully as you design properties and methods:
Note: the Constructor method takes a String,int, String as parameters. These represent name,age,rank.
Design the full Ufo class.
Can you figure out how the Constructor method works? As with the Alien class, make sure you use data-hiding as you encapsulate properties and methods into the same class file. Consider also the you will use.
The getAlienName method takes a String as a parameter. It represents a rank. It searches the aliens property and returns the name of any one Alien who has the same rank as the parameter.
If no Alien in aliens has that rank, the method returns null.
pre-condition: any of the elements in aliens may be null.
post-condition: the method returns a String or null and the contents of aliens remains unchanged.
The getAverageAlienAge() method calculates and returns the average age of every Alien in aliens.
pre-condition: any of the elements in aliens may be null.
post-condition: the method returns a double and the contents of aliens remains unchanged.
A new Ufo class method is required, getOldestAlien().
This method sorts aliens in ascending order of age, and returns a reference to the object at the biggest index.
pre-condition: assume aliens is full of Alien object references.
post-condition: aliens is sorted by age and an Alien reference is returned.
is one solution.
Name the sorting algorithm used.
The Ufo class requires another constructor.
This constructor will:
Add this constructor method to your design of the Ufo class.
State what OOP concept is being demonstrated here hint: it is a form of POLYMORPHISM!
Can you define the following terms? If there is +/- next to a term, can you also think of advantages and disadvantages?
Make sure you can define each of the following terms clearly:
This can be printed out and given to students as a pair activity.
classprivate public protected static final method constructor encapsulation mutator data-hiding accessormodifiers