Why Choose Us?
0% AI Guarantee
Human-written only.
24/7 Support
Anytime, anywhere.
Plagiarism Free
100% Original.
Expert Tutors
Masters & PhDs.
100% Confidential
Your privacy matters.
On-Time Delivery
Never miss a deadline.
The Lottery Problem: A lottery requires that you select six different numbers from the integers 1 to 49
The Lottery Problem:
A lottery requires that you select six different numbers from the integers 1 to 49. Write a Java program that will do this for you and generate five sets of six numbers as a result.
For generating random numbers you can use the random() static method of class Math. It returns a double so you will need to cast it as an integer. If you set the range to 49, you can generate a random number between 1 and 49 through:
number = (int) ( range * Math.random() ) + 1;
Note that you need 5 sets of numbers and in each set you have should have six different numbers. There should not be duplicate numbers within each set. Of course the same number can occur in multiple sets, but within the same set of 6 numbers it should only occur once, if at all.
Here is an example of a valid set of numbers: 5, 41, 3, 9, 22, 30
Here is an example of an invalid set of numbers: 15, 8, 19, 33, 8, 21
It is invalid because the number 8 appears twice.
Expert Solution
I have coded a very compact yet elegant code by exploiting the object oriented functionality of Java. The code is a like a story, it is full of comments that will tell you how things are happening as well as why.
I have attached both the .java and .class files so that you can check it immediately.
/**
*
* User: Abdun Mahmood, OTA 103644
* Date: Dec 9, 2006
* Time: 11:38:56 AM
* All Rights Reserved. 2006. Permission granted to reproduce in part or full, provided a mention of credits
*/
public class randomNumberSets {
public int [] numSet; // the set of random numbers to be generated
public static final int RANGE = 49; //range of acceptable integer values
public static final int setSize = 6; //Number of elements in each set
randomNumberSets(){
//The constructor. Please note bulk of the work of generating random numbers take place here
int intRandom; //the temporary random number
numSet = new int[setSize]; //Dynamically allocated array, allocating setSize number of element placeholderz
for (int i=0;i<numSet.length;i++){ //this for loop runs through each position and for each position generates a valid random number
do { //a do while loop that repeats generating a random number until it finds one not present in the array numSet
//generate a random integer
intRandom = (int) ( RANGE * Math.random() ) + 1; //generating random number
}while (hasDuplicate(numSet, intRandom, i) == true); //if this method returns true then intRandom already exists, so loop
numSet[i] = intRandom; //finally we have found a unique random number, so keep it location i
} //the for loop continues for each position and fills itself up with unique random numbers
}
public boolean hasDuplicate(int [] arr, int element, int lastPos){
//checks if element exists in arr, upto position lastPos(not the whole array to save time)
for (int i=0;i<lastPos;i++){
if (element == arr[i]) //if the element exists return true, else continue looking
return true;
}
return false; //we did not return yet, so there are no duplicates, return false
}
public void print(){ // a print method which prints the array
System.out.print("Set of Numbers: ");
for (int i=0;i<numSet.length;i++)
System.out.print(numSet[i]+" ");
System.out.println();
}
public static void main( String[] args) {
//To make our life easier, we initialize an array of randomNumberSets objects of size 5
randomNumberSets set[] = new randomNumberSets[5];
for (int i=0;i<set.length;i++){
//Each element of set[] is infact a set of random numbers, so initialize the set of numbers
set[i] = new randomNumberSets(); //numbers get generated
//and printed out
System.out.println("Set "+(i+1)+ " containing "+setSize+" random Non-duplicate numbers");
set[i].print();
}
}
}
PFA
Need this Answer?
This solution is not in the archive yet. Hire an expert to solve it for you.





