Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import java.io . BufferedReader; import java.io . FileReader; import java.io . IOException; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.Objects; class
import java.ioBufferedReader;
import java.ioFileReader;
import java.ioIOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Objects;
class Zip implements Cloneable
private String zipCode;
private String primaryCity;
private String state;
public ZipString zipCode, String primaryCity, String state
this.zipCode zipCode;
this.primaryCity primaryCity;
this.state state;
public ZipString dataLine
String parts dataLine.split;
if partslength
this.zipCode parts;
this.primaryCity parts;
this.state parts;
else
throw new IllegalArgumentExceptionInvalid data format: dataLine;
@Override
public String toString
return zipCode primaryCity state;
@Override
public boolean equalsObject o
if this o return true;
if o null getClass ogetClass return false;
Zip zip Zip o;
return zipCode.equalszipzipCode && state.equalszipstate;
@Override
public int hashCode
return Objects.hashzipCode state;
@Override
public Object clone throws CloneNotSupportedException
return super.clone;
public String getPrimaryCity
return primaryCity;
public String getState
return state;
class ZipCodes implements Iterable
private Map zipMap;
private Set uniqueCities;
private Set uniqueStates;
public ZipCodes
this.zipMap new HashMap;
this.uniqueCities new HashSet;
this.uniqueStates new HashSet;
public void addZipZip zip
zipMap.putzipgetZipCode zip;
uniqueCities.addzipgetPrimaryCity;
uniqueStates.addzipgetState;
public Zip getZipByCodeString zipCode
return zipMap.getzipCode;
public void populateFromFileString filePath throws IOException
try BufferedReader br new BufferedReadernew FileReaderfilePath
String line;
while line brreadLine null
if line.trimisEmpty
Zip zip new Zipline;
addZipzip;
@Override
public Iterator iterator
return zipMap.valuesiterator;
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index out of bounds for length
at Zip.Zipjava:
at ZipCodeTest.mainZipCodeTestjava:
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