The question is asking: A run is a sequence of adjacent repeated values. Write a program that generates a sequence of 20 random die tosses and that prints the die values, marking the runs by including them in parentheses, like this:

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

This is what I have so far:

Die.java

import java.util.Random;
public class Die
{
   /**
      Constructs a die with a given number of sides
      @param s the number of sides, e.g. 6 for a normal die
   */
   public Die(int s)
   {
      sides = s;
      generator = new Random();
   }

   /**
      Simulates a throw of the die
      @return the face of the die 
   */
   public int cast()
   {
      return 1 + generator.nextInt(sides);
   }

   
   private Random generator;
   private int sides;
}

DieTest.java

public class DieTest
{
   public static void main(String[] args)
   {
      Die d = new Die(6);
      final int TRIES = 20;
      for (int i = 1; i <= TRIES; i++)
      {  
         int n = d.cast();
         System.out.print(n + " ");
      }
      System.out.println();
   }
}

I was able to print 20 random die tosses but I don't know how to put them in parentheses like what the program is asking.

put all your cast values in an array. then make another method called compare(), which takes adjacent 2 elements of this array and if both are equal, returns 1 and 0 if both are unequal. put these 19 comparison values (of 1s and 0s) in a new array of ints.

next step, read adjacent values of those 19 ints- if they are 0 1, add a left parenthesis, if they are 1 0, add right parenthesis, if they are 0 0 or 1 1, dont do anything.. this way, you can solve your question..

if you need clarifications, dont hesitate to ask.. its quite an easy method, and ive already run the code.. im assuming the parenthesis are required when you want to group cast values if they are the same in succession..

put all your cast values in an array. then make another method called compare(), which takes adjacent 2 elements of this array and if both are equal, returns 1 and 0 if both are unequal. put these 19 comparison values (of 1s and 0s) in a new array of ints.

next step, read adjacent values of those 19 ints- if they are 0 1, add a left parenthesis, if they are 1 0, add right parenthesis, if they are 0 0 or 1 1, dont do anything.. this way, you can solve your question..

if you need clarifications, dont hesitate to ask.. its quite an easy method, and ive already run the code.. im assuming the parenthesis are required when you want to group cast values if they are the same in succession..

Thank you very much.
But I suck at converting words into code so can you reword it into code? I learn better by looking at codes, sorry:(
I greatly appreciate your effort on this.

1. Why not start your for loop with 0 then end it with i < TRIES rather thatn starting with 1 then nding with i <= TRIES??

2. For casting your die faces into an array like what Dhruv Gairola said, 1st you need to create an array of size 20. then after you do

int n = d.cast();

you add n to your array at index i

arrayName[i] = n;

3. Then for the comparison, I have another suggestion.

You can do a for loop comparing array with array and output the value into a string.


for (int i = 0; i < arrayName.length; i++) {
    \\compare array[i] w/ array [i + 1]
    \\ if equal, chk if there is right parentheses, if there is add array[i] to your 
    \\ result string. if no right parentheses, then add a right prentheses then add
    \\ array [i].

    \\ if array[i] != array[i + 1], check for right parentheses. If there is, add 
    \\ array[i] then add a close parentheses. If none then add array[i].
}

Try posting some sample codes after you tried our suggestions (",).

This is all I could figured out so far:

public class DieTest
{
   public static void main(String[] args)
   {
      Die d = new Die(6);
      final int TRIES = 20;
      int arrayName[] = new int[20];
      for (int i = 0; i < TRIES; i++)
      {  
         int n = d.cast();
         arrayName[i] = n;
         System.out.print(arrayName[i] + " ");
      }
      System.out.println();
   }
}

It compiles but doesn't really do anything new.
Still can't figure out how to do the compare method, really noob on array sorry:(

Well at least you have now casted your values into your arrayName (",) Now comes the compare part. Since arrayName is an array of integers you can compare its values eg

if (array[0] == array[1]) { \* this is to compare the 1st 2 elements of the array  and since you want to compare all the elements in the array, then you need to put this condition inside a for loop */

Try modifying your code adding the comparison part after you have casted all your die faces into your arrayName[]. (",)

if your head aches and eyes hurt here is an implementation you can reference, ran it a few times and seems to be working ok...

import java.util.ArrayList;

public class DieTest
{
  public static void main(String[] args)
  {
  Die d = new Die(6);
  final int TRIES = 20;

  int[] arr = new int[25];

  for (int i = 0; i < TRIES; i++) { 
    int n = d.cast();
    arr[i] = n;
       System.out.print(arr[i] + " ");
  }
  
  System.out.println();
  
  
  if(arr[0]==arr[1])
    System.out.print("("+arr[0]);
  else
    System.out.print(arr[0]);
	 
  for (int j=1; j<=19; j++) {
          if(arr[j]==arr[j-1]  &&  arr[j]==arr[j+1])
	    System.out.print(arr[j]);

	 if(arr[j]!=arr[j-1]  &&  arr[j]==arr[j+1]) 
		 System.out.print("("+arr[j]);
		 
	 if(arr[j]!=arr[j-1]  &&  arr[j]!=arr[j+1])  
	    System.out.print(arr[j]);
    
	 if(arr[j]==arr[j-1]  &&  arr[j]!=arr[j+1]) 
	    System.out.print(arr[j]+")");
 }
  
 }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.