Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help me finish my code: Please read my //comments to understand what I need: Obj

ID: 3681200 • Letter: H

Question

Help me finish my code:

Please read my //comments to understand what I need:

Objective of the code

1000
ABBOTT 588
ACEVEDO 824
ACOSTA 377
ADAMS 39

I finished the main method, so no need to change anything:

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);
      
       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();
       }
      
   }
}

I need to finish two more classes to make it work

package NameLookup;

// You need to complete the constructor and the getCount method in this file

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Surnames {
   SurnameInfo [] surnames;
  
   // 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
   {
  
   }
  
   // This method should return the number of surname/rank pairs in the array.
   public int getCount ()
   {
      
   }
  
   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;
   }
  
}

Another class

// 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) {
  
}

}

The program is designed to allow users to see the popularity rank of surnames (i.e., lastnames). It reads in a file that contains, in alphabetical order, a list of names and their popularity rank. Here is what the first few lines of the file look like:

1000
ABBOTT 588
ACEVEDO 824
ACOSTA 377
ADAMS 39


The first line contains the number of name/rank pairs in the file, which is followed by that many name/rank pairs.

The main class is called NameLookup, and it handles all the interaction with the user. You don't have to modify anything in this file. It asks the user for the name of the data file and then creates a Surnames object, passing it that filename. It then repeatedly asks the user for a name and displays that name's rank. If the name is not found it prints out a message to that effect. You can see this in action in the first screenshot below.

The name/rank pairs are stored as SurnameInfo objects. This is a simple object that just stores that info and has getters and setters for name and rank.

The middle class is called Surnames and it stores all of the SurnameInfo objects in an array. It also has a method that returns the rank of a name if it exists and -1 otherwise. It also has a method that returns the number of SurnameInfo objects stored.

Explanation / Answer

Hi, Please find below updated classes as per your requirement.

Surnames.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Surnames {
SurnameInfo [] surnames;
  
// 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, Exception
{
//Created reader object for reading the contents from file.   

BufferedReader br = new BufferedReader(new FileReader(fileName));

//Fetching first line record from file which indicates number of ranks.
int n = Integer.parseInt(br .readLine());

//Initialised the surnames array with size (number of records).
surnames = new SurnameInfo[n];

//String object for holding each line while fetching from file.
String line;

// Each line holds two values one is Student name and second is Rank. This array will hold both values while fetching each line from file.
String values[];

//indicates current record number
int count = 0;

//loop for iterating for fetching each line from file.
while ((line = br.readLine()) != null) {

//Splitting the line by using space delimiter. \s+ indicates space here.
values = line.split("\s+");

//calling SurnameInfo constructor for setting name and rank for each student.

//values[0] indicates student name string, values[1] indicates Rank integer. since values[1] is string object thats why converted from string to int by using Integer.parseInt method.
SurnameInfo si = new SurnameInfo(values[0], Integer.parseInt(values[1]));

//Each SurnameInfo object si storing in array.
surnames[count] = si;

// increasing count value for getting next record number.
count++;
}

}

// 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;
}
  
}

SurnameInfo.java

public class SurnameInfo {
String name;
int rank;
  
public SurnameInfo (String n, int r) {
this.name = n;
this.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;
   }
}

Output:

Welcome to the Surname/Rank Lookup Program.
Please enter name of the surname data file: D:\myfile.txt

Please enter a surname (or "quit" to halt processing): ACEVEDO

The rank for ACEVEDO is 824

Please enter the next student name (or "quit" to halt processing): dasd
That surname is not in the top 4. Sorry!

Please enter the next student name (or "quit" to halt processing): quit