Lab 62 CS 1113 Write a java program to simulate rolling of
// Lab 6_2 // CS 1113 Write a java program to simulate rolling of dice. Your class name should be Lab6_2 Note: You need to use random numbers If you do not know how to use random numbers, refer Chapter 4 slides import java.util.Scanner; public class Lab6_2 { public static void main(String[] args) { //Define Scanner Object [Add code here] //Declare a string variable choice and initialize it [Add code here] //Prompt user to make a choice to play the game or not //Look for sample output for what message to prompt [Add code here] //Use defensive programming to read the value for choice [Add code here] //Define a variable ch of character type and read the value for it. //The value for ch will be the first character of your choice variable //Hint: use choice.charAt(0) to read the first charater of choice [Add code here] //Check if the value of ch is n or N //If yes, print the message as shown in sample output and exit the program //Because choice n or N means the user does not want to play the game [Add code here] //Use event controlled loop to continue until the user presses y or Y //Choice y or Y means the user wants to play the game while([Add code here]) { //Declare a varaibel dice_value of int type //Generate a random number between 1 and 6 and store the value in dice_value //To generate random int value, use Math.random() method //Look lecture notes of Chapter 4 to see how to generate a random number of int type [Add code here] //Display what the value you got by rooling the dice //Look for sample output for message and format of the ouput you need to displaySystem [Add code here] //Prompt the message as shown in sample output to decice whether the user wants to play again or not [Add code here] //Use defensive programming to read the value for choice [Add code here] //Check if the value of ch is n or N //If yes, print the message as shown in sample output and exit the program //Because choice n or N means the user does not want to play the game [Add code here] //Get the value of ch from choice by using charAt(0) method as you did earlier in the program [Add code here] }//End of while loop //Display the final message to thank the user for playing the game as shown in sample output [Add code here] }//End of main method }//End of class Sample output 1: Do you want to play the dice? Type (y/n). n I am sad that you don\'t want to play. Have a nice day. Sample output 2: Do you want to play the dice? Type (y/n). y You rolled 4 Do you want to play the dice again? Type (y/n). n Thank you for playing. Please play again. Bye!!!! Sample output 3: Do you want to play the dice? Type (y/n). y You rolled 5 Do you want to play the dice again? Type (y/n). y You rolled 1 Do you want to play the dice again? Type (y/n). n Thank you for playing. Please play again. Bye!!!! Solution
import java.util.Scanner;
class Lab6_2
{
public static void main(String[] args)
{
//Define Scanner Object
Scanner stdin = new Scanner(System.in);
//Declare a string variable choice and initialize it
String choice;
//Prompt user to make a choice to play the game or not
//Look for sample output for what message to prompt
System.out.println(\"Do you want to play the dice? Type (y/n).\");
//Use defensive programming to read the value for choice
choice = stdin.next();
//Define a variable ch of character type and read the value for it.
//The value for ch will be the first character of your choice variable
//Hint: use choice.charAt(0) to read the first charater of choice
char ch = choice.charAt(0);
//Check if the value of ch is n or N
//If yes, print the message as shown in sample output and exit the program
//Because choice n or N means the user does not want to play the game
if( ch == \'N\' || ch == \'n\')
{
System.out.println(\"I am sad that you don\'t want to play.\ Have a nice day.\");
System.exit(0);
}
//Use event controlled loop to continue until the user presses y or Y
//Choice y or Y means the user wants to play the game
while(ch == \'Y\' || ch == \'y\')
{
//Declare a varaibel dice_value of int type
//Generate a random number between 1 and 6 and store the value in dice_value
//To generate random int value, use Math.random() method
//Look lecture notes of Chapter 4 to see how to generate a random number of int type
int dice_value = 1 + (int)(Math.random() * 6);
//Display what the value you got by rooling the dice
//Look for sample output for message and format of the ouput you need to displaySystem
System.out.println(\"You rolled \"+ dice_value);
//Prompt the message as shown in sample output to decice whether the user wants to play again or not
System.out.println(\"Do you want to play the dice again? Type (y/n).\");
//Use defensive programming to read the value for choice
choice = stdin.next();
//Check if the value of ch is n or N
//If yes, print the message as shown in sample output and exit the program
//Because choice n or N means the user does not want to play the game
if( ch == \'N\' || ch == \'n\')
System.out.println(\"I am sad that you don\'t want to play.\ Have a nice day.\");
//Get the value of ch from choice by using charAt(0) method as you did earlier in the program
ch = choice.charAt(0);
}//End of while loop
//Display the final message to thank the user for playing the game as shown in sample output
System.out.println(\"Thank you for playing.\ Please play again.\ Bye!!!!\");
}//End of main method
}//End of class
Output:
Success time: 0.06 memory: 711680 signal:0

