This assignment demonstrates your understanding of the concepts from the CMIS 14
ID: 3856418 • Letter: T
Question
This assignment demonstrates your understanding of the concepts from the CMIS 141 class. Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. 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: a. 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%) b. Years where the maximum and minimum Murder rates occurred. c. Years where the maximum and minimum Robbery rates occurred. The following are some design criteria and specific requirements that need to be addressed: a. Use command line arguments to send in the name of the US Crime Data file. b. You should also use Java classes to their full extent to include multiple methods and at least two classes c. You are not allowed to modify the Crime.csv Statistic data file included in this assignment. d. 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.) e. You should create separate methods for each of the required functionality. (e.g. getMaxMurderYear() will return the Year where the Murder rate was highest. ) f. 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. g. 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 h. 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. i. 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
.......................
Explanation / Answer
Note: I have assumed data to be in a format of Year,Population,Murders,Robberies per line. And in a sequential manner. You have to make adjustments in program, if data format is different.
Solution==========================================
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
class USCrimeStats{
private int year;
private long population;
private long murders;
private long robbery;
private float populationGrowth;
public USCrimeStats(int year, long population,long previousYearPopulation, long murders, long robbery) {
this.year = year;
this.population = population;
this.murders = murders;
this.robbery = robbery;
if(previousYearPopulation==0){
populationGrowth=-1; //A code for N/A
}else populationGrowth=((float)(population-previousYearPopulation)/previousYearPopulation)*100;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
public long getMurders() {
return murders;
}
public void setMurders(long murders) {
this.murders = murders;
}
public long getRobbery() {
return robbery;
}
public void setRobbery(long robbery) {
this.robbery = robbery;
}
public float getPopulationGrowth() {
return populationGrowth;
}
public void setPopulationGrowth(float populationGrowth) {
this.populationGrowth = populationGrowth;
}
}
public class USCrimeProgram {
private HashMap<Integer,USCrimeStats> stats;
public USCrimeProgram(){
stats=new LinkedHashMap<>();
}
public USCrimeProgram(String fileName){
stats=new LinkedHashMap<>();
addStatsViaFile(fileName);
}
private void printDataFor(USCrimeStats stat){
String growth=stat.getPopulationGrowth() ==-1 ? "N/A":(stat.getPopulationGrowth()+"%");
System.out.println("Year:"+stat.getYear()+" Population: "+stat.getPopulation()
+" Population Growth: "+growth
+" Murders: "+stat.getMurders()+" Robbery: "+stat.getRobbery()
);
}
private void addStatsViaFile(String fileName) {
File file=new File(fileName);
try {
Scanner in = new Scanner(file);
long previousYearsPop=0;
while(in.hasNextLine()){
String[] lineData = in.nextLine().split(",");
//Assuming a format of Year,Population,Murders,Robberies
//Also Assuming that data is in sequence by "year"
int year=Integer.valueOf(lineData[0]);
long population=Long.valueOf(lineData[1]);
long murders=Long.valueOf(lineData[2]);
long robberies=Long.valueOf(lineData[3]);
USCrimeStats stat = new USCrimeStats(year,population,previousYearsPop,murders,robberies);
previousYearsPop=population;
addStatsToList(stat);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addStatsToList(USCrimeStats stat){
if(stats.containsKey(stat.getYear())){
stats.replace(stat.getYear(), stat);
}else stats.put(stat.getYear(), stat);
}
public int getYearOfHighestMurders(){
int yearOfHighestMurder=-1;
long maxMurder=-1;
for(USCrimeStats stat:stats.values()){
if(stat.getMurders()>maxMurder){
yearOfHighestMurder=stat.getYear();
maxMurder=stat.getMurders();
}
}
return yearOfHighestMurder;
}
public int getYearOfLowestMurders(){
int yearOfLowestMurder=-1;
long minMurder=Long.MAX_VALUE;
for(USCrimeStats stat:stats.values()){
if(stat.getMurders()<minMurder){
yearOfLowestMurder=stat.getYear();
minMurder=stat.getMurders();
}
}
return yearOfLowestMurder;
}
public int getYearOfHighestRobbery(){
int yearOfHighestRobbery=-1;
long maxRobbery=-1;
for(USCrimeStats stat:stats.values()){
if(stat.getRobbery()>maxRobbery){
yearOfHighestRobbery=stat.getYear();
maxRobbery=stat.getMurders();
}
}
return yearOfHighestRobbery;
}
public int getYearOfLowestRobbery(){
int yearOfLowestRobbery=-1;
long minRobbery=Long.MAX_VALUE;
for(USCrimeStats stat:stats.values()){
if(stat.getRobbery()<minRobbery){
yearOfLowestRobbery=stat.getYear();
minRobbery=stat.getMurders();
}
}
return yearOfLowestRobbery;
}
public void runMainMenu(){
Scanner in = new Scanner(System.in);
boolean exit=false;
while(!exit){
System.out.println();
System.out.println("========US Crime Statistics========");
System.out.println("1.Show all years data");
System.out.println("2.Show data for specified year");
System.out.println("3.Show year with Max and Min Murder");
System.out.println("4.Show year with Max and Min Robbery");
System.out.println("===================================");
System.out.println("Press q to Quit");
String input=in.nextLine();
switch(input){
case "1": for(USCrimeStats stat:stats.values()){
printDataFor(stat);
}
break;
case "2": System.out.println("Enter Year");
int yr=in.nextInt();
if(stats.containsKey(yr)){
printDataFor(stats.get(yr));
}else System.out.println("Data could not be found");
break;
case "3": yr=getYearOfHighestMurders();
System.out.println("Year Of Highest Murders: "+yr+", Murders: "+stats.get(yr).getMurders());
yr=getYearOfLowestMurders();
System.out.println("Year Of Lowest Murders: "+yr+", Murders: "+stats.get(yr).getMurders());
break;
case "4": yr=getYearOfHighestRobbery();
System.out.println("Year Of Highest Robbery: "+yr+", Robberies: "+stats.get(yr).getRobbery());
yr=getYearOfLowestRobbery();
System.out.println("Year Of Lowest Robbery: "+yr+", Robberies: "+stats.get(yr).getRobbery());
break;
case "q": exit=true;
break;
default: System.out.println("Wrong choice, please enter again");
}
}
in.close();
}
public static void main(String[] args) {
new USCrimeProgram(args[0].trim()).runMainMenu();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.