Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. package week02;
  2. import java.util.Random;
  3. /**
  4.  * This program class represents a coin.
  5.  * @author Adrian Yadav
  6.  * @version  : 0.1
  7.  * @param
  8.  *
  9.  */
  10.  
  11.  
  12. public class Coins{
  13.  
  14.     //variable declarlations
  15.     /**
  16.      * Boolean HEADS = true.
  17.      */
  18.     public static final boolean HEADS = true;
  19.     /**
  20.      * Boolean HEADS represented by TRUE.
  21.      */  
  22.     public static final boolean TAILS = false;
  23.     /**
  24.      * String output.
  25.      */
  26.     public String output = "";
  27.     /**
  28.      * Boolean array coin.
  29.      */
  30.  
  31.     /**
  32.      * Boolean array.
  33.      */
  34.    
  35.     private boolean[] coins;
  36.  
  37.     /**
  38.      * Boolean array return coins.
  39.      */
  40.  
  41.     private  boolean [] returnCoins;
  42.  
  43.     /**
  44.      * this is the main method.
  45.      * @param args not used
  46.      *
  47.      */
  48.  
  49.     public static void main(String[] args) {
  50.         boolean[] b = {HEADS, TAILS, HEADS, HEADS, TAILS};
  51.         Coins c = new Coins("HHTTTTTHHH");
  52.     }
  53.  
  54.     /**
  55.      *
  56.      * Constructor class that uses a boolean array.
  57.      * to make a coins object
  58.      * @param coins -
  59.      */
  60.      
  61.     public Coins(boolean[] coins) {
  62.         this.coins = coins;
  63.     }
  64.  
  65.     /**
  66.      *
  67.      * Constructor class that uses a string to make a coins object.
  68.      * @param c -
  69.      */
  70.  
  71.    
  72.     public Coins(String c){
  73.         int x = c.length();
  74.         returnCoins = new boolean[x];
  75.         for (int i = 0; i <  x; i++){
  76.             if (c.charAt(i) == 'H' ){
  77.                 returnCoins[i] = HEADS;
  78.             }   else{
  79.                 returnCoins[i] = TAILS;
  80.             }
  81.  
  82.         }
  83.         this.coins = returnCoins;
  84.  
  85.     }
  86.  
  87.     /**
  88.      * Constructs a coins object with the.
  89.      * specified length with random heads / tail values
  90.      *  which are generated by the rand int method
  91.      *@param length of the total / sides
  92.      * */
  93.  
  94.     public Coins (int length){
  95.         int i = 0;
  96.         //1 = HEADS. 2 = TAILS
  97.         returnCoins = new boolean[length];
  98.         while (length > i){
  99.             int r = randInt(1,2);
  100.             for (int j = 0; j < length; j++){
  101.                 if (r == 1){
  102.                     returnCoins[j] = true;
  103.                 }  else if ( r == 2){
  104.                     returnCoins[j] = false;
  105.                 }
  106.             }
  107.             this.coins = returnCoins;
  108.  
  109.             i ++;
  110.         }
  111.  
  112.     }
  113.     /**
  114.      *
  115.      * Keeps track of Heads and Tails in the program.
  116.      * @return H
  117.      * */
  118.     public int countHeads(){
  119.         int h = 0;
  120.         int t = 0;
  121.         for (boolean a : coins){
  122.             if (a){
  123.                 h++;
  124.             }  else {
  125.                 t++;
  126.             }
  127.         }
  128.         return h;
  129.     }
  130.  
  131.  
  132.     /**
  133.      *
  134.      * Returns a String consisting of H and T values.
  135.      * which represent the heads and tail sides of each coin
  136.      * @return output
  137.      **/
  138.     public String toString(){
  139.         for (boolean c : coins){
  140.             if (c){
  141.                 output += "H";
  142.             } else{
  143.                 output += "T";
  144.             }
  145.         }
  146.         return output;
  147.  
  148.     }
  149.  
  150.     /**
  151.      * Counts "streaks" of Heads and Tails.
  152.      * and provides a total
  153.      *  @return int
  154.      */
  155.     public int countRuns(){
  156.         int i=0;
  157.         i+=1;
  158.         return i;
  159.     }
  160.  
  161.     /**
  162.      * Returns a psuedo-random number between low and high, inclusive.
  163.      *@return int
  164.      *@param high - its a max value
  165.      *@param low - its a min value
  166.      */
  167.     public static int randInt(int low, int high) {
  168.         Random rand = new Random();
  169.         int randomNum = rand.nextInt((high - low) + 1) + low;
  170.         return randomNum;
  171.     }
  172.        
  173. }
  174.  
  175.  
  176.  
  177. PASSED: countHeads_allHeads
  178. PASSED: countHeads_allTails
  179. PASSED: countHeads_multi
  180. PASSED: toString_allHeads
  181. PASSED: toString_allTails
  182. PASSED: toString_multi
  183. PASSED: constructor_length
  184. PASSED: countRuns_one
  185.  
  186. FAILED: countRuns_multi
  187.    expected [5] but found [1]
  188.  
  189.  
  190. FAILED: constructor_string
  191.    expected [HHTTHTHHHTTHTH] but found [HHTTHTH]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement