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

Help me finish my code: PS- I\'m taking the introduction course for CSC so pleas

ID: 3682436 • Letter: H

Question

Help me finish my code:

PS- I'm taking the introduction course for CSC so please do NOT use complicate code (E.g DO NOT USE try catch) nor import anything else--use just what I posted here. and if you can follow the //instructions, I'd appreciate it. if you could add your own //comments I would also appreciate it. Thank you so much.

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

Objective of the code 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.

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

//Main method: no need to change anything here

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

//class surname please help me finish this

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

//class surnameinfo I believe this class is finished, but please look to see if it works

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 (in rank)
{
this.rank = rank;
}
  
}

//Main method: no need to change anything here

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

//class surname please help me finish this

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

//class surnameinfo I believe this class is finished, but please look to see if it works

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 (in rank)
{
this.rank = rank;
}
  
}

Explanation / Answer

NameLookup.java

package NameLookup;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Scanner;

public class NameLookup {

  

public static void main (String [] args) throws IOException{

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

}

  

}

}

Surname.java

package NameLookup;

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

import java.io.*;

import java.util.Scanner;

public class Surnames {

   SurnameInfo[] surnames; // attribute

// 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 IOException {

       //using bufferedreader to read file line by line

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

       String line;

       // reading the first line which gives the count of Surname/rank pairs

       line = br.readLine();

       // converting count to integer

       int count = Integer.parseInt(line);

       // creating an array of SurnameInfo's

       surnames = new SurnameInfo[count];

       String params[];

       String name;

       int rank;

       for (int i = 0; i < count; ++i) {

           line = br.readLine();

           //splitting each line by space so that we can get the name and rank separately

           params = line.split(" ");

           name = params[0];

           rank = Integer.parseInt(params[1]);

           //creating a SurnameInfo object and adding it to the array

           surnames[i] = new SurnameInfo(name, rank);

       }

   }

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

   }

}

Output

Welcome to the Surname/Rank Lookup Program.

Please enter name of the surname data file: name.txt

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

The rank for ABBOTT is 588

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

That surname is not in the top 4. Sorry!

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