Object-Oriented Programming with Java, part I – Exercise 82

This exercise creates an array list of random numbers for a lottery draw.

My Code

Main

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        LotteryNumbers lotteryNumbers = new LotteryNumbers();
        ArrayList<Integer> numbers = lotteryNumbers.numbers();

        System.out.println("Lottery numbers:");
        for (int number : numbers) {
            System.out.print(number + " ");
        }
        System.out.println("");
        
    }
}

LotteryNumbers

import java.util.ArrayList;
import java.util.Random;

public class LotteryNumbers {
    private ArrayList<Integer> numbers;
    private Random random = new Random();

    public LotteryNumbers() {
        // Draw numbers as LotteryNumbers is created
        this.numbers = new ArrayList<Integer>();
        this.drawNumbers();
        this.random = new Random();
    }

    public ArrayList<Integer> numbers() {
        return this.numbers;
    }

    public void drawNumbers() {
        // We'll format a list for the numbers
        this.numbers = new ArrayList<Integer>();
        // Write the number drawing here using the method containsNumber()

        while (this.numbers.size() < 7){
        int draw = this.random.nextInt(39) + 1;
        if (!containsNumber(draw)){
            this.numbers.add(draw);
        }

        }
        
    }

    public boolean containsNumber(int number) {
        // Test here if the number is already in the drawn numbers
        if (numbers.contains(number)){
            return true;
        }
        return false;
    }
}

Model Code

Main

import java.util.ArrayList;

 

public class Main {

    public static void main(String[] args) {

        LotteryNumbers lotteryNumbers = new LotteryNumbers();

        ArrayList<Integer> numbers = lotteryNumbers.numbers();

 

        System.out.println("Lottery numbers:");

        for (int number : numbers) {

            System.out.print(number + " ");

        }

        System.out.println("");

    }

}

LotteryNumbers

import java.util.ArrayList;

import java.util.Random;

 

public class LotteryNumbers {

    private ArrayList<Integer> numbers;

 

    public LotteryNumbers() {

        // Draw numbers as LotteryNumbers is created

        this.drawNumbers();

    }

 

    public ArrayList<Integer> numbers() {

        return this.numbers;

    }

 

    public void drawNumbers() {

        // We'll format a list for the numbers

        this.numbers = new ArrayList<Integer>();

        // Write the number drawing here using the method containsNumber()

        Random random = new Random();

        while (this.numbers.size() < 7) {

            int newNumber = random.nextInt(39) + 1;

            boolean alreadyDrawn = this.containsNumber(newNumber);

 

            // we add the number only if it is not already among the drawn numbers

            if (!alreadyDrawn) {

                this.numbers.add(newNumber);

            }

        }

    }

 

    public boolean containsNumber(int number) {

        // Test here if the number is already in the drawn numbers

        for (int n : this.numbers) {

            if (n == number) {

                return true;

            }

        }

        return false;

    }

}

Comments

I had a lot of trouble with the arraylist size. That the arraylist size changes depending on whether the numbers are a single or double digit is somewhat bizarre as well.

Leave a comment