Java

OOP GUIDE / WORK

 

COIN CLASS

 

Topics

  • Simple class design, Using objects

 

 

TASK – PART 1 – COIN CLASS

 

Create a Coin class with the following:

 

  • It has a state (“heads” or “tails”)
  • It keeps track of the number of times heads or tails have been tossed.
  • It has a toss() method that randomly sets its state to “head” or “tails”.
  • It has a constructor with no parameters.
  • Its initial state is randomly set but this doesn’t count as a toss.
  • It has a toString method that returns a String of the form:

         
          Coin is showing heads.  (H5T6)

where heads in the state of the coin and H5T6 means the coin has been tossed 11 times with heads resulting 5 times and tails resulting 6 times.

 

 

TASK – PART 2 – TESTING YOUR COIN CLASS

 

Create a CoinTester class.  Inside the main function, create a Coin object and test all of the methods several times.