Objectives

Students will be able to:

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

Spaceship class

public class Spaceship{

private String name;

private String captain;

public Spaceship(){

this.name = null;

this.captain = null;

}

public Spaceship(String name, String captain){

this.name = name;

this.captain = captain;

}

public String getName(){

return this.name;

}

public String getCaptain(){

return this.captain;

}

public void setCaptain(String newCaptain){

this.captain = newCaptain;

}

public String toString() {

return "Name: "+this.name+"; Captain: "+this.captain;

}

}

Gun class

public class Gun{

private int numBullets;

public Gun(){

this.numBullets = (int)(Math.random()*10)+1;

}

public void shoot(){

this.numBullets = this.numBullets - 1;

}

public int getNumBullets(){

return this.numBullets;

}

public String toString(){

return "Num Bullets: "+this.numBullets;

}

}

MilleniumFalcon class

public class MilleniumFalcon extends Spaceship{

private Gun[ ] guns;

static final int numGuns = 10;

public MilleniumFalcon(String name, String captain){

super(name, captain);

this.guns = new Gun[numGuns]

for(int i = 0; i<numGuns; i++){

guns[i] = new Gun();

}

}

public Gun getGun(){

for(int i = 0; i<numGuns; i++){

//return a loaded gun or null if none available

if(guns[i].getNumBullets() >0){

return guns[i];

}

}

return null;

}

public Gun getGun(int index){

if(guns[index].getNumBullets() > 0 ){

return guns[index];

}else{

return null;

}

}

public String toString(){

int loadedCount = 0;

for(int i = 0; i

if(guns[i].getNumBullets()>0) {

loadedCount++;

}

}

return super.toString()+"; Loaded Guns: "+loadedCount;

}

}

Review 2

key concepts so far

A MilleniumFalcon IS-A Spaceship

 

This is an example of inheritance.

Here are UML diagrams describing the MilleniumFalcon and Spaceship classes:

Note the way IS-A and HAS-A relationships are illustrated on a uml diagram.

Task 1

We will analyse these classes and review some of the concepts that we have covered so far in Object Oriented Programming.

Complete the exam questions.

Pros/Cons of Inheritance

Inheritance reduces the amount of code that needs to be written. It promotes code reuse.

Inheritance reduces the amount of maintenance required because often only the superclass code needs to be altered.

Inheritance can result in complex class heirarchies.

Glossary of Terms

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

  • constructor method
  • accessor method
  • mutator method
  • encapsulation
  • polymorphism (method overriding)
  • polymorphism (method overloading)
  • private (modifier)
  • public (modifier)
  • static (modifier)
  • final (modifier)
  • protected (modifier)
  • class
  • instance of a class (object)
  • inheritance
  • super class
  • sub class
  • dependencies

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