The first assignments deals with the statistical data of population in any count
ID: 3744078 • Letter: T
Question
The first assignments deals with the statistical data of population in any country. Two samples are provided, the first for USA (in file data/USAStateData.txt) and the second for Australia (in file data/AustraliastateData.txt The class Allstates reads the contents of the file through the method load(String) populates the instance variable states which is an array of state objects. and Your job is perform some operations on this array based on method javadoc comments (comment above the method header) Marking The methods that you need to complete are given below, in the order we suggest you complete them. Each test is worth 8 marks. taveragePopulation(): double +exists): boolean tgetLargestatecount): int +getsmal1statecount(): int +getNameByAbbreviation(): String tcountStatescountByInitial: int +leastPopulatedstate: String +tostring: String tgetstatescountByInitial: Statetl tarrangeByPopulation: void Note that we have used UML syntax to list the methods:Explanation / Answer
CODE :
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author VISHAL
*/
public class AllStates {
public static class State
{
String Abbrevaiation;
String Fullname;
int polpulation;
State(String A,String B,String C)
{
this.Abbrevaiation=A;
this.Fullname=B;
this.polpulation=Integer.parseInt(C);
}
String get_Abbreviation()
{
return this.Abbrevaiation;
}
String get_Fullname()
{
return this.Fullname;
}
int get_population()
{
return this.polpulation;
}
}
ArrayList<State> S;
AllStates()
{
S=new ArrayList<State>();
}
double averagecount()
{
double total=0.0;
for(int i=0; i<S.size(); i++)
{
State C=S.get(i);
total=total+C.get_population();
}
double average=total/S.size();
return average;
}
boolean exist(String name)
{
for(int i=0; i<S.size(); i++)
{
State C=S.get(i);
if(C.get_Fullname()==name)
return true;
}
return false;
}
int getLargeStateCount()
{
int large=Integer.MIN_VALUE;
for(int i=0; i<S.size(); i++)
{
State C=S.get(i);
if(C.get_population()>large)
large=C.get_population();
}
return large;
}
int getSmallStateCount()
{
int small=Integer.MAX_VALUE;
for(int i=0; i<S.size(); i++)
{
State C=S.get(i);
if(C.get_population()<small)
small=C.get_population();
}
return small;
}
String getnameByAbbreviation(String Abb)
{
for(int i=0; i<S.size(); i++)
{
State C=S.get(i);
if(C.get_Abbreviation()==Abb)
return C.get_Fullname();
}
return null;
}
int countStatesCountbyInitial()
{
return S.size();
}
String leastPopulateState()
{
int p=this.getSmallStateCount();
for(int i=0; i<S.size(); i++)
{
State C=S.get(i);
if(C.get_population()==p)
return C.get_Fullname();
}
return null;
}
@Override
public String toString()
{
String str="";
for(int i=0; i<S.size(); i++)
{
State C=S.get(i);
str=str+C.get_Abbreviation()+" ("+C.get_Fullname()+" ) - Population "+C.get_population()+" ";
}
return str;
}
void arrangeByPopulation()
{
for(int i=0; i<S.size()-1; i++)
{
for(int j=0; j<S.size()-1-i; j++)
{
if(S.get(j).get_population()>S.get(j+1).get_population())
{
State temp=S.get(j);
S.add(j, S.get(j+1));
S.add(j+1, temp);
}
}
}
}
public static void main(String[]args) throws FileNotFoundException
{
AllStates A=new AllStates();
Scanner sc=new Scanner (new File("INPUTFILE.txt"));
while(sc.hasNext())
{
String line=sc.nextLine();
String temp[]=line.split(",");
State C=new State(temp[0],temp[1],temp[2]);
A.S.add(C);
}
A.toString();
}
}
please give thumbs up, thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.