Spaces:
Running
Running
int vs ArrayList<Integer>
Browse files
Week 4: Writing classes/07. Lottery rounds' attributes and constructor
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The program defines a class called LotteryRound.
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
Complete the class by typing
|
| 5 |
+
1. Attributes
|
| 6 |
+
week (integer)
|
| 7 |
+
numbers (integer-typed list)
|
| 8 |
+
jackpot (double)
|
| 9 |
+
|
| 10 |
+
2. Constructor, which takes as parameters the values of the attributes in the order given in step 1.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
import java.util.Random;
|
| 18 |
+
import java.util.ArrayList;
|
| 19 |
+
|
| 20 |
+
public class Test{
|
| 21 |
+
public static void main(String[] args){
|
| 22 |
+
final Random r = new Random();
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 26 |
+
for (int i=0; i<7; i++) {
|
| 27 |
+
numbers.add(r.nextInt(39) + 1);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
int round = r.nextInt(52) + 1;
|
| 31 |
+
|
| 32 |
+
double jackpot = r.nextInt(5000000) + 1000000;
|
| 33 |
+
|
| 34 |
+
System.out.println("Creating an object with parameters");
|
| 35 |
+
System.out.println("Week: " + round);
|
| 36 |
+
System.out.println("Numbers: " + round);
|
| 37 |
+
System.out.println("Jackpot: " + jackpot);
|
| 38 |
+
|
| 39 |
+
LotteryRound lk = new LotteryRound(round, numbers, jackpot);
|
| 40 |
+
System.out.println("Olio: " + lk);
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
class LotteryRound {
|
| 44 |
+
|
| 45 |
+
// ADD ATTRIBUTES
|
| 46 |
+
int week;
|
| 47 |
+
ArrayList<Integer> numbers;
|
| 48 |
+
double jackpot;
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
// ADD CONSTRUCTOR
|
| 53 |
+
public LotteryRound(int week, ArrayList<Integer> numbers, double jackpot) {
|
| 54 |
+
this.week = week;
|
| 55 |
+
this.numbers = numbers;
|
| 56 |
+
this.jackpot = jackpot;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@Override
|
| 62 |
+
public String toString() {
|
| 63 |
+
return "LotteryRound [week=" + week + ", numbers=" + numbers + ", jackpot=" + jackpot + "]";
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
Creating an object with parameters
|
| 74 |
+
Week: 17
|
| 75 |
+
Numbers: 17
|
| 76 |
+
Jackpot: 2222680.0
|
| 77 |
+
Olio: LotteryRound [week=17, numbers=[2, 2, 3, 6, 33, 39, 5], jackpot=2222680.0]
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|