Question

Please Help! I need to update the code below to meet these criteria! 1. The constructor...

Please Help! I need to update the code below to meet these criteria!

1. The constructor of die class initializes face of Die to 3

2. roll method updates face of die to value from 1-6

3. getFace() method provides current face to the main program

4. main create a Die object

5. main create a Die object

6. main rolls die 5 times

7. main computes sum of rolls

8. main computes actual average of the rolls

<--------- Code Listed Bellow -------->

import java.util.Random;

public class Die {
  
private final int MAX = 6;
  
private int face; //value showing on the face of the die

private Random rand = new Random(); //random object to use for rolling

//constructor whcih initializes the die to show a 3
public Die() {
faceValue = 3;
}

//rolls the die and updates it's face
public void roll() {
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}

  
//returns the value of the die face
public int getFace() {
return faceValue;
}

//tests the Die class
public static void main (String [] args) {
  
String result = Integer.toString(faceValue);

return result;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE:

import java.util.Scanner;
import java.util.Random;

public class Die
{
   private final int MAX = 6;
  
private int faceValue; //value showing on the face of the die

private Random rand = new Random(); //random object to use for rolling

//constructor whcih initializes the die to show a 3
public Die() {
faceValue = 3;
}


//rolls the die and updates it's face
public int roll() {
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}

//returns the value of the die face
public int getFace() {
return faceValue;
}
   public String toString()
{
String result = Integer.toString(faceValue);
return result;
}

//tests the Die class
public static void main (String [] args) {

   Die die1, die2;
int sum=0;
   float avg;

   // create a Die object
   die1 = new Die();
   // create a Die object
die2 = new Die();
   // rolls die 5 times
   int rollCount =5;
   // rolls die 5 times
   while(rollCount>0){
       // rolls dies
       die1.roll();
       die2.roll();
       // display the dies
System.out.println ("Die One: " + die1.getFace() + ", Die Two: " + die2.getFace());
       // computes sum of rolls
   sum+=die1.getFace();
       sum+=die2.getFace();
         
       rollCount--;// rollCount decrement by 1
   }
   // computes actual average of the rolls
   avg = (float)sum / 5;
   // display the sum
   System.out.println("Sum: "+sum);
// display the average
   System.out.println("Average: "+avg);

   }
}

OUTPUT:


  

Add a comment
Know the answer?
Add Answer to:
Please Help! I need to update the code below to meet these criteria! 1. The constructor...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed...

    Using the Die class defined in this chapter, design and implement a class called PairOfDice, composed of two Die objects. Include methods to set and get the individual die values, a method to roll the dice, and a method that returns the current sum of the two die values. Create a driver class called RollingDice2 to instantiate and use a PairOfDice object. I need the main method or test class public class Die { private final int MAX = 6;...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • I need to update the code listed below to meet the criteria listed on the bottom....

    I need to update the code listed below to meet the criteria listed on the bottom. I worked on it a little bit but I could still use some help/corrections! import java.util.Scanner; public class BankAccount { private int accountNo; private double balance; private String lastName; private String firstName; public BankAccount(String lname, String fname, int acctNo) { lastName = lname; firstName = fname; accountNo = acctNo; balance = 0.0; } public void deposit(int acctNo, double amount) { // This method is...

  • (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1...

    (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1,000,000 times) Given below that the class die, oddstester, and gameSimulator(partially complete and the bold missing parts need to be done), need to find the probability of winning game 1 and 2 (after the 1 million plays) hence the getWins and...

  • Goal: Translate this Python script into Java: magic8ball.py Deliverable: Magic8Ball.java Grading Breakdown: Functionality: 35; Source code...

    Goal: Translate this Python script into Java: magic8ball.py Deliverable: Magic8Ball.java Grading Breakdown: Functionality: 35; Source code comments: 5; Indentation: 5; Properly Submitted: 5. Example: Here is a Java class that simulates the roll of a single die: import java.util.Random; public class DiceRoll { public static void main(String[ ] args) { Random r = new Random( ); int roll1 = r.nextInt(6) + 1; int roll2 = r.nextInt(6) + 1; System.out.println("Sum of rolls:" + (roll1 + roll2)); } } Here is the...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Comment your code. At the top of the program include your name, a brief description of...

    Comment your code. At the top of the program include your name, a brief description of the program and what it does and the due date. The program must be written in Java and submitted via D2L. The code matches the class diagram given above. The code uses Inheritance. In the following, you are given code for two classes: Coin and TestCoin. You study these classes first and try to understand the meaning of every line. You can cut and...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT