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

Java: Write a zip code lookup program. Read a data set of 1,000+ zip codes and c

ID: 3596041 • Letter: J

Question

Java: Write a zip code lookup program. Read a data set of 1,000+ zip codes and city names from a file that contains zip codes and city names in Iowa in random order. Handle lookups by zip code and also reverse lookups by city name. Use a binary search for both lookups.

Four files are provided with the assignment.

1. ZipLookup.java: class containing main method. No modification needed.

2. Item.java: class to store an item. Use string variable ‘key’ for zip code and string variable ‘value’

for city name. No modification needed.

3. LookupTable.java: core class of this assignment. Complete all methods.

4. iazip.txt: zip codes and city names in Iowa.

Submit one source file (LookupTable.java). In other words, ZipLookup.java, Item.java, or iazip.txt are not submitted

Ziplookup:

import java.io.IOException;

import java.io.FileReader;

import java.util.Scanner;

/* The input file has the format

50001

ACKWORTH

50002

ADAIR

50003

ADEL

50005

ALBION

50006

ALDEN

50007

ALLEMAN

50008

. . .

*/

public class ZipLookup

{

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

{

Scanner in = new Scanner(System.in);

System.out.println("Enter the name of the zipcode file: ");

String fileName = in.nextLine();

LookupTable table = new LookupTable();

FileReader reader = new FileReader(fileName);

table.read(new Scanner(reader));

boolean more = true;

while (more)

{

System.out.println("Lookup Z)ip, C)ity name, Q)uit?");

String cmd = in.nextLine();

if (cmd.equalsIgnoreCase("Q"))

more = false;

else if (cmd.equalsIgnoreCase("Z"))

{

System.out.println("Enter Zipcode:");

String n = in.nextLine();

System.out.println("City name: " + table.lookup(n) + " ");

}

else if (cmd.equalsIgnoreCase("C"))

{

System.out.println("Enter city name:");

String n = in.nextLine();

Item.java:

/**

An item with a key and a value.

*/

public class Item implements Comparable<Item>

{

private String key;

private String value;

/**

Constructs an Item object.

@param k the key string

@param v the value of the item

*/

public Item(String k, String v)

{

key = k;

value = v;

}

/**

Gets the key.

@return the key

*/

public String getKey()

{

return key;

}

/**

Gets the value.

@return the value

*/

public String getValue()

{

return value;

}

public int compareTo(Item otherObject)

{

Item other = (Item) otherObject;

return key.compareTo(other.key);

}

}

LookupTable.java:

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

/**

* Code for HW6

* @author

*/

/**

A table for lookups and reverse lookups

*/

public class LookupTable

{

private ArrayList<Item> byKey;

private ArrayList<Item> byValue;

/**

Constructs a LookupTable object.

*/

public LookupTable()

{

byKey = new ArrayList<Item>();

byValue = new ArrayList<Item>();

}

/**

Reads key/value pairs.

@param in the scanner for reading the input

*/

public void read(Scanner in)

{

. . .

}

/**

Looks up an item in the table.

@param k the key to find

@return the value with the given key, or null if no

such item was found.

*/

public String lookup(String k)

{

. . .

}

/**

Looks up an item in the table.

@param v the value to find

@return the key with the given value, or null if no

such item was found.

*/

public String reverseLookup(String v)

{

. . .

}

}

Explanation / Answer

//ZipLookup.java

package zip;

import java.io.IOException;

import java.io.FileReader;

import java.util.Scanner;

/* The input file has the format

. . .

*/

public class ZipLookup

{

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

   {

       Scanner in = new Scanner(System.in);

       System.out.println("Enter the name of the zipcode file: ");

       String fileName = in.nextLine();

       LookupTable table = new LookupTable();

       FileReader reader = new FileReader(fileName);

       table.read(new Scanner(reader));

       boolean more = true;

       while (more)

       {

           System.out.println("Lookup Z)ip, C)ity name, Q)uit?");

           String cmd = in.nextLine();

           if (cmd.equalsIgnoreCase("Q"))

               more = false;

           else if (cmd.equalsIgnoreCase("Z"))

           {

               System.out.println("Enter Zipcode:");

               String n = in.nextLine();

               System.out.println("City name: " + table.lookup(n) + " ");

           }

           else if (cmd.equalsIgnoreCase("C"))

           {

               System.out.println("Enter city name:");

               String n = in.nextLine();
               System.out.println("Zip code is : " + table.reverseLookup(n) + " ");
           } // else-if end
       } // while end
   } // main end
} // class

// Item.java

package zip;

/**

An item with a key and a value.

*/

public class Item implements Comparable<Item>

{

private String key;

private String value;

/**

Constructs an Item object.

@param k the key string

@param v the value of the item

*/

public Item(String k, String v)

{

key = k;

value = v;

}

/**

Gets the key.

@return the key

*/

public String getKey()

{

return key;

}

@Override
public String toString() {
   return "Item [key=" + key + ", value=" + value + "]";
}

/**

Gets the value.

@return the value

*/

public String getValue()

{

return value;

}

public int compareTo(Item otherObject)

{

Item other = (Item) otherObject;

return key.compareTo(other.key);

}

}

// LookupTable.java

package zip;


import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

/**

* Code for HW6

* @author

*/

/**

A table for lookups and reverse lookups

*/

public class LookupTable

{

private ArrayList<Item> byKey;

private ArrayList<Item> byValue;

/**

Constructs a LookupTable object.

*/
Item item=null;
Item item2=null;

public LookupTable()

{

byKey = new ArrayList<Item>();

byValue = new ArrayList<Item>();

}

/**

Reads key/value pairs.

@param in the scanner for reading the input

*/

public void read(Scanner s) throws FileNotFoundException

{
  
  
   StringBuilder strline=new StringBuilder();
   try{
       while(s.hasNextLine()){
       String line = s.nextLine();
       //Scanner lineScanner = new Scanner(line);
       //Will check if there are exactly 3 numbers per line
       //and split them into three arrays
      
       strline.append(line).append(",");
       }
       String[] numbers = strline.toString().split(",");
       System.out.println(Arrays.toString(numbers));
      
       for(int i=0;i<numbers.length;i=i+2){
                    //   System.out.println(numbers[i]+"        "+numbers[i+1]);
           item=new Item(numbers[i], numbers[i+1]);
           item2=new Item(numbers[i+1], numbers[i]);
           //System.out.println("Item "+(i+1)+" : "+item);
           byKey.add(item);
           byValue.add(item2);
       }
      
       }
        catch (IndexOutOfBoundsException e){
       System.out.println(e.getMessage());
      
       } catch (InputMismatchException e){
       System.out.println("There's an invalid value in the file");
      
       }
  
}

/**

Looks up an item in the table.

@param k the key to find

@return the value with the given key, or null if no

such item was found.

*/

public String lookup(String k)

{      //System.out.println(byKey);
      //System.out.println(byValue);
   Item itm=null;
      for(int i=0;i<byKey.size();i++){
          if(byKey.get(i).getKey().equals(k)) itm=byKey.get(i);
      }
return itm.getValue();
}

/**

Looks up an item in the table.

@param v the value to find

@return the key with the given value, or null if no

such item was found.

*/

public String reverseLookup(String v)

{   System.out.println(v);
   Item itm2=null;
    for(int i=0;i<byValue.size();i++){
       //System.out.println(byValue.get(i));
      if(byValue.get(i).getKey().equalsIgnoreCase(v)){
          itm2=byValue.get(i);
          System.out.println(itm2);
      }
    }
return itm2.getValue();

}

}

/* output:

Enter the name of the zipcode file:
iazip.txt
[50001, ACKWORTH, 50002, ADAIR, 50003, ADEL, 50005, ALBION, 50006, ALDEN, 50007, ALLEMAN, 50008]
13
Lookup Z)ip, C)ity name, Q)uit?
c
Enter city name:
alden
alden
Item [key=ALDEN, value=50006]
Zip code is : 50006

Lookup Z)ip, C)ity name, Q)uit?
q

*/

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