Abstracting

 

Abstracting means to ignore or hide everything except essential data that we require for the given problem or situation.

The London tube map is a great example of an abstraction. We don't need info about shops, house addresses or precise directions. We just need station names linked together.

Non-essential information is ignored as we develop an abstraction.

Pre-condition

A precondition is a condition that must be met for a following block of code to run successfully.

For example, if a precondition states that an array will not have nulls in it then the programmer does not have to care about checking for nulls when writing code.

Pay attention to preconditions when coding - they help programmers determine what actually needs to be coded.

Objectives

Students will be able to:

  • define a class file storing properties
  • define a constructor method
  • create an instance of an object(s) in a computer program

The Tank Class

public class Tank{

//first declare the class properties

private String id;

private Missile[] missiles;

int nextAvailableMissile;

//one constructor method

public Tank(){

this.id = null;

this.missiles = new Missile[10];

this.nextAvailableMissile = -1;

}

//another constructor method, same name, different parameter list!

public Tank(String id){

this.id = id;

this.missiles = new Missile[10];

this.nextAvailableMissile = -1;

}

public void addMissile(Missile m){

if(nextAvailableMIssile < 9){

nextAvailableMissile = nextAvailableMissile+1;

missiles[nextAvailableMissile] = m;

}

}

public void shootMissile(){

//code missing

}

public boolean isEmpty(){

if(nextAvailableMissile == -1){

return true;

}else{

return false;

}

}

}

The Missile Class

public class Missile{

//first declare the class properties

private int damageFactor;

//one constructor method

public Missile(){

this.damageFactor = (int)(Math.random()*10)+1; //between 1 and 10

}

//another constructor method, same name, different parameter list

public Missile(int damageFactor){

this.damageFactor = damageFactor;

}

public int getDamageFactor(){

return this.damageFactor;

}

}

Object Oriented Programming

Arrays of Objects

Start a new project in your IDE - TheTankProject

This project will have 3 .java files:

  • Main.java
  • Tank.java
  • Missile.java

A Tank has lots and lots of Missiles. It stores the Missiles in an array.

Each Missile has a Damage Factor - the higher the Damage Factor, the more destructive the missile.

Let's create an in the form of a class for Tanks.

Copy and paste this code into your Tank.java file.

And here is an abstraction of the current design of the Missile class:

Copy and paste this code into your Missile.java file.

Code analysis

A Tank HAS-A array of Missile objects. This is an example of aggregation/containment.

Task 1

Complete the Worksheet.

Extension I

Note: Task 4 and 5 are aimed at students who are motivated a 4 or 5 in AP Computer Science A or a 6 or 7 in IBDP Computer Science.

The City class has a number of buildings eg 40.

When a City object is constructed (instantiated), it stores this number in a class property (attribute).

It also has a method hit which processes Missile objects. It analyses the damage factor of the Missile and reduces the number of buildings by the damage factor of the Missile.

Design the City class using data hiding and any appropriate accessor and mutator methods.

Extension II

Use the City, Missile and Tank classes to design a game or scenario involving Cities, Tanks and Missiles!

Can you find a way to use the Tank class's getAverageRating method, for example?

Glossary

object

property

method

create an object/instance of a class

instantiate an object

encapsulate

access

mutate

abstraction

Tags

idesyntax file class instantiate constructor type property/attributemethod/behaviour indexabstraction