TurkuBasicOOPinJava / Week 4: Writing classes /07. Lottery rounds' attributes and constructor
KaiquanMah's picture
int vs ArrayList<Integer>
dc0be62 verified
The program defines a class called LotteryRound.
Complete the class by typing
1. Attributes
week (integer)
numbers (integer-typed list)
jackpot (double)
2. Constructor, which takes as parameters the values of the attributes in the order given in step 1.
import java.util.Random;
import java.util.ArrayList;
public class Test{
public static void main(String[] args){
final Random r = new Random();
ArrayList<Integer> numbers = new ArrayList<>();
for (int i=0; i<7; i++) {
numbers.add(r.nextInt(39) + 1);
}
int round = r.nextInt(52) + 1;
double jackpot = r.nextInt(5000000) + 1000000;
System.out.println("Creating an object with parameters");
System.out.println("Week: " + round);
System.out.println("Numbers: " + round);
System.out.println("Jackpot: " + jackpot);
LotteryRound lk = new LotteryRound(round, numbers, jackpot);
System.out.println("Olio: " + lk);
}
}
class LotteryRound {
// ADD ATTRIBUTES
int week;
ArrayList<Integer> numbers;
double jackpot;
// ADD CONSTRUCTOR
public LotteryRound(int week, ArrayList<Integer> numbers, double jackpot) {
this.week = week;
this.numbers = numbers;
this.jackpot = jackpot;
}
@Override
public String toString() {
return "LotteryRound [week=" + week + ", numbers=" + numbers + ", jackpot=" + jackpot + "]";
}
}
Creating an object with parameters
Week: 17
Numbers: 17
Jackpot: 2222680.0
Olio: LotteryRound [week=17, numbers=[2, 2, 3, 6, 33, 39, 5], jackpot=2222680.0]