Password search
Start with this zip.
Do not use the code at the bottom.
For every method, start the method with a call to startClock()
and finish with stopClock()
- public boolean findSalary(int salary) that will return true
if salary is located or false if is not there
- public boolean findSalary(int[] salariesToCheck) that will print
out all salariesToCheck of array in it and return true if any
of the salaries are in it
- to test say .
- ArrayActivities a = new ArrayActivities();
int[] sal={2342333,3089780,3987232,3423223,2345333,
4788385,3423432,1369298,1914420};
a.findSalary(sal);
Now, lets first sort the array, for now there is a static method
that you can use:
Arrays.sort(allSalary); After you sort, I want you to write a method:
- public boolean findFast(int salary) that will print out the
salary if its found and then return true;
- public boolean findFast(int[] salariesToCheck) that will print
out all the the salary it finds and then return true;
- to test say .
- ArrayActivities a = new ArrayActivities();
int[] sal={2342333,3089780,3987232,3423223,2345333,
4788385,3423432,1369298,1914420};
a.findFast(sal)
Now the passwords, here
are the sorted and unsorted lists. Write these methods.:
- public int findHowMany (String yourPassword) that will return
how many of the list are yourPassword
- public String[] findTop(int howMany) this will return an array
of the all passwords that have been used howMany times
- public void findAllDerivatives(String word) that will print
all the passwords that contain word and how many of each there
are. So if they put hello; it might say helloDude - 4 and so on.
Start with the code below:
/*
* This class will be used to work with searching salaries
* @author Jeff Borland(change)
* @date 3-26-06
*/
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class ArrayActivities
{
private String[] allPasswords = new String[200000];
/*
* The constructor below will automatically read in all of the passwords
*/
public ArrayActivities()
{
Scanner scan = null;
//To reset your scanner:
try
{
scan=new Scanner(new File("outSorted.txt"));
}
catch (IOException e){}
int i=0;
while (scan.hasNext() && i<200000)
{
String theLine=scan.nextLine(); //This reads in the next password
allPasswords[i]=theLine;
i++;
}
}
public int find(String x)
{
for (int i=0;iallPasswords[middle])
low=middle+1;
//else
// return middle;
middle=(high+low)/2;
}
return -1;
}
public int findHowMany (String yourPassword)
{
return 0;
}
public String[] findTop(int howMany)
{
return new String[5];
}
public void findAllDerivatives(String word)
{
}
public static void main(String[] args) {
ArrayActivities a=new ArrayActivities();
System.out.println("dude00 has been used "+a.findHowMany("dude00"));
System.out.println("Listing all passwords that have been used atleast 100 times:");
for (String pw:a.findTop(100))
{
System.out.println(pw + " - "+a.findHowMany(pw)+"times");
}
a.findAllDerivatives("dude");
}
}
/*
* This class will be used to work with searching salaries
* @author Jeff Borland(change)
* @date 3-26-06
*/
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class StringArraySearch
{
String[] allPasswords = new String [200000];
Scanner scan;
private long start;
private long finish;
/*
* The constructor below will automatically read in all of the Baseball player salaries
*/
public StringArraySearch()
{
startClock();
reset(); //resets the scanner to the beginning of the file
int i=0;
while (scan.hasNext() && i<200000)
{
String theLine=scan.nextLine(); //This reads in the next player line
allPasswords[i]=theLine;
i++;
if (i%1000==0)
System.out.println(i);
}
stopClock();
startClock();
stopClock();
}
public void startClock()
{
start = System.currentTimeMillis();
}
public void stopClock()
{
finish= System.currentTimeMillis();
System.out.println( "This operation took " + (finish-start) + " milliseconds.");
}
//Conrad cleverly put this code in a method so that we can reset quickly.
//He also appropriately made it private
private void reset()
{
//To reset your scanner:
try
{
scan=new Scanner(new File("smallerPasswords.txt"));
}
catch (IOException e){}
}
public static void main(String[] args) {
StringArraySearch a=new StringArraySearch();
}
}
|