Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week02;
- import java.util.Random;
- /**
- * This program class represents a coin.
- * @author Adrian Yadav
- * @version : 0.1
- * @param
- *
- */
- public class Coins{
- //variable declarlations
- /**
- * Boolean HEADS = true.
- */
- public static final boolean HEADS = true;
- /**
- * Boolean HEADS represented by TRUE.
- */
- public static final boolean TAILS = false;
- /**
- * String output.
- */
- public String output = "";
- /**
- * Boolean array coin.
- */
- /**
- * Boolean array.
- */
- private boolean[] coins;
- /**
- * Boolean array return coins.
- */
- private boolean [] returnCoins;
- /**
- * this is the main method.
- * @param args not used
- *
- */
- public static void main(String[] args) {
- boolean[] b = {HEADS, TAILS, HEADS, HEADS, TAILS};
- Coins c = new Coins("HHTTTTTHHH");
- }
- /**
- *
- * Constructor class that uses a boolean array.
- * to make a coins object
- * @param coins -
- */
- public Coins(boolean[] coins) {
- this.coins = coins;
- }
- /**
- *
- * Constructor class that uses a string to make a coins object.
- * @param c -
- */
- public Coins(String c){
- int x = c.length();
- returnCoins = new boolean[x];
- for (int i = 0; i < x; i++){
- if (c.charAt(i) == 'H' ){
- returnCoins[i] = HEADS;
- } else{
- returnCoins[i] = TAILS;
- }
- }
- this.coins = returnCoins;
- }
- /**
- * Constructs a coins object with the.
- * specified length with random heads / tail values
- * which are generated by the rand int method
- *@param length of the total / sides
- * */
- public Coins (int length){
- int i = 0;
- //1 = HEADS. 2 = TAILS
- returnCoins = new boolean[length];
- while (length > i){
- int r = randInt(1,2);
- for (int j = 0; j < length; j++){
- if (r == 1){
- returnCoins[j] = true;
- } else if ( r == 2){
- returnCoins[j] = false;
- }
- }
- this.coins = returnCoins;
- i ++;
- }
- }
- /**
- *
- * Keeps track of Heads and Tails in the program.
- * @return H
- * */
- public int countHeads(){
- int h = 0;
- int t = 0;
- for (boolean a : coins){
- if (a){
- h++;
- } else {
- t++;
- }
- }
- return h;
- }
- /**
- *
- * Returns a String consisting of H and T values.
- * which represent the heads and tail sides of each coin
- * @return output
- **/
- public String toString(){
- for (boolean c : coins){
- if (c){
- output += "H";
- } else{
- output += "T";
- }
- }
- return output;
- }
- /**
- * Counts "streaks" of Heads and Tails.
- * and provides a total
- * @return int
- */
- public int countRuns(){
- int i=0;
- i+=1;
- return i;
- }
- /**
- * Returns a psuedo-random number between low and high, inclusive.
- *@return int
- *@param high - its a max value
- *@param low - its a min value
- */
- public static int randInt(int low, int high) {
- Random rand = new Random();
- int randomNum = rand.nextInt((high - low) + 1) + low;
- return randomNum;
- }
- }
- PASSED: countHeads_allHeads
- PASSED: countHeads_allTails
- PASSED: countHeads_multi
- PASSED: toString_allHeads
- PASSED: toString_allTails
- PASSED: toString_multi
- PASSED: constructor_length
- PASSED: countRuns_one
- FAILED: countRuns_multi
- expected [5] but found [1]
- FAILED: constructor_string
- expected [HHTTHTHHHTTHTH] but found [HHTTHTH]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement