Advertisement
ksimovski

Class

Feb 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. package hwk3_kristian_simovski;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Coin {
  6.     private int value;
  7.     private String sideUp;
  8.     private Random rnd;
  9.    
  10.     //constructor method
  11.     public Coin(){
  12.        rnd = new Random();
  13.        flipCoin();
  14.     }
  15.    
  16.     //method to flip the coin
  17.     public void flipCoin(){
  18.         value = rnd.nextInt(2);  
  19.         if (value == 1)
  20.         {
  21.             sideUp = "Heads" ;
  22.         }
  23.         else
  24.         {
  25.            sideUp = "Tails" ;
  26.         }
  27.     }
  28.    
  29.     //returns the value to the side that will be displayed
  30.     public String getFace(){
  31.     return sideUp;
  32.     }
  33.    
  34.     //returns s string containing the value and side facing up
  35.     public String toString(){
  36.     return "Value: " + value + ",sideUp: " + sideUp;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement