In your IDE, start a new project, BeachSheep
Create 2 classes:
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.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.
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.
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.
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.
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
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.
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.
Keep it modular! Consider designing helper functions, not just main(String[] args).
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.
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?
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.
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.
idesyntax file class instantiate constructor type propertymethod