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!