Design a Java application that will read a file containing data related to the U
ID: 3855266 • Letter: D
Question
Design a Java application that will read a file containing data related to the US. Crime statistics from 1994-2013. The description of the file is at the end of this file. The application should provide statistical results on the data including:
Population growth in percentages from each consecutive year (e.g. 1994-1995 calculation is ((262803276 - 260327021)/260327021)*100 = 0.9512%, 1995-1996 would be ((265228572 - 262803276)/262803276)*100 = 0.9229%)
Years where the maximum and minimum Murder rates occurred.
Years where the maximum and minimum Robbery rates occurred.
The following are some design criteria and specific requirements that need to be addressed:
Use command line arguments to send in the name of the US Crime Data file.
You should also use Java classes to their full extent to include multiple methods and at least two classes
You are not allowed to modify the Crime.csv Statistic data file included in this assignment.
Use arrays and Java classes to store the data. (Hint: You can and should create a USCrimeClass to store the fields. You can also have an Array of US Crime Objects.)
You should create separate methods for each of the required functionality. (e.g. getMaxMurderYear() will return the Year where the Murder rate was highest. )
A user-friendly and well-organized menu should be used for users to select which data to return. A sample menu is shown in run example. You are free to enhance your design and you should add additional menu items and functionality.
The menu system should be displayed at the command prompt, and continue to redisplay after results are returned or until Q is selected. If a user enters an invalid menu item, the system should redisplay the menu with a prompt asking them to enter a valid menu selection
The application should keep track of the elapsed time (in seconds) between once the application starts and when the user quits the program. After the program is exited, the application should provide a prompt thanking the user for trying the US Crime Statistics program and providing the total time elapsed.
Hint: When reading the Crimes file, read one line at a time (See ReadEmail.java) and then within the loop parse each line into the USCrimeClass fields and then store that USCrimeClass Object into an array. Note you can use String.split(“,”) to split the CSV line into a the fields for setting the USCrimeClass Object.
Here is sample run:
java TestUSCrime Crime.csv
********** Welcome to the US Crime Statistical Application ************************** Enter the number of the question you want answered. Enter ‘Q’ to quit the program :
What were the percentages in population growth for each consecutive year from 1994 – 2013?
What year was the Murder rate the highest?
What year was the Murder rate the lowest?
What year was the Robbery rate the highest?
What year was the Robbery rate the lowest?
Quit the program
Enter your selection: 2
The Murder rate was highest in 1994
Enter the number of the question you want answered. Enter ‘Q’ to quit the program :
What were the percentages in population growth for each consecutive year from 1994 – 2013?
What year was the Murder rate the highest?
What year was the Murder rate the lowest?
What year was the Robbery rate the highest?
What year was the Robbery rate the lowest?
Quit the program
Enter your selection: 5
The Robbery rate was lowest in 2013
Enter the number of the question you want answered. Enter ‘Q’ to quit the program :
What were the percentages in population growth for each consecutive year from 1994 – 2013?
What year was the Murder rate the highest?
What year was the Murder rate the lowest?
What year was the Robbery rate the highest?
What year was the Robbery rate the lowest?
Q. Quit the program
Enter your selection: Q
Thank you for trying the US Crimes Statistics Program.
Elapsed time in seconds was: 32
Year Population Violent crime Violent crime rate Murder and nonnegligent manslaughter Murder and nonnegligent manslaughter rate Rape Rape rate Robbery Robbery rate Aggravated assault Aggravated assault rate Property crime Property crime rate Burglary Burglary rate Larceny-theft Larceny-theft rate Motor vehicle theft Motor vehicle theft rate 1994 260327021 1857670 713.6 23326 9 102216 39.3 618949 237.8 1113179 427.6 12131873 4660.2 2712774 1042.1 7879812 3026.9 1539287 591.3 1995 262803276 1798792 684.5 21606 8.2 97470 37.1 580509 220.9 1099207 418.3 12063935 4590.5 2593784 987 7997710 3043.2 1472441 560.3 1996 265228572 1688540 636.6 19645 7.4 96252 36.3 535594 201.9 1037049 391 11805323 4451 2506400 945 7904685 2980.3 1394238 525.7 1997 267783607 1636096 611 18208 6.8 96153 35.9 498534 186.2 1023201 382.1 11558475 4316.3 2460526 918.8 7743760 2891.8 1354189 505.7 1998 270248003 1533887 567.6 16974 6.3 93144 34.5 447186 165.5 976583 361.4 10951827 4052.5 2332735 863.2 7376311 2729.5 1242781 459.9 1999 272690813 1426044 523 15522 5.7 89411 32.8 409371 150.1 911740 334.3 10208334 3743.6 2100739 770.4 6955520 2550.7 1152075 422.5 2000 281421906 1425486 506.5 15586 5.5 90178 32 408016 145 911706 324 10182584 3618.3 2050992 728.8 6971590 2477.3 1160002 412.2 2001 285317559 1439480 504.5 16037 5.6 90863 31.8 423557 148.5 909023 318.6 10437189 3658.1 2116531 741.8 7092267 2485.7 1228391 430.5 2002 287973924 1423677 494.4 16229 5.6 95235 33.1 420806 146.1 891407 309.5 10455277 3630.6 2151252 747 7057379 2450.7 1246646 432.9 2003 290788976 1383676 475.8 16528 5.7 93883 32.3 414235 142.5 859030 295.4 10442862 3591.2 2154834 741 7026802 2416.5 1261226 433.7 2004 293656842 1360088 463.2 16148 5.5 95089 32.4 401470 136.7 847381 288.6 10319386 3514.1 2144446 730.3 6937089 2362.3 1237851 421.5 2005 296507061 1390745 469 16740 5.6 94347 31.8 417438 140.8 862220 290.8 10174754 3431.5 2155448 726.9 6783447 2287.8 1235859 416.8 2006 299398484 1435123 479.3 17309 5.8 94472 31.6 449246 150 874096 292 10019601 3346.6 2194993 733.1 6626363 2213.2 1198245 400.2 2007 301621157 1422970 471.8 17128 5.7 92160 30.6 447324 148.3 866358 287.2 9882212 3276.4 2190198 726.1 6591542 2185.4 1100472 364.9 2008 304059724 1394461 458.6 16465 5.4 90750 29.8 443563 145.9 843683 277.5 9774152 3214.6 2228887 733 6586206 2166.1 959059 315.4 2009 307006550 1325896 431.9 15399 5 89241 29.1 408742 133.1 812514 264.7 9337060 3041.3 2203313 717.7 6338095 2064.5 795652 259.2 2010 309330219 1251248 404.5 14722 4.8 85593 27.7 369089 119.3 781844 252.8 9112625 2945.9 2168459 701 6204601 2005.8 739565 239.1 2011 311587816 1206005 387.1 14661 4.7 84175 27 354746 113.9 752423 241.5 9052743 2905.4 2185140 701.3 6151095 1974.1 716508 230 2012 313873685 1217057 387.8 14856 4.7 85141 27.1 355051 113.1 762009 242.8 9001992 2868 2109932 672.2 6168874 1965.4 723186 230.4 2013 316128839 1163146 367.9 14196 4.5 79770 25.2 345031 109.1 724149 229.1 8632512 2730.7 1928465 610 6004453 1899.4 699594 221.3Explanation / Answer
Below are the classes USCrime class and main report generator class . I used array list and custom Comparator to sort that helps in returning year of highest/lowest rates. I could not complete a method due to time contraints. Rest of the code is good . please upvote if satisfied else comment for queries (Ignore package names of the classes)
USCrimeClass.java
package car.simulation;
public class USCrimeClass {
private int year;
private int population;
private int violentCrime;
private double violentCrimeRate;
private int MurderAndNonnegligentManslaughter;
private double MurderAndNonnegligentManslaughterRate;
private int rape;
private double rapeRate;
private int robbery;
private double robberyRate;
private int aggravatedAssault;
private double aggravatedAssaultRate;
private int propertyCrime;
public USCrimeClass(int year, int population, int violentCrime, double violentCrimeRate,
int murderAndNonnegligentManslaughter, double murderAndNonnegligentManslaughterRate, int rape,
double rapeRate, int robbery, double robberyRate, int aggravatedAssault, double aggravatedAssaultRate,
int propertyCrime) {
this.year = year;
this.population = population;
this.violentCrime = violentCrime;
this.violentCrimeRate = violentCrimeRate;
MurderAndNonnegligentManslaughter = murderAndNonnegligentManslaughter;
MurderAndNonnegligentManslaughterRate = murderAndNonnegligentManslaughterRate;
this.rape = rape;
this.rapeRate = rapeRate;
this.robbery = robbery;
this.robberyRate = robberyRate;
this.aggravatedAssault = aggravatedAssault;
this.aggravatedAssaultRate = aggravatedAssaultRate;
this.propertyCrime = propertyCrime;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public int getViolentCrime() {
return violentCrime;
}
public void setViolentCrime(int violentCrime) {
this.violentCrime = violentCrime;
}
public double getViolentCrimeRate() {
return violentCrimeRate;
}
public void setViolentCrimeRate(double violentCrimeRate) {
this.violentCrimeRate = violentCrimeRate;
}
public int getMurderAndNonnegligentManslaughter() {
return MurderAndNonnegligentManslaughter;
}
public void setMurderAndNonnegligentManslaughter(int murderAndNonnegligentManslaughter) {
MurderAndNonnegligentManslaughter = murderAndNonnegligentManslaughter;
}
public double getMurderAndNonnegligentManslaughterRate() {
return MurderAndNonnegligentManslaughterRate;
}
public void setMurderAndNonnegligentManslaughterRate(double murderAndNonnegligentManslaughterRate) {
MurderAndNonnegligentManslaughterRate = murderAndNonnegligentManslaughterRate;
}
public int getRape() {
return rape;
}
public void setRape(int rape) {
this.rape = rape;
}
public double getRapeRate() {
return rapeRate;
}
public void setRapeRate(double rapeRate) {
this.rapeRate = rapeRate;
}
public int getRobbery() {
return robbery;
}
public void setRobbery(int robbery) {
this.robbery = robbery;
}
public double getRobberyRate() {
return robberyRate;
}
public void setRobberyRate(double robberyRate) {
this.robberyRate = robberyRate;
}
public int getAggravatedAssault() {
return aggravatedAssault;
}
public void setAggravatedAssault(int aggravatedAssault) {
this.aggravatedAssault = aggravatedAssault;
}
public double getAggravatedAssaultRate() {
return aggravatedAssaultRate;
}
public void setAggravatedAssaultRate(double aggravatedAssaultRate) {
this.aggravatedAssaultRate = aggravatedAssaultRate;
}
public int getPropertyCrime() {
return propertyCrime;
}
public void setPropertyCrime(int propertyCrime) {
this.propertyCrime = propertyCrime;
}
}
USCrimeReportGenerator.java
package car.simulation;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class USCrimeReportGenerator {
/**
* Returns list of percentages by years of population growth
*
* @param crimes
* @return
*/
private static ArrayList<Double> populationGrowth(List<USCrimeClass> crimes) {
return null;
}
/**
* Returns year with highest murder rate.
*
* @param crimes
* @return
*/
private static int calculateHighestMurderRate(List<USCrimeClass> crimes) {
int year = 0;
crimes.sort(new Comparator<USCrimeClass>() {
@Override
public int compare(USCrimeClass o1, USCrimeClass o2) {
if (o1.getMurderAndNonnegligentManslaughterRate() == o2.getMurderAndNonnegligentManslaughterRate()) {
return 0;
} else if (o1.getMurderAndNonnegligentManslaughterRate() > o2
.getMurderAndNonnegligentManslaughterRate()) {
return 1;
} else {
return -1;
}
}
});
year = crimes.get(crimes.size() - 1).getYear();
return year;
}
/**
* Returns year with lowest murder rate.
*
* @param crimes
* @return
*/
private static int calculateLowestMurderRate(List<USCrimeClass> crimes) {
int year = 0;
crimes.sort(new Comparator<USCrimeClass>() {
@Override
public int compare(USCrimeClass o1, USCrimeClass o2) {
if (o1.getMurderAndNonnegligentManslaughterRate() == o2.getMurderAndNonnegligentManslaughterRate()) {
return 0;
} else if (o1.getMurderAndNonnegligentManslaughterRate() > o2
.getMurderAndNonnegligentManslaughterRate()) {
return 1;
} else {
return -1;
}
}
});
year = crimes.get(0).getYear();
return year;
}
/**
* Returns year with highest robbery rate.
*
* @param crimes
* @return
*/
private static int calculateHighestRobberyRate(List<USCrimeClass> crimes) {
int year = 0;
crimes.sort(new Comparator<USCrimeClass>() {
@Override
public int compare(USCrimeClass o1, USCrimeClass o2) {
if (o1.getRobberyRate() == o2.getRobberyRate()) {
return 0;
} else if (o1.getRobberyRate() > o2.getRobberyRate()) {
return 1;
} else {
return -1;
}
}
});
year = crimes.get(crimes.size() - 1).getYear();
return year;
}
/**
* Returns year with lowest robbery rate.
*
* @param crimes
* @return
*/
private static int calculateLowestRobberyRate(List<USCrimeClass> crimes) {
int year = 0;
crimes.sort(new Comparator<USCrimeClass>() {
@Override
public int compare(USCrimeClass o1, USCrimeClass o2) {
if (o1.getRobberyRate() == o2.getRobberyRate()) {
return 0;
} else if (o1.getRobberyRate() > o2.getRobberyRate()) {
return 1;
} else {
return -1;
}
}
});
year = crimes.get(0).getYear();
return year;
}
public static void main(String[] args) {
List<USCrimeClass> crimes = new ArrayList<USCrimeClass>();
USCrimeClass usCrimeClass = null;
String csvFile = "C:/Users/Piduruchetan_Reddy/Desktop/crimeReport.csv";
boolean temp = true;
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int year;
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] crimeRecord = line.split(cvsSplitBy);
for (int i = 1; i < crimeRecord.length; i++) {
usCrimeClass = new USCrimeClass(Integer.parseInt(crimeRecord[0]), Integer.parseInt(crimeRecord[1]),
Integer.parseInt(crimeRecord[2]), Double.parseDouble(crimeRecord[3]),
Integer.parseInt(crimeRecord[4]), Double.parseDouble(crimeRecord[5]),
Integer.parseInt(crimeRecord[6]), Double.parseDouble(crimeRecord[7]),
Integer.parseInt(crimeRecord[8]), Double.parseDouble(crimeRecord[9]),
Integer.parseInt(crimeRecord[10]), Double.parseDouble(crimeRecord[11]),
Integer.parseInt(crimeRecord[12]));
crimes.add(usCrimeClass);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Scanner sc = new Scanner(System.in);
do {
System.out
.println("********** Welcome to the US Crime Statistical Application ************************** ");
System.out.println("Enter the number of the question you want answered. Enter ‘Q’ to quit the program");
System.out.println(
"1. What were the percentages in population growth for each consecutive year from 1994 – 2013?");
System.out.println("2. What year was the Murder rate the highest?");
System.out.println("3. What year was the Murder rate the lowest?");
System.out.println("4. What year was the Robbery rate the highest?");
System.out.println("5. What year was the Robbery rate the lowest?");
System.out.println("6. Quit the program");
System.out.println("Enter your selection: ");
try {
int c = sc.nextInt(); // Get
// user
// input
// from
// the
// keyboard
switch (c) {
case 1:
populationGrowth(crimes);
break;
case 2:
year = calculateHighestMurderRate(crimes);
System.out.println("Murder Rate was highest in " + year);
break;
case 3:
year = calculateLowestMurderRate(crimes);
System.out.println("Murder Rate was lowest in " + year);
break;
case 4:
year = calculateHighestRobberyRate(crimes);
System.out.println("Robbery Rate was highest in " + year);
break;
case 5:
year = calculateLowestRobberyRate(crimes);
System.out.println("Robbery Rate was lowest in " + year);
break;
case 6:
temp = false;
System.out.println("Thank you for trying the US Crimes Statistics Program");
System.out.println("Elapsed time is");
break;
}
} catch (NumberFormatException e) {
}
} while (temp); //
}
}