I am writing a program that rolls 2 dice and gives the user the option to repeat the roll (continue y/n). Additionally the application needs to recognize when certain rolls are made like rolling a 7 or 2 (snake eyes) im not sure if that needs to go in the main method or have its own class, im lost! please help. below is what i have for the outline of what the application needs to be im not sure how to code it.
This is my first time writing my own classes to be used in a main method the layout is as follows:
public class DiceRollerApp
{
public static void main(String[] args)
{
}// end main method
}// end App class
class Die
{
public Die()
{} // default to six-sided die
public Die(int sides)
{} // variable number of sides
public void roll()
{} // randomly picks a face value
public int getValue()
{} // returns the face value
}// end class Die
class PairOfDice
{
public PairOfDice()
{} // default to six-sided dice
public PairOfDice(int sides)
{} // variable number of sides
public void roll()
{} // roll both dice
public int getValue1(){} // get value of die1
public int getValue2(){} // get value of die2
public int getSum() {}
// get sum of both dice
}// end class PairOfDice
public class Validator
{
public static String getString(Scanner sc, String prompt)
{
System.out.print(prompt);
String s = sc.next(); // read user entry
sc.nextLine(); // discard any other data entered on the line
return s;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static int getInt(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static double getDouble(Scanner sc, String prompt,
double min, double max)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}
} // end class Validator
I have written the validator class already but did not post it here, its purpose is to simply validate the string from the question “Continue? y/n: “
im not sure how to code the die class and the pair of dice class, and i dont know if i need to have a separate class for the DiceRollerApp.
1 Answer