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 3

Exercises
 

The Planet class describes planets in general.

Every Planet has:

  • distance from Earth
  • name
  • age

As well as these attributes, a Planet also has appropriate accessor/mutator methods and a toString() method which returns the values stored in a Planet object as a String.

Task 1

Start a new project in your IDE and design the full Planet class.

Test it by constructing an instance of a Planet in another project and call the toString() method, displaying the returned value.

Task 2

Pluta IS-A Planet.

However, unlike many other Planets, it is in the Kuiper Belt.

Design the full Pluta class, making sure an additional property indicating that it is in the Kuiper Belt is included.

Also, find a way of making sure that when we call the Pluto class's toString() method, that this information is displayed, along with its name, age etc.

Task 3

When the Pluto class's getName method is called, it displays the name of the Planet along with the message (Dwarf Planet)

Override the Planet class's getName() method by creating a getName method in the Pluto class.

This method returns a String with the name of the Pluto instance followed by the text (Dwarf planet)

Task 4

How can we update the Pluto class's toString() method to ensure that the information (Dwarf Planet) is included?

Tags

behavioursyntax method inherit instantiate constructor prototype attributeconcurrency