1st java file Write a Java program that will prompt the user
(1st java file) Write a Java program that will prompt the user for their name and then roll the dice for the player and the computer. Display a message with the point values and a descriptive message, such as \"Congratulations! You win!\", or \"It\'s a tie!\", or \"Computer won. Better luck next time.\"
(2nd java file) Implement a Die class which will simulate a six-sided Die. The Die class should include appropriate instance fields (there should only be one), a constructor, accessor and mutator methods including a roll() method which will simulate rolling the die. To simulate rolling a die, you will need to generate a random number. To generate a random number, use the random() method from the Math class. Math.random() will generate a random double between 0 and 1, including 0 but not 1. To simulate rolling a die, we need to generate a random number between 1 and 6, inclusive.
The following code segment simulates rolling a die: int ran = 1 + (int) (Math.random() * 6);
Your main() method will need to instantiate 4 objects of the Die class. Two for the player and two for the computer.
Solution
//File DieGame.java
import java.util.Scanner;
public class DieGame {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);// Scanner class to get the input from the user
String name;
System.out.println(\"Please enter your name: \");
name=scan.next();// getting the name from the user
Die playerDice1= new Die();//creating 1st instance for player
Die playerDice2= new Die();//creating 2nd instance for player
Die computerDice1= new Die();//creating 1st instance for computer
Die computerDice2= new Die();//creating 2nd instance for computer
playerDice1.setValue(playerDice1.roll());// rolling the dice for player 1st time
playerDice2.setValue(playerDice2.roll());// rolling the dice for player 2nd time
computerDice1.setValue(computerDice1.roll());// rolling the dice for computer 1st time
computerDice2.setValue(computerDice2.roll());// rolling the dice for computer 2nd time
int playerScore= playerDice1.getValue()+playerDice2.getValue();// calculating the score of the player in both instances
int computerScore= computerDice1.getValue()+computerDice2.getValue();// calculating the score of the computer in both instances
System.out.println(\"Player Score: \"+playerScore);//printing the player score
System.out.println(\"Computer Score: \"+computerScore);//printing the computer score
if(playerScore>computerScore){
System.out.println(\"Congratulations! \"+name +\" win!\");//use case when the player score is more than the computer
}else if(playerScore<computerScore){
System.out.println(\"Computer won. Better luck next time.\");//use case when the player score is less than the computer
}else{
System.out.println(\"It\'s a tie!\");// tie condition
}
}
}
--------------------------------------------------------
File: Die.java
public class Die {
private int value;// variable to store the number
//default constructor
public Die() {
}
//accessor method for value var
public int getValue() {
return value;
}
// mutator method for value var
public void setValue(int value) {
this.value = value;
}
//method to generate the number from 1 to 6
public int roll(){
int ran = 1 + (int) (Math.random() * 6);// to generate the number from 1 to 6
return ran;
}
}
----------------output---------------
Please enter your name:
John
Player Score: 6
Computer Score: 10
Computer won. Better luck next time.
Please enter your name:
John
Player Score: 8
Computer Score: 3
Congratulations! John win!
Please enter your name:
John
Player Score: 8
Computer Score: 8
It\'s a tie!
----------------output---------------
Note: Please create 2 files for two classes above with .java extension inorder for the program to work. Feel free to ask queries/doubts. God bless you!!

