-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleLottery.java
More file actions
88 lines (78 loc) · 2.34 KB
/
BubbleLottery.java
File metadata and controls
88 lines (78 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.Random;
class BubbleLottery{
/**
* Main function that runs the program
*/
public static void main(String[] args){
BubbleLottery instance = new BubbleLottery();
instance.run();
}
/**
* Main function that calls all functions and controls the program
*/
public void run(){
int[] ticket = this.generateTicket();
this.outputTicket(ticket);
int[] sortedTicket = this.bubbleSort(ticket);
this.outputHighestPlayerNumber(sortedTicket);
this.outputLowestPlayerNumber(sortedTicket);
}
/**
* Generates ticket with 6 random numbers, returns result as int array
*/
private int[] generateTicket(){
Random random = new Random();
int[] ticket = new int[6];
for(int i = 0; i < 6; i++){
ticket[i] = random.nextInt(48) + 1;
}
return ticket;
}
/**
* Output highest number from the int array given by the parameters
*/
private void outputHighestPlayerNumber(int[] sortedTicket){
System.out.println("Highest number: " + sortedTicket[0]);
}
/**
* Output lowest number from the int array given by the parameters
*/
private void outputLowestPlayerNumber(int[] sortedTicket){
System.out.println("Lowest number: " + sortedTicket[sortedTicket.length - 1]);
}
/**
* Accepts ticket as int array and outputs it
*/
private void outputTicket(int[] ticket){
System.out.print("Ticket: ");
for(int i = 0; i < ticket.length - 1; i++){
System.out.print(ticket[i] + " ");
}
System.out.println("");
}
/**
* Accepts ticket as int array and performs bubble sort in ascending order.
* Returns sorted ticket as int array
*/
private int[] bubbleSort(int[] ticket){
int currentElement = 0;
int tempItem;
boolean swap = true;
while(swap){
swap = false;
for(int i = 0; i < ticket.length - 1; i++){
if(currentElement != ticket.length - 1 && ticket[currentElement] < ticket[currentElement + 1]){
tempItem = ticket[currentElement + 1];
ticket[currentElement + 1] = ticket[currentElement];
ticket[currentElement] = tempItem;
swap = true;
}
currentElement++;
if(currentElement == ticket.length){
currentElement = 0;
}
}
}
return ticket;
}
}