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

package stateInformation; import java.io.BufferedReader; import java.io.FileInpu

ID: 3744034 • Letter: P

Question

package stateInformation;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.DecimalFormat;

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

}

}

STATE CLASS:

//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;

}

}

Explanation / Answer

package stateInformation;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.DecimalFormat;

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() {
    StringBuilder sbStatesInRequiredFormat = new StringBuilder();
  
    for (int i=0;i<states.length;i++)
    {
       
        sbStatesInRequiredFormat.append(states[i].abbrev + ' (' + states[i].name + ') - Population: '+ states[i].population +' ');
       
    }

return sb.toString(); //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) {

    for (var i = 0; i < states.length; i++) {
        if (states[i].abbrev === abbreviation) {
            return states[i].name;
        }
    }
    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 (var i = 0; i < states.length; i++) {
        if (states[i].name === 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 minValue = states[0].population;
    for(int i=1;i<states.length;i++){
    if(states[i] < minValue){
      minValue = states[i];
   }
}
return minValue;
  

}

/**

* 15 marks

* Function description: get the average population of all states

*

* @return: the average population

*/

public double averagePopulation() {

    int sum = 0;
    for (int i = 0; i < states.length; i++)
    {
        sum += states[i].population;
    }

    double average = sum / n;
    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 < states.length; i++)
    {
        if(states[i].population > 10000000)
            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 < states.length; i++)
    {
        if(states[i].population < 10000000)
            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;
    for (int i = 0; i < states.length; i++)
    {
        if(states[i].name.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) {
  
    State[] statesByInitial = new State[countStatesCountByInitial(initial)];
    for (int i = 0; i <states.length ; i++)
    {
        if(states[i].name.charAt(0) === initial)
            statesByInitial[i] = states[i];
    }

//you'll need to determine the size of the resulting array, populate it, and return it

return statesByInitial;

}

/**

*

* re-arrange the states in decreasing order of population

*/

public void arrageByPopulation() {

    States[] temp = new States[1];
    for(int i=0; i<states.length; i++)
    {
        for(int j=i+1; j<states.length; j++)
        {
            if(states[i].population < states[j].population)
            {
                temp = states[i];
                states[i] = states[j];
                states[j] = temp;
            }
        }
    }

}

}

STATE CLASS:

//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;

}

}