In: Computer Science
The following is a Java programming question:
Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():
Complete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly, use the state name to retrieve the correct state name/population pair from the ArrayList statePopulation and output the pair.
Ex: If the input is:
21044
the output is:
Maryland: 6079602
The code below is included in the problem:
public class StatePair , Type2 extends Comparable> {
private Type1 value1;
private Type2 value2;
// TODO: Define a constructor, mutators, and accessors
// for StatePair
// TODO: Define printInfo() method
}
}
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
public class StatePopulations {
public static ArrayList> fillArray1(ArrayList>
statePairs,
Scanner inFS) {
StatePair pair;
int intValue;
String stringValue;
while (inFS.hasNextLine()) {
intValue = inFS.nextInt();
stringValue = inFS.next();
pair = new StatePair (intValue, stringValue);
statePairs.add(pair);
}
return statePairs;
}
public static ArrayList> fillArray2(ArrayList>
statePairs,
Scanner inFS) {
StatePair pair;
String stringValue1;
String stringValue2;
while (inFS.hasNextLine()) {
stringValue1 = inFS.next();
inFS.nextLine();
stringValue2 = inFS.nextLine();
pair = new StatePair (stringValue1, stringValue2);
statePairs.add(pair);
}
return statePairs;
}
public static ArrayList> fillArray3(ArrayList>
statePairs,
Scanner inFS) {
StatePair pair;
String stringValue;
int intValue;
while (inFS.hasNextLine()) {
stringValue = inFS.nextLine();
intValue = inFS.nextInt();
inFS.nextLine();
pair = new StatePair (stringValue, intValue);
statePairs.add(pair);
}
return statePairs;
}
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
int myZipCode;
int i;
// ZIP code - state abbrev. pairs
ArrayList> zipCodeState = new ArrayList>();
// state abbrev. - state name pairs
ArrayList> abbrevState = new ArrayList>();
// state name - population pairs
ArrayList> statePopulation = new ArrayList>();
// Fill the three ArrayLists
// Try to open zip_code_state.txt file
fileByteStream = new FileInputStream("zip_code_state.txt");
inFS = new Scanner(fileByteStream);
zipCodeState = fillArray1(zipCodeState, inFS);
fileByteStream.close(); // close() may throw IOException if
fails
// Try to open abbreviation_state.txt file
fileByteStream = new
FileInputStream("abbreviation_state.txt");
inFS = new Scanner(fileByteStream);
abbrevState = fillArray2(abbrevState, inFS);
fileByteStream.close();
// Try to open state_population.txt file
fileByteStream = new FileInputStream("state_population.txt");
inFS = new Scanner(fileByteStream);
statePopulation = fillArray3(statePopulation, inFS);
fileByteStream.close();
// Read in ZIP code from user
myZipCode = scnr.nextInt();
for (i = 0; i < zipCodeState.size(); ++i) {
// TODO: Using ZIP code, find state abbreviation
}
for (i = 0; i < abbrevState.size(); ++i) {
// TODO: Using state abbreviation, find state name
}
for (i = 0; i < statePopulation.size(); ++i) {
// TODO: Using state name, find population. Print pair info.
}
}
}
I hope you like the code and understood each subpart of it and don't forget to give UPVOTE as it took so much time to code it up and it means a lot, Thanks:)
You can revert back and feel free to ask doubts if you have any.
// StatePair.java
public class StatePair<Type1 extends Comparable<Type1>, Type2 extends Comparable<Type2>> {
private Type1 value1;
private Type2 value2;
// default constructor
public StatePair()
{
value1 = (Type1)null;
value2 = (Type2)null;
}
// parameterized constructor
public StatePair(Type1 data1, Type2 data2)
{
value1 = data1;
value2 = data2;
}
// mutators
public void set_the_Value1(Type1 data1)
{
value1 = data1;
}
public void set_the_Value2(Type2 data2)
{
value2 = data2;
}
// accessors
public Type1 get_the_Value1()
{
return value1;
}
public Type2 get_the_Value2()
{
return value2;
}
// method to print the contents
public void print_Information()
{
System.out.println(value1 +" : "+value2);
}
}
//end of StatePair.java
// state_Populations.java
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
public class state_Populations {
public static ArrayList<StatePair<Integer, String>> fillArray1(ArrayList<StatePair<Integer, String>> statePairs,Scanner inFS) {
StatePair<Integer, String> pair;
int int_Value;
String string_Value;
while (inFS.hasNextLine()) {
int_Value = inFS.nextInt();
string_Value = inFS.next();
pair = new StatePair<Integer, String> (int_Value, string_Value);
statePairs.add(pair);
}
return statePairs;
}
public static ArrayList<StatePair<String, String>> fillArray2(ArrayList<StatePair<String, String>> statePairs,
Scanner inFS) {
StatePair<String, String> pair;
String string_Value1;
String string_Value2;
while (inFS.hasNextLine()) {
string_Value1 = inFS.next();
inFS.nextLine();
string_Value2 = inFS.nextLine();
pair = new StatePair<String, String> (string_Value1, string_Value2);
statePairs.add(pair);
}
return statePairs;
}
public static ArrayList<StatePair<String, Integer>> fillArray3(ArrayList<StatePair<String, Integer>> statePairs,
Scanner inFS) {
StatePair<String, Integer> pair;
String string_Value;
int int_Value;
while (inFS.hasNextLine()) {
string_Value = inFS.nextLine();
int_Value = inFS.nextInt();
if(inFS.hasNextLine())
inFS.nextLine();
pair = new StatePair<String, Integer> (string_Value, int_Value);
statePairs.add(pair);
}
return statePairs;
}
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
FileInputStream fileByteStream = null; // File input stream
Scanner inFS = null; // Scanner object
int myZipCode;
int i;
// ZIP code - state abbrev. pairs
ArrayList<StatePair<Integer, String>> zipCodeState = new ArrayList<StatePair<Integer, String>>();
// state abbrev. - state name pairs
ArrayList<StatePair<String, String>> Abbrevation_State = new ArrayList<StatePair<String, String>>();
// state name - population pairs
ArrayList<StatePair<String, Integer>> state_Population = new ArrayList<StatePair<String, Integer>>();
// Fill the three ArrayLists
// Try to open zip_code_state.txt file
fileByteStream = new FileInputStream("zip_code_state.txt");
inFS = new Scanner(fileByteStream);
zipCodeState = fillArray1(zipCodeState, inFS);
fileByteStream.close(); // close() may throw IOException if fails
// Try to open abbreviation_state.txt file
fileByteStream = new FileInputStream("abbreviation_state.txt");
inFS = new Scanner(fileByteStream);
Abbrevation_State = fillArray2(Abbrevation_State, inFS);
fileByteStream.close(); // close() may throw IOException if fails
// Try to open state_population.txt file
fileByteStream = new FileInputStream("state_population.txt");
inFS = new Scanner(fileByteStream);
state_Population = fillArray3(state_Population, inFS);
fileByteStream.close(); // close() may throw IOException if fails
// Read in ZIP code from user
System.out.print("Enter the zip code : ");
myZipCode = scnr.nextInt();
String state_Abbrevation = null;
String state_Name = null;
boolean found = false;
// loop over zipCodeState to find the state abbreviation of the zip code
for (i = 0; i < zipCodeState.size(); ++i) {
if(zipCodeState.get(i).getValue1() == myZipCode)
{
state_Abbrevation = zipCodeState.get(i).getValue2();
break;
}
}
// if state abbreviation is found
if(state_Abbrevation != null) {
// loop over Abbrevation_State to get the state name
for (i = 0; i < Abbrevation_State.size(); ++i) {
if(Abbrevation_State.get(i).getValue1().equalsIgnoreCase(state_Abbrevation))
{
state_Name = Abbrevation_State.get(i).getValue2();
break;
}
}
// if state name exists
if(state_Name != null) {
// loop over state_Population to get the population of the state
for (i = 0; i < state_Population.size(); ++i) {
if(state_Population.get(i).getValue1().equalsIgnoreCase(state_Name))
{
state_Population.get(i).print_Information();
found = true;
break;
}
}
}
}
// if population for the given zip code not found
if(!found)
System.out.println("Zipcode : "+myZipCode+ " not found");
scnr.close();
}
}