Question

Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...

Classes/ Objects(programming language java ) in software (ATOM)

Write a Dice class with three data fields and appropriate types and permissions

  • diceType
  • numSides
  • sideUp

The class should have

  • A 0 argument (default) constructor
    • default values are diceType: d6, numSides: 6, sideUp: randomValue
  • 1 argument constructor for the number of sides
    • default values are diceType: d{numSides}, sideUp: randomValue
  • 2 argument constructor for the number of sides and the diceType
  • appropriate accessors and mutators *theoretical question: can you change the number of sides on dice, and if you do does it's name change?
  • a roll method to generate a random value. If I play D&D with you and your dice are always rolling high or max, I'm going to be suspicious

Write a DiceGame program to test each your Dice class

  • create different sized dice using different constructors
  • roll dice
  • set a dice to a specific side up
  • BONUS: create 5 six-sided dice. Roll them in a loop until you get a cold-Yahtzee (5 of a kind with one roll). How many rolls did it take?

Checklist

  • 3 data fields (1)
  • 3 constructors (1 each = 3)
  • accessors and mutators (0.5 each = 3)
  • roll (1)
  • successfully tests a variety of methods (2)
  • If I try your app and one of the following things happens, you get 100% (no cheating)
    • I create a dice object with 20 sides and it rolls 20 the first time
    • The cold-Yahtzee happens on the first roll   NOTE-(Give a code review of about 3 or 4 lines in the form of paragraph. )   

Homework Answers

Answer #1

The Java Code for the program is as follows:

It has three different Constructor

  • Default Contructor which sets the instance variable as diceType as "d6", numSides as 6 and sideUp as random Value
  • Constructor with "Number of Sides" as parameter will set the class attributes repectively.
  • Contructor with two parameters will set the class attributes respectively

The below Code only validate the following conditions:

  • creating different sized dice using different constructors
  • roll dice
  • set a dice to a specific side up
import java.util.*;

class Dice{
        private String diceType;
        private int numSides;
        private int sideUp;
        private Random random = new Random();
        private int NumberOfRolls = 0;

        // Constructors 
        // zero value constructor
        Dice(){
                diceType = "d6";
                numSides = 6;
                sideUp = 1 + random.nextInt(6);
        }

        // Constructor for Number of sides as parameter
        Dice(int numSides){
                this.numSides = numSides;
                this.diceType = "d"+numSides;
                this.sideUp =  1 + random.nextInt(numSides);
        }

        // Constructor for Dice Type and Number of Sides as parameters
        Dice(String diceType, int numSides){
                this.diceType = diceType;
                this.numSides = numSides;
                this.sideUp = 1 + random.nextInt(numSides);
        }

        // Accessors 
        public int getNumberOfSides(){
                return numSides;
        }

        public int getNumberOfRolls(){
                return NumberOfRolls;
        }

        public String getDiceType(){
                return diceType;
        }

        public int getSideUp(){
                return sideUp;
        }

        // Mutators

        public void setDiceType(String diceType){
                this.diceType = diceType;
        }

        public void setNumberOfSides(int numSides){
                this.numSides = numSides;
        }

        public void setSideUp(int value){
                if(value < numSides && value > 0){
                        this.sideUp = value;
                }else{
                        this.sideUp = 1 + random.nextInt(numSides);
                }
        }
        // This method returns a random number between 1 and Number of Sides (numSides)
        public int roll(){
     ++NumberOfRolls;
     return 1 + random.nextInt(numSides);
   }
}

// Main Class to test each Dice Constructor
public class DiceTest
{
    public static void main(String[] args)
    {
       
        Dice dice1 = new Dice();
        Dice dice2 = new Dice(8); 
        Dice dice3 = new Dice("d7",7);
        System.out.println("Rolling the first Dice Which has "+ dice1.getNumberOfSides() + " Sides");
        System.out.println("Before setting the sidesUp of the Dice, The SideUp is "+ dice2.getSideUp());
        dice2.setSideUp(2);
        System.out.println("After setting the SideUp of the Dice, The Side up is "+dice2.getSideUp());
        
    }
}

As I told you already that the above code does not validate the BONUS condition:

The below Code will Validate the BONUS Condition(Cold-Yahtzee):

import java.util.*;

class Dice{
        private String diceType;
        private int numSides;
        private int sideUp;
        private Random random = new Random();
        private int NumberOfRolls = 0;

        // Constructors 
        // zero value constructor
        Dice(){
                diceType = "d6";
                numSides = 6;
                sideUp = 1 + random.nextInt(6);
        }

        // Constructor for Number of sides as parameter
        Dice(int numSides){
                this.numSides = numSides;
                this.diceType = "d"+numSides;
                this.sideUp =  1 + random.nextInt(numSides);
        }

        // Constructor for Dice Type and Number of Sides as parameters
        Dice(String diceType, int numSides){
                this.diceType = diceType;
                this.numSides = numSides;
                this.sideUp = 1 + random.nextInt(numSides);
        }

        // Accessors 
        public int getNumberOfSides(){
                return numSides;
        }

        public int getNumberOfRolls(){
                return NumberOfRolls;
        }

        public String getDiceType(){
                return diceType;
        }

        public int getSideUp(){
                return sideUp;
        }

        // Mutators

        public void setDiceType(String diceType){
                this.diceType = diceType;
        }

        public void setNumberOfSides(int numSides){
                this.numSides = numSides;
        }

        public void setSideUp(int value){
                if(value < numSides && value > 0){
                        this.sideUp = value;
                }else{
                        this.sideUp = 1 + random.nextInt(numSides);
                }
        }
        // This method returns a random number between 1 and Number of Sides (numSides)
        public int roll(){
     ++NumberOfRolls;
     return 1 + random.nextInt(numSides);
   }
}



// Testing Class to validate the Cold-Yahtzee
public class DiceTest
{
    public static void main(String[] args)
    {
       
        Dice dice1 = new Dice(6);
        Dice dice2 = new Dice(6); 
        Dice dice3 = new Dice(6);
        Dice dice4 = new Dice(6);
        Dice dice5 = new Dice(6);
      
        int roll1 = dice1.roll();
        int roll2 = dice2.roll(); 
        int roll3 = dice3.roll(); 
        int roll4 = dice4.roll();
        int roll5 = dice5.roll();
        while(true){
                if(roll1 == roll2 && roll1 == roll3 && roll1 == roll4 && roll1 == roll5){
                        System.out.println("Bingo Cold-Yahtzee happens on the " + dice1.getNumberOfRolls()+" rolls");
                        break;
                }else{
                        System.out.printf("%d %d %d %d %d \n", roll1, roll2, roll3, roll4, roll4);
                        roll1 = dice1.roll();
                        roll2 = dice2.roll();
                        roll3 = dice3.roll(); 
                        roll4 = dice4.roll();
                        roll5 = dice5.roll();
                }
        }
    }
}

Hope this helps 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
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...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h andRun.cpp to separate class header and implementation. In this program, we are going to create a small scale Telivision Management System. A) Create a class Episode with following variables: char* episode_name, char* episode_venue, char episode_date[22] and char episode_time[18]. Input format for episode_date: dd-mm-yyyy Input format for episode_time: hh:mm am/pm B) Implement default constructor and overloaded constructor. Print “Default Constructor Called” and “Overloaded Constructor Called”...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
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;...
***Programming language is Java. After looking at this scenario please look over the requirements at the...
***Programming language is Java. After looking at this scenario please look over the requirements at the bottom (in bold) THIS IS ALL THAT WAS PROVIDED. PLEASE SPECIFY ANY QUESTIONS IF THIS IS NOT CLEAR (don't just say more info, be specific)*** GMU in partnership with a local sports camp is offering a swimming camp for ages 10-18. GMU plans to make it a regular event, possibly once a quarter. You have been tasked to create an object-oriented solution to register,...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT