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

The data file statedata. dat in the canvas files for Week 1 is a plain text file

ID: 3663817 • Letter: T

Question

The data file statedata. dat in the canvas files for Week 1 is a plain text file containing three pieces of information for each state in the United States: the name of the state the state capital the population (as of July 2015) each item is on separate line in the file, such as Alabama Montgomery 4858979 Alaska Juneau 738432 Arizona Phoenix 6828065 and so on ... Your task, after reading Appendix B- Software Flexibility, is to 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. Each class should be in its own file, within the same package in the same NetBeans project. The end of Appendix B addresses how to do this. The NetBeans project city Project from CSCI 211, shows an example of this. In the project, you can see that each of the classes - City, Vertex, Edge, etc.,... is in its own file within the project. This is included as an example in the files for Week 1. Most of what's in the project is beyond the scope of this course, but it does show you how Java projects should contain separate classes in separate files within the same project. Please ask me if you have any questions or run into any trouble.

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class States {

   String stateName, capitalName;
   int population;

   /**
   * @param stateName
   * @param capitalName
   * @param population
   */
   public States(String stateName, String capitalName, int population) {
       super();
       this.stateName = stateName;
       this.capitalName = capitalName;
       this.population = population;
   }

   /**
   * @return the stateName
   */
   public String getStateName() {
       return stateName;
   }

   /**
   * @return the capitalName
   */
   public String getCapitalName() {
       return capitalName;
   }

   /**
   * @return the population
   */
   public int getPopulation() {
       return population;
   }

   /**
   * @param stateName
   * the stateName to set
   */
   public void setStateName(String stateName) {
       this.stateName = stateName;
   }

   /**
   * @param capitalName
   * the capitalName to set
   */
   public void setCapitalName(String capitalName) {
       this.capitalName = capitalName;
   }

   /**
   * @param population
   * the population to set
   */
   public void setPopulation(int population) {
       this.population = population;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "States [stateName=" + stateName + ", capitalName="
               + capitalName + ", population=" + population + "]";
   }

}

import java.io.File;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class TestStates {
   static States statesArr[] = new States[100];
   static int count = 0;

   /**
   * @param args
   */
   public static void main(String[] args) {

       try {

           Scanner scanner = new Scanner(new File("statedata.dat"));

           while (scanner.hasNext()) {

               // System.err.println(scanner.nextLine()+" "+scanner.nextLine()+" "+Integer.parseInt(scanner.nextLine()));
               States states = new States(scanner.nextLine(),
                       scanner.nextLine(),
                       Integer.parseInt(scanner.nextLine()));
               statesArr[count] = states;
               count++;
               //

           }
           System.out.println("States Info");
           for (int i = 0; i < count; i++) {
               System.out.println((i + 1) + ". " + statesArr[i].toString());

           }

           System.out.println("Enter the Name of state to search:");
           scanner = new Scanner(System.in);
           String stateName = scanner.nextLine();
           int index = findState(stateName);
           if (index == -1) {

               System.out.println("state was not found!");
           } else {

               System.out.println(statesArr[index].toString());

           }

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }

   }

   public static int findState(String stateName) {
       int index = -1;
       for (int i = 0; i < count; i++) {

           if (statesArr[i].getStateName().equalsIgnoreCase(stateName)) {

               return i;
           }

       }

       return index;
   }

}

statedata.dat

nameofstate1
nameofcapital1
4858979
nameofstate2
nameofcapital2
4858979
nameofstate3
nameofcapital3
4858979
nameofstate4
nameofcapital4
4858979
nameofstate5
nameofcapital5
4858979

OUTPUT:

States Info
1. States [stateName=nameofstate1, capitalName=nameofcapital1, population=4858979]
2. States [stateName=nameofstate2, capitalName=nameofcapital2, population=4858979]
3. States [stateName=nameofstate3, capitalName=nameofcapital3, population=4858979]
4. States [stateName=nameofstate4, capitalName=nameofcapital4, population=4858979]
5. States [stateName=nameofstate5, capitalName=nameofcapital5, population=4858979]
Enter the Name of state to search:
nameofstate3
States [stateName=nameofstate3, capitalName=nameofcapital3, population=4858979]

States Info
1. States [stateName=nameofstate1, capitalName=nameofcapital1, population=4858979]
2. States [stateName=nameofstate2, capitalName=nameofcapital2, population=4858979]
3. States [stateName=nameofstate3, capitalName=nameofcapital3, population=4858979]
4. States [stateName=nameofstate4, capitalName=nameofcapital4, population=4858979]
5. States [stateName=nameofstate5, capitalName=nameofcapital5, population=4858979]
Enter the Name of state to search:
nameofstate6
state was not found!

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