Objectives

Students will be able to:

  • review OOP concepts so far, including UML diagrams, encapsulation, polymorphism and containment(aggregation)

Encapsulation

Encapsulation is when data and method which operate on the data are stored in the same class/object.

One advantage of encapsulation is that code is easier to organise because data and related methods are stored in the same place.

Another advantage is that classes can be designed concurrently by multiple designers, speeding up development time.

Encapsulation also promotes code reuse - one class can be used to instantiate and process many objects.

Modifiers

Modifiers are used to control the access level for attributes and methods in a class. 3 commonly used access modifiers are:

  1. public: the attribute or method will be visible to other classes
  2. private: the attribute or method will not be visible to other classes
  3. protected: the attribute or method will only be visible to other classes in the same package.

Sorting

public Alien getOldestAlien(){

for(int i = 0; i<aliens.length; i++){

int smallest = i;

for(int j = i+1; j<aliens.length; j++){

if(aliens[j].getAge()<aliens[smallest].getAge();){

smallest = j;

}

}

Alien temp = aliens[i];

aliens[i] = aliens[smallest];

aliens[smallest] = temp;

}

return aliens[aliens.length-1];

}

Review

key concepts so far

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.

 

Task 1

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:

  • public
  • private
  • protected
  • static
  • final

Note: the Constructor method takes a String,int, String as parameters. These represent name,age,rank.

Task 2

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.

Task 3

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.

Task 4

The Ufo class requires another constructor.

This constructor will:

  1. set the topSpeed to 0
  2. initialise aliens as an array of 5 Alien objects
  3. set the number of lasers to 3 (a standard number in the Alien community).

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!

Task 5

Can you define the following terms? If there is +/- next to a term, can you also think of advantages and disadvantages?

  • encapsulation +/-
  • polymorphism
  • aggregation
  • constructor method
  • modifier
  • library routines/files +/-
  • accessor method
  • mutator method
  • object reference
  • parameter list
  • method signature

Glossary of Terms

Make sure you can define each of the following terms clearly:

  • constructor method
  • accessor method
  • mutator method
  • encapsulation
  • polymorphism (method overloading)
  • containment/aggregation
  • private (modifier)
  • public (modifier)
  • static (modifier)
  • final (modifier)
  • protected (modifier)
  • class
  • instance of a class (object)

Printable

This can be printed out and given to students as a pair activity.

Tags

classprivate public protected static final method constructor encapsulation mutator data-hiding accessormodifiers