SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
4.20 LAB: Zip code and population (generic types) JAVA
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:
the output is:
WHATS ALREADY GIVEN:
StatePopulations.java
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.
}
}
}
StatePair.java
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
}

Weitere ähnliche Inhalte

Ähnlich wie 4.20 LAB Zip code and population (generic types) JAVADefine a cla.pdf

BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdfBasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
ankitmobileshop235
 
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdfJAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
suresh640714
 
I need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdfI need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdf
fonecomp
 
In Java- Create a Graduate class derived from Student- A graduate has.pdf
In Java- Create a Graduate class derived from Student- A graduate has.pdfIn Java- Create a Graduate class derived from Student- A graduate has.pdf
In Java- Create a Graduate class derived from Student- A graduate has.pdf
Stewart29UReesa
 
Introduction NullPointerException is a RuntimeException In.pdf
Introduction NullPointerException is a RuntimeException  In.pdfIntroduction NullPointerException is a RuntimeException  In.pdf
Introduction NullPointerException is a RuntimeException In.pdf
adinathknit
 
cant figure out how to add $20 to variables if it equals one of the .pdf
cant figure out how to add $20 to variables if it equals one of the .pdfcant figure out how to add $20 to variables if it equals one of the .pdf
cant figure out how to add $20 to variables if it equals one of the .pdf
xlynettalampleyxc
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
aromanets
 
Sorting Questions (JAVA)See attached classes below.Attached Clas.pdf
Sorting Questions (JAVA)See attached classes below.Attached Clas.pdfSorting Questions (JAVA)See attached classes below.Attached Clas.pdf
Sorting Questions (JAVA)See attached classes below.Attached Clas.pdf
akritigallery
 
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdfAdapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
albarefqc
 
project2.classpathproject2.project project2 .docx
project2.classpathproject2.project  project2 .docxproject2.classpathproject2.project  project2 .docx
project2.classpathproject2.project project2 .docx
briancrawford30935
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
dickonsondorris
 
Complete skeletonimport java.util.ArrayList; public class My.pdf
Complete skeletonimport java.util.ArrayList; public class My.pdfComplete skeletonimport java.util.ArrayList; public class My.pdf
Complete skeletonimport java.util.ArrayList; public class My.pdf
arhamnighty
 

Ähnlich wie 4.20 LAB Zip code and population (generic types) JAVADefine a cla.pdf (20)

Java 8 Examples
Java 8 ExamplesJava 8 Examples
Java 8 Examples
 
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdfBasicPizza.javapublic class BasicPizza { Declaring instance .pdf
BasicPizza.javapublic class BasicPizza { Declaring instance .pdf
 
Introduction to objects and inputoutput
Introduction to objects and inputoutput Introduction to objects and inputoutput
Introduction to objects and inputoutput
 
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdfJAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
 
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdfUsing Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
 
I need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdfI need help with this program for java.The program you are given t.pdf
I need help with this program for java.The program you are given t.pdf
 
In Java- Create a Graduate class derived from Student- A graduate has.pdf
In Java- Create a Graduate class derived from Student- A graduate has.pdfIn Java- Create a Graduate class derived from Student- A graduate has.pdf
In Java- Create a Graduate class derived from Student- A graduate has.pdf
 
Complete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docxComplete the following Java code such that it opens a file named Fruit.docx
Complete the following Java code such that it opens a file named Fruit.docx
 
Introduction NullPointerException is a RuntimeException In.pdf
Introduction NullPointerException is a RuntimeException  In.pdfIntroduction NullPointerException is a RuntimeException  In.pdf
Introduction NullPointerException is a RuntimeException In.pdf
 
cant figure out how to add $20 to variables if it equals one of the .pdf
cant figure out how to add $20 to variables if it equals one of the .pdfcant figure out how to add $20 to variables if it equals one of the .pdf
cant figure out how to add $20 to variables if it equals one of the .pdf
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Sorting Questions (JAVA)See attached classes below.Attached Clas.pdf
Sorting Questions (JAVA)See attached classes below.Attached Clas.pdfSorting Questions (JAVA)See attached classes below.Attached Clas.pdf
Sorting Questions (JAVA)See attached classes below.Attached Clas.pdf
 
Java stream
Java streamJava stream
Java stream
 
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdfAdapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
Adapt your code from Assignment 2 to use both a Java API ArrayList a.pdf
 
project2.classpathproject2.project project2 .docx
project2.classpathproject2.project  project2 .docxproject2.classpathproject2.project  project2 .docx
project2.classpathproject2.project project2 .docx
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Use the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docxUse the following data set that compares age to average years lef.docx
Use the following data set that compares age to average years lef.docx
 
Complete skeletonimport java.util.ArrayList; public class My.pdf
Complete skeletonimport java.util.ArrayList; public class My.pdfComplete skeletonimport java.util.ArrayList; public class My.pdf
Complete skeletonimport java.util.ArrayList; public class My.pdf
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
 

Mehr von rambagra74

5.1.a Contrast the functions of the DNA and protein components of ch.pdf
5.1.a Contrast the functions of the DNA and protein components of ch.pdf5.1.a Contrast the functions of the DNA and protein components of ch.pdf
5.1.a Contrast the functions of the DNA and protein components of ch.pdf
rambagra74
 
5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf
5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf
5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf
rambagra74
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
rambagra74
 
7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf
7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf
7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf
rambagra74
 

Mehr von rambagra74 (20)

5.27 Company H has an insurance policy for which it pays an annual.pdf
5.27 Company H has an insurance policy for which it pays an annual.pdf5.27 Company H has an insurance policy for which it pays an annual.pdf
5.27 Company H has an insurance policy for which it pays an annual.pdf
 
5.1.a Contrast the functions of the DNA and protein components of ch.pdf
5.1.a Contrast the functions of the DNA and protein components of ch.pdf5.1.a Contrast the functions of the DNA and protein components of ch.pdf
5.1.a Contrast the functions of the DNA and protein components of ch.pdf
 
5.2 Par�s a Mosc�. En su programa de estudios de verano en el extr.pdf
5.2 Par�s a Mosc�. En su programa de estudios de verano en el extr.pdf5.2 Par�s a Mosc�. En su programa de estudios de verano en el extr.pdf
5.2 Par�s a Mosc�. En su programa de estudios de verano en el extr.pdf
 
5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf
5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf
5. Cyanobacteria live mostly in aquatic habitats but they can also b.pdf
 
5. The Federal Reserves organization1) There are (5712) Feder.pdf
5. The Federal Reserves organization1) There are (5712)   Feder.pdf5. The Federal Reserves organization1) There are (5712)   Feder.pdf
5. The Federal Reserves organization1) There are (5712) Feder.pdf
 
5. Mrs. Toguchi wants to determine if politicians tend to have highe.pdf
5. Mrs. Toguchi wants to determine if politicians tend to have highe.pdf5. Mrs. Toguchi wants to determine if politicians tend to have highe.pdf
5. Mrs. Toguchi wants to determine if politicians tend to have highe.pdf
 
41. a and b 41. Introductory Statistics Course Withdrawals. A univer.pdf
41. a and b 41. Introductory Statistics Course Withdrawals. A univer.pdf41. a and b 41. Introductory Statistics Course Withdrawals. A univer.pdf
41. a and b 41. Introductory Statistics Course Withdrawals. A univer.pdf
 
41) �La justificaci�n legal como �nico criterio �tico es adecuada de.pdf
41) �La justificaci�n legal como �nico criterio �tico es adecuada de.pdf41) �La justificaci�n legal como �nico criterio �tico es adecuada de.pdf
41) �La justificaci�n legal como �nico criterio �tico es adecuada de.pdf
 
40. What is the common challenge in the world of financea. It gen.pdf
40. What is the common challenge in the world of financea. It gen.pdf40. What is the common challenge in the world of financea. It gen.pdf
40. What is the common challenge in the world of financea. It gen.pdf
 
4.a. Calculate Target�s current ratio as of January 29, 2022 (show cur.pdf
4.a. Calculate Target�s current ratio as of January 29, 2022 (show cur.pdf4.a. Calculate Target�s current ratio as of January 29, 2022 (show cur.pdf
4.a. Calculate Target�s current ratio as of January 29, 2022 (show cur.pdf
 
5. Earth HistoryPaleogeography - World. Check and double-click the .pdf
5. Earth HistoryPaleogeography - World. Check and double-click the .pdf5. Earth HistoryPaleogeography - World. Check and double-click the .pdf
5. Earth HistoryPaleogeography - World. Check and double-click the .pdf
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
 
7. Para comprender mejor la prehistoria de la isla hawaiana de Lana.pdf
7. Para comprender mejor la prehistoria de la isla hawaiana de Lana.pdf7. Para comprender mejor la prehistoria de la isla hawaiana de Lana.pdf
7. Para comprender mejor la prehistoria de la isla hawaiana de Lana.pdf
 
7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf
7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf
7. Lawn Spray Inc. desarrolla y produce equipos de pulverizaci�n.pdf
 
701 () � Kenneth_Laudon,_J... a a+ as Part One Ogriesions, Mrugemer.pdf
701 () �  Kenneth_Laudon,_J... a a+ as Part One Ogriesions, Mrugemer.pdf701 () �  Kenneth_Laudon,_J... a a+ as Part One Ogriesions, Mrugemer.pdf
701 () � Kenneth_Laudon,_J... a a+ as Part One Ogriesions, Mrugemer.pdf
 
71 of all students at a college still need to take another math class.pdf
71 of all students at a college still need to take another math class.pdf71 of all students at a college still need to take another math class.pdf
71 of all students at a college still need to take another math class.pdf
 
7.4.3 4.3 Consider a set of independent data observations x1,�.pdf
7.4.3 4.3 Consider a set of independent data observations x1,�.pdf7.4.3 4.3 Consider a set of independent data observations x1,�.pdf
7.4.3 4.3 Consider a set of independent data observations x1,�.pdf
 
7.16 LAB Smallest and largest numbers in a listWrite a program th.pdf
7.16 LAB Smallest and largest numbers in a listWrite a program th.pdf7.16 LAB Smallest and largest numbers in a listWrite a program th.pdf
7.16 LAB Smallest and largest numbers in a listWrite a program th.pdf
 
7. El balance general de Tucker Company reflejaba activos por $12,.pdf
7. El balance general de Tucker Company reflejaba activos por $12,.pdf7. El balance general de Tucker Company reflejaba activos por $12,.pdf
7. El balance general de Tucker Company reflejaba activos por $12,.pdf
 
4.3 Expresi�n positiva y cort�s Revise las siguientes declaracio.pdf
4.3 Expresi�n positiva y cort�s Revise las siguientes declaracio.pdf4.3 Expresi�n positiva y cort�s Revise las siguientes declaracio.pdf
4.3 Expresi�n positiva y cort�s Revise las siguientes declaracio.pdf
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Kürzlich hochgeladen (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

4.20 LAB Zip code and population (generic types) JAVADefine a cla.pdf

  • 1. 4.20 LAB: Zip code and population (generic types) JAVA 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: the output is: WHATS ALREADY GIVEN: StatePopulations.java 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; }
  • 2. 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;
  • 3. 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
  • 4. } 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. } } } StatePair.java 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 }