” Write a Java program that demonstrates two Runnable classes, each of which contains a Thread.
Objects in one class generate random rolls of a pair of dice and display them. The number of rolls can be set in a constructor.
Objects in the other class display random Strings selected from an array. The number of Strings selected and displayed can be set in a constructor.
Show how the behavior of the threads can interleave.
Show how to synchronize them using join.”
I know how to do a dice and string generator. But i am having trouble create two runnable classes of threads for this problem.
Heres my code:
package testing;
import java.util.Random;
import java.math.*;
public class dice
{
public int sides;
public dice(int sides)// DIY howm many side of dice you want
{ // use: diec diceA = new dice(7) = now means you want 7 sides dice
this.sides = sides;
}
public int roll()
{
int roll = 0;
Random generator = new Random();
roll = Math.abs(generator.nextInt()%sides+1);// this will give us a number from -6 to +6 so need abs function
return roll;// give a number from -7 to +7
}
}
———-this is string generator——-
package stringgenerator;
import java.util.Random;
import java.util.Scanner;
public class StringGenerator
{
public static void main(String[] args)
{
int length;
Scanner input = new Scanner(System.in);
System.out.println(“How many friends name that you are gonna enter:”);
length = input.nextInt();
String[] names = new String[length];
for (int counter = 0;counter < length; counter ++) //read from user
{
System.out.println(“Enter the name of the friend:” + (counter+1));
names[counter] = input.next();
}
input.close(); //stop
System.out.println(“your friends are”); // display
for (int counter =0;counter < length; counter++)
{
System.out.println(names[counter]);
}
System.out.println(“Random name!:”+names[ new Random().nextInt(names.length)]);
//generate random name from the list
}
}