I have this program that look in a file txt that has names and the ranks of the
ID: 3682473 • Letter: I
Question
I have this program that look in a file txt that has names and the ranks of the names ( The top 1000 most popular names in america)
e.g first 10
1000
ABBOTT 588
ACEVEDO 824
ACOSTA 377
ADAMS 39
ADKINS 408
AGUILAR 213
AGUIRRE 508
ALEXANDER 111
ALI 876
I created a code that asks the user to type a name and the code returns the rank of the name.
package NameLookup;
// You don't have to change anything in this file
import java.io.FileNotFoundException;
import java.util.Scanner;
public class NameLookup {
public static void main (String [] args) throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the Surname/Rank Lookup Program.");
System.out.print("Please enter name of the surname data file: ");
String fileName = scan.nextLine();
Surnames theNames = new Surnames(fileName);//object created in the main method, then I can use the class surnames
System.out.print(" Please enter a surname (or "quit" to halt processing): ");
String name = scan.nextLine();
while (!name.equalsIgnoreCase("quit")) {
int rank = theNames.findRank(name);
if (rank == -1)
System.out.println("That surname is not in the top " + theNames.getCount() + ". Sorry!");
else
System.out.println("The rank for " + name + " is " + rank);
System.out.print(" Please enter the next student name (or "quit" to halt processing): ");
name = scan.nextLine();
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Surnames {
SurnameInfo [] surnames; //atributte //suranmeinfo consiste
// This constructor should open the file with the name passed to it and read the
// count. It should then create an array of that size which contains SurnameInfo
// objects. It should then read that many name/rank pairs and for each it
// should create a SurnameInfo object and store it in the array.
public Surnames (String fileName) throws FileNotFoundException
{
//i e uma int interator
Scanner scan = new Scanner(new File(fileName));
int count = scan.nextInt();
surnames= new SurnameInfo[count];
for (int i =0;i<count;i++){
String surname = scan.next();
int rank = scan.nextInt();
SurnameInfo data = new SurnameInfo (surname, rank);
surnames [i]= data;
}
}
// This method should return the number of surname/rank pairs in the array.
public int getCount ()
{
return surnames.length;
}
public int findRank (String name){
for (int i = 0; i < surnames.length; i++)
if (name.compareToIgnoreCase(surnames[i].getName()) == 0)
return surnames[i].getRank();
return -1;
}
}
package NameLookup;
// You need to complete the constructor and also add getter
// and setter methods to this file
public class SurnameInfo {
String name;
int rank;
public SurnameInfo (String n, int r)
{
name = n;
rank = r;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getRank()
{
return rank;
}
public void setRank (int rank)
{
this.rank = rank;
}
}
The output is :
Now I want to implent the following:
Part 2: Add a Counter
Modify the findRank method in the Surnames class so that while it is searching it counts the number of locations it examines. Before the method returns have it print out that count as in the screenshot below.
Part 3: Change the Search Method to Ordered Linear
Add a new method (don't delete the old one) that finds the rank for a name using the ordered linear search approach. Call it findRankBetter. Change the main method in the NameLookup class to call that method instead of findRank. Your results should be the same as in the screenshot below.
My professor wants me to use the following algorithm to make the changes;
But I dont understand how to implement this into my code. Please help.
e.g first 10
1000
ABBOTT 588
ACEVEDO 824
ACOSTA 377
ADAMS 39
ADKINS 408
AGUILAR 213
AGUIRRE 508
ALEXANDER 111
ALI 876
Explanation / Answer
//package NameLookup;
// You don't have to change anything in this file
import java.io.FileNotFoundException;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Collections;
public class NameLookup {
public static void main (String [] args) throws FileNotFoundException{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to the Surname/Rank Lookup Program.");
System.out.print("Please enter name of the surname data file: ");
String fileName = scan.nextLine();
Surnames theNames = new Surnames(fileName);//object created in the main method, then I can use the class surnames
System.out.print(" Please enter a surname (or "quit" to halt processing): ");
String name = scan.nextLine();
while (!name.equalsIgnoreCase("quit")) {
int rank = theNames.findRank(name);
if (rank == -1)
System.out.println("That surname is not in the top " + theNames.getCount() + ". Sorry!");
else
System.out.println("The rank for " + name + " is " + rank);
System.out.print(" Please enter the next student name (or "quit" to halt processing): ");
name = scan.nextLine();
}
}
}
class Surnames{
SurnameInfo [] surnames; //atributte //suranmeinfo consiste
// This constructor should open the file with the name passed to it and read the
// count. It should then create an array of that size which contains SurnameInfo
// objects. It should then read that many name/rank pairs and for each it
// should create a SurnameInfo object and store it in the array.
public Surnames (String fileName) throws FileNotFoundException
{
//i e uma int interator
Scanner scan = new Scanner(new File(fileName));
int count = scan.nextInt();
surnames= new SurnameInfo[count];
for (int i =0;i<count;i++){
String surname = scan.next();
int rank = scan.nextInt();
SurnameInfo data = new SurnameInfo (surname, rank);
surnames [i]= data;
}
}
// This method should return the number of surname/rank pairs in the array.
public int getCount ()
{
return surnames.length;
}
public int findRank (String name){
for (int i = 0; i < surnames.length; i++){
if (name.compareToIgnoreCase(surnames[i].getName()) == 0){
System.out.println("Number of locations searched to find this name: "+(i+1));
return surnames[i].getRank();
}
}
return -1;
}
public int findRankBetter(String name){
Collections.sort(surnames, new Comparator<SurnameInfo>(){
public int compare(SurnameInfo s1, SurnameInfo s2){
return s1.getName().compareTo(s2.getName());
}
});
int hi = surnames.length-1;
int low = 0, count=0;
while(hi >= low){
int mid = (hi + low) / 2;
count++;
if (name.compareToIgnoreCase(surnames[mid].getName()) == 0){
System.out.println("Number of locations searched to find this name: "+(count));
return surnames[mid].getRank();
}
else if (name.compareToIgnoreCase(surnames[mid].getName()) < 0){
hi = mid - 1;
}
else{
low = mid + 1;
}
}
return -1;
}
}
//package NameLookup;
// You need to complete the constructor and also add getter
// and setter methods to this file
class SurnameInfo/* implements Comparable<SurnameInfo>*/{
String name;
int rank;
public SurnameInfo (String n, int r)
{
name = n;
rank = r;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getRank()
{
return rank;
}
public void setRank (int rank)
{
this.rank = rank;
}
/*public int compareTo(SurnameInfo surnamesinfo){
return name.compareTo(surnamesinfo.name);
}*/
}