Driver class

public class Driver{

//class properties/attributes

private String name;

//constructor method

public Driver(String name){

this.name = name;

}

//accessor methods

public String getName(){

return this.name;

}

public void driveCar(Car car){

car.drive();

}

}

Car class

public class Car{

//class properties/attributes

private double distance;

private int fare;

//constructor method

public Car(){

this.distance = 0.0;

this.fare = 0;

}

//another constructor method (method overloading!)

public Car(double distance){

this.distance = distance;

this.fare = 0;

}

//other methods

public void drive(){

this.distance = this.distance + 0.1;

this.fare = this.fare + 1;

System.out.println(toString());

}

public double getDistance(){

return this.distance;

}

public int getFare(){

return this.fare;

}

public void reset(){

this.distance = 0;

this.fare = 0;

}

public String toString(){

System.out.println("Distance: "+distance+"; Fare: "+fare);

}

}

Method signatures

Method signatures define/describe:

  • the name/identifier of the method
  • the parameters of the method (identifier and data type)
  • the return type of the method eg void or int etc

Objectives

Students will be able to:

  • review concept of HAS-A relationship (dependency)
  • appreciate that dependencies should be minimized when writing computer programs
  • review binary search algorithm

General Review

USES-A (Dependency relationship)

 

A Driver USES-A Car. This is an example of a Dependency relationship between a TaxiDriver and a Car.

The Driver class is .

The Car class is .

Take a moment to analyse the two classes.

Can you find examples of:

  • abstraction
  • encapsulation
  • data hiding
  • constructor methods
  • accessor methods
  • mutator methods
  • method overloading

Task 1

Create a project environment in your IDE and create the Driver, Car and Main classes.

In the Main class, can you figure out how to make a Driver drive a Car for 20kms.

Task 2

A Driver drives a Car 10 times. Each journey is a random distance.

At the end of each journey, the fare is stored in a 10-element integer array, fareData, in the Main class.

Then, a function, searchFare, is called. This function uses the algorithm to search for a given fare.

It returns true if the given fare is found in the array and false otherwise. Here is an example of a Main program:

Driver jack = new Driver("Jack");

Car sporty1 = new Car();

int[] fareData = new int[10];

for(i =0; i<10; i++){

//code to make Driver drive the car not shown.

fareData[i] = sporty1.getFare();

sporty1.reset(); //reset distance and fare before next journey

}

boolean result = searchFare(fareData, 100);

if(!result){

System.out.println("Fare not found!");

}else{

System.out.println("Fare found!");

}

Implement the searchFare helper function in the Main class.

You may need to design another helper function to sort searchFare into ascending order of value before searching!

Search Algorithms

If you are an IBDP student you will need to be familiar with 2 popular search algorithms:

  • Linear Search - start at index 0 and traverse the array until the item is found or the end of the array is reached.
  • Binary Search - repetetively identify the middle element of an array and, if it does not contain the item we are looking for, update the upper or lower bound of the search area and check the middle element again. Divide and Conquer!

Submission

Submit your solution as instructed.

Glossary

object

property

method

create an object/instance of a class

instantiate an object

encapsulate

access

mutate

abstraction

signature

Tags

methodsyntax overriding overloading instantiate instance super parameterattribute