**THIS IS A JAVA PROBLEM, NEEDS TO BE DONE WITH ECLIPSE!** 3 java files are give
ID: 3598379 • Letter: #
Question
**THIS IS A JAVA PROBLEM, NEEDS TO BE DONE WITH ECLIPSE!** 3 java files are given.
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
/*
* In this class we have 2 private lists byKey for looking by key (zipcode),
* and byValue for looking by city name.
* byKey has Items in the form Item(ZipCode,CityName) and byValue has Items in the form Item(CityName,Zipcode)
* We take input from the file and store them in lists as said above.
* The method lookup() uses byKey for binarysearching and
* the method reverselookup() uses byValue for binarySearching
* No changes have been made in the other 2 classes. You can use them directly.
Kindly rate the answer if you find it useful.*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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) {
int i = 0;
Item item;
String k = null, v = null;
while (in.hasNextLine()) {
if (i % 2 == 0)
k = in.nextLine();
else {
v = in.nextLine();
item = new Item(k, v);//Zipcode is key and city name is value
byKey.add(item);
item = new Item(v, k);//City name is key and zipcode is value
byValue.add(item);
}
i++;
}
//Sorting byKey list
Collections.sort(byKey, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
// TODO Auto-generated method stub
return o1.getKey().compareTo(o2.getKey());
}
});
//Sorting byValue list
Collections.sort(byValue, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
// TODO Auto-generated method stub
return o1.getKey().compareTo(o2.getKey());
}
});
}
/**
* 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) {
//Comparator to compare the keys
Comparator<Item> c = new Comparator<Item>() {
public int compare(Item u1, Item u2) {
return u1.compareTo(u2);
}
};
//Binary Search on the sorted list
int index = Collections.binarySearch(byKey, new Item(k, null), c);
if (index < 0)//If not found
return null;
Item item = byKey.get(index);
return item.getValue();//return the value
}
/**
* 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) {
//Comparator to compare the keys
Comparator<Item> c = new Comparator<Item>() {
public int compare(Item u1, Item u2) {
return u1.compareTo(u2);
}
};
//Binary Search on the sorted list
int index = Collections.binarySearch(byValue, new Item(v, null), c);
if (index < 0)//If not found
return null;
Item item = byValue.get(index);
return item.getValue();//returning the value
}
}
/*
Sample input: iazip.txt
50001
ACKWORTH
50002
ADAIR
2
RST
50003
ADEL
40
ABC
50005
ALBION
50006
ALDEN
50007
ALLEMAN
29
XYZ
Sample output:
Enter the name of the zipcode file:
iazip.txt
Lookup Z)ip, C)ity name, Q)uit?
z
Enter Zipcode:
123456
City name: null
Lookup Z)ip, C)ity name, Q)uit?
z
Enter Zipcode:
50001
City name: ACKWORTH
Lookup Z)ip, C)ity name, Q)uit?
z
Enter Zipcode:
40
City name: ABC
Lookup Z)ip, C)ity name, Q)uit?
c
Enter city name:
ADEL
Zipcode: 50003
Lookup Z)ip, C)ity name, Q)uit?
c
Enter city name:
XYZ
Zipcode: 29
Lookup Z)ip, C)ity name, Q)uit?
c
Enter city name:
ABC
Zipcode: 40
Lookup Z)ip, C)ity name, Q)uit?
c
Enter city name:
ABCD
Zipcode: null
Lookup Z)ip, C)ity name, Q)uit?
c
Enter city name:
ADAIR
Zipcode: 50002
Lookup Z)ip, C)ity name, Q)uit?
q
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.