Question
package stateInformation; import java.io.BufferedReader; public class AllStates { private State[] states; //holds an array of State objects //go through week 4 lecture notes to study
package stateInformation;
import java.io.BufferedReader;
public class AllStates {
private State[] states; //holds an array of State objects
//go through week 4 lecture notes to study this topic
//DO NOT MODIFY
public AllStates(String filename) throws IOException {
load(filename);
}
/*
* DO NOT MODIFY - This method reads information about the states from the file
* and stores it in the array of State objects
*/
public void load(String filename) throws IOException{
FileInputStream inputStream1 = new FileInputStream(filename);
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(inputStream1));
int count = 0;
while(bufferedReader1.readLine() != null) {
count++;
}
bufferedReader1.close();
FileInputStream inputStream2 = new FileInputStream(filename);
BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(inputStream2));
states = new State[count];
String line = null;
for(int i=0; i < count; i++) {
line = bufferedReader2.readLine();
String tokens[] = line.split(",");
String abbrev = tokens[0];
String name = tokens[1];
int population = Integer.parseInt(tokens[2]);
states[i] = new State(abbrev, name, population);
}
bufferedReader2.close();
}
/**
* 10 marks
* Function description: print a list of all states
* @return: String representing the states
* The example of the format:
* "NSW (New South Wales) - Population: 7704300
* VIC (Victoria) - Population: 6039100
* SA (South Australia) - Population: 1706500"
*/
public String toString() {
return ""; //to be completed
}
/**
*
* Function description: Find the state name by abbreviation in the dataset
* , abbreviation
* @return: the state name if we can find the state name by the input abbreviation,
* else return "No state with given abbreviation exists"
*/
public String getNameByAbbreviation(String abbreviation) {
return ""; //to be completed
}
/**
*
* Function description: find whether the given state is in the dataset
* , name
* @return: If the state exists in the dataset, return true, else return false
*/
public boolean exists(String name) {
return false; //to be completed
}
/**
*
* Function description: get the state that has the lowest population
*
* @return: the state name with the minimum population
*/
public String leastPopulatedState() {
return ""; //to be completed
}
/**
* 15 marks
* Function description: get the average population of all states
*
* @return: the average population
*/
public double averagePopulation() {
return 0; //to be completed
}
/**
*
* Function description: find the states that the population is higher than 10000000 (10 million)
*
* @return: the number of state with the population more than 10000000 (10 million)
*/
public int getLargeStateCount() {
return 0; //to be completed
}
/**
*
* Function description: find the states that the population is less than 1000000 (1 million)
*
* @return: the number of state with the population less than 1000000 (1 million)
*/
public int getSmallStateCount() {
return 0; //to be completed
}
/**
*
* Function description: determine the number of states that the names start with initial passed
* @param: initial: first letter of required states
* @return: return the number of state where the state names start with initial passed
*/
public int countStatesCountByInitial(char initial) {
return 0; //to be completed
}
/**
*
* Function description: find the states that the names start with initial passed
* @param: initial: first letter of required states
* @return: return the states where the state names start with initial passed
*/
public State[] getStatesCountByInitial(char initial) {
//you'll need to determine the size of the resulting array, populate it, and return it
return new State[] {}; //to be completed
}
/**
*
* re-arrange the states in decreasing order of population
*/
public void arrageByPopulation() {
//to be completed
}
}
FOR STATE CLASS (STATE.java):
package stateInformation;
//DO NOT MODIFY THIS CLASS
/**
*
* State is defined by its name, abbreviation and population
*
*/
public class State {
private String abbrev, name;
private int population;
public String getAbbrev() {
return abbrev;
}
public void setAbbrev(String abbrev) {
this.abbrev = abbrev;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = Math.max(0, population);
}
/**
* constructor
* @param abbrev
* @param name
* @param population
*/
public State(String abbrev, String name, int population) {
setAbbrev(abbrev);
setName(name);
setPopulation(population);
}
/**
*
* @return true if state is "large", defined as population of more than 10 million, return false otherwise
*/
public boolean isLargeState() {
return population > 10000000;
}
/**
*
* @return true if state is "small", defined as population of less than 1 million, return false otherwise
*/
public boolean isSmallState() {
return population < 1000000;
}
/**
* String representation of a State objects
*/
public String toString() {
return abbrev+" ("+name+") - Population: "+population;
}
}
package stateInformation;
FOR CLIENT CLASS (CLIENT.java):
import java.io.IOException;
public class Client {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
/*
* 1. create object states of class AllStates populated with file data/AustraliaStateData.txt
* 2. Display the toString() output of the object in the format specified in the pdf specifications
* 3. Display the state whose abbreviation is NSW in the format specified in the pdf specifications
* 4. Display if Victoria exists or not in the format specified in the pdf specifications
* 5. Display the least population state in the format specified in the pdf specifications
* 6. Display the average population up to 2 decimal places of precision using DecimalFormat
* (more details in pdf specifications)
* 7. Display the number of "large" states in the format specified in the pdf specifications
* 8. Display the number of "small" states in the format specified in the pdf specifications
* 9. Display the number of states that begin with an 'A' in the format specified in the pdf specifications
*/
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started