Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dice rolling project: Sample Console Output: Welcome to the Paradise Roller Roll

ID: 3589613 • Letter: D

Question

Dice rolling project:

Sample Console Output:

Welcome to the Paradise Roller

Roll the dice? (y/n): y

Roll 1:   2 & 5

Craps!

Roll again? (y/n): y

Roll 2:   2 & 1

Roll again? (y/n): y

Roll 3:   4 & 6

Roll again? (y/n): y

Roll 4:   6 & 6

Box cars!

Roll again? (y/n): y

Roll 5:   1 & 1

Snake eyes!

Roll again? (y/n): n

Operation:

If the user chooses to roll the dice, the application rolls two six-sided dice, displays the results of each, and asks if the user wants to roll again.

Specifications:

Create a public class named DiceRollerApp that has the main method and uses the PairOfDice class to roll the dice each time the user responds ‘y’ to the request prompts. This app class should display special messages for “craps” (sum of both dice is 7), “snake eyes” (double 1’s), and “box cars” (double 6’s). For this application, assume that two six-sided dice are used.

Create a public class named Die to store the data for a single die. The class will have two state instance variables: # of sides and Value (face value of die once it is rolled). This class will contain these constructors and methods:

public Die()              // default to a six-sided die
public Die(int sides)     // allow a variable number of sides
public void roll()        // roll die to determine its value using a random method
public int getValue()

Create a public class named PairOfDice to store two dice. This class should contain two instance variables of the Die class type, an instance variable that holds the sum of the two dice, and these constructors and methods:

public PairOfDice() // default to two six-sided dice
public PairOfDice(int sides) // allow a variable number of sides for the two die
public void roll() // call each Die object’s roll method
public int getValue1() // get value of die1
public int getValue2() // get value of die2
public int getSum() // get the sum of both dice

You can use the random method of the Math class to generate a random number from 1 to the number of sides on a die like this:

int value = (int) (Math.random() * sides);

Notes:

Do not add additional methods to the Die or PairOfDice classes. The data about the rolls is not being stored so there is no need to create an array. Remember to keep the methods simple. The methods should be designed to do one thing only. This helps the classes and methods have high cohesion. Our projects will follow the principles of modularity covered in CIS 2110.

You can write test code to test the methods of the Die class before creating the PairOfDice class but remember to remove it from the program because only the PairOfDice class is called from the DiceRollerApp class.

Create a Java Dice Rolling program with NetBeans that satisfies the specifications above.

Projects can require the use of material from any of the previous chapters covered in the textbook.

Explanation / Answer

/* CLASS DIE */

package dice;

public class die

{

private int of_sides;

private int value;

public die() {

this.of_sides = 6;

}

public die(int paramInt) {

this.of_sides = paramInt;

}

public void roll() //sets the face value

{

value = ((int)(Math.random() * of_sides) + 1 );  

}

public int getValue()

{

return value;

}

}

/* class PairOfDice */

package PairOfDice;

import dice.die;

public class PairOfDice

{

die one;

die two;

int Sum;

public PairOfDice()

{

this.one=new die();

this.two=new die();

}

public PairOfDice(int sides)

{

this.one=new die(sides);

this.two=new die(sides);

}

public void roll()

{

one.roll();

two.roll();

}

public int getValue1()

{

return one.getValue();

}

public int getValue2()

{

return two.getValue();

}

public int getSum()

{

return getValue1()+ getValue2();

}

}

/* class DiceRollerApp */

import dice.die;

import PairOfDice.PairOfDice;

import java.util.Scanner;

class DiceRollerApp

{

public static void main(String a[])

{

PairOfDice p=new PairOfDice();

Scanner sc=new Scanner(System.in);

char roll;int sum,count=1;

System.out.println("Welcome to the Paradise Roller");

System.out.println("Roll the Dice ?(y/n)");

roll=sc.next().charAt(0); //get the char from the input

while(roll!='n')  

{

p.roll();

sum=p.getSum();

System.out.println("Roll"+" "+count+" "+p.getValue1()+" "+"&"+" "+p.getValue2());

if(p.getSum()==7)

{

System.out.println("Craps");

}

else if(p.getValue1()==1&&p.getValue2()==1)

{

System.out.println("Snake Eyes");

}

else if(p.getValue1()==6&&p.getValue2()==6)

{

System.out.println("box cars");

}

System.out.println("Roll Again? (y/n)");

roll=sc.next().charAt(0);

count++;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote