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

I am having an issue with my code, it keeps failing to run and I need these para

ID: 3827732 • Letter: I

Question

I am having an issue with my code, it keeps failing to run and I need these paramerters....

Write a program that prompts the user for the number n of die tosses, then calls a constructor with argument n. Your class should have a method which then generates a sequence of n random die tosses in an array and returns the array reference. Your main program then prints the die values, marking runs by including them in parentheses, like this:

1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1

Use the following pseudocode:

Set a boolean value inRun to false.
For each index i in the array {
If inRun {
if(values[i] is different from the preceding value{
print )
inRun = false
}
}
if not inRun {
if values[i] is the same as the following value{
      print (
inRun = true
}

}
print values[i]
}
If inRun, print )

this is what I got so far................

import java.util.Scanner;
public class DieTosses {
public static void printRun(int[] values) {
boolean inRun = false;
int previousValue = values[0];
for (int i = 0; i < values.length - 1; i++) {
if (inRun) {
if (values[i] != previousValue) {
System.out.print(")");
inRun = false;
}
}
else {
if (values[i] == values[i + 1]) {
System.out.print(" (");
inRun = true;
}
else {
System.out.print(" ");
}
}
previousValue = values[i];
System.out.print(values[i]);
}
if (inRun && values[values.length - 1] == previousValue) {
System.out.print(" " + values[values.length - 1] + ")");
}
else if (inRun && values[values.length - 1] != previousValue) {
System.out.print(") " + values[values.length - 1]);
}
else {
System.out.print(" " + values[values.length - 1]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value for n ? ");
int n = sc.nextInt();
Dice d = new Dice(n);
int tosses[] = d.getTosses();
printRun(tosses);
sc.close();
}
}

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.util.Scanner;

public class DieTosses {

   public static void printRun(int[] values) {

       boolean inRun = false;

       int previousValue = values[0];

       for (int i = 0; i < values.length - 1; i++) {

           if (inRun) {

               if (values[i] != previousValue) {

                   System.out.print(")");

                   inRun = false;

               }

           } else {

               if (values[i] == values[i + 1]) {

                   System.out.print(" (");

                   inRun = true;

               } else {

                   System.out.print(" ");

               }

           }

           previousValue = values[i];

           System.out.print(values[i]);

       }

       if (inRun && values[values.length - 1] == previousValue) {

           System.out.print(" " + values[values.length - 1] + ")");

       } else if (inRun && values[values.length - 1] != previousValue) {

           System.out.print(") " + values[values.length - 1]);

       } else {

           System.out.print(" " + values[values.length - 1]);

       }

   }

  

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter n value ? ");

       int n = sc.nextInt();

      

       Dice dice = new Dice(n);

      

       int toses[] = dice.getTosses();

      

       printRun(toses);

       sc.close();

   }

}

/*

* Sample run:

Enter n value ? 15

4 5 1 (333)2 5 1 3 6 4 5 2 1

*/

class Dice{

  

   private int tosses[];

   // constructor

   public Dice(int n){

       tosses = new int[n];  

       generateDieTosses();

   }

  

   private void generateDieTosses() {

       for (int i = 0; i < tosses.length; i++) {

           tosses[i] = (int) (Math.random() * 6 + 1);

       }

   }

  

   public int[] getTosses(){

       return tosses;

   }

}