Question

Currently stuck on this part of a project. I am struggling with creating the String for...

Currently stuck on this part of a project. I am struggling with creating the String for creating the dice values in the boxes for the "showDice" string. I have already completed the first part which was creating the class for the Die. This is Javascript.

For this part, we will create the class that allows you to roll and score the dice used in a game of Yahtzee. This will be the longest part of the project, so I suggest that you don’t leave this one until the last minute.

We will grade Part 2 for programming style. You should make sure you review the section on style guidelines before you start this section.

Setting up the Class Yahtzee

We are going to look at an instance of the class Yahtzee as an object that consists of five dice. A user can roll some or all dice. A user can also look at current values of all five dice, check the score of each category as well as get the current score card. First, let's focus on five dice and its related methods.

For simplicity, we can use a one-dimensional array named dice of type Die to represent all five dice. A user can roll some or all dice as he/she wishes. S/he can also check the value of each individual die. Thus, the structure of the class Yahtzee (for now) should look like the following:

public class Yahtzee
{
private Die[] dice;

public Yahtzee()
{
dice = new Die[5];

for(int i = 0; i < 5; i++)

        {
dice[i] = new Die();

        }

        rollAllDice();
}

    public void rollADie(int dieNumber)
{

}

public void rollAllDice()
{

}

public int getADie(int dieNumber)
{

}

public String showDice()
{

}
}

From the above code, there are four methods related to dice as follows:

  • rollADie(int dieNumber):This method allows the user to roll a specific die. The dieNumber should be between 1 and 5 (inclusive). Do not forget that we use the variable dice of type array. In other words, die number 1 is associated with the variable dice[0], die number 2 is associated with dice[1], and so on. This method should return nothing.

  • rollAllDice(): When called, this method simply rolls all die and return nothing. This method should make use of the rollADie method

  • getADie(int dieNumber): This method simply returns the current value of a given die number.

  • showDice() returns a string representation of all dice and their value in the format shown below:

+------+---+---+---+---+---+
| Dice | 1 | 2 | 3 | 4 | 5 |
+------+---+---+---+---+---+
| Face | 2 | 5 | 2 | 3 | 6 |
+------+---+---+---+---+---+

Note: this method does not print anything on the console screen. It simply returns a String in the above format. In other words, you should see something like the above example if a user uses the following statement:

System.out.println(yahtzee.showDice());


where yahtzee is a reference variable of type Yahtzee.

Homework Answers

Answer #1

Code

class Die
{
private int face;

public Die()
{
   face=1;
}
public void rollDie()
{
   face=(int )(Math. random() * 6 + 1);
}

public int getFace() {
return face;
}
  
}
public class Yahtzee {

   private Die[] dice;

   public Yahtzee()
   {
       dice = new Die[5];
       for(int i = 0; i < 5; i++)
   {
           dice[i] = new Die();
   }
       rollAllDice();
   }

   public void rollADie(int dieNumber)
   {
       dice[dieNumber].rollDie();
   }

   public void rollAllDice()
   {
       for(int i = 0; i < 5; i++)
   {
           dice[i].rollDie();
   }
   }

   public int getADie(int dieNumber)
   {
       return dice[dieNumber].getFace();
   }

   public String showDice()
   {
       String str="+------+---+---+---+---+---+\n";
       str+="| Dice |";
       for(int i=1;i<=5;i++)
           str+=" "+(i)+" |";
       str+="\n+------+---+---+---+---+---+\n";
       str+="| Dice |";
       for(int i=0;i<5;i++)
           str+=" "+dice[i].getFace()+" |";
       str+="\n+------+---+---+---+---+---+\n";
       return str;
   }
}

YahtzeeMain class for main method


public class YahtzeeMain {

   public static void main(String[] args) {
       Yahtzee yahtzee=new Yahtzee();
       yahtzee.rollAllDice();
       System.out.println(yahtzee.showDice());
   }

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Know the answer?
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
Similar Questions
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
For this assignment, design and implement a class to represent a die (singular of "dice"). A...
For this assignment, design and implement a class to represent a die (singular of "dice"). A normal bag of dice for playing Dungeons and Dragons, for example, contains dice with the following numbers of sides: 4, 6, 8, 10, 12, 20, and 100. In this program, the sides (or faces) of the die are numbered, starting with one. The current face value of the die corresponds to the side that is currently facing upward. By default, the face value is...
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;...
import java.util.Scanner; import java.util.Random; public class DiceRoll {    public static final int SIDES = 6;    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Enter the number of times a 6 sided die should be rolled ");        Scanner keyboard = new Scanner(System.in);        Random r = new Random();               int times = keyboard.nextInt();        boolean valid = false;        while(!valid) {           ...
3.2 Class Dictionary This class implements a dictionary using a hash table in which collisions are...
3.2 Class Dictionary This class implements a dictionary using a hash table in which collisions are resolved using separate chaining. The hash table will store objects of the class Data. You will decide on the size of the table, keeping in mind that the size of the table must be a prime number. A table of size between 5000-10000, should work well. You must design your hash function so that it produces few collisions. A bad hash function that induces...
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT