There are 3 files 1. client.java(first file) package stateInformation; import ja
ID: 3744423 • Letter: T
Question
There are 3 files
1. client.java(first file)
package stateInformation;
import java.io.IOException;
import java.text.DecimalFormat;
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
*/
}
}
2. graded.java (second file)
package stateInformation;
//DO NOT MODIFY THIS CLASS
public @interface Graded {
int marks();
String description();
}
3. state.java (third file)
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;
}
}
// thankyou for your help!!
Client After you pass all the tests, you must complete the tasks listed in Client.java These will be manually marked and are worth 20%. If all tasks are successfully completed, the output should look like the following All states: NSW (New South Wales) Population: 7704300 VIC (victoria)-Population: 6039100 SA (South Australia)-Population: 1706500 OL (Queensland)Population: 4827000 TAS (Tasmania)-Population: 518500 WA (Western AustraliaPopulation: 2613700 ACT (Australian Capital Territory)-Population: 397397 NT (Northern Territory)-Population: 244000 NSW is the abbreviation for: New South Wales victoria exists: true Least populated state: Northern Territory Average population: 3006312.12 Number of states with over 10mn people: Number of states with less than 1mn people: 3 Number of states with initials A': 1 The average population should be dispalyed up to two decimal places. A floating-point number val is displayed to two decimal places using DecimalFormat df new DecimalFormat ("#.00"); System.out-println (df.format (val)) You need to add following import statement to use DecimalFormat import java.text.DecimalFormat;Explanation / Answer
All the methods of AllStates.java required in order are defined below:
/**
* 1)
* Function description: get the average population of all states
*
* @return: the average population
*/
public double averagePopulation() {
double totalPopulation = 0.0;
for(int i = 0; i < this.states.length; i++){
totalPopulation += this.states[i].getPopulation();
}
DecimalFormat df = new DecimalFormat("#.00");
//Calculating average population and returning it
String averagePopulation = df.format(totalPopulation/this.states.length);
return Double.parseDouble(averagePopulation);
}
/**
*
* 2)
* 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.states.length; i++){
if(this.states[i].getName().equals(name))
return true;
}
return false;
}
/**
* 3)
* 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 numberOfStates = 0;
for(int i = 0; i < this.states.length; i++){
if(this.states[i].getPopulation() > 10000000)
numberOfStates++;
}
return numberOfStates;
}
/**
* 4)
* 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 numberOfStates = 0;
for(int i = 0; i < this.states.length; i++){
if(this.states[i].getPopulation() < 10000000){
numberOfStates++;
}
}
return numberOfStates;
}
/**
* 5)
* 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(int i = 0; i < this.states.length; i++){
if(this.states[i].getAbbrev().equals(abbreviation))
return this.states[i].getName();
}
return "No state with given abbreviation exists";
}
/**
* 6)
* 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 numberOfStates = 0;
for(int i = 0; i < this.states.length; i++){
if(this.states[i].getName.charAt(0) == initial){
numberOfStates++;
}
}
return numberOfStates;
}
/**
* 7)
* Function description: get the state that has the lowest population
*
* @return: the state name with the minimum population
*/
public String leastPopulatedState() {
//Consider the first city to be least populated initially
int population = this.states[0].getPopulation();
String name = this.states[0].getName();
//Check if any other city with less population exist
for(int i = 1; i < this.states.length; i++){
if(this.states[i].getPopulation() < population){
population = this.states[i].getPopulation();
name = this.states[i].getName();
}
}
return name;
}
/**
* 8)
* 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 allStateInformation = "";
for(int i = 0; i < this.states.length; i++){
allStateInformation += this.states[i].toString()+" ";
}
return allStateInformation;
}
/**
* 9)
* 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
//Function defined previously already does this job of finding total states with initials
int numberOfStates = countStatesCountByInitial(initial);
State[] statesWithInitial = new State[numberOfStates];
//Counter for populating statesWithInitial array
int j=0;
for(int i = 0; i < this.states.length; i++){
if(this.states[i].getName.charAt(0) == initial){
statesWithInitial[j] = this.states[i];
j++;
}
}
return statesWithInitial;
}
/**
* 10)
* re-arrange the states in decreasing order of population
*/
public void arrageByPopulation() {
/**
* Bubble sort implementation to sort the states array based on the population.
* With every pass(i.e iteration of outer loop) the state with highest population goes to the end of states array
*/
int n = this.states.length;
for (int i = 0; i < n-1; i++){
for (int j = 0; j < n-i-1; j++){
if (this.states[j].getPopulation() > this.states[j+1].getPopulation()){
//Creating a new temp State from the state at index j
State tempState = new State(this.states[j].getAbbrev(), this.states[j].getName(), this.states[j].getPopulation());
this.states[j] = this.states[j+1];
this.states[j+1] = tempState;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.