Question
Add code to main in the HashTableTester.java file so it ALSO declares a HashSC variable and adds at least 3 Location objects to it AND
Add code to main in the HashTableTester.java file so it ALSO declares a HashSC
ADD to the Location class here in the Location.java file (in the "Hash Table Files" link in Catalyst) so it overrides the equals method (compare ONLY the name) AND the hashCode method (use the body of the Hash method shown on page 2, but instead of someStringMember, do the operations on the Location's latitude & longitude as described below).
The Locations' hashCode method is assign to a local variable the latitude + 90, and to another local variable the longitude+180. Then convert each of those into a String (saving in local variables), BUT using a DecimalFormat with "000.0000" (will be shown in the Class Notes for 05-17). Remove the decimal points in each String, concatenate the 2 Strings, and return the hashCode of the resulting String). (You'll need to import java.text.*; )
HashTableTester.java:
//Example of an Employee class (to be used in main below) class Employee { public static final int MAX_LEN = 50;
private String name; private int ss;
public Employee( String name , int ss) { this(); setName(name); setSS(ss); }
public Employee() { name = "undefined"; ss = 0; }
String getName() { return name; } int getSS() { return ss; }
boolean setName( String name ) { if (name == null) return false; if (name.length() > MAX_LEN) return false; this.name = name; return true; }
boolean setSS( int ss ) { if (ss < 0 || ss > 999999999 ) return false; this.ss = ss; return true; }
public String toString() { return name + " (" + ss + ")"; }
public boolean equals( Object rhs ) // UPDATED! { return name.equalsIgnoreCase(((Employee)rhs).name); }
public int hashCode() { return ss; // return name.hashCode(); // another possibility } };
public class HashTableTester {
// ------- main -------------- public static void main(String[] args) throws Exception { // first set of tests -------------------- HashSC
Employee e1 = new Employee("Jane Austin", 1), e2 = new Employee("Rene Descartes", 2), e3 = new Employee("John Locke", 3);
if ( hashTable.insert(e1) ) System.out.println( "inserted " + e1 ); if ( hashTable.insert(e1) ) System.out.println( "inserted " + e1 ); if ( hashTable.insert(e2) ) System.out.println( "inserted " + e2 ); if ( hashTable.insert(e2) ) System.out.println( "inserted " + e2 ); if ( hashTable.insert(e3) ) System.out.println( "inserted " + e3 ); if ( hashTable.insert(e3) ) System.out.println( "inserted " + e3 );
System.out.println( hashTable.size() );
if ( hashTable.contains(e3) ) System.out.println( "contains " + e3 );
if ( hashTable.remove(e1) ) System.out.println( "removed " + e1 ); if ( hashTable.remove(e1) ) System.out.println( "removed " + e1 ); if ( hashTable.remove(e2) ) System.out.println( "removed " + e2 ); if ( hashTable.remove(e2) ) System.out.println( "removed " + e2 ); if ( hashTable.remove(e3) ) System.out.println( "removed " + e3 ); if ( hashTable.remove(e3) ) System.out.println( "removed " + e3 ); System.out.println( hashTable.size() );
if ( hashTable.contains(e3) ) System.out.println( "contains " + e3 );
// second set of tests --------------------
HashSC
substrate = substrate + substrate;
for (k = 0; k < substrate.length() - 6; k++) strArray[k] = substrate.substring(k, k + 5); limit = k;
hashTable2.setMaxLambda(.5); for (k = 0; k < limit; k++) if ( hashTable2.insert(strArray[k]) ) System.out.println( "inserted #" + k + " " + strArray[k] ); System.out.println( " #strings generated: " + limit + " #elements in ht: " + hashTable2.size() ); } // end main
}
Location.java:
public class Location { private String name=""; private String address=""; private double latitude; // default 0 private double longitude; // default 0 public Location(String nm, String addr, double lat, double lon) { setName(nm); setAddress(addr); setCoordinates(lat, lon); } public Location(String nm) { setName(nm); } public void setName(String nm) { if( nm != null ) name = nm; } public void setAddress(String addr) { if( addr != null ) address = addr; } public void setCoordinates(double lat, double lon) { latitude = lat; longitude = lon; } public String getName(){ return name; } public String getAddress(){ return address; } public double getLatitude(){ return latitude; } public double getLongitude(){ return longitude; } public String toString() { return "Location: "+ name + ", " + address + ", (" + latitude + ", " + longitude + ")"; } // YOU WRITE equals: public boolean equals(Object obj) // YOU WRITE hashCode: public int hashCode() }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started