0% found this document useful (0 votes)
69 views4 pages

Java Powerball Lottery Simulator

This document contains the code for a lottery ticket simulator program written in Java. It defines constants for the prize amounts of matching different numbers and the powerball. The main method initializes variables, displays the game rules, prompts the user to purchase tickets, generates random winning numbers, and prints the results of the user's tickets. Additional methods initialize and sort the tickets, check for matches, generate random numbers within the allowed ranges, and calculate changes to the user's starting balance.

Uploaded by

api-636650239
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

Java Powerball Lottery Simulator

This document contains the code for a lottery ticket simulator program written in Java. It defines constants for the prize amounts of matching different numbers and the powerball. The main method initializes variables, displays the game rules, prompts the user to purchase tickets, generates random winning numbers, and prints the results of the user's tickets. Additional methods initialize and sort the tickets, check for matches, generate random numbers within the allowed ranges, and calculate changes to the user's starting balance.

Uploaded by

api-636650239
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;
public class CIS129_WhitneyHenderson_Lab4{
static Scanner keyboard = new Scanner([Link]);

static final int FIVE_NUM_ONE_POWER = 100000000;


static final int FIVE_NUM_NO_POWER = 1000000;
static final int FOUR_NUMS_ONE_POWER = 50000;
static final int FOUR_NUMS_NO_POWER = 100;
static final int THREE_NUMS_ONE_POWER = 100;
static final int THREE_NUMS_NO_POWER = 7;
static final int TWO_NUMS_ONE_POWER = 7;
static final int ONE_NUMS_ONE_POWER =4;
static final int ZERO_NUMS_ONE_POWER = 4;
static final int LOTTERY_TICKET = 5;
static final int STARTING_MONEY = 1000;
static final int TICKET = 2;
static final int TICKET_LOW = 1;
static final int TICKET_HIGH = 69;
static final int POWERBALL_HIGH = 26;

public static void main (String [] args){

int purchase;
int newMoney;
int powerball;

int [] userTickets = new int [LOTTERY_TICKET];


int [] winningTicket = new int [LOTTERY_TICKET];
//boolean [] ticket = new boolean [LOTTERY_TICKET];
displayPowerBallMenu();

//initBoolArray(ticket);
[Link]("You have $"+STARTING_MONEY);
purchase = getValidPurchase("How many $"+TICKET+" lottery cards would you like to
purchase?");
newMoney = getMoney(purchase);
[Link](newMoney);
powerball = initializePowerball();
initializeTicket(winningTicket);
sortLotteryTicket(winningTicket);
winningTicket(winningTicket, powerball);
[Link]("\n");
//how many tickets the user purchases
for(int x = 0; x < purchase; x++){
initializeTicket(userTickets);
sortLotteryTicket(userTickets);
powerball = initializePowerball();
printTicketNumbers(winningTicket, powerball, userTickets);
}

}//end main

public static void displayPowerBallMenu(){


[Link]("**************************************************");
[Link]("\t Let's play Powerball");
[Link]("**************************************************");
[Link]("5 numbers correct plus powerball = $"+FIVE_NUM_ONE_POWER);
[Link]("5 numbers correct, no powerball = $"+FIVE_NUM_NO_POWER);
[Link]("4 numbers correct plus powerball = $"+FOUR_NUMS_ONE_POWER);
[Link]("4 numbers correct, no powerball = $"+FOUR_NUMS_NO_POWER);
[Link]("3 numbers correct plus powerball = $"+THREE_NUMS_ONE_POWER);
[Link]("3 numbers correct, no powerball = $"+THREE_NUMS_NO_POWER);
[Link]("2 numbers correct plus powerball = $"+TWO_NUMS_ONE_POWER);
[Link]("1 number correct plus powerball = $"+ONE_NUMS_ONE_POWER);
[Link]("0 numbers correct plus powerball = $"+ZERO_NUMS_ONE_POWER);
[Link]("**************************************************");
[Link]("\n");
}//end display

public static void initializeTicket(int [] arr){


for (int x = 0; x < [Link]; x++){
arr[x] = getValidRando(arr);
}// end for
}// end initialize

public static int getValidRando(int [] arr){


int random = [Link](TICKET_LOW, TICKET_HIGH);
while (validRandom(arr, random)){
random = [Link](TICKET_LOW, TICKET_HIGH);
}
return random;
}
public static boolean validRandom(int [] arr, int random){
for(int x = 0; x < [Link]; x++){
if(random == arr[x]){
return true;
}
}
return false;
}
public static void winningTicket(int [] winTick, int powerball){
for(int x = 0; x < [Link]; x++){
if(winTick[x]<10){
[Link](winTick[x]+", ");
}else{
[Link](winTick[x]+", ");
}
}
[Link](" "+powerball);
[Link]();
}// end winningTicket

public static void printTicketNumbers(int [] winTick, int powerball, int [] userTickets){


for (int x = 0; x < [Link]; x++){
if ((userTickets[x]<10) && (searchTickets(winTick, userTickets[x]))){
[Link](userTickets[x]+", ");
}else if(searchTickets(winTick, userTickets[x])){
[Link](userTickets[x]+", ");
}
else{
[Link](userTickets[x]+", ");

}
}
[Link](" "+powerball);
[Link]();
}

public static int initializePowerball(){


int powerball = [Link](TICKET_LOW, TICKET_HIGH);
return powerball;
}

public static boolean searchTickets(int [] winningTicket, int userNum){


for (int x = 0; x < [Link]; x++){
if(userNum == winningTicket[x]){
return true;
}
}
return false;

}// end search


public static int getValidPurchase(String msg){
int purchase = [Link](msg);
while(validInput(purchase)){
[Link]("Invalid value, you have entered a number out of range. Please try
again.");
purchase = [Link](msg);
}
return purchase;
}// end valid purchase

public static boolean validInput(int purchase){


if(purchase < 0){
return true;
}
if((TICKET*purchase) > STARTING_MONEY){
return true;
}else{
return false;
}
}// end valid purchase

public static int getMoney(int purchase){


int newMoney;
int ticksPurchased;
ticksPurchased = purchase * TICKET;
newMoney = STARTING_MONEY - ticksPurchased;
return newMoney;
}
public static void sortLotteryTicket(int [] arr){
int temp = 0;
for (int i = 0; i < [Link]; i++){
for (int j = i+1; j < [Link]; j++){
if(arr[i]>arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

}//end sort

}//end program

Common questions

Powered by AI

The 'sortLotteryTicket' method organizes the numbers in a lottery ticket using a simple bubble sort-like algorithm. It iterates through the array, comparing each pair of elements and swapping them if they are in the wrong order. This process is repeated until the entire array is sorted in ascending order .

The program checks if a user ticket is a winning ticket by comparing each user's ticket number to the winning ticket using the 'searchTickets' method. It also verifies if the corresponding Powerball number matches. A ticket's winning condition is determined by matching these numbers, and different prize categories are determined by the exact number of matches .

The program uses the 'getValidPurchase' method to request the number of lottery cards for purchase. It validates user input by checking if the entered number is non-negative and if the total cost of the tickets exceeds the available starting money. If invalid, it prompts the user again .

To handle a dynamic number of user tickets, the array initialization should be replaced with an ArrayList structure or dynamically allocated arrays based on user input. The 'userTickets' array and related logic in the code need replacement with dynamic data structures to accommodate varying lengths efficiently, using methods like ArrayList's 'add' and resizing loops .

The program calculates the new amount of money by invoking the 'getMoney' method, which computes the cost of the purchased tickets by multiplying the number of tickets by their price ('TICKET'). It then subtracts this amount from the starting money ('STARTING_MONEY') to derive the new total .

The 'initializePowerball' method generates a random Powerball number and returns it. This number is crucial to the lottery outcome since it influences winning categories and prizes when combined with other matching numbers on the ticket .

The primary objective of the main method in the Java code is to simulate a Powerball lottery game. It initializes the player's and the winning lottery tickets, allows the user to purchase a specified number of lottery cards, and calculates the remaining money after the purchase. It then compares the user's tickets with the winning ticket and outputs the details .

The program employs the 'validRandom' method to validate a random number. This method checks if the number is duplicated within the existing ticket by iterating through the array and returning 'true' if the number matches an existing entry, prompting regeneration .

The program uses the 'winningTicket' method to display winning lottery numbers. It prints each number from the winning ticket array and appends the Powerball number at the end. If a number is less than 10, it prints a leading zero for clarity .

The program ensures uniqueness through the 'getValidRando' and 'validRandom' methods. 'getValidRando' generates a random number and checks for uniqueness using 'validRandom', which iterates over the ticket array to verify that the number is not already present. If it's duplicate, 'getValidRando' generates a new number .

You might also like