Question
Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value
Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die.
Altogether you will have one attribute (face), and the following methods: constructor (calls roll), roll, getFace.
Test it by writing a main method in which you roll 2 dice. If you get exactly 7 you win, 11 you lose, anything else roll both dice again until you either win or lose. in order to verify your program works correctly put a println in the loop so that every time you roll the dice you print out the total value.
In Java
must include
Comments to state what this program does
Comments within the program
correct constructor for die class (returns random # between 1 and 6)
correct roll method within the die class
correct method getFace in die class
corect use of class methods in main
correct output
why does it give me the error on line 40 (Bold) and how can i fix it?
public class die {
int face; //constructor
die() { face = 0; //initialize face to 0 }
void roll() //method to roll dice { face = 1 + (int) (Math.random() * 5); //returns random number between 1 to 6 }
int getFace() //return face value of dice { return face; //return face value of dice } }
public static void main(String arga[]) //main method to control the program { die di = new die(); //crating object of die class. it will call constructor Scanner scnr = new Scanner(System.in); //To take user input to make program interactive String s; //for user input int score = 0; while (true) { System.out.print(System.lineSeparator() + "Press any key to dice : "); s = scnr.nextLine(); System.out.print(System.lineSeparator() + "Rolling dice.... "); di.roll(); //rolling dice for first time score = di.getFace(); //getting face value of dice System.out.print(System.lineSeparator() + "You got: " + score);//showing first number System.out.print(System.lineSeparator() + "Rolling again...."); di.roll(); //rolling dice again for second time score = score + di.getFace(); //second time getting face value of dice and add it to previous score System.out.print(System.lineSeparator() + "You total score: " + score); if (score == 7) { System.out.print(System.lineSeparator() + "Congratulation, You Won!"); break; } else if (score == 11) { System.out.print(System.lineSeparator() + "Sorry, You Lost!"); break; } score = 0; //set value of score to zero so that player can play it again } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started