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)

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 / reusability

Programs will be easier to update / reuse

Because changes to the encapsulated class should not impact on other classes / Because all data and methods are in the same place

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)

Library files

The TimeUnit class is an example of a Library file that we can import into our own projects.

Library files are great because they have already been written and tested, saving project development time.

BeachSheep Class

public class BeachSheep{

//attributes

private String name;

private int score;

//contructor method

public BeachSheep(String name){

this.name = name;

this.score = 0;

}

//accessor methods

public String getName(){

return this.name;

}

public int getScore(){

return this.score;

}

//mutator methods

public void setScore(){

this.score = this.score+ 1;

}

}

Case Study 1

 

Beach Sheep

Set up

In your IDE, start a new project, BeachSheep

Create 2 classes:

  • Main.java
  • BeachSheep.java

Complete the Task Document as you work your way through the Case Study.

Beach basketball is a game player by two BeachSheep.

Each BeachSheep in the game has a name and a score.

Everytime the BeachSheep scores, his/her score increases by 1.

is an abstraction of a BeachSheep in the form of a class.

Task 1

Copy/paste the code above into your BeachSheep.java file.

In Main.java, write a line of code which will instantiate (create an instance of) a BeachSheep called Pocohontas. The reference variable is bs1

Make sure there are no errors and then paste the line of code into the Task Document where indicated.

Task 2

We can write other methods to describe a BeachSheep's behaviour.

Every instance of a BeachSheep can shoot. Sometimes (s)he misses, and sometimes (s)he scores.

Inside the BeachSheep class, design the shoot() method. When it is called it returns true if the BeachSheep scores, false otherwise.

hint: design a solution which randomly returns true or false.

Task 3

Test your design by using reference variable bs1 to call the shoot() method. Display the result.

Once it is working, copy/paste the shoot() code into the Task Document where indicated.

Task 4

Another behaviour that the BeachSheep exhibits is that it can tell its name and current score when asked.

Design a method toString which returns a String containing a BeachSheep's name and current score in a sentence eg:

Name: Pocohontas; Score: 13

Call this method in your main program. Once it is working, copy and paste your code in the Task Document where indicated.

Task 5

Another interesting BeachSheep behaviour is that it can print a ticket with its own name and score, AND its opponent's on it!

Have a think about the design of this interesting BeachSheep class method. Here is a starter idea:

The signature of this method is:

public void printTicket(BeachSheep opponent)

and when called the output will be, for example:

Name: Pocohontas; Score: 13

Name: Geronimo; Score: 7 //this is the opponent's data

Task 6

In your Main program, create another instance of a BeachSheep called Geronimo with a reference variable bs2

Call the printTicket() method of bs1 and bs2 and check that it works as expected.

Copy and paste your printTicket() solution in the Task Document where indicated.

Beach Basketball

Now, it is time to play!

A game lasts for 60 seconds.

During the game, each time a BeachSheep scores, its score is updated by one.

At the end of the game, a ticket is produced with the name and score of the winning BeachSheep followed by the name and score of the opponent.

DESIGN THIS GAME!

Keep it modular! Consider designing helper functions, not just main(String[] args).


Delay

In the game loop is it possible to slow down the output to make it more realistic?

Can we introduce a delay? For example, using the class in Java.

Adaptation

Time permitting, you may want to adapt the game.

For example, a game uses MagicBalls.

Every MagicBall has a power.

When a BeachSheep shoots a MagicBall, if the power of the ball is greater than 5, the player's score increases by 3 instead of 1.

How could we implement such a scenario?

Or do you have another scenario/adaptation you want to implement?

Review

More than a Case Study, this activity offered an opportunity to review concepts covered so far.

How did you do?

Are you struggling with any aspect of this approach to managing data in a computer program?

Take some time to refelct and write down any questions for your teacher or for further research.

Remember, encapsulation involves data hiding. External classes can only access the data via methods.

Case Study

The Case Studies are intended to help you test your current knowledge and explore your own ideas.

They are also intended to help you prepare for upcoming assessments.

Work with your network of coders in class and share ideas and solutions as you make progress.

Tags

idesyntax file class instantiate constructor type propertymethod