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
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
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,...
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...
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...
I am trying to take a string of numbers seperated by a single space and covert...
I am trying to take a string of numbers seperated by a single space and covert them into a string array. I have created the following code but it only works if the numbers are seperated a a comma or something similar to that. Example of what I am trying to achieve: string input = "1 1 1 1 1" turn it into.... int[] x = {1,1,1,1} so that it is printed as... [1, 1, 1, 1]    This is...
The AssemblyLine class has a potential problem. Since the only way you can remove an object...
The AssemblyLine class has a potential problem. Since the only way you can remove an object from the AssemblyLine array is when the insert method returns an object from the last element of the AssemblyLine's encapsulated array, what about those ManufacturedProduct objects that are "left hanging" in the array because they never got to the last element position? How do we get them out? So I need to edit my project. Here is my AssemblyLine class: import java.util.Random; public class...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
import java.util.Scanner; public class ListDriver {    /*    * main    *    * An...
import java.util.Scanner; public class ListDriver {    /*    * main    *    * An array based list is populated with data that is stored    * in a string array. User input is retrieved from that std in.    * A text based menu is displayed that contains a number of options.    * The user is prompted to choose one of the available options:    * (1) Build List, (2) Add item, (3) Remove item, (4) Remove...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
USING JAVA: I was asked to write a function that obtains a name from the user...
USING JAVA: I was asked to write a function that obtains a name from the user and returns it in reverse order (So if the user inputs "MIKE" the function returns "EKIM"). You can't use string variables it can only be done using a Char Array. Additionally, you can use a temporary Char Array but you are supposed to return the reverse order in the same char array that the user input, this is for hypothetical cost purposes -we are...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT