Comparing elements from an object
Hi, I am writing a program that constructs a lottery ticket with 6 random numbers and compares the elements to another lottery ticket. I am having troubles comparing the two lottery tickets. The LotteryDraw Class calls the LotteryNumbers class. I have completed most of the code except for the hasNum() method. I am having troubles comparing a certain number to the contents of an object. Any help is greatly appreciated. Thanks :)-:
This is the Lotterdraw class that calls LotteryNumbers
Code:
public class LotteryDraw
{
public static void main(String[] args)
{
int numDraws = 10;
LotteryNumbers myTicket = new LotteryNumbers();
System.out.println("My lucky ticket is:");
System.out.println(myTicket);
if (args.length != 0)
{
numDraws = Integer.parseInt(args[0]);
}
System.out.print("Performing " + numDraws);
System.out.println(" draws. Good luck!");
for (int i=0;i<numDraws;++i)
{
LotteryNumbers winner = new LotteryNumbers();
if (myTicket.matches(winner) == true)
{
System.out.println("YOU WIN!!!!");
System.out.println("Time to quit school.");
return;
}
else
{
System.out.println("You didn't match " + winner.toString());
}
}
}
}
Here is the LotteryNumbers class
Code:
/**
* LotteryNumbers.java
*
* A simple representation of a set of numbers used to
* simulate a lottery.
*
* In this lottery, there are six numbers. Each number
* must be unique and between 1 and 49 inclusive.
*/
import java.util.Random;
import java.util.Arrays;
public class LotteryNumbers
{
public static final int Total_Numbers = 6;
public static final int Max_Number = 49;
int [] WinTicket = new int [Total_Numbers];
/**
* This constructor creates the object and
* initializes the state associated with the object`
*/
public LotteryNumbers()
{
generate();
}
/**
* This method returns true if all the numbers
* in this LotteryNumber match the numbers
* in other. Otherwise, it returns false.
*/
public boolean matches (LotteryNumbers other)
{
int num = 0;
int i = 0;
while(i<Total_Numbers){
num = this.WinTicket[i];
if((hasNumber(num, other))== true){
i++;
}
else{
return false;
}
}
return true;
}
/**
* Return a textual representation of the LotteryNumbers
* in the form {1,2,3,4,5,6}
*/
public String toString()
{
StringBuffer s = new StringBuffer();
s.append("{" +this.WinTicket[0]);
for(int i=1; i<Total_Numbers; i++){
s.append(", " + this.WinTicket[i]);
}
s.append("}");
return s.toString();
}
/**
* Generate the random numbers for this LotteryNumber
* object. No duplicates are allowed. After generation
* the numbers should be sorted in ascending order.
*/
public void generate()
{
Random r = new Random();
for (int i=0;i<Total_Numbers;i++)
{
this.WinTicket[i] = r.nextInt(Max_Number)+1;
}
Arrays.sort(this.WinTicket);
for(int i=0;i<Total_Numbers;i++)
{
check(i);
}
Arrays.sort(this.WinTicket);
}
/**
*Checks the lottery ticket for doubles and picks a different
*random number if there is.
*/
public void check(int i)
{
Random r = new Random();
int j = 0;
while(j < i){
if (this.WinTicket[i] == this.WinTicket[j]){
this.WinTicket[i] = r.nextInt(Max_Number)+1;
}else
j++;
}
}
/**
* Returns true if num is one of the numbers in
* this object, false if it isn't.
*/
private boolean hasNumber (int num, LotteryNumbers other)
{
int i =0;
while(i<Total_Numbers){
if(num == other[i]){
return true;
}else{
i++;
}
return false;
}
}
}
Re: Comparing elements from an object
When you have a compilation error you really should give us the error text.
Code:
if(num == other[i]){
"other" is not an array. It's a LotteryNumbers object. It happens to contain an array.
So that should be other.WinTicket[i].
Your variable naming is a bit wrong, that should be winTicket.
And the "static final" ones should (generally) be all upper case.
Thumbs up for good indentation, though spaces are generally better than tabs.
Oh yes, you also want to return false after the loop, not in it.
ETA: Actually, some of the indenting is a bit shot, but at least I can (largely) follow the flow...which can be a novelty around here.
Re: Comparing elements from an object
Thanks for all of the helpful tips! I will remember that for next time! Thank you soo much!