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
I need to make some modifications to the code below. Numbers 1 through 3 are the...
I need to make some modifications to the code below. Numbers 1 through 3 are the required modifications. 1. Write a static method to have the players guess what was rolled 2. If guessed what was rolled you get 10 points 3. You win if you reach 30 points before the end of the round otherwise the person with the points wins at the end of five rounds The code is below import java.util.Scanner; public class ChoHan { public static...
For this assignment, we will be creating at least two classes. The first will be the...
For this assignment, we will be creating at least two classes. The first will be the Die class. The second will be the DiceTower class. The specifications for the classes are below. You will also be expected to create and deliver a test plan that demonstrates how you will prove your classes fulfill requirements and are bug-free. Use expected naming conventions and commenting. Part I: Die: Die(int sides): creates a die with sides quantity of sides with values from 1...
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,...
Hello i am working on a project for my programming course and i am a little...
Hello i am working on a project for my programming course and i am a little lost. The assignment is as follows: Part A Do all of problem 3.13 (Employee Class), including the main, as written. Part B Now, suppose a new company policy says that instead of the monthly salary we will now store the yearly salary. We will need to make this change inside Employee and support the new usage, without breaking existing code (such as the old...
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, and100. 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 set...
I am creating a towers of hanoi game board using c#, and am having trouble understanding...
I am creating a towers of hanoi game board using c#, and am having trouble understanding my instructors instructions. So far I have an outline setup as follows: using System; namespace Towers { public class Towers { int numberOfDiscs; //Number of discs to be place on the leftmost pole (between 1 and 9) int numberOfMoves; //Number of moves executed thus far bool isComplete; //True if all discs have been succesfully moved to the righmost pole int minimumPossibleMoves; public Towers(int numberOfDiscs)...
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...
C# programming Assume we have a student final exam score lookup table like this: John 40...
C# programming Assume we have a student final exam score lookup table like this: John 40 Henry 0 Mary 59 Hillary 39 Pikachu 100 Write a class that helps check if student passes or fails the course. If a student's score is greater than or equal to a parameter passScore, we will consider it a pass. If a student's name is NOT found in the table, we assume they have missed the exam and thus consider failed. The method should...
Design a class that can be used to simulate rolling a multi-faced die. 2. Write a...
Design a class that can be used to simulate rolling a multi-faced die. 2. Write a Java class called Die in a class file called Die.java.    3. The Die class will have three private class attributes: a. The first attribute, called numSides, is of type int and represents the number of sides on the die. b. The second attribute, called sumRolls, is static of type int and will keep track of the sum of multiple die rolls by multiple...
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...