Purpose: Use data types/variables, various selection structures, random number g
ID: 3845316 • Letter: P
Question
Purpose: Use data types/variables, various selection structures, random number generation, input/output, performing calculations and formatting output.
Description: You have just been given the opportunity to win prizes from a sweepstakes entry you made at the county fair! The opportunity includes winning 4 prizes, each will be determined by a different game of chance. You may create your own descriptions for the prizes and values as you like, just make sure they are patterned as follows. Each of these requires a random number generation.
1. Flip a coin: Depending upon the heads or tails result, you will get prize1:
Heads: A smartphone charger worth $9.99
Tails: A 9-volt battery and a wire worth $0.49 (the booby prize)
2. Roll a six-sided die: Depending upon the number that comes up, you will get prize2:
1: A refrigerator magnet worth $1.49
2: A handy grocery bag worth $.59 (the booby prize)
3: A half-price ticket to next year’s fair worth $4.00
4: A CD with hits from 12 bands playing at the fair worth $12.00
5: A coupon for a free item at one of the concession stands worth $5.00
6: A stuffed animal penguin worth $8.00
3. Spin a roulette wheel: Depending on the color (red or black) and the number (1 to 100), you will get prize3:
Red doubles the prize, Black triples the prize
Spin 1-20: Earn a penny (the booby prize)
Spin 21-40: Earn $4.00
Spin 41-60: Earn $1.00
Spin 61-80: Earn $2.00
Spin 81-100: Earn $3.00
4. Draw one card from a deck of 52: Depending on the number, suit, or face card, you will get prize4:
Suits (multiplier): Club (x1), Diamond (x2), Heart (x3), Spade (x4)
Numbers 2-10: Get a deck of cards worth $1.00
Number 11 (Jack): Get a game of jacks worth $.50 (the booby prize)
Number 12 (Queen): Get an autographed picture of the fair queen worth $2.00
Number 13 (King): Get an autographed picture of the fair king worth $2.00
Number 14: (Ace): Get $10.00
After counting the number of booby prizes one person may get, use the following scale to award a consolation prize:
a. 1 booby prize: $1.00
b. 2 booby prizes: $2.00
c. 3 booby prizes: $3.00
d. 4 booby prizes: $4.00
As you generate the random numbers (use any method you like), here are suggested variables:
· Store the following double values as prize1Value, prize2Value, prize3Value, prize4Value;
· Store the following string values as prize1Desc, prize2Desc, prize3Desc, prize4Desc;
· Store the following string values as result1Desc, result2Desc, result3Desc, result4Desc;
· You should shorten the descriptions when storing them, keeping them to 20 chars or less.
· Store the count of booby prizes;
· Accumulate the total dollar value of all prizes;
· Utilize as many other variables as you like
· You can experiment with validation loops (optional).
· Generate a report the information with a header and 4 detail lines formatted as such:
Example Output:
Game Result Value Booby Prize Description
1 flipped tails $ 0.49 yes 9-volt battery and wire
2 rolled 4 $12.00 no CD of hits
3 came up black 48 $ 3.00 no Money
4 drew Queen/Hearts $ 6.00 no Picture of Queen
Total Value: $21.49
Total Booby Prizes: 1
You also earned $1.00 for on the booby prize contest!
Explanation / Answer
Here is your code below: -
Game.java
import java.util.Random;
public class Game {
static double prize1Value, prize2Value, prize3Value, prize4Value;
static String prize1Desc, prize2Desc, prize3Desc, prize4Desc;
static String result1Desc, result2Desc, result3Desc, result4Desc;
static String isbooby1 = "no", isbooby2 = "no", isbooby3 = "no", isbooby4 = "no";
static int boobyCount = 0;
public static void main(String[] args) {
playCoins();
playDice();
playWheel();
playDeck();
String[][] results = { { "Game", "Result", "Value", "Booby Prize", "Description" },
{ "1", result1Desc, "$" + prize1Value, isbooby1, prize1Desc },
{ "2", result2Desc, "$" + prize2Value, isbooby2, prize2Desc },
{ "3", result3Desc, "$" + prize3Value, isbooby3, prize3Desc },
{ "4", result4Desc, "$" + prize4Value, isbooby4, prize4Desc } };
printTable(results);
System.out.println();
System.out.println();
System.out.println("Total Value: $" + (prize1Value + prize2Value + prize3Value + prize4Value));
System.out.println("Total Booby Prizes: " + boobyCount);
System.out.println();
System.out.println();
if (boobyCount == 1) {
System.out.println("You also earned $1.00 for on the booby prize contest!");
} else if (boobyCount == 2) {
System.out.println("You also earned $2.00 for on the booby prize contest!");
} else if (boobyCount == 3) {
System.out.println("You also earned $3.00 for on the booby prize contest!");
} else if (boobyCount == 4) {
System.out.println("You also earned $4.00 for on the booby prize contest!");
}
}
public static void playCoins() {
Random rand = new Random(System.currentTimeMillis());
int x = rand.nextInt(2);
if (x == 0) {
result1Desc = "flipped tails";
prize1Desc = "9-volt battery and wire";
prize1Value = 0.49;
boobyCount++;
isbooby1 = "yes";
} else if (x == 1) {
result1Desc = "flipped heads";
prize1Desc = "smartphone charger";
prize1Value = 9.99;
}
}
public static void playDice() {
Random rand = new Random(System.currentTimeMillis());
int x = rand.nextInt(6) + 1;
result2Desc = "rolled " + x;
if (x == 1) {
prize2Desc = "refrigerator magnet";
prize2Value = 1.49;
} else if (x == 2) {
prize2Desc = "handy grocery bag";
prize2Value = 0.59;
boobyCount++;
isbooby2 = "yes";
} else if (x == 3) {
prize2Desc = "half-price ticket to fair";
prize2Value = 4.00;
} else if (x == 4) {
prize2Desc = "CD with hits";
prize2Value = 12.00;
} else if (x == 5) {
prize2Desc = "free item concession coupon";
prize2Value = 5.00;
} else if (x == 6) {
prize2Desc = "stuffed animal penguin";
prize2Value = 8.00;
}
}
public static void playWheel() {
Random rand = new Random(System.currentTimeMillis());
int x = rand.nextInt(2);
prize3Desc = "Money";
if (x == 0) {
result3Desc = "came up black ";
prize3Value = 3;
} else if (x == 1) {
result3Desc = "came up red ";
prize3Value = 2;
}
rand = new Random(System.currentTimeMillis());
x = rand.nextInt(100) + 1;
result3Desc = result3Desc + x;
if (x >= 1 && x <= 20) {
prize3Value = prize3Value * 0.01;
boobyCount++;
isbooby3 = "yes";
} else if (x >= 21 && x <= 40) {
prize3Value = prize3Value * 4.00;
} else if (x >= 41 && x <= 60) {
prize3Value = prize3Value * 1.00;
} else if (x >= 61 && x <= 80) {
prize3Value = prize3Value * 2.00;
} else if (x >= 81 && x <= 100) {
prize3Value = prize3Value * 3.00;
}
}
public static void playDeck() {
Random rand = new Random(System.currentTimeMillis());
int x = rand.nextInt(13) + 1;
if (x >= 2 && x <= 10) {
result4Desc = "drew " + x + "/";
prize4Desc = "Deck of Cards";
prize4Value = 1;
} else if (x == 1) {
result4Desc = "drew Ace/";
prize4Desc = "Money";
prize4Value = 10;
} else if (x == 11) {
result4Desc = "drew Jack/";
prize4Desc = "Game of jacks";
boobyCount++;
isbooby4 = "yes";
prize4Value = 0.50;
} else if (x == 12) {
result4Desc = "drew Queen/";
prize4Desc = "Picture of Queen";
prize4Value = 2;
} else if (x == 13) {
result4Desc = "drew King/";
prize4Desc = "Picture of King";
prize4Value = 2;
}
rand = new Random(System.currentTimeMillis());
x = rand.nextInt(4) + 1;
if (x == 1) {
result4Desc = result4Desc + "Club";
prize4Value = prize4Value * 1;
} else if (x == 2) {
result4Desc = result4Desc + "Diamond";
prize4Value = prize4Value * 2;
} else if (x == 3) {
result4Desc = result4Desc + "Heart";
prize4Value = prize4Value * 3;
} else if (x == 4) {
result4Desc = result4Desc + "Spade";
prize4Value = prize4Value * 4;
}
}
public static void printTable(String data[][]) {
int col = data[0].length;
int maxWidth[] = new int[col];
for (String[] rowD : data)
for (int i = 0; i < col; i++) {
if (maxWidth[i] < rowD[i].length())
maxWidth[i] = rowD[i].length();
}
String format = "";
for (int x : maxWidth)
format += "%-" + (x + 2) + "s ";
format += "%n";
for (String[] rowD : data) {
System.out.printf(format, rowD);
}
}
}
Sample Runs: -
1.
Game Result Value Booby Prize Description
1 flipped tails $0.49 yes 9-volt battery and wire
2 rolled 3 $4.0 no half-price ticket to fair
3 came up black 51 $3.0 no Money
4 drew 8/Diamond $2.0 no Deck of Cards
Total Value: $9.49
Total Booby Prizes: 1
You also earned $1.00 for on the booby prize contest!
2.
Game Result Value Booby Prize Description
1 flipped tails $0.49 yes 9-volt battery and wire
2 rolled 2 $0.59 yes handy grocery bag
3 came up black 15 $0.03 yes Money
4 drew 4/Diamond $2.0 no Deck of Cards
Total Value: $3.1100000000000003
Total Booby Prizes: 3
You also earned $3.00 for on the booby prize contest!
3.
Game Result Value Booby Prize Description
1 flipped heads $9.99 no smartphone charger
2 rolled 6 $8.0 no stuffed animal penguin
3 came up red 28 $8.0 no Money
4 drew Ace/Spade $40.0 no Money
Total Value: $65.99000000000001
Total Booby Prizes: 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.