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
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...
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...
***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,...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....