Question

In: Computer Science

The following is a Java programming question: Define a class StatePair with two generic types (Type1...

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():

  • ArrayList> zipCodeState: Contains ZIP code/state abbreviation pairs
  • ArrayList> abbrevState: Contains state abbreviation/state name pairs
  • ArrayList> statePopulation: Contains state name/population pairs

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.
}
}
}

Solutions

Expert Solution

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();

}

  

  

}


Related Solutions

**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor:...
**Java Programming Question** A class that implements a data type, “Point,” which has the following constructor: Point(double x, double y, double z) and, the following API: double distanceto(Point q) it returns the Euclidean distance between this and q. The Euclidean distance between (x1, y1, z1) and (x2, y2, z2) is defined as sqrt( (x1-x2)^2 + (y1-y2)^2) + (z1-z2)^2). String toString() – it returns the string representation of the point. An example would be (2.3,4.5,3.0). Write a main method in the...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
Generic types A class or interface that declares one or more generic variables is called a...
Generic types A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic. Open GenericsC.java in jGRASP then compile it. At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection. Since the List being declared (al) is...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT