Answered step by step
Verified Expert Solution
Question
1 Approved Answer
pls help me with this java questionty so much teachers these are the files in the folder these are the following code of each files
pls help me with this java questionty so much teachers
these are the files in the folder
these are the following code of each files
ALLSTATES.JAVA
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
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
}
}
ALLSTATESTEST.JAVA
package stateInformation;
import static org.junit.Assert.*;
import java.io.*;
import java.text.*;
import java.util.*;
import org.junit.*;
//DO NOT MODIFY THIS CLASS
public class AllStatesTest {
private static final int mark = 8;
private static int marks = 0;
private static String feedback = "";
private AllStates usStates;
private AllStates australiaStates;
@Before
public void beforeEachTest() throws IOException {
usStates = new AllStates("data/USAStateData.txt");
australiaStates = new AllStates("data/AustraliaStateData.txt");
}
@Test @Graded(marks = mark, description = "toString")
public void testToString() {
assertEquals("AL (Alabama) - Population: 4860545 " +
"AK (Alaska) - Population: 741522 " +
"AZ (Arizona) - Population: 6908642 " +
"AR (Arkansas) - Population: 2988231 " +
"CA (California) - Population: 39296476 " +
"CO (Colorado) - Population: 5530105 " +
"CT (Connecticut) - Population: 3587685 " +
"DE (Delaware) - Population: 952698 " +
"DC (District of Columbia) - Population: 684336 " +
"FL (Florida) - Population: 20656589 " +
"GA (Georgia) - Population: 10313620 " +
"HI (Hawaii) - Population: 1428683 " +
"ID (Idaho) - Population: 1680026 " +
"IL (Illinois) - Population: 12835726 " +
"IN (Indiana) - Population: 6634007 " +
"IA (Iowa) - Population: 3130869 " +
"KS (Kansas) - Population: 2907731 " +
"KY (Kentucky) - Population: 4436113 " +
"LA (Louisiana) - Population: 4686157 " +
"ME (Maine) - Population: 1330232 " +
"MD (Maryland) - Population: 6024752 " +
"MA (Massachusetts) - Population: 6823721 " +
"MI (Michigan) - Population: 9933445 " +
"MN (Minnesota) - Population: 5525050 " +
"MS (Mississippi) - Population: 2985415 " +
"MO (Missouri) - Population: 6091176 " +
"MT (Montana) - Population: 1038656 " +
"NE (Nebraska) - Population: 1907603 " +
"NV (Nevada) - Population: 2939254 " +
"NH (New Hampshire) - Population: 1335015 " +
"NJ (New Jersey) - Population: 8978416 " +
"NM (New Mexico) - Population: 2085432 " +
"NY (New York) - Population: 19836286 " +
"NC (North Carolina) - Population: 10156689 " +
"ND (North Dakota) - Population: 755548 " +
"OH (Ohio) - Population: 11622554 " +
"OK (Oklahoma) - Population: 3921207 " +
"OR (Oregon) - Population: 4085989 " +
"PA (Pennsylvania) - Population: 12787085 " +
"RI (Rhode Island) - Population: 1057566 " +
"SC (South Carolina) - Population: 4959822 " +
"SD (South Dakota) - Population: 861542 " +
"TN (Tennessee) - Population: 6649404 " +
"TX (Texas) - Population: 27904862 " +
"UT (Utah) - Population: 3044321 " +
"VT (Vermont) - Population: 623354 " +
"VI (Virginia) - Population: 8414380 " +
"WA (Washington) - Population: 7280934 " +
"WV (West Virginia) - Population: 1828637 " +
"WI (Wisconsin) - Population: 5772917 " +
"WY (Wyoming) - Population: 584910 " +
"PR (Puerto Rico) - Population: 3406520 ", usStates.toString());
assertEquals("NSW (New South Wales) - Population: 7704300 " +
"VIC (Victoria) - Population: 6039100 " +
"SA (South Australia) - Population: 1706500 " +
"QL (Queensland) - Population: 4827000 " +
"TAS (Tasmania) - Population: 518500 " +
"WA (Western Australia) - Population: 2613700 " +
"ACT (Australian Capital Territory) - Population: 397397 " +
"NT (Northern Territory) - Population: 244000 ", australiaStates.toString());
marks+=mark;
feedback=feedback+mark+" marks: toString passed ";
}
@Test @Graded(marks = mark, description = "getNameByAbbreviation")
public void testGetNameByAbbreviation() {
assertEquals("Alabama", usStates.getNameByAbbreviation("AL"));
assertEquals("Delaware", usStates.getNameByAbbreviation("DE"));
assertEquals("Florida", usStates.getNameByAbbreviation("FL"));
assertEquals("Georgia", usStates.getNameByAbbreviation( "GA"));
assertEquals("Idaho", usStates.getNameByAbbreviation("ID"));
assertEquals("Louisiana", usStates.getNameByAbbreviation("LA"));
assertEquals("Delaware", usStates.getNameByAbbreviation("DE"));
assertEquals("Nebraska", usStates.getNameByAbbreviation("NE"));
assertEquals("Pennsylvania", usStates.getNameByAbbreviation("PA"));
assertEquals("South Carolina", usStates.getNameByAbbreviation("SC"));
assertEquals("Victoria", australiaStates.getNameByAbbreviation("VIC"));
marks+=mark;
feedback=feedback+mark+" marks: getNameByAbbreviation passed ";
}
@Test @Graded(marks = mark, description = "exists")
public void testExists() {
assertTrue(usStates.exists("Alabama"));
assertTrue(usStates.exists("Georgia"));
assertTrue(usStates.exists("Kansas"));
assertTrue(usStates.exists("Minnesota"));
assertTrue(usStates.exists("Nevada"));
assertFalse(australiaStates.exists("Nevada"));
assertTrue(australiaStates.exists("Tasmania"));
//even though it might be easy to forget sometimes, it's still there
marks+=mark;
feedback=feedback+mark+" marks: exists passed ";
}
@Test @Graded(marks = mark, description = "leastPopulatedState")
public void testLeastPopulatedState() throws IOException {
assertEquals("Wyoming", usStates.leastPopulatedState());
assertEquals("Northern Territory", australiaStates.leastPopulatedState());
marks+=mark;
feedback=feedback+mark+" marks: leastPopulatedState passed ";
}
@Test @Graded(marks = mark, description = "average")
public void testAveragePopulation() {
assertEquals(6284854.9, usStates.averagePopulation(), 0.01);
assertEquals(3006312.125, australiaStates.averagePopulation(), 0.01);
marks+=mark;
feedback=feedback+mark+" marks: average passed ";
}
@Test @Graded(marks = mark, description = "getLargeStateCount")
public void testGetLargeStateCount() {
assertEquals(9, usStates.getLargeStateCount());
assertEquals(0, australiaStates.getLargeStateCount());
marks+=mark;
feedback=feedback+mark+" marks: getLargeStateCount passed ";
}
@Test @Graded(marks = 15, description = "getSmallStateCount")
public void testGetSmallStateCount() {
assertEquals(7, usStates.getSmallStateCount());
assertEquals(3, australiaStates.getSmallStateCount());
marks+=mark;
feedback=feedback+mark+" marks: getSmallStateCount passed ";
}
@Test @Graded(marks = mark, description = "countStatesCountByInitial")
public void testCountStatesCountByInitial() {
assertEquals(4, usStates.countStatesCountByInitial('A'));
assertEquals(0, usStates.countStatesCountByInitial('Z'));
assertEquals(2, australiaStates.countStatesCountByInitial('N'));
assertEquals(1, australiaStates.countStatesCountByInitial('V'));
marks+=mark;
feedback=feedback+mark+" marks: countStatesCountByInitial passed ";
}
@Test @Graded(marks = mark, description = "getStatesCountByInitial")
public void testGetStatesCountByInitial() {
State[] result = usStates.getStatesCountByInitial('A');
assertEquals(4, result.length);
assertEquals("[AL (Alabama) - Population: 4860545, AK (Alaska) - Population: 741522, AZ (Arizona) - Population: 6908642, AR (Arkansas) - Population: 2988231]", Arrays.toString(result));
result = usStates.getStatesCountByInitial('X');
assertEquals(0, result.length);
assertEquals("[]", Arrays.toString(result));
result = australiaStates.getStatesCountByInitial('N');
assertEquals(2, result.length);
assertEquals("[NSW (New South Wales) - Population: 7704300, NT (Northern Territory) - Population: 244000]", Arrays.toString(result));
marks+=mark;
feedback=feedback+mark+" marks: getStatesCountByInitial passed ";
}
@Test @Graded(marks = mark, description = "arrangeByPopulation")
public void testArrangeByPopulation() {
usStates.arrageByPopulation();
assertEquals("CA (California) - Population: 39296476 " +
"TX (Texas) - Population: 27904862 " +
"FL (Florida) - Population: 20656589 " +
"NY (New York) - Population: 19836286 " +
"IL (Illinois) - Population: 12835726 " +
"PA (Pennsylvania) - Population: 12787085 " +
"OH (Ohio) - Population: 11622554 " +
"GA (Georgia) - Population: 10313620 " +
"NC (North Carolina) - Population: 10156689 " +
"MI (Michigan) - Population: 9933445 " +
"NJ (New Jersey) - Population: 8978416 " +
"VI (Virginia) - Population: 8414380 " +
"WA (Washington) - Population: 7280934 " +
"AZ (Arizona) - Population: 6908642 " +
"MA (Massachusetts) - Population: 6823721 " +
"TN (Tennessee) - Population: 6649404 " +
"IN (Indiana) - Population: 6634007 " +
"MO (Missouri) - Population: 6091176 " +
"MD (Maryland) - Population: 6024752 " +
"WI (Wisconsin) - Population: 5772917 " +
"CO (Colorado) - Population: 5530105 " +
"MN (Minnesota) - Population: 5525050 " +
"SC (South Carolina) - Population: 4959822 " +
"AL (Alabama) - Population: 4860545 " +
"LA (Louisiana) - Population: 4686157 " +
"KY (Kentucky) - Population: 4436113 " +
"OR (Oregon) - Population: 4085989 " +
"OK (Oklahoma) - Population: 3921207 " +
"CT (Connecticut) - Population: 3587685 " +
"PR (Puerto Rico) - Population: 3406520 " +
"IA (Iowa) - Population: 3130869 " +
"UT (Utah) - Population: 3044321 " +
"AR (Arkansas) - Population: 2988231 " +
"MS (Mississippi) - Population: 2985415 " +
"NV (Nevada) - Population: 2939254 " +
"KS (Kansas) - Population: 2907731 " +
"NM (New Mexico) - Population: 2085432 " +
"NE (Nebraska) - Population: 1907603 " +
"WV (West Virginia) - Population: 1828637 " +
"ID (Idaho) - Population: 1680026 " +
"HI (Hawaii) - Population: 1428683 " +
"NH (New Hampshire) - Population: 1335015 " +
"ME (Maine) - Population: 1330232 " +
"RI (Rhode Island) - Population: 1057566 " +
"MT (Montana) - Population: 1038656 " +
"DE (Delaware) - Population: 952698 " +
"SD (South Dakota) - Population: 861542 " +
"ND (North Dakota) - Population: 755548 " +
"AK (Alaska) - Population: 741522 " +
"DC (District of Columbia) - Population: 684336 " +
"VT (Vermont) - Population: 623354 " +
"WY (Wyoming) - Population: 584910 ", usStates.toString());
australiaStates.arrageByPopulation();
assertEquals("NSW (New South Wales) - Population: 7704300 " +
"VIC (Victoria) - Population: 6039100 " +
"QL (Queensland) - Population: 4827000 " +
"WA (Western Australia) - Population: 2613700 " +
"SA (South Australia) - Population: 1706500 " +
"TAS (Tasmania) - Population: 518500 " +
"ACT (Australian Capital Territory) - Population: 397397 " +
"NT (Northern Territory) - Population: 244000 ", australiaStates.toString());
marks+=mark;
feedback=feedback+mark+" marks: arrangeByPopulation passed ";
}
//log reporting
@AfterClass
public static void wrapUp() throws IOException {
System.out.println(feedback.substring(0,feedback.length()-1));
System.out.println("--------------------------------------------");
System.out.println("Total: "+marks+" out of 80");
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
File file = new File("report"+timeStamp+".txt");
FileWriter writer = new FileWriter(file);
writer.write(feedback.substring(0,feedback.length()-1)+" ");
writer.write("-------------------------------------------- ");
writer.write(marks+" ");
writer.flush();
writer.close();
}
}
Client.java
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
*/
}
}
Graded.java
package stateInformation;
//DO NOT MODIFY THIS CLASS
public @interface Graded {
int marks();
String description();
}
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
}
/**
* String representation of a State objects
*/
public String toString() {
return abbrev+" ("+name+") - Population: "+population;
}
}
There are the following data you need:
AustraliaStateData.txt
NSW,New South Wales,7704300
VIC,Victoria,6039100
SA,South Australia,1706500
QL,Queensland,4827000
TAS,Tasmania,518500
WA,Western Australia,2613700
ACT,Australian Capital Territory,397397
NT,Northern Territory,244000
UsaStateData.txt
AL,Alabama,4860545
AK,Alaska,741522
AZ,Arizona,6908642
AR,Arkansas,2988231
CA,California,39296476
CO,Colorado,5530105
CT,Connecticut,3587685
DE,Delaware,952698
DC,District of Columbia,684336
FL,Florida,20656589
GA,Georgia,10313620
HI,Hawaii,1428683
ID,Idaho,1680026
IL,Illinois,12835726
IN,Indiana,6634007
IA,Iowa,3130869
KS,Kansas,2907731
KY,Kentucky,4436113
LA,Louisiana,4686157
ME,Maine,1330232
MD,Maryland,6024752
MA,Massachusetts,6823721
MI,Michigan,9933445
MN,Minnesota,5525050
MS,Mississippi,2985415
MO,Missouri,6091176
MT,Montana,1038656
NE,Nebraska,1907603
NV,Nevada,2939254
NH,New Hampshire,1335015
NJ,New Jersey,8978416
NM,New Mexico,2085432
NY,New York,19836286
NC,North Carolina,10156689
ND,North Dakota,755548
OH,Ohio,11622554
OK,Oklahoma,3921207
OR,Oregon,4085989
PA,Pennsylvania,12787085
RI,Rhode Island,1057566
SC,South Carolina,4959822
SD,South Dakota,861542
TN,Tennessee,6649404
TX,Texas,27904862
UT,Utah,3044321
VT,Vermont,623354
VI,Virginia,8414380
WA,Washington,7280934
WV,West Virginia,1828637
WI,Wisconsin,5772917
WY,Wyoming,584910
PR,Puerto Rico,3406520
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