Advertisement
Sergiovan

Throwable coin

Mar 26th, 2015
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1.  
  2. class IllegalCoinSideException extends RuntimeException{
  3.  
  4.     private static final long serialVersionUID = -2225459441999368571L;
  5.     IllegalCoinSideException(){};
  6.    
  7. }
  8.  
  9. public class Coin extends Throwable{
  10.  
  11.     private static final long serialVersionUID = 3630805023927806932L;
  12.     boolean heads = false;
  13.    
  14.     Coin(){
  15.         double chans = Math.random() * 100.0;
  16.         if(chans > 50){
  17.             heads = true;
  18.         }else if(chans < 50){
  19.             heads = false;
  20.         }else{
  21.             throw new IllegalCoinSideException();
  22.         }
  23.     }
  24.    
  25.     @Override
  26.     public String toString(){
  27.         return heads ? "Heads" : "Tails";
  28.     }
  29.    
  30.    
  31.     public static void main(String[] args){
  32.         try{
  33.             throw new Coin();
  34.         }catch(Coin c){
  35.             System.out.println(c);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement