Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSC-111 HW9 Generate Lucky for Life Numbers
/*
* Name: Prof. Antonio C. Silvestri
* Date: 12/3/2018
* Course Number: CSC-111
* Course Name: Intro to Java Programming
* Problem Number: HW9
* Email: silvestri@stcc.edu
* Description: Generate Lucky for Life Numbers
*/
package luckyforlife;
import java.util.Scanner;
public class LuckyForLife {
private static int[] initArray(int number) {
int list[] = new int[number];
for (int i = 0; i < list.length; i++)
list[i] = i + 1;
return list;
}
private static void shuffle(int[] list) {
for (int i = 0; i < list.length; i++) {
int randomIndex = (int)(Math.random() * list.length);
int temp = list[i];
list[i] = list[randomIndex];
list[randomIndex] = temp;
}
}
private static int[] dealOutRandoms(int[] list, int numberToDealOut) {
int[] luckies = new int[numberToDealOut];
for (int i = 0; i < numberToDealOut; i++)
luckies[i] = list[i];
return luckies;
}
private static void sort(int[] lucky) {
for (int i = 0; i < lucky.length - 1; i++)
for (int j = i + 1; j < lucky.length; j++)
if (lucky[j] < lucky[i]) {
int temp = lucky[i];
lucky[i] = lucky[j];
lucky[j] = temp;
}
}
private static int generateLuckyBall(int max) {
return (int)(Math.random() * max) + 1;
}
private static void printQuickPick(int[] lucky, int luckyBall) {
for (int i = 0; i < lucky.length; i++)
System.out.printf("%3d", lucky[i]);
System.out.printf(" -%3d", luckyBall);
System.out.println();
}
//**********************************************
private static void process(Scanner sc, String args[]) {
int list[] = initArray(43);
shuffle(list);
int lucky[] = dealOutRandoms(list, 5);
sort(lucky);
int luckyBall = generateLuckyBall(43);
printQuickPick(lucky, luckyBall);
}
//**********************************************
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.equalsIgnoreCase("Y");
}
//**********************************************
public static void main(String args[]) {
final String TITLE = "Lucky For Life Quick Pick Generator V1.0";
final String CONTINUE_PROMPT = "Do this again? [y/N] ";
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment