Netbeans Java. Well documented. The code must read the file. The data file state
ID: 3663880 • Letter: N
Question
Netbeans Java. Well documented. The code must read the file.
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
// StateProject
package stateproject;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author PINERONE
*/
public class StateProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
// TODO code application logic here
controlState c = new controlState("D:\Documents and Settings\pinerone\Mis documentos\NetBeansProjects\StateProject\src\stateproject\statedata.dat");
c.PrintStates();
String result = c.findState();
System.out.println(result);
} catch (IOException ex) {
Logger.getLogger(StateProject.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error al abrir el archivo.");
}
}
}
// class controlState
package stateproject;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author PINERONE
*/
public class controlState {
// variables
private state States[];
private List list;
private List<state> listStates;
// construct 1
public controlState(){
States = null;
list = null;
}
// construct 2 + filePath Data
public controlState(String filePath) throws IOException{
if (openFile(filePath)){
loadData();
}
}
// open file
private boolean openFile(String filePath) throws IOException{
FileReader f = null;
list = new ArrayList();
// cathing the exceptions
try {
String cadena;
f = new FileReader(filePath); // open file
BufferedReader b = new BufferedReader(f);
while((cadena = b.readLine())!=null) { //read date file
list.add(cadena);
System.out.println(cadena);
} b.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(controlState.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("impossible to open 1");
return false;
} finally {
try {
f.close();
} catch (IOException ex) {
Logger.getLogger(controlState.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("impossible to open 2");
return false;
}
}
return true;
}
// load Data in array
private void loadData(){
States = new state[list.size()/3];
int k=0;
for (int i=0; i<list.size(); i+=3){
String s=list.get(i).toString();
String c=list.get(i+1).toString();
int p = Integer.parseInt(list.get(i+2).toString());
// load in array state
States[k++] = new state(s,c,p);
}
System.out.println(States.length);
}
// print States values
public void PrintStates(){
if (States.length > 0){
System.out.println("States from United States");
System.out.println();
for (int i=0; i<States.length; i++){
System.out.println("State: " + States[i].getState() + ", Capital: " + States[i].getCapital() + ", Population: " + States[i].getPopulation());
}
}
}
// find a state
public String findState(){
Scanner sc = new Scanner(System.in); //create an object Scanner
String name;
System.out.print("Enter state's name: ");
name = sc.nextLine(); //read a String
for (int i=0; i<States.length; i++){
// compare to state's name
if (States[i].getState().toUpperCase().equals(name.toUpperCase())){
return "Been Found State: " + name;
}
}
return "No Found State.";
}
}
// class state
package stateproject;
/**
*
* @author PINERONE
*/
public class state {
// variables
private String state;
private String capital;
private int population;
// construct 1
public state(){
state = null;
capital = null;
population = 0;
}
// construct 2
public state(String state, String capital, int population){
this.state = state;
this.capital = capital;
this.population = population;
}
// construct 3
public state(state s){
this.state = s.state;
this.capital = s.capital;
this.population = s.population;
}
// edit Data
public boolean editData(String state, String capital, int population){
boolean result = true;
this.state = state;
this.capital = capital;
this.population = population;
return result;
}
// get State name
public String getState(){
return state;
}
// get Capital name
public String getCapital(){
return capital;
}
// get Population number
public int getPopulation(){
return population;
}
}
// statedata.dat
Alabama
Montgomery
4858979
Alaska
Juneau
738432
Arizona
Phoenix
6828065