Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

the skeleton code shows above. Exercise part1: add 2 functions, countHead() , a

ID: 3833371 • Letter: T

Question

the skeleton code shows above.

Exercise part1:

add 2 functions,

countHead(), a method that returns an int which is the number of occurences of "heads" in the coin tosses.

toString(), a method that returns a String representation of the coin tosses, H represents heads, T represents tails.

* if the code works correctlly, you should see the number 3 printed out when you run it.

Exercise 2:

add 2 constructors,

Coins (String c), creates a Coins object from a String consisting entirely of the character H and T.

Coins (int length), Constructs a Coins object consisting of a series of length coins, the value of each coin should be determined by a random coin toss.

Exercise 3:

add 1 method,

countRuns(), return an int which is the number of runs in this sequence of coins (a run is a block of coins all showing the same face, so for example in HHTHHHTTT there are four runs namely HH, T, HHH, and TTT.

public class coins public static final boolean HEADS true public static final boolean TAILS false private boolean coins public coins (boolean coins) this coins coins; public static void main (String [jargs) boolean b HEADS TAILS HEADS HEADS TAILS) coins c new coins (b) System.out.println (c.countHeads

Explanation / Answer

Please find all the answers below. See comments in the code. Output is also there at the end.

import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class Coins {


public static final boolean HEADS = true;
public static final boolean TAILS = false;


private boolean[] coins;


public Coins(boolean[] coins) {
    this.coins = coins;
}

/**
   * Part of Exercise 2
   * creates a Coins object from a String consisting entirely of the character H and T
   *
   * @param coins
   */
Coins(String coins) {
    char[] coinsArray = coins.toCharArray();
    this.coins = new boolean[coinsArray.length];
    int i = 0;
    for (char c : coinsArray) {
      if (c == 'H') {
        this.coins[i] = true;
      }
      if (c == 'T') {
        this.coins[i] = false;
      }
      i++;
    }

}

/**
   * Part of Exercise 2
   * Constructs a Coins object consisting of a series of length coins, the value of each coin should
   * be determined by a random coin toss
   *
   * @param length
   */
Coins(int length) {
    this.coins = new boolean[length];
    for (int i = 0; i < length; i++) {
      this.coins[i] = new Random().nextBoolean();
    }

}


/**
   * Exercise 1
   * method to count head.
   * @return
   */
private int CountHeads() {

    int i = 0;
    for (boolean val : this.coins) {
      if (val) {
        i++;
      }
    }


    return i;
}

/**
   * Exercise: 3
   * method to give count Runs
   * @return
   */
private int countRuns(){
    Stack<Boolean> stack = new Stack<Boolean>();
   
    for(Boolean coin :this.coins){
      stack.push(coin);
    }
    int size = stack.size();
    int pass=0;
    boolean current = stack.pop();
    boolean previous = current;
    boolean next = false;
    while(!stack.isEmpty()){
      next = stack.pop();
      boolean tmp = current;
      if(!current==next){
        pass++;
        current=next;
        previous=tmp;
      }
    }
   
    if(previous!=current){
      pass++;
    }
   
    if(size!=0 && pass==0 ){
      pass++;
    }
    return pass;
}


@Override
public String toString() {
   
    StringBuilder string = new StringBuilder();
    for(boolean coin: this.coins){
      if(coin){
        string.append("H");
      }else{
        string.append("T");
      }
     
    }
    return "Coins [coins=" + string.toString() + "]";
}

public static void main(String[] args) {
    boolean[] b = {HEADS, TAILS, HEADS, HEADS, TAILS};
    Coins c = new Coins(b);
    System.out.println("Coins = "+ c.toString());
    System.out.println("Head Count = "+ c.CountHeads());
    System.out.println("Count Runs = "+ c.countRuns());
    System.out.println("*************");

    // Test constructor Coins(String coins)
    Coins coinByString = new Coins("HHHHH");
    System.out.println("Coins = "+ coinByString.toString());
    System.out.println("Head Count = "+coinByString.CountHeads());
    System.out.println("Count Runs = "+ coinByString.countRuns());
    System.out.println("*************");
   
    // Test constructor Coins(String coins)
    Coins coinByString2 = new Coins("TTTTT");
    System.out.println("Coins = "+ coinByString2.toString());
    System.out.println("Head Count = "+coinByString2.CountHeads());
    System.out.println("Count Runs = "+ coinByString2.countRuns());
    System.out.println("*************");
   
    // Test constructor Coins(String coins)
    Coins coinByString3 = new Coins("TTTTH");
    System.out.println("Coins = "+ coinByString3.toString());
    System.out.println("Head Count = "+coinByString3.CountHeads());
    System.out.println("Count Runs = "+ coinByString3.countRuns());
    System.out.println("*************");
   
    // Test constructor Coins(String coins)
    Coins coinByString4 = new Coins("HTTTT");
    System.out.println("Coins = "+ coinByString4.toString());
    System.out.println("Head Count = "+coinByString4.CountHeads());
    System.out.println("Count Runs = "+ coinByString4.countRuns());
    System.out.println("*************");
   
    // Test constructor Coins(String coins)
    Coins coinByString5 = new Coins("HTTTH");
    System.out.println("Coins = "+ coinByString5.toString());
    System.out.println("Head Count = "+coinByString5.CountHeads());
    System.out.println("Count Runs = "+ coinByString5.countRuns());
    System.out.println("*************");

    // Test constructor Coins(int length)
    Coins coinByRandom = new Coins(5);
    System.out.println("Coins = "+ coinByRandom.toString());
    System.out.println("Head Count = "+coinByRandom.CountHeads());
    System.out.println("Count Runs = "+ coinByRandom.countRuns());
    System.out.println("*************");

}


}

Output:

Coins = Coins [coins=HTHHT]
Head Count = 3
Count Runs = 4
*************
Coins = Coins [coins=HHHHH]
Head Count = 5
Count Runs = 1
*************
Coins = Coins [coins=TTTTT]
Head Count = 0
Count Runs = 1
*************
Coins = Coins [coins=TTTTH]
Head Count = 1
Count Runs = 2
*************
Coins = Coins [coins=HTTTT]
Head Count = 1
Count Runs = 2
*************
Coins = Coins [coins=HTTTH]
Head Count = 2
Count Runs = 3
*************
Coins = Coins [coins=THTTH]
Head Count = 2
Count Runs = 4
*************