SHARE
TWEET

Untitled

a guest Jun 25th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class LotteryNumbers {
  5.     private ArrayList<Integer> numbers = new ArrayList<Integer>(); /***** DOMANDA 2 ****/
  6.  
  7.     public LotteryNumbers() {
  8.         // Draw numbers as LotteryNumbers is created
  9.         this.drawNumbers();                              /*****DOMANDA 1 ******/
  10.     }
  11.    
  12.    
  13.     public ArrayList<Integer> numbers() {
  14.         return this.numbers;
  15.     }
  16.  
  17.     public void drawNumbers() {
  18.         // We'll format a list for the numbers
  19.      
  20.         Random random = new Random () ;
  21.         //this.numbers = new ArrayList<Integer>();         /**** DOMANDA 2 ******/
  22.        
  23.         // Write the number drawing here using the method containsNumber()
  24.         for (int i = 0 ; i < 7 ; i++ ) {
  25.             int num = random.nextInt(39)+1; //int random 1-39
  26.             if(!containsNumber(num))
  27.             numbers.add(num) ;
  28.             else
  29.                 i--;
  30.         }
  31.     }
  32.  
  33.     public boolean containsNumber(int number) {
  34.         // Test here if the number is already in the drawn numbers
  35.         return this.numbers.contains(number) ;
  36.     }
  37.    
  38.     @Override
  39.     public String toString() {
  40.        
  41.         String numbersString = "Lottery numbers:\n- ";
  42.         for ( int num : numbers) {
  43.             numbersString += num +" - " ;
  44.         }
  45.         return numbersString  ;
  46.        
  47.     }
  48. }
  49.  
  50. //******************************* MAIN **********************************
  51.  
  52. import java.util.ArrayList;
  53.  
  54. public class Main {
  55.  
  56.     public static void main(String[] args) {
  57.         LotteryNumbers lotteryNumbers = new LotteryNumbers();
  58.         //ArrayList<Integer> numbers = lotteryNumbers.numbers();
  59.  
  60.         //System.out.println("Lottery numbers:");
  61.         /*for (int number : numbers) {
  62.             System.out.print(number + " ");
  63.         }
  64.         System.out.println("");*/
  65.         System.out.println(lotteryNumbers);
  66.     }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top