Results 1 to 3 of 3
  1. #1
    AnneS is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default 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


    Java 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

    Java 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; 
    		}
    	}
    }
    Last edited by DarrylBurke; 09-23-2011 at 07:40 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: Comparing elements from an object

    When you have a compilation error you really should give us the error text.
    Java 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.

  3. #3
    AnneS is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: Comparing elements from an object

    Thanks for all of the helpful tips! I will remember that for next time! Thank you soo much!

Similar Threads

  1. Comparing elements of an array
    By fluteattack in forum New To Java
    Replies: 1
    Last Post: 05-09-2011, 05:43 PM
  2. Searching and comparing Array elements
    By jmanswrd in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 06:06 AM
  3. Trouble comparing Elements while using Generics
    By ryuzog in forum New To Java
    Replies: 13
    Last Post: 12-16-2010, 09:26 AM
  4. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 03:01 PM
  5. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •