Question

State Lookup Table: Below are two tables, stateStrings (50 rows by 6 columns) and stateInts (50...

State Lookup Table:

Below are two tables, stateStrings (50 rows by 6 columns) and stateInts (50 rows by 3 columns), that contain data for the 50 states. Below are examples for the first state, Alabama:

stateStrings:

State Abbrev

Name

Capital

Largest City

Governor

Political Party

AL

Alabama

Montgomery

Birmingham

Robert Julian Bentley

Republican

stateInts:

State Index

Population

Electoral Votes

0

4,858,979

9

Write a java application that uses the full tables below to look up the corresponding data using the “Main menu” approach described in the sample output below. You can use a switch statement to handle the choice entered. You must be able to handle mixed case input for the state abbreviation (ar, AR, aR, Ar) and you must format the population data with commas (e.g. 1,234,567) by importing the DecimalFormat class in the java.text library, create an object of that type, and then use the format instance method in that class to format the population with commas.

Sample run:

Enter a state abbreviation code: ar

State abbreviation code: AR

State index: 3

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 1

Name: Arkansas

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 2

Capital: Little Rock

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 3

Largest city: Little Rock

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 4

Governor: Asa Hutchinson

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 5

Governor's political party: Republican

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 6

Population: 3,020,327

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 7

Electoral votes: 6

Main menu

1: Name

2: Capital

3: Largest City

4: Governor

5: Political Party

6: Population

7: Electoral Votes

8: Exit

Enter a choice: 8

0 0
Add a comment Improve this question Transcribed image text
Answer #1
StateStrings.java
/**
 * Utility Class to store the information of a single state
 * @author 
 *
 */
public class StateStrings {

    // private instance variables
    private String stateAbbrev;
    private String name;
    private String capital;
    private String largestCity;
    private String governor;
    private String politicalParty;

    // default constructor
    public StateStrings()
    {

    }

    // parameterized constructor
    public StateStrings(String stateAbbrev, String name, String capital, String largestCity, String governor,
            String politicalParty) {
        this.stateAbbrev = stateAbbrev;
        this.name = name;
        this.capital = capital;
        this.largestCity = largestCity;
        this.governor = governor;
        this.politicalParty = politicalParty;
    }

    // method to return the state abbreviation
    public String getStateAbbrev() {
        return stateAbbrev;
    }

    // method to return the name of state
    public String getName() {
        return name;
    }

    // method to return capital of the state
    public String getCapital() {
        return capital;
    }

    // method to return the largest city of the state
    public String getLargestCity() {
        return largestCity;
    }

    // method to return the governor of the state
    public String getGovernor() {
        return governor;
    }

    // method to return the political party of the state
    public String getPoliticalParty() {
        return politicalParty;
    }

    // method to set the state abbreviation
    public void setStateAbbrev(String stateAbbrev) {
        this.stateAbbrev = stateAbbrev;
    }

    // method to set the name of the state
    public void setName(String name) {
        this.name = name;
    }

    // method to set the capital of the state
    public void setCapital(String capital) {
        this.capital = capital;
    }

    // method yo set the largest city of the state
    public void setLargestCity(String largestCity) {
        this.largestCity = largestCity;
    }

    // method to set the governor of the state
    public void setGovernor(String governor) {
        this.governor = governor;
    }

    // method to set the political party of the state
    public void setPoliticalParty(String politicalParty) {
        this.politicalParty = politicalParty;
    }



}
StateInts.java
/**
 * Utility class to store a single state's population, index, and electoral votes
 * @author 
 *
 */
public class StateInts {

    // private instance variables
    private int stateIndex;
    private long population;
    private int electoralVotes;

    // default constructor
    public StateInts()
    {
        stateIndex = -1;
        population = 0;
        electoralVotes = 0;
    }

    // parameterized constructor
    public StateInts(int stateIndex, long population, int electoralVotes) {
        this.stateIndex = stateIndex;
        this.population = population;
        this.electoralVotes = electoralVotes;
    }

    // method to return the state index
    public int getStateIndex() {
        return stateIndex;
    }

    // method to return the state population
    public long getPopulation() {
        return population;
    }

    // method to return the electoral votes
    public int getElectoralVotes() {
        return electoralVotes;
    }

    // method to set state index
    public void setStateIndex(int stateIndex) {
        this.stateIndex = stateIndex;
    }

    // method to set population of the state
    public void setPopulation(long population) {
        this.population = population;
    }

    // method to set the state electoral votes
    public void setElectoralVotes(int electoralVotes) {
        this.electoralVotes = electoralVotes;
    }



}
StateTable.java
/**
 * Composite class which holds the object of StateStrings and StateInts for a single state
 * @author
 *
 */
public class StateTable {

    // instance variables of StateStrings and StateInts
    // for a single state
    private StateStrings stateStrings;
    private StateInts stateInts;

    // default constructor
    public StateTable()
    {
        stateStrings = new StateStrings();
        stateInts = new StateInts();
    }

    // parameterized constructor
    public StateTable(StateStrings stateStrings, StateInts stateInts) {
        this.stateStrings = stateStrings;
        this.stateInts = stateInts;
    }

    // methdo to return the state string object
    public StateStrings getStateStrings() {
        return stateStrings;
    }

    // methdo tos get stateints object
    public StateInts getStateInts() {
        return stateInts;
    }

    // method to set state string instance to the state
    public void setStateStrings(StateStrings stateStrings) {
        this.stateStrings = stateStrings;
    }

    // method to set the state int object
    public void setStateInts(StateInts stateInts) {
        this.stateInts = stateInts;
    }



}
StateLookupTable.java (Driver File)
import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * Driver file to run the state table
 * @author
 *
 */
public class StateLookupTable {

    public static void main(String args[])
    {
        String abbv;
        Scanner kb = new Scanner(System.in);
        // array to store the state info
        StateTable[] table = new StateTable[50];

        // adding data to table
        // here 5 states are stored in the array
        // but you can store more if you want
        table[0] = new StateTable(new StateStrings("AL", "Alabama", "Montgomery", 
                "Birmingham", "Robert Julian Bentley", "Republican"), new StateInts(0, 4858979, 9));
        table[1] = new StateTable(new StateStrings("AK", "Alaska", "Juneau", 
                "Anchorage", "Bill Walker", "Independent"), new StateInts(1, 739795, 3));
        table[2] = new StateTable(new StateStrings("AZ", "Arizona", "Phoenix", 
                "Phoenix", "Doug Ducey", "Republican"), new StateInts(2, 7016270, 11));
        table[3] = new StateTable(new StateStrings("AR", "Arkansas", "Little Rock", 
                "Little Rock", "Asa Hutchinson", "Republican"), new StateInts(3, 3004279, 6));
        table[4] = new StateTable(new StateStrings("CA", "California", "Sacramento", 
                "Los Angeles", "Jerry Brown", "Democratic"), new StateInts(4, 39536653, 55));
        System.out.print("Enter a state abbreviation code: ");
        abbv = kb.nextLine();
        int index = getIndex(table, abbv);
        if(index == -1)
        {
            System.out.println("No such state with that abbreviation exist in the StateTable array.");
            System.out.println("Please store that state information first in the array");
        }
        else
        {
            int choice = mainMenu();
            while(choice != 8)
            {
                processChoice(table, index, choice);
                choice = mainMenu();
                if(choice == 8)
                {
                    System.out.println("\nGoodBye...");
                }
            }
        }
    }

    // static method to process input choice
    public static void processChoice(StateTable[] table, int index, int choice)
    {
        if(choice == 1)
        {
            System.out.println("Name: " + table[index].getStateStrings().getName());
        }

        else if(choice == 2)
        {
            System.out.println("Capital: " + table[index].getStateStrings().getCapital());
        }
        else if(choice == 3)
        {
            System.out.println("Largest City: " + table[index].getStateStrings().getLargestCity());
        }

        else if(choice == 4)
        {
            System.out.println("Governor: " + table[index].getStateStrings().getGovernor());
        }

        else if(choice == 5)
        {
            System.out.println("Political Party: " + table[index].getStateStrings().getPoliticalParty());
        }
        else if(choice == 6)
        {
            DecimalFormat putComma = new DecimalFormat("#,###");
            long pop = table[index].getStateInts().getPopulation();
            System.out.println("Population: " + putComma.format(pop));
        }

        else if(choice == 7)
        {
            System.out.println("Electoral Votes: " + table[index].getStateInts().getElectoralVotes());
        }

        System.out.println();



    }

    // method to show menu to the user and do validation
    // and return the input choice
    public static int mainMenu()
    {
        int choice = -1;
        Scanner kb = new Scanner(System.in);
        while(choice < 1 || choice > 8)
        {
            System.out.println("Main menu");
            System.out.println("1: Name");
            System.out.println("2: Capital");
            System.out.println("3: largest City");
            System.out.println("4: Governor");
            System.out.println("5: Political Party");
            System.out.println("6: Population");
            System.out.println("7: Electoral Votes");
            System.out.println("8: Exit");
            System.out.print("Enter a choice: ");
            choice = Integer.parseInt(kb.nextLine());
            if(choice < 1 || choice > 8)
            {
                System.out.println("Error! Invalid Input. input must be in range 1 to 8.\n");
            }
        }
        return choice;
    }

    // method to return the index of a particular state abbreviation
    // into state array
    public static int getIndex(StateTable[] table, String abbv)
    {
        for(int i = 0; i < 50; i++)
        {
            if(table[i] == null)
                return -1;
            if(table[i].getStateStrings().getStateAbbrev().equalsIgnoreCase(abbv))
                return i;
        }
        return -1;
    }


}

OUTPUT

Console 3 <terminated> StatelookupTable [Java Application] C:\Program Files\Java Enter a state abbreviation code: Ar Main menu 1: Name 2: Capital 3: largest City 4: Governor 5: Political Party 6: Population 7: Electoral Votes 8: Exit Enter a choice: 1 Name: Arkansas Main menu 1: Name 2: Capital 3:largest City 4: Governor 5: Political Party 6: Population 7: Electoral Votes 8: Exit Enter a choice: 4 Governor: Asa Hutchinson Main menu 1: Name 2: Capital 3:largest City 4: Governor 5: Political Party 6: Population 7: Electoral Votes 8: Exit

Instructions: Total 4 java classes have been used to create the application. StateStrings and StateInts have been created as 2 different classes. Another composite class is created to combine the objects of the former two classes to be put into one single unit known as StateTable class and finally, one driver file StateLookupTable has been created to run the application. In the main method of StateLookupTable class, an array of size 50 of StateTable has been created to store the info of 50 states.

Create the 4 java files with the exact names as given in table headers (underlined and bolded) and then copy-paste the codes into their respective files and save. Please note that only 5 states info have been stored into the state table array in the main method to test the application but you can store the remaining state info into the array. Finally, run the driver file StateLookupTable.java

Add a comment
Know the answer?
Add Answer to:
State Lookup Table: Below are two tables, stateStrings (50 rows by 6 columns) and stateInts (50...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Use the regression equation to calculate the number of Electoral votes that a state with...

    1. Use the regression equation to calculate the number of Electoral votes that a state with a population of 1,000,000 would have. Since our units of population are in thousands, substitute x=1000 into the formula, find the value of y and round to the nearest whole number (since it is measuring electoral votes). Do this in Excel using what you know about Excel formulas. 2. Is this meaningful as a state population? Why or why not? 3. Discuss, briefly, what...

  • E D Correlation coefficient 0.9400279 المة 1800 Presidential Election Electoral Votes 25 20 15 A 1...

    E D Correlation coefficient 0.9400279 المة 1800 Presidential Election Electoral Votes 25 20 15 A 1 State 2. CT 3 DE 4 GA 5 KY 6 MD 7 MA 8 NH 9 NJ 10 NY 11 NC 12 IPA 13 RI 14 SC 15 TN 16 VT 17 VA 18 B С Population(thousands) Electoral Votes 251 9 64 163 4 221 4 342 10 423 16 184 6 211 7 589 12 478 12 602 15 66 4 346 8...

  • Program Description: A bus has 28 seats, arranged in 7 rows and 4 columns: This seating...

    Program Description: A bus has 28 seats, arranged in 7 rows and 4 columns: This seating arrangement is mapped, row-wise, to a 1D-array of size 28: 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 0 1 2 3 21 22 23 24 25 Implement a well-structured Java program to enable a user to make and cancel seat reservations for the bus. The program uses Array to store the reservation information: 0...

  • Please post following code with criteria below... please leave as many comments as possible Exam Four:...

    Please post following code with criteria below... please leave as many comments as possible Exam Four: the election Outcome: Student will demonstrate all core competencies from this class: Program Design (design tools) Variables Decision Error Checking Looping Functions Arrays Program Specifications: Let’s pretend that we are tracking votes for the next presidential election. There will be two candidates: Ivanka Trump and Michele Obama. You can assume that there are fifty states casting votes. You will do not need to deal...

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • A bus has 28 seats, arranged in 7 rows and 4 columns: This seating arrangement is...

    A bus has 28 seats, arranged in 7 rows and 4 columns: This seating arrangement is mapped, row-wise, to a 1D-array of size 28: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 Implement a well-structured C program to enable a user to make and cancel seat reservations for the bus. The program uses a text-file seats.txt to store the reservation...

  • JAVA Create a Governor class with the following attributes: name : String party - char (the...

    JAVA Create a Governor class with the following attributes: name : String party - char (the character will be either D, R or I) ageWhenElected : int Create a State class with the following attributes: name : String abbreviation : String population : long governor : Governor Your classes will have all setters, getters, typical methods to this point (equals(), toString()) and at least 3 constructors (1 must be the default, 1 must be the copy). You will be turning...

  • Congress is made up of the house of representatives and the senate- Please help with questions...

    Congress is made up of the house of representatives and the senate- Please help with questions 5, 6, 7 and 8: Congress is made up of the House of Representatives and the Senate. Members of the House of Representatives serve two-year terms and represent a district in a state. The number of representatives for each state is determined by population. States with larger populations have more representatives than states with smaller pulations. The total number of representatives is set by...

  • Hardware Inventory – Write a database to keep track of tools, their cost and number. Your...

    Hardware Inventory – Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session. Example Program Session: (First Run)Enter request 1 - Input new tool 2 - Delete...

  • Add a Staff table to the database with the following columns

    Query1: Add a Staff table to the database with the following columns: staff_id, staff_first, staff_last, staff_phone, staff_position, hotel_id. hotel_id should be designated as a foreign key referencing the hotel table. Hint: use integer as the data type for hotelno. Make sure to designate the primary key. Query 2: Add a column to the appropriate table to record the state the hotel is in. Query 3: change the name of the Holiday Inn in Tulsa to Holiday Inn Supreme query 4:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT