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

In JAVA Add the hashCode method to the FamousPerson class. Write an application

ID: 3820876 • Letter: I

Question

In JAVA

Add the hashCode method to the FamousPerson class.

Write an application that reads the input/FamousCS.txt file, creates a FamousPerson object for each line of the file, and generates and saves its associated hashCode() value in an array.

Output the values from the array in sorted order.

Replace each array value A with the value A % 1,000, and again output the values in the array in sorted order.

Replace each array value A with the value A % 100, and again output the values in the array in sorted order.

Replace each array value A with the value A % 10, and again output the values in the array in sorted order.

Write a report about your observations on the output.

Explanation / Answer


//---------------------------------------------------------------------------
// CSInfo.java by Dale/Joyce/Weems Chapter 5
//
// Reads information about famous computer scientists from the file
// FamousCS.txt. Allows user to enter names and provides them the information
// from the file that matches the name.
//---------------------------------------------------------------------------
//package ch05.apps;

import java.io.*;
import java.util.*;
//import ch05.collections.*;
//import support.FamousPerson;

public class CSInfo
{
public static void main(String[] args) throws IOException
{
// instantiate collection
// final int CAPACITY = 300;
ArrayList<Integer> hashcodes=new ArrayList<Integer>();

// set up file reading
FileReader fin = new FileReader("input/FamousCS.txt");
Scanner info = new Scanner(fin);
info.useDelimiter("[, ]"); // delimiters are commas, line feeds

Scanner scan = new Scanner(System.in);
FamousPerson person;
String fname, lname, fact;
int year;

// read the info from the file and put in collection
while (info.hasNext())
{
fname = info.next(); lname = info.next();
year = info.nextInt(); fact = info.next();
person = new FamousPerson(fname, lname, year, fact);
hashcodes.add(person.hashCode());
}

sortPrint(hashcodes);

for(int i=0;i<hashcodes.size();i++)
{
   hashcodes.set(i,hashcodes.get(i)%1000);
}
  

sortPrint(hashcodes);
  
for(int i=0;i<hashcodes.size();i++)
{
   hashcodes.set(i,hashcodes.get(i)%100);
}
  
sortPrint(hashcodes);
for(int i=0;i<hashcodes.size();i++)
{
   hashcodes.set(i,hashcodes.get(i)%10);
}
  

sortPrint(hashcodes);
  
  
}
  
  
public static void sortPrint(ArrayList<Integer> hashcodes)
{
   Collections.sort(hashcodes);
   for(int i=0;i<hashcodes.size();i++)
   {
       System.out.println(hashcodes.get(i));
   }
  
}
}

--------------------------------------

public class FamousPerson implements Comparable<FamousPerson>
{

   String firstName;
   String lastName;
   int year;
   String fact;
  
   FamousPerson(String f,String l,int y,String fa)
   {
       firstName=f;
       lastName=l;
       year=y;
       fact=fa;
   }
  
   @Override
   public int hashCode()
   // Returns a hash code value for this FamousPerson object.
   {
  
   return Math.abs((lastName.hashCode() * 3) + firstName.hashCode());
   }
  
   @Override
   public boolean equals(Object obj)
   // Returns true if 'obj' is a FamousPerson with same first and last
   // names as this FamousPerson, otherwise returns false.
   {
   if (obj == this)
   return true;
   else
   if (obj == null || obj.getClass() != this.getClass())
   return false;
   else
   {
   FamousPerson fp = (FamousPerson) obj;
   return (this.firstName.equals(fp.firstName) &&
   this.lastName.equals(fp.lastName));
   }
   }
   @Override

   public int compareTo(FamousPerson other)
   // Precondition: 'other' is not null
   //
   // Compares this FamousPerson with 'other' for order. Returns a
   // negative integer, zero, or a positive integer as this object
   // is less than, equal to, or greater than 'other'.
   {
   if (!this.lastName.equals(other.lastName))
   return this.lastName.compareTo(other.lastName);
   else
   return this.firstName.compareTo(other.firstName);
   }

  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote