Objectives

Students will be able to:

  • understand that property and behaviour definitions can be encapsulated in a class file
  • understand that we can instantiate objects by calling a class file's constructor method
  • write a program to communicate with properties and methods encapsulated in an instantiated object(s)

Hopper Class

public class Hopper{

private String name;

private int distance;

private int numHops;

public Hopper(String name){

this.name = name;

this.distance = 0;

this.numHops = 0;

}

public String getName(){

return this.name;

}

public int getDistance(){

return this.distance;

}

public int getNumHops(){

return this.numHops;

}

public void Hop(int d){

setDistance(d);

scream();

setNumHops();

}

public void setDistance(int distance){

this.distance = this.distance + distance;

}

public void setNumHops(){

numHops = numHops + 1;

}

public void scream(){

System.out.println(name+" is hopping: ");

}

public String toString(){

return "Name: "+name+"; Distance: "+distance+"; Num Hops: "+numHops;

}

}

Advantages

Improved security

Using data-hiding / Use of private variables

So that other classes cannot (accidentally) access/change the data (directly) / So other classes have to use accessor methods to access the data / So programs can be made read-only

Ease of maintenance

Programs will be easier to update

Because changes to the encapsulated class should not impact on other classes

Code reuse

Methods are stored in a class.

These methods can be used by mutiple instances of the class ie code can be reused, reducing the amount of code needed in a program.

Ease of understanding

Programs will be easier to follow

Because all data and methods are in the same place

Ease of testing

Because the class can be fully tested in isolation

And limited code will contain fewer mistakes than the full program

Faster development (of whole program)

Different classes can be worked on by teams

Each class is effectively independent of others (although this is not always true - see dependencies)

Hopper

Hoppers

3 Hoppers hop in a crazy-fun-wild Hopper jump-fest!

Check out the Hopper class. Data and methods are into a single file.

Every Hopper object stores:

  • Its name
  • Its distance covered during a race (starting at 0)
  • The number of hops it takes to reach a target distance.

Every Hopper has regular accessor/mutator methods, and it screams when it hops! It also has a toString() method which returns a String of data currently stored in the object.

is a Hopper abstraction in the form of a class.

The Race - Frenzied, Unique, Wild!

Let's get these guys and gals racing!

When a race is complete, the Selection Sort algorithm is used to determine the winner.

The Hopper array, hoppers is sorted by distance and the Hopper at index 2 is determined the winner.

is the race code.

Task

In your IDE set up a new project called HopperProject.

Somehow import the Hopper class code and the Main class code into your project.

Answer the questions in the worksheet and complete the coding tasks.

Helper Video

Extension

Other algorithms you may be familiar with are:

  • Bubble Sort
  • Linear Search
  • Binary Search
  • other searching/sorting algoirthms

Can we create Helper Functions in our Main class to search and sort the Hopper array in different ways?

This may be a review activity or an opportunity to explore other algorithms.

Glossary

class

attributes

constructor method

accessor method

mutator method

create an instance of a class (ie an object)

instantiate an object

a reference variable (to an object)

parameter