The “Numbers” Game: Homework Help for Java In recent years, many states have leg
ID: 3829060 • Letter: T
Question
The “Numbers” Game: Homework Help for Java
In recent years, many states have legalized various forms of gambling as a way to raise revenues. The most popular are different types of lottery games, which are based on a traditional illegal game known as “the numbers” or “boleta.”
In your basic numbers game, players make either a “Straight” bet or a “Box” bet with a 3-digit number. The winning combination is chosen at random. If the player’s number matches the winning number in any of the following ways, then the player wins the amount indicated, otherwise they lose.
Type
of Bet
Description
If your number is:
Winning Numbers
Payout
Actual Odds
Straight
Exact order match
546
546
$600
to 1
1 in 1000
Box
Match Any Order
(3 different numbers)
546
546,564,
654,645,
456,465
$100
to 1
1 in 167
Match Any Order
(1 duplicate number)
919
199,919,991
$200
to 1
1 in 333
Payouts shown are for the extinct, mob-run numbers game. Naturally, the payouts in the legalized, state-run games (e.g. “Cash 3”) are substantially lower.
Note that there is only 1 way to win a straight bet but 6 ways to win a box bet without duplicate numbers and 3 ways to win a box bet with duplicate numbers
This assignment is to create a class to simulate the numbers game.
II. Numbers Game Class Specifications
Your class will have 4 instance variables:
bet type
bet amount
player’s number
winning number
and a constructor to initialize them to values passed by the user. You may also have any additional instance variables you find necessary or useful.
To receive credit for this assignment, the player’s number and the winning number must both be 3-digit ints. No credit for entering 3 separate int’s (or strings) for either number, and no credit for using any String class methods.
HINT: Use the int division and mod operations to isolate the individual digits of each number.
Your class will also have two additional methods:
a method that returns the input data as a string
a method that compares the player’s number to the winning number and returns the amount won (or 0 if the player loses)
III. Test Class Specifications
All data - bet type, bet amount, player’s number, and winning number - are to be entered by the user.
Output must include all the input values and must be neatly presented.
All output is to be done in the test class. None of the methods of the NumbersGame class do any output.
IV. Data to be Used
Run your program 6 times using this data:
Straight bet, $1, player number 123, winning number 123
Straight bet, $1, player number 123, winning number 321
Box bet, $2, player number 123, winning number 123
Box bet, $2, player number 123, winning number 322
Box bet, $2, player number 484, winning number 748
Box bet, $2, player number 808, winning number 880
Although you are to use the above data, your numbers game class must work for all possible data values. However, you may assume that neither number will begin with zero.
Thorough testing will involve quite a few more sets of data. Can you figure out how many?
VII. Algorithm
Here is a high-level algorithm for the method that evaluates the player’s bet:
if ( Straight bet)
{
if (all three numbers match in exact order)
player wins $600 for each $1 wagered
else
player loses
}
else // Box bet
{
if (all three numbers match in any order)
{
// player wins
if (player’s number contains duplicates)
player wins $200 for each $1 wagered
else
player wins $100 for each $1 wagered
}
else
player loses
}
VIII. Extra Credit!
In your numbers game class, have the method that evaluates the player’s number call separate boolean methods to
determine whether there is an “exact order” match
determine whether there is an “any order” match
determine whether a Box bet contains duplicate numbers
Type
of Bet
Description
If your number is:
Winning Numbers
Payout
Actual Odds
Straight
Exact order match
546
546
$600
to 1
1 in 1000
Box
Match Any Order
(3 different numbers)
546
546,564,
654,645,
456,465
$100
to 1
1 in 167
Match Any Order
(1 duplicate number)
919
199,919,991
$200
to 1
1 in 333
Explanation / Answer
// Betting.java
import java.util.Arrays;
public class Betting {
public Betting(String betType, double betAmount, int playersNumber,
int winningNumber) {
this.betType = betType;
this.betAmount = betAmount;
this.playersNumber = playersNumber;
this.winningNumber = winningNumber;
}
private String betType;
private double betAmount;
private int playersNumber;
private int winningNumber;
private int[] getDigitArrayFromNumber(int number)
{
int[] digits = new int [3];
for (int i = 0; i < 3; i++)
{
digits[i] = number%10;
number = number / 10;
}
return digits;
}
public double compareNumbers()
{
if (betType.equals("Straight"))
{
if (playersNumber == winningNumber)
{
return 600*betAmount;
}
else
{
return 0;
}
}
int[] playersDigit = getDigitArrayFromNumber(playersNumber);
int[] winningDigit = getDigitArrayFromNumber(winningNumber);
Arrays.sort(playersDigit);
Arrays.sort(winningDigit);
boolean won = true;
for (int i = 0; i < playersDigit.length; i++)
{
if (playersDigit[i] != winningDigit[i])
won = false;
}
if (won)
{
boolean repeatedDigit = false;
for (int i = 0; i < playersDigit.length-1; i++)
{
if (playersDigit[i] == playersDigit[i+1])
repeatedDigit = true;
}
if (repeatedDigit)
{
return 200*betAmount;
}
else
{
return 100*betAmount;
}
}
return 0;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(betType);
s.append("bet, $");
s.append(betAmount);
s.append(", player number ");
s.append(playersNumber);
s.append(", winning number ");
s.append(winningNumber);
return s.toString();
}
}
// BettingTest.java
import java.util.Scanner;
public class BettingTest {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int type;
System.out.println("Enter bet type. 1. for straight Anything else for box");
type = sc.nextInt();
String betType = "Box";
if (type == 1)
{
betType = "Straight";
}
System.out.print("Enter bet amount: ");
double betAmount = sc.nextDouble();
System.out.print("Enter player number: ");
int playersNumber = sc.nextInt();
System.out.print("Enter winning number: ");
int winningNumber = sc.nextInt();
Betting b = new Betting(betType, betAmount, playersNumber, winningNumber);
System.out.println(b);
System.out.println("Amount after bet: " + b.compareNumbers());
sc.close();
}
}
// Sample runs
Enter bet type. 1. for straight
Anything else for box
1
Enter bet amount: 1
Enter player number: 123
Enter winning number: 123
Straightbet, $1.0, player number 123, winning number 123
Amount after bet: 600.0
Enter bet type. 1. for straight
Anything else for box
1
Enter bet amount: 1
Enter player number: 123
Enter winning number: 321
Straightbet, $1.0, player number 123, winning number 321
Amount after bet: 0.0
Enter bet type. 1. for straight
Anything else for box
2
Enter bet amount: 2
Enter player number: 123
Enter winning number: 123
Boxbet, $2.0, player number 123, winning number 123
Amount after bet: 200.0
Enter bet type. 1. for straight
Anything else for box
2
Enter bet amount: 2
Enter player number: 123
Enter winning number: 322
Boxbet, $2.0, player number 123, winning number 322
Amount after bet: 0.0
Enter bet type. 1. for straight
Anything else for box
2
Enter bet amount: 2
Enter player number: 484
Enter winning number: 748
Boxbet, $2.0, player number 484, winning number 748
Amount after bet: 0.0
Enter bet type. 1. for straight
Anything else for box
2
Enter bet amount: 2
Enter player number: 808
Enter winning number: 880
Boxbet, $2.0, player number 808, winning number 880
Amount after bet: 400.0
Enter bet type. 1. for straight
Anything else for box
2
Enter bet amount: 2
Enter player number: 123
Enter winning number: 231
Boxbet, $2.0, player number 123, winning number 231
Amount after bet: 200.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.