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.
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)
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
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
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:
1 booby prize: $1.00
2 booby prizes: $2.00
3 booby prizes: $3.00
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!
Expert Answer
import java.util.*;
public class Games
{
public static double[] price_value= new double[4];
public static String[] description= new String[4];
public static String[] Booby= new String[4];
public static String[] game= new String[4];
public static int booby=0;
void coin(int i,int x)
{
if(x==1)
{
price_value[i]=9.99;
description[i]=”Smart Phone charger”;
Booby[i]=”no”;
game[i]=”flipped heads”;
}
else
{
price_value[i]=0.49;
description[i]=”9 volt battery”;
booby=booby+1;
Booby[i]=”yes”;
game[i]=”flipped tails”;
}
}
void dice(int i,int x)
{
if(x==1)
{
price_value[i]=1.49;
description[i]=”refrigerator magnet”;
Booby[i]=”no”;
game[i]=”rolled “+x;
}
else if(x==2)
{
price_value[i]=.59;
description[i]=”Grocery bag”;
booby=booby+1;
Booby[i]=”yes”;
game[i]=”rolled “+x;
}
else if(x==3)
{
price_value[i]=4;
description[i]=”half price ticket”;
Booby[i]=”no”;
game[i]=”rolled “+x;
}
else if(x==4)
{
price_value[i]=12;
description[i]=”CD hits”;
Booby[i]=”no”;
game[i]=”rolled “+x;
}
else if(x==5)
{
price_value[i]=5;
description[i]=”free coupon”;
Booby[i]=”no”;
game[i]=”rolled “+x;
}
else
{
price_value[i]=8;
description[i]=”Animal penguin”;
Booby[i]=”no”;
game[i]=”rolled “+x;
}
}
void wheel(int i,int x)
{
if(get_number(2)==1)
{
if((x>=1)&&(x<=20))
{
price_value[i]=1*2;
description[i]=”Penny”;
booby=booby+1;
Booby[i]=”yes”;
game[i]=”came up red “+x;
}
else if((x>=21)&&(x<=40))
{
price_value[i]=4*2;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up red “+x;
}
else if((x>=41)&&(x<=60))
{
price_value[i]=1*2;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up red “+x;
}
else if((x>=61)&&(x<=80))
{
price_value[i]=2*2;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up red “+x;
}
else
{
price_value[i]=3*2;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up red “+x;
}
}
else
{
if((x>=1)&&(x<=20))
{
price_value[i]=1*3;
description[i]=”Penny”;
booby=booby+1;
Booby[i]=”yes”;
game[i]=”came up black “+x;
}
else if((x>=21)&&(x<=40))
{
price_value[i]=4*3;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up black “+x;
}
else if((x>=41)&&(x<=60))
{
price_value[i]=1*3;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up black “+x;
}
else if((x>=61)&&(x<=80))
{
price_value[i]=2*3;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up black “+x;
}
else
{
price_value[i]=3*3;
description[i]=”Money”;
Booby[i]=”no”;
game[i]=”came up black “+x;
}
}
}
void card(int i,int x)
{
int cd=get_number(14);
if(x==1)
{
if((cd>=2)&&(cd<=10))
{
price_value[i]=1;
description[i]=”Numer “+cd;
Booby[i]=”no”;
game[i]=”drew “+cd+”/club”;
}
else if(cd==11)
{
price_value[i]=.50;
description[i]=”Picture of jack”;
Booby[i]=”yes”;
game[i]=”drew jack/club”;
booby=booby+1;
}
else if(cd==12)
{
price_value[i]=2;
description[i]=”Picture of Queen”;
Booby[i]=”no”;
game[i]=”drew Queen/club”;
}
else if(cd==13)
{
price_value[i]=2;
description[i]=”Picture of king”;
Booby[i]=”no”;
game[i]=”drew king/club”;
}
else
{
price_value[i]=10;
description[i]=”Picture of Ace”;
Booby[i]=”no”;
game[i]=”drew Ace/club”;
}
}
else if(x==2)
{
if((cd>=2)&&(cd<=10))
{
price_value[i]=1*2;
description[i]=”Numer “+cd;
Booby[i]=”no”;
game[i]=”drew “+cd+”/Diamond”;
}
else if(cd==11)
{
price_value[i]=.50*2;
description[i]=”Picture of jack”;
Booby[i]=”yes”;
game[i]=”drew jack/diamond”;
booby=booby+1;
}
else if(cd==12)
{
price_value[i]=2*2;
description[i]=”Picture of Queen”;
Booby[i]=”no”;
game[i]=”drew Queen/diamond”;
}
else if(cd==13)
{
price_value[i]=2*2;
description[i]=”Picture of king”;
Booby[i]=”no”;
game[i]=”drew king/diamond”;
}
else
{
price_value[i]=10*2;
description[i]=”Picture of Ace”;
Booby[i]=”no”;
game[i]=”drew Ace/diamond”;
}
}
else if(x==3)
{
if((cd>=2)&&(cd<=10))
{
price_value[i]=1*3;
description[i]=”Numer “+cd;
Booby[i]=”no”;
game[i]=”drew “+cd+”/heart”;
}
else if(cd==11)
{
price_value[i]=.50*3;
description[i]=”Picture of jack”;
Booby[i]=”yes”;
game[i]=”drew jack/heart”;
booby=booby+1;
}
else if(cd==12)
{
price_value[i]=2*3;
description[i]=”Picture of Queen”;
Booby[i]=”no”;
game[i]=”drew Queen/heart”;
}
else if(cd==13)
{
price_value[i]=2*3;
description[i]=”Picture of king”;
Booby[i]=”no”;
game[i]=”drew king/heart”;
}
else
{
price_value[i]=10*3;
description[i]=”Picture of Ace”;
Booby[i]=”no”;
game[i]=”drew Ace/heart”;
}
}
else
{
if((cd>=2)&&(cd<=10))
{
price_value[i]=1*4;
description[i]=”Numer “+cd;
Booby[i]=”no”;
game[i]=”drew “+cd+”/spade”;
}
else if(cd==11)
{
price_value[i]=.50*4;
description[i]=”Picture of jack”;
Booby[i]=”yes”;
game[i]=”drew jack/spade”;
booby=booby+1;
}
else if(cd==12)
{
price_value[i]=2*4;
description[i]=”Picture of Queen”;
Booby[i]=”no”;
game[i]=”drew Queen/spade”;
}
else if(cd==13)
{
price_value[i]=2*4;
description[i]=”Picture of king”;
Booby[i]=”no”;
game[i]=”drew king/spade”;
}
else
{
price_value[i]=10*4;
description[i]=”Picture of Ace”;
Booby[i]=”no”;
game[i]=”drew Ace/spade”;
}
}
}
int get_number(int n)
{
Random t = new Random();
return t.nextInt(n);
}
public static void main(String []args)
{
Games h= new Games();
double c=0;
h.coin(0,h.get_number(2));
h.dice(1,h.get_number(6));
h.wheel(2,h.get_number(100));
h.card(3,h.get_number(4));
System.out.print(“Game”);
System.out.print(“tvalue”);
System.out.print(“tBooby price”);
System.out.print(“tDescription”);
System.out.print(“n”);
for(int i=0;i<4;i++)
{
System.out.print(game[i]);
c=c+price_value[i];
System.out.print(“t”+price_value[i]+”t”+Booby[i]+”t”+description[i]);
System.out.print(“n”);
}
System.out.print(“nTotal Price: “+c);
System.out.print(“nBooby Price: “+booby);
System.out.print(“n”);
}
}