Objectives

Students will be able to:

  •  
  •  

Vehicle class (describes any class that IS-A Vehicle)

public class Vehicle{

//class properties/attributes

private String regNum;

private int price;

private int topSpeed;

//constructor method

public Vehicle(String regNum, int price, int topSpeed){

this.regNum = regNum;

this.price = price;

this.topSpeed = topSpeed;

}

//accessor methods

public String getRegNum(){

return this.regNum;

}

public int getPrice(){

return this.price;

}

public int getTopSpeed(){

return this.topSpeed;

}

//mutator methods

public void setPrice(int newPrice){

this.price = newPrice;

}

//other methods

public void honk(){

System.out.println("Honk!");

}

public String toString(){

return "RegNum: "+this.regNum+"; Price: "+this.price+"; Top Speed: "+this.topSpeed;

}

}

Concurrency

Concurrency is when 2 or more events can happen at the same time.

OOP promotes concurrency because a project can be broken down into multiple classes and programmers can work concurrently to design, test and implement the classes.

Sequential events, on the other hand, happen one after the other.

Prototypes

A prototype of a product gives an impression (a look and feel) of what the final product might be like. It can be a cheap and fast way to help decide if the development of a product is worthwhile.

Designers often share prototypes with clients to give them a look and feel of what a product might be like, and to get feedback from the client to aid product development.

Inheritance 2

Exercises
 

A Car IS-A Vehicle

A Truck is a Vehicle

A Dragster is a Vehicle

A Tractor is a Vehicle

Every Vehicle has:

  • a registration number eg "x10312"
  • a price eg 24000
  • a topspeed eg 160

As well as these attributes, a Vehicle also has the following behaviour:

  • honk() - when this method is called, the Vehicle says "Honk!"
  • toString() - this returns a String of all the attribute values of a Vehicle

   Design the full Car, Tractor and Truck classes.

A Dragster IS-A Vehicle. It has all of the same behaviour and attributes as a Vehicle.

However, it also has a raceDistance attribute. This stores the distance that the dragster has covered in a race, starting at 0.

It also has crash() behaviour. This method randomly returns true if a dragster crashes, false otherwise.

When a Dragster honks, it honks like any Vehicle does, but after that it says "Growl!!!" like any true Dragster should!

   Design the full Dragster class. Make sure you override the Vehicle class's toString() method to include all of a Vehicle object's data + the raceDistance data.

A game is being developed.

You are coding the game logic and, other software designers are designing prototypes of the graphics.

public static void main(String[] args){

Dragster[] dragsters = new Dragster[2];

dragsters[0] = new Dragster("DG-101", 7500, 220);

dragsters[1] = new Dragster("DG-403", 11000, 240);

int raceDistance = 150;

race(dragsters, raceDistance);

}

public static void race(dragsters[] draggies, int distance){

//your game code

}

Create a game where the 2 dragsters race. The first dragster to reach the race distance is the winner.

During the race, if a dragster crashes it honks and its toString method is called.

   Code this game.

Adaptation

Instead of simply having a honk() method to handle honking, let's create a Honk object.

A Dragster HAS-A Honk object.

A Honk object has a honkWord eg "Honk!" and a volume eg 10

Everytime the Honk object honks, its volume decreases by 1.

Using your own ideas, include a Honk object as a property of a Dragster.

While the race is continuiing, Dragsters honk when they crash!

But what happens when a Dragster's honk volume reaches 0?!?!

Tags

behavioursyntax method inherit instantiate constructor prototype attributeconcurrency