Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a software package as a NetBeans project that contains three classes: an

ID: 3676184 • Letter: C

Question

Create a software package as a NetBeans project that contains three classes:

an executable class for the project itself

a class of states, with the properties name, capital, and population

a class for an array of state objects

The state and array classes should contain appropriate properties and methods to work with those properties.

Your project’s class (with the same name as the package) should contain a main method, and methods to test your other classes as follows:

load the data from the data file into the array of states.

print the list of states -- including each state’s name, capital and population -- with the data for each state on a separate line.

a method that asks the user for the name of a state, then either displays the data for that state, or says the state was not found in the list.

The main method should simply call the other methods in the class to show that they work.

Explanation / Answer


StatesApp.java


import java.util.*;
import java.lang.*;

public class StatesApp {
   static States[] usa = new States[50]; // array of 50 States

    public static void main(String[] args) throws Exception {
        load();
        print();
        input();

    }//end of main()
//==============================================================================
    /**
     * Load the data from the data file into the array of states.
     *
     * @throws Exception
     */
    static void load() throws Exception {
        //sending empty array to loadArray
        Array.loadArray(usa);
    }//end load()
//==============================================================================
  
    static void print() throws Exception {
        System.out.println(Arrays.toString(usa));

    }// end print()
//==============================================================================
    /**
     * Asks the user for the name of a state, then either displays
     * the data for that state, or says the state was not found in the list.
     */
    static void input() {
        String target;         // the city for which we are searching
        int n;                 // loop counter
        char c;                // used to check the first letter
        boolean found = false; // true if the target city is found in the array

        // set up input stream from the keyboard
        Scanner keyboard = new Scanner(System.in);

        // print intro message
        System.out.print("Please Enter the name of the state you are searching for. ");

        // get the target name of the state from the user
        System.out.print("State: ");
        target = keyboard.nextLine();
        c = target.charAt(0);   //looks at the first letter

        // search array of US states
        // the loop coninues to the end of the array if the city is not found
        for (n = 0; (!found) && (n < usa.length); n++) {
            States us = usa[n];//sets class object to the current position

            if (!usa[n].toString().contains(target)) { //first checks for this string
            } else if (us.getState().matches(target)) { //then checks if it's a state
                //print found message and set found to true
                System.out.println(usa[n]);
                found = true;
            } // end if

        } // for loop

        if (!found) {
            System.out.println(target + " was not found.");
        }//end if
        if (!Character.isUpperCase(c)) {
            System.out.println("All states should be capitalized correctly"
                    + ", Please try again. ");
        }//end if
    }//end input()
}//end class StatesApp

States.java
class States {

    private String state;
    private String capital;
    private int population;

    //**************************************************************************
    // constructors
    States() {

    }

    public States(String state, String capital, int population) {
        this.state = state;
        this.capital = capital;
        this.population = population;

    } // end States((String state, String capital, int population)
    //**************************************************************************
    // accesors

    public String getState() {
        return state;
    } //end getState()

    public String getCapital() {
        return capital;
    } //end getCapital()

    public int getPopulation() {
        return population;
    } //end getPopulation()

    //**************************************************************************
    // a method to return the States's data as a String
    public String toString() {
        String info;
        info = (capital + ", " + state + " " + population + " ");
        return info;
    } //end toString()

  

} // end class States


Array.java
import java.io.File;
import java.util.*;
import java.lang.*;

public class Array {
  
    public static void loadArray(States[] usa) throws Exception {
        // a loop counter
        int i;

        // declare temporary variables to hold
        String st; //state
        String ca; //capital
        int pop;    //population

        // Create a File class object linked to the name of the file to be read
        File stateFile = new File("statedata.txt");

        // Create a Scanner named infile to read the input stream from the file
        Scanner infile = new Scanner(stateFile);

        //loop to read data in the array
        for (i = 0; i < 50; i++) {
            //read in temporary variables
            st = infile.nextLine();
            ca = infile.nextLine();
            pop = Integer.parseInt(infile.nextLine());

            //initialize each variable with the States contructor
            usa[i] = new States(st, ca, pop);
        }//end for
        infile.close();
    }//end loadArray()
}//end class Array


statedata.txt
Alabama
Montgomery
4858979
Alaska
Juneau
738432
Arizona
Phoenix
6828065
Arkansas
Little Rock
2978204
California
Sacramento
39144818
Colorado
Denver
5456574
Connecticut
Hartford
3590886
Delaware
Dover
945934
Florida
Tallahassee
20271272
Georgia
Atlanta
10214860
Hawaii
Honolulu
1431603
Idaho
Boise
1654930
Illinois
Springfield
12859995
Indiana
Indianapolis
6619680
Iowa
Des Moines
3123899
Kansas
Topeka
2911641
Kentucky
Frankfort
4425092
Louisiana
Baton Rouge
4670724
Maine
Augusta
1329328
Maryland
Annapolis
6006401
Massachusetts
Boston
6794422
Michigan
Lansing
9922576
Minnesota
Saint Paul
5489594
Mississippi
Jackson
2992333
Missouri
Jefferson City
6083672
Montana
Helena
1032949
Nebraska
Lincoln
1896190
Nevada
Carson City
2890845
New Hampshire
Concord
1330608
New Jersey
Trenton
8958013
New Mexico
Santa Fe
2085109
New York
Albany
19795791
North Carolina
Raleigh
10042802
North Dakota
Bismarck
756927
Ohio
Columbus
11613423
Oklahoma
Oklahoma City
3911338
Oregon
Salem
4028977
Pennsylvania
Harrisburg
12802503
Rhode Island
Providence
1056298
South Carolina
Columbia
4896146
South Dakota
Pierre
858469
Tennessee
Nashville
6600299
Texas
Austin
27469114
Utah
Salt Lake City
2995919
Vermont
Montpelier
626042
Virginia
Richmond
8382993
Washington
Olympia
7170351
West Virginia
Charleston
1844128
Wisconsin
Madison
5771337
Wyoming
Cheyenne
586107


sample output

Nashville, Tennessee 6600299                                                                                                                              
, Austin, Texas 27469114                                                                                                                                    
, Salt Lake City, Utah 2995919                                                                                                                              
, Montpelier, Vermont 626042                                                                                                                                
, Richmond, Virginia 8382993                                                                                                                                
, Olympia, Washington 7170351                                                                                                                               
, Charleston, West Virginia 1844128                                                                                                                         
, Madison, Wisconsin 5771337                                                                                                                                
, Cheyenne, Wyoming 586107                                                                                                                                  
]                                                                                                                                                           
Please Enter the name of the state you are searching for.                                                                                                   
                                                                                                                                                            
State: Wyoming                                                                                                                                              
Cheyenne, Wyoming 586107                                                                                                                                    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote