Answered step by step
Verified Expert Solution
Link Copied!

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.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 Zip implements Cloneable {
private String zipCode;
private String primaryCity;
private String state;
public Zip(String zipCode, String primaryCity, String state){
this.zipCode = zipCode;
this.primaryCity = primaryCity;
this.state = state;
}
public Zip(String dataLine){
String[] parts = dataLine.split("");
if (parts.length >=7){
this.zipCode = parts[0];
this.primaryCity = parts[3];
this.state = parts[6];
} else {
throw new IllegalArgumentException("Invalid data format: "+ dataLine);
}
}
@Override
public String toString(){
return zipCode +""+ primaryCity +""+ state;
}
@Override
public boolean equals(Object o){
if (this == o) return true;
if (o == null || getClass()!= o.getClass()) return false;
Zip zip =(Zip) o;
return zipCode.equals(zip.zipCode) && state.equals(zip.state);
}
@Override
public int hashCode(){
return Objects.hash(zipCode, 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 addZip(Zip zip){
zipMap.put(zip.getZipCode(), zip);
uniqueCities.add(zip.getPrimaryCity());
uniqueStates.add(zip.getState());
}
public Zip getZipByCode(String zipCode){
return zipMap.get(zipCode);
}
public void populateFromFile(String filePath) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))){
String line;
while ((line = br.readLine())!= null){
if (!line.trim().isEmpty()){
Zip zip = new Zip(line);
addZip(zip);
}
}
}
}
@Override
public Iterator iterator(){
return zipMap.values().iterator();
}
} Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 2
at Zip.(Zip.java:16)
at ZipCodeTest.main(ZipCodeTest.java:7)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn Mysql The Easy Way A Beginner Friendly Guide

Authors: Kiet Huynh

1st Edition

B0CNY7143T, 979-8869761545

More Books

Students also viewed these Databases questions