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:
Code :
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:
Code :
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);
}
}
}
Code :
//---------------------------------------------------------------------
// 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.
}
Code :
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!
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.
Re: Having trouble understanding Int Logs.
Quote:
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.
Re: Having trouble understanding Int Logs.
Quote:
Originally Posted by
pbrockway2
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?
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.
Re: Having trouble understanding Int Logs.
Quote:
Originally Posted by
helloworld922
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:
Code :
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.
Re: Having trouble understanding Int Logs.
Quote:
Originally Posted by
orbin
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
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.
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.
Code :
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);
}
}
}
}
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:
Quote:
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.
Re: Having trouble understanding Int Logs.
Quote:
Originally Posted by
helloworld922
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. :P