Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: lucky draw.. (pls help)

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default lucky draw.. (pls help)

    thanks
    Attached Files Attached Files
    Last edited by amin; October 20th, 2009 at 12:32 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: lucky draw.. (pls help)

    Hello amin,

    Welcome to the Java Programming Forums.

    There is no code attached?

    Is there a lot of code? If there isn't that much, please post your code within the code tags. (check my sig)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Re: lucky draw.. (pls help)

    here u go.. pls reply asap..

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: lucky draw.. (pls help)

    1st question: are you allowed to create other classes? That would make this task a lot easier.

    public class Contestant
    {
         String id;
         String name;
         String phoneNo;
         String email;
         String address;
         int drawNumber;
     
         public Contestant(String id, String name, String phoneNo, String email, String address, int drawNumber)
         {
              this.id = id;
              this.name = name;
              this.phoneNo = phoneNo;
              this.email = email;
              this.address = address;
              this.drawNumber = drawNumber;
         }
     
         public void printOutInfo()
         {
              System.out.println("ID: " + this.id);
              System.out.println("Name: " + this.name);
              System.out.println("phone number: " + phoneNo);
              System.out.println("email: " + email);
              System.out.println("address: " + address);
              System.out.println("draw number: " + drawNumber);
         }
     
         public int getDrawNumber()
         {
              return this.drawNumber();
         }
     
     
         public String getName()
         {
              return this.name();
         }
    }

    Then, in your main class create an array of Contestants. There are 2 options for generating random numbers: the Random class, or using Math.random(). Personally, I prefer using Math.random(), but here i'll show you the Random class because it should be easier.

    Random generator = new Random();
    generator.nextInt(upperLimit);

    I'm not sure what the range on the draw number are, but just plug that into the upperLimit.

    Then to find your winner, just loop through all the contestants, looking for a contestant that has a matching drawNumber using the getDrawNumber method I had above.

  5. #5
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lucky draw.. (pls help)

    Sorry.. im not allowed 2 use classes since i also need to xplain the flow of the program n even im also new to this language :s

  6. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    1. done > user enter the registration of participation (ID, full name, telephone number, email address, lucky draw number)

    2. done > user the number of winning numbers.

    3. done > generate random number from within the specified range

    4. done > display the winning number and the winner’s name.

    5. not done > generate random number until all the winning numbers are generated. and the generated number must match with the participant lucky draw number.

    help me with this..

    import java.util.*;
    import java.io.*;
     
    public class luck
    {
    	public static void main(String[] args)
    		{
    		Scanner input = new Scanner(System.in);
     
    		System.out.println(" ");
    		System.out.println("-------------------------- WELCOME TO LUCKYDRAW EVENT --------------------------");
    		System.out.println(" ");
    		System.out.print("Please enter number of participants for lucky draw event: ");
    		int numberOfParticipants = input.nextInt();
    		System.out.print("Please enter number of winners for lucky draw event: ");
    		int numberOfWinners = input.nextInt();
    		System.out.println("");
     
    		String[] luckyDrawId = new String[numberOfParticipants];
    		String[] luckyDrawName = new String[numberOfParticipants];
    		int[] luckyDrawPhoneNo = new int[numberOfParticipants];
    		String[] luckyDrawEmail = new String[numberOfParticipants];
    		String[] luckyDrawAddress = new String[numberOfParticipants];
    		int[] luckyDrawNumber = new int[numberOfParticipants];
    		String information = " ";
     
    		System.out.println("-------------------------- PARTICIPANT REGISTRATION ---------------------------");
     
    		for (int i = 0; i < luckyDrawNumber.length; i++)
    			{
    			/*System.out.println(" ");
    			System.out.print("Please enter lucky draw number's Id: ");
    			luckyDrawId[i] = input.next();*/
    			System.out.print("Please enter lucky draw number's name: ");
    			luckyDrawName[i] = input.next();
    			/*System.out.print("Please enter lucky draw number's Phone No: ");
    			luckyDrawPhoneNo[i] = input.nextInt();
    			System.out.print("Please enter lucky draw number's Email: ");
    			luckyDrawEmail[i] = input.next();
    			System.out.print("Please enter lucky draw number's Address: ");
    			luckyDrawAddress[i] = input.next();*/
    			System.out.print("Please enter lucky draw's number: ");
    			luckyDrawNumber[i] = input.nextInt();
    			System.out.println(" ");
    			}
     
    		System.out.println(" ");
    		System.out.println("---------------------- RANGE OF GENERATING RANDOM NUMBER ----------------------- ");
     
    		System.out.print("enter maximum number for generating random number: ");		
    		int max = input.nextInt();
     
    		int[] winningNum = new int[numberOfWinners];
    		for(int i=0;i<numberOfWinners;++i)
    			{
    			winningNum[i] = (int) (Math.random()*max);
    			for(int j=0;j<i;++j)
    				{
    				if(winningNum[i] == winningNum[j])
    				++i;
    				}
    			}
    		for (int i = 0; i < winningNum.length; i++)
    			{
    			if (winningNum[i] == luckyDrawNumber[i])
    				{
    				System.out.println("congratulations!! the winner is " + luckyDrawName[i] );
    				System.out.println("congratulations!! the random lucky number is " + winningNum[i] );
     
    				}
    			else 
    				{
    				System.out.println("our random number is " + winningNum[i] + " does not match with any participant lucky draw number " +luckyDrawNumber[i]);
    				}
    			}
    		}
    }

    my time is ticking
    Last edited by helloworld922; October 20th, 2009 at 03:13 PM. Reason: Please don't post two topics with the same content.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: lucky draw.. (pls help)

    Please don't make multiple posts with the same question...

    Since you said you're not allowed to use other objects, use a for loop:

    for (int i = 0; i < id.length;i++)
    {
        System.out.println("ID: " + id[i]);
              System.out.println("Name: " + name[i]);
              System.out.println("phone number: " + phoneNo[i]);
              System.out.println("email: " + email[i]);
              System.out.println("address: " + address[i]);
              System.out.println("draw number: " + drawNumber[i]);
    }

    To find a matching draw number:
    int matchIndex = 0;
    for (int i = 0; i < drawNumber.length; i++)
    {
         if (drawNumber[i] == numberToMatchTo)
         {
              matchIndex = i;
              break;
         }
    }
    // matchIndex contains the index to the info of the winner

  8. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lucky draw.. (pls help)

    can u help me check my new codes before ur post.. i got problems in that codes.. including the comparing.. *comparing generated random number n participant random number. sorry if i've make trouble.. :S ned this in 5 hrs.. below are the requirement that i aactually needed:

    •Participants registration
    This function allows the registration of participation (ID, full name, telephone number, email address, lucky draw number) into the Lucky Draw

    •Lucky Draw gnerator
    As a start, this part of the system should accept a range of lucky draw numbers and the number of winning numbers. Then upon user command, it should be able to automatically generate random number from within the specified range and then display the winning number and the winner’s name. This process will be repeated until all the required winning numbers are generated.

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: lucky draw.. (pls help)

    You have to compare each random number with every contestant.

    for (int i = 0; i < winningNum.length; i++)
    {
    	for (int j = 0; j < luckyDrawNumber.length; j++)
    	{
    		if (winningNum[i] == luckyDrawNumber[i])
    		{
    			System.out.println("congratulations!! luckyDrawName[i] + " has one with a luckyDraw number of " + winningNum[i]);
    		}
    	}
    }

  10. #10
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lucky draw.. (pls help)

    its still doesnt work.. can u help & show to me. either

    1. to loop the generating random number until te generated number is matched with the participant luckydraw number *luckyDrawNumber[]*

    or 2. generate random number then it check the lucky number is the same or not. if it is the same, it will decrement number of winner. it is not the same then number of winner wont decrement. number of winner will only decrement if the generated number is the same with the participant lucky draw number. pls quickly reply me

  11. #11
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lucky draw.. (pls help)

    correction:
    repeatition-
    1. repeatitiom
    generate random number then it check the lucky number if both have the same number or not.

    2. condition-
    2.1.
    if it is the same, it will decrement number of winner.
    2.2.
    it is not the same then number of winner wont decrement. number of winner will only decrement if the generated number is the same with the participant lucky draw number.

    3. repeatition-
    3.repeat the process 1 and 2 until number of winner become 0 and display the winner.

    pls quickly reply me ASAP

  12. #12
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lucky draw.. (pls help)

    heloo.. ??

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: lucky draw.. (pls help)

    Hmm, well instead of generating luck-draw numbers, you can generate random indices, and add that to the list of winners. Then you just need to make sure you don't pick the same person to be the winner multiple times. This is kind of like making the computer shuffle the deck: instead of shuffling all the cards and picking from the top, simply have the computer draw out a random card.

Similar Threads

  1. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM
  2. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM