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<StatePair
: Contains ZIP code/state abbreviation pairs
ArrayList<StatePair
: Contains state abbreviation/state name pairs
ArrayList<StatePair
: 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
StatePair.java
public class StatePair <Type1 extends Comparable, Type2 extends Comparable> { private Type1 value1; private Type2 value2; // TODO: Define a constructor, mutators, and accessors // for StatePair // TODO: Define printInfo() method }
StatePopulations.java
import java.util.Scanner; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; public class StatePopulations { public static ArrayList<StatePair> fillArray1(ArrayList<StatePair> 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<StatePair> fillArray2(ArrayList<StatePair> 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<StatePair> fillArray3(ArrayList<StatePair> 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<StatePair> zipCodeState = new ArrayList<StatePair>(); // state abbrev. - state name pairs ArrayList<StatePair> abbrevState = new ArrayList<StatePair>(); // state name - population pairs ArrayList<StatePair> statePopulation = new ArrayList<StatePair>(); // 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. } } }
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(); String stateName = null; String stateAbb = null; boolean found = false; for (i = 0; i < zipCodeState.size(); ++i) { // TODO: Using ZIP code, find state abbreviation if (zipCodeState.get(i).getValue1() == myZipCode) { stateAbb = zipCodeState.get(i).getValue2(); break; } } if (stateAbb != null) { for (i = 0; i < abbrevState.size(); ++i) { // TODO: Using state abbreviation, find state name if (abbrevState.get(i).getValue1().equalsIgnoreCase(stateAbb)) { stateName = abbrevState.get(i).getValue2(); break; } } if (stateName != null) { for (i = 0; i < statePopulation.size(); ++i) { // TODO: Using state name, find population. Print pair info. if (statePopulation.get(i).getValue1().equalsIgnoreCase(stateName)) { statePopulation.get(i).printInfo(); found = true; break; } } } } if (!found) { System.out.println("Zipcode: " + myZipCode + "not found"); } } }
StatePair.java
public class StatePair, Type2 extends Comparable > { private Type1 value1; private Type2 value2; // TODO: Define a constructor, mutators, and accessors for public StatePair(Type1 val1, Type2 val2) { value1 = val1; value2 = val2; } public void setValue1(Type1 val1) { this.value1 = val1; } public void setValue2(Type2 val2) { this.value2 = val2; } public Type1 getValue1() { return value1; } public Type2 getValue2() { return value2; } public void printInfo() { System.out.println(value1 + ": " + value2); } }
13.5 LAB: Zip code and population (class templates) Define a class StatePair with two template types (T1 and T2), a constructor, mutators, accessors, and a Printlnfo0 method. Three vectors have been pre-filled with StatePair data in main: . vector<StatePair <int, string>> zipCodeState: ZIP code - state abbreviation pairs . vector<StatePair<string, string>> abbrevState: state abbreviation - state name pairs • vector<StatePair<string, int>> statePopulation: state name - population pairs Complete mainO to use an input ZIP code to retrieve the correct state abbreviation from the vector zipCodeState. Then use...
I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...
10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10 flower Hydrangea 30 false lilac flower Rose 6 false white plant Mint 4...
7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the quicksort() and partition() methods to sort the IDs in ascending order using the Quicksort algorithm, and output the sorted IDs one per line. Ex. If the input is: kaylasimms julia myron1994 kaylajones -1 the output is: julia kaylajones kaylasimms myron1994 Code: import java.util.Scanner; import java.util.ArrayList; public class UserIDSorting { // TODO: Write the partitioning algorithm - pick the middle element as the // pivot,...
LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information: Name: Dobby Age: 2 Pet Information: Name: Kreacher Age: 3 Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Pet myPet = new Pet(); Dog myDog = new Dog(); String petName, dogName, dogBreed; int petAge, dogAge; petName = scnr.nextLine(); petAge = scnr.nextInt(); scnr.nextLine();...
In JAVA, In two classes: As a zookeeper, it is important to know the activities of the animals in your care and to monitor their living habitats. Create a monitoring system that does all of the following: -Asks a user if they want to monitor an animal, monitor a habitat, or exit -Displays a list of animal/habitat options (based on the previous selection) as read from either the animals or habitats file and asks the user to enter one of...
Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...
*JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before the one entered. I need help correcting my code. I'm unsure why I am getting a name, year, and genre in my output. Input: Jericho Output: Similar title finder. Enter a movie name.\n Here are the 3 movies that are listed before the one you entered\n Jeremy Paxman Interviews...\n Jeremy Taylor\n Jeremy Vine Meets...\n Current output: Similar title finder. Enter a movie name.\n Here...
Please help complete the items marked TODO in the code and get the tests to pass: import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestIterator { private List<Integer> list; // See the Java List Interface documentation to understand what all the List methods do ... @Before public void setUp() throws Exception { list = new ArrayList<Integer>(); // TODO also try with a...
Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q { public static LinkedList<String> dogs = new LinkedList<String> (); public static LinkedList<String> cats = new LinkedList<String> (); public static LinkedList<String> animals = new LinkedList<String> (); public static void enqueueCats(){ System.out.println("Enter name"); Scanner sc=new Scanner(System.in); String name = sc.next(); cats.addLast(name); animals.addLast(name); } ...