package stateInformation; import java.io.BufferedReader; public class AllStates
ID: 3744502 • Letter: P
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 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
*/
}
}
Explanation / Answer
please give thumbs up, thanks
CLIENT.JAVA
/**
*
* @author VISHAL
*/
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
String filename="data/AustraliaStateData.txt";
AllStates S=new AllStates(filename);
//* 2. Display the toString() output of the object in the format specified in the pdf specifications
S.toString();
//* 3. Display the state whose abbreviation is NSW in the format specified in the pdf specifications
S.getNameByAbbreviation("NSW");
//* 4. Display if Victoria exists or not in the format specified in the pdf specifications
System.out.println(S.exists("Victoria"));
//* 5. Display the least population state in the format specified in the pdf specifications
System.out.println(S.leastPopulatedState());
//* 6. Display the average population up to 2 decimal places of precision using DecimalFormat
System.out.println(S.averagePopulation());
//* (more details in pdf specifications)
//* 7. Display the number of "large" states in the format specified in the pdf specifications
System.out.println(S.getLargeStateCount());
//* 8. Display the number of "small" states in the format specified in the pdf specifications
System.out.println(S.getSmallStateCount());
//* 9. Display the number of states that begin with an 'A' in the format specified in the pdf specifications
System.out.println(String.valueOf(S.getNameByAbbreviation("A")));
}
}
ALLSTATES.JAVA
/**
*
* @author VISHAL
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class AllStates {
private State[] states; //holds an array of State objects
private int n; //hold number of states
//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++;
}
n=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() {
String output="";
for(int i=0; i<this.n; i++)
{
output=output+states[i].toString()+" ";
}
return output;
}
/**
*
* Function description: Find the state name by abbreviation in the dataset
* , abbreviation
* @param 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) {
for(int i=0; i<this.n; i++)
{
if(states[i].getAbbrev()==abbreviation)
return states[i].getName();
}
return "No state with given abbreviation exists";
}
/**
*
* 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) {
for(int i=0; i<this.n; i++)
{
if(states[i].getName()==name)
return true;
}
return false;
}
/**
*
* Function description: get the state that has the lowest population
*
* @return: the state name with the minimum population
*/
public String leastPopulatedState() {
int min=Integer.MAX_VALUE;
int index=-1;
for(int i=0; i<this.n; i++)
{
if(states[i].getPopulation()<min)
{
min=states[i].getPopulation();
index=i;
}
}
return states[index].getName();
}
/**
* 15 marks
* Function description: get the average population of all states
*
* @return: the average population
*/
public double averagePopulation() {
double total=0.0,average;
for(int i=0; i<this.n; i++)
{
total+=states[i].getPopulation();
}
average=total/(this.n*1.0);
return average;
}
/**
*
* 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() {
int count=0;
for(int i=0; i<this.n; i++)
{
if(states[i].isLargeState())
count++;
}
return count;
}
/**
*
* 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() {
int count=0;
for(int i=0; i<this.n; i++)
{
if(states[i].isSmallState())
count++;
}
return count;
}
/**
*
* 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) {
int count=0;
for(int i=0; i<this.n; i++)
{
if(states[i].getName().charAt(0)==initial)
count++;
}
return count;
}
/**
*
* 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
int count=this.countStatesCountByInitial(initial);
State[] newstates=new State[count];
for(int i=0,j=0; i<this.n; i++)
{
if(states[i].getName().charAt(i)==initial)
newstates[j++]=states[i];
}
return newstates;
}
/**
*
* re-arrange the states in decreasing order of population
*/
public void arrageByPopulation() {
for(int i=0; i<this.n-1; i++)
{
for(int j=0; j<this.n-1-i; j++)
{
if(states[j].getPopulation()<states[j+1].getPopulation())
{
State temp=states[j];
states[j]=states[j+1];
states[j+1]=temp;
}
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.