Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.*;
  2. public class Coin {
  3.     private int c;
  4.     private Random rand;
  5.     private int h;
  6.     private int t;
  7.     private ArrayList<Integer> history;
  8.     public Coin(){
  9.        this.rand = new Random();
  10.        history = new ArrayList<Integer>();
  11.     }
  12.     public String flip(){
  13.         c = rand.nextInt(2);
  14.         history.add(c);
  15.         if(c==0){
  16.             return "H";
  17.         }
  18.         else{
  19.             return "T";
  20.         }
  21.     }
  22.     public int getNumHeads(){
  23.         for (int i = 0; i < history.size(); i++){
  24.             if(history.get(i)==0){
  25.                 h = (h + 1);
  26.             }
  27.         }
  28.         return h;
  29.     }
  30.     public int getNumTails(){
  31.         for (int j = 0; j< history.size(); j++){
  32.             if(history.get(j)==1){
  33.                 t = (t + 1);
  34.             }
  35.         }
  36.         return t;
  37.     }
  38.    public void reset(){
  39.        history.clear();
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement