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 11 of 11

Thread: Having trouble understanding Int Logs.

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble understanding Int Logs.

    I'm currently trying to finish an assignment but having trouble conceptualizing what I'm trying to achieve.

    The assignment is as follows:

    Create an example/application called TestLuck that repeatedly generates random 
    numbers between 1 and 100 until it generates the same numbers twice. The program 
    should report how many numbers it had to generate before it matched a 
    previously generated number. Your application should store generated numbers in an
     ArrayIntLog and use its contains method to test for matches.


    Here is my code so far:


    import java.util.Random;
     
    public class TestLuck{
     
    	public static void main(String[] args){
     
    		ArrayIntLog log = new ArrayIntLog("Randomly Generated Numbers");
     
     
    		Random number = new Random();
     
     
     
    		for(int i =0; i<100;i++){
     
     
    		int random = number.nextInt(1000)+1;
    		log.insert(random);
    		}
     
     
    	}
    }


     
    //---------------------------------------------------------------------
    // Interface for a class that implements a log of Strings.
    // A log "remembers" the elements placed into it.
    //
    // A log must have a "name".
    //---------------------------------------------------------------------
     
    public interface IntLogInterface{
     
    	public void insert(int element);
    	// Precondition:   This StringLog is not full.
      	// 
        // Places element into this StringLog.
     
        public boolean isFull();
      // Returns true if this StringLog is full, otherwise returns false.
     
      public int size();
      // Returns the number of Strings in this StringLog.
     
      public boolean contains(int element);
      // Returns true if element is in this StringLog, 
      // otherwise returns false.
      // Ignores case differences when doing string comparison.
     
      public void clear();
      // Makes this StringLog empty.
     
      public String getName();
      // Returns the name of this StringLog.
     
      public String toString();
      // Returns a nicely formatted string representing this StringLog. 
    }

    public class ArrayIntLog implements IntLogInterface{
     
     
      protected String name;              // name of this log
      protected int[] log;             // array that holds log ints
      protected int lastIndex = -1;       // index of last string in array 
     
    public ArrayIntLog(String name, int maxSize)
    {
      log = new int[maxSize];
      this.name = name;
    }
     
    public ArrayIntLog(String name) 
    {
      log = new int[100];
      this.name = name;
    }
     
    public void insert(int element)
    {      
      lastIndex++;
      log[lastIndex] = element;
    } 
     
    public void clear()
    {   
     for (int i = 0; i <= lastIndex; i++)
    log[i] = 0;
    lastIndex = -1;
    }
     
    public boolean isFull()
    {              
      if (lastIndex == (log.length - 1)) 
        return true;
      else
        return false;
    }
     
    public int size()
    {
      return (lastIndex + 1);
    }
     
    public String getName()
    {
      return name;
    }
     
    public boolean contains(int element)
    {                 
      int location = 0;
      while (location <= lastIndex) 
      {
        if (element == (log[location])) //if they match
          return true;
        else
          location++;
      }
      return false;
    }
     
    public String toString()
    {
      String logInt = "Log: " + name + "\n\n";
      for (int i = 0; i <= lastIndex; i++)
        logInt = logInt + (i+1) + ". " + log[i] + "\n";
      return logInt;
    }
     
    }

    I understand what I did first was name the log. Then I generated 100 random numbers between 1 and 1000 and inserted them into the log array. What I think I'm trying to do is take the number that was just randomly generated and compare it to every number in that log that already exists. I'm not sure if that's the correct way to think. Any tips would be greatly appreciated!
    Last edited by orbin; October 8th, 2012 at 09:44 PM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Having trouble understanding Int Logs.

    Just curious but I think your assignment says numbers between 1-100, not 1-1000, unless it was a typo.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Having trouble understanding Int Logs.

    What I think I'm trying to do is take the number that was just randomly generated and compare it to every number in that log that already exists
    Yes, that's exactly what you are supposed to do. And if it does already exist you can stop generating more numbers because you know at that point how many numbers it took until you got a duplicate (it's more or less i).

    So write some code in the for loop of main() to do that. Although the question says to do this repeatedly, it's a good idea to have it function once correctly before you work on doing the operation repeatedly.

    It might help to observe that the log thing does have a method to assist you in finding if a number already exists.

  4. #4
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble understanding Int Logs.

    Quote Originally Posted by pbrockway2 View Post
    Yes, that's exactly what you are supposed to do. And if it does already exist you can stop generating more numbers because you know at that point how many numbers it took until you got a duplicate (it's more or less i).

    So write some code in the for loop of main() to do that. Although the question says to do this repeatedly, it's a good idea to have it function once correctly before you work on doing the operation repeatedly.

    It might help to observe that the log thing does have a method to assist you in finding if a number already exists.
    Thank you for replying. I understand that I can use the contain method, but I'm still having trouble understanding finding a duplicate. If I use the contain method, it goes through the whole array, and if it finds ONE number that matches the number I chose for contain, it returns true. I'm still having trouble understanding it so that if I do the contain method it goes through the whole array regardless of how many times it finds the number as long as it finds it once. So do I need a counter of how many times it finds that number? And then use the counter to determine if it found the duplicate if it's 2 or more?

  5. #5
    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: Having trouble understanding Int Logs.

    You just need to find the first duplicate. The only counter you should need is already a part of the ArrayIntLog class (the last index or size of the log). So generate a random number and see if the log already has that number. If it does, you're done. Otherwise, add it to your log and repeat the process.

  6. #6
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble understanding Int Logs.

    Quote Originally Posted by helloworld922 View Post
    You just need to find the first duplicate. The only counter you should need is already a part of the ArrayIntLog class (the last index or size of the log). So generate a random number and see if the log already has that number. If it does, you're done. Otherwise, add it to your log and repeat the process.
    I think might be overthinking this but the way I keep looking at it is...let's say I use log.insert(randomnumber); and then use the contain method to search for 433(just a random number I picked between 1-1000). I do log.contain(433); So then it searches the array(which only has one randomly generated number so far) and finds it doesn't contain that number in the array. Okay, then it adds another randomly generated number by doing the insert method and rechecks the array for 433 all over again. If it finds 433, then it returns a true. If not, it continues following the same pattern. The trouble I'm having is how to keep track that it had to find the number twice in the int log? The index part is easier since I know the last one that it finds has to be the position, but I'm not understanding the first number, then finding the duplicate.

    EDIT: This is what I have so far:

    import java.util.Random;
     
    public class TestLuck{
     
    	public static void main(String[] args){
     
    		ArrayIntLog log = new ArrayIntLog("Randomly Generated Numbers");
     
    		boolean flag = false;
     
    		Random number = new Random();
    		int numCheck =22;
     
    		for(int i =0; i<100; i++){
     
    		int randomNum = number.nextInt(100)+1;
     
    		flag = log.contains(numCheck);
     
    		if(flag == true){
    			log.insert(randomNum);
     
    			flag= log.contains(numCheck);
     
    			if(flag == true)
    			{
    			System.out.println(i);
    			break;
    			}
    			else{
    				continue;
    			}
    		}
    		else{
    			log.insert(randomNum);
    			flag = false;
     
    		}
    		}
     
    	}
    }

    I changed the number generation to between 1 and 100 because it was taking forever to find if it really found the correct/duplicate number.
    Last edited by orbin; October 9th, 2012 at 03:10 PM.

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Having trouble understanding Int Logs.

    Quote Originally Posted by orbin View Post
    I think might be overthinking this but the way I keep looking at it is...let's say I use log.insert(randomnumber); and then use the contain method to search for 433(...
    Ummm...How would things work out if you tested to see whether the log contains the currently generated number before you inserted the new one? Isn't that what helloworld922 suggested a couple of posts ago?

    Just a thought.


    Cheers!

    Z
    Last edited by Zaphod_b; October 9th, 2012 at 02:58 PM.

  8. #8
    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: Having trouble understanding Int Logs.

    Before you add a new number you know there are no duplicates in your log. If there were, you would have terminated your loop already.

  9. #9
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble understanding Int Logs.

    I think I had a breakthrough, but it frustrated me! I was thinking about it all wrong looking back. This is what I have so far.


     
    import java.util.Random;
     
    public class TestLuck{
     
    	public static void main(String[] args){
     
    		ArrayIntLog log = new ArrayIntLog("Randomly Generated Numbers");
     
    		boolean flag = false;
     
    		Random number = new Random();
     
    		for(int i =0; i<100; i++){
     
    		int randomNum = number.nextInt(1000)+1;
    		flag = log.contains(randomNum);
     
    		if(flag == true){
    			System.out.println("The program had to generate " + (i-1) + " random numbers before it found the duplicate.");
                            break;
    		}
     
    		else{
    			log.insert(randomNum);
    		}
     
    		}
     
    	}
    }
    Last edited by orbin; October 10th, 2012 at 12:11 PM.

  10. #10
    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: Having trouble understanding Int Logs.

    Does it do what you want it to? I would read the instructions again to make sure you're program does what was asked for, in particular:

    Create an example/application called TestLuck that repeatedly generates random numbers between 1 and 100 until it generates the same numbers twice. The program should report how many numbers it had to generate before it matched a previously generated number. Your application should store generated numbers in an ArrayIntLog and use its contains method to test for matches.

  11. #11
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble understanding Int Logs.

    Quote Originally Posted by helloworld922 View Post
    Does it do what you want it to? I would read the instructions again to make sure you're program does what was asked for, in particular:
    I forgot to correct that. I had to type it out since it was in the book, and it was meant to be 1 and 1000. (It originally said 1 and 10000...but the teacher said to just use 1000.) Also, there is another part to where it says run the program 100 times, and record the results and record what happens. I'm not sure what's supposed to be happening because it almost never finds a duplicate. Also, for my "for" loop which I chose to run 100 times, if it doesn't specify how many times you need to generate a random number, you're essentially doing it how many times you choose, correct?

    Thank you for your help! Helped me quite a bit once it really entered my brain. I was making a simple problem a lot more complicated....like usual.

Similar Threads

  1. Inheritance trouble understanding how to use it?
    By orbin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2012, 11:24 AM
  2. Replies: 3
    Last Post: July 12th, 2012, 07:11 AM
  3. Replies: 3
    Last Post: July 8th, 2012, 03:44 PM
  4. Having trouble understanding what teacher said for build Tree.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 16th, 2010, 08:22 PM
  5. Unable to redirect all the eclipse console logs to buffer
    By skrishnapradeep in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 23rd, 2010, 11:03 PM