Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a Java application that will read a file containing data related to the U

ID: 3571962 • 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 shown below. 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. d. Total percentage change in Motor Vehicle Theft between the years 1998 and 2012. e. Two (2) additional crime statistics results you add to enhance the application functionality. 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 are not allowed to modify the Crime.csv Statistic data file included in this assignment. c. 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.) d. Your design should include multiple classes to separate the functionality of the application. 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. Here is sample run: java TestUSCrime Crime.csv 2 ********** Welcome to the US Crime Statistical Application ************************** Enter the number of the question you want answered. Enter ‘Q’ to quit the program : 1. What were the percentages in population growth for each consecutive year from 1994 – 2013? 2. What year was the Murder rate the highest? 3. What year was the Murder rate the lowest? 4. What year was the Robbery rate the highest? 5. What year was the Robbery rate the lowest? 6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012? 7. What was [enter your first unique statistic here]? 8. What was [enter your second unique statistic here]? Q. 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 : 1. What were the percentages in population growth for each consecutive year from 1994 – 2013? 2. What year was the Murder rate the highest? 3. What year was the Murder rate the lowest? 4. What year was the Robbery rate the highest? 5. What year was the Robbery rate the lowest? 6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012? 7. What was [enter your first unique statistic here]? 8. What was [enter your second unique statistic here]? Q. 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 : 1. What were the percentages in population growth for each consecutive year from 1994 – 2013? 2. What year was the Murder rate the highest? 3. What year was the Murder rate the lowest? 4. What year was the Robbery rate the highest? 5. What year was the Robbery rate the lowest? 6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012? 7. What was [enter your first unique statistic here]? 8. What was [enter your second unique statistic here]? Q. Quit the program Enter your selection: Q Thank you for trying the US Crimes Statistics Program. Elapsed time in seconds was: 32

Explanation / Answer

//CrimeData.java

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.math.*;

public class CrimeData {
  
//private vailables
private ArrayList<CrimeObject> CrimeData = new ArrayList<CrimeObject>();   
//other variables

public void setCrimeData(BufferedReader file){
BufferedReader CSVFile = file;
try {
String dataRow = CSVFile.readLine();
dataRow = CSVFile.readLine();
int count = 0;
while(dataRow != null){
String[] dataArray = dataRow.split(",");
CrimeObject CrimeObject = new CrimeObject( //create objects with correct data type
(Short.valueOf(dataArray[0])),//year
(Integer.valueOf(dataArray[1])),//population
(Integer.valueOf(dataArray[2])),//violentcrime
(Double.valueOf(dataArray[3])),//violentrate
(Integer.valueOf(dataArray[4])),//murder
(Double.valueOf(dataArray[5])),//murder rate
(Integer.valueOf(dataArray[6])),//rape
(Double.valueOf(dataArray[7])),//rape rate
(Integer.valueOf(dataArray[8])),//robbery
(Double.valueOf(dataArray[9])),//robery rate
(Integer.valueOf(dataArray[10])),//Assault
(Double.valueOf(dataArray[11])),//Assault rate
(Integer.valueOf(dataArray[12])),//property crime
(Double.valueOf(dataArray[13])),//property crime rate
(Integer.valueOf(dataArray[14])),//Burglary
(Double.valueOf(dataArray[15])),//burglary rate
(Integer.valueOf(dataArray[16])),//larceny
(Double.valueOf(dataArray[17])),//larceny rate
(Integer.valueOf(dataArray[18])),//vehicle
(Double.valueOf(dataArray[19]))//vehicle rate
);
CrimeData.add(CrimeObject); // add object to the CrimeData List
dataRow = CSVFile.readLine();//move to the next line
count++;
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
//Available Methods
public int getLength(){
return CrimeData.size();
}
  
public int getPopulation(int i){
int population = CrimeData.get(i).getPopulation();
return population;
}
public short getYear(int i){
return CrimeData.get(i).getYear();
}
  
public double getGrowth(int i){
CrimeObject thisYear = CrimeData.get(i);
CrimeObject nextYear = CrimeData.get(i+1);
double growth = nextYear.getPopulation() - thisYear.getPopulation();
growth = growth/thisYear.getPopulation();
growth = growth*100;
return growth;   
}
  
public CrimeObject getObject(int i){
CrimeObject Object = CrimeData.get(i);
return Object;
}
  
}

  
========================================================================

// CrimeMenu

public class CrimeMenu {
final String showMenu = //Create Menu Variable
" Enter the number of the question you want answered. Enter ‘0’ to quit the program :" +
" 1. What were the percentages in population growth for each consecutive year from 1994 to 2013?"+
" 2. What year was the Murder rate the highest?"+
" 3. What year was the Murder rate the lowest?"+
" 4. What year was the Robbery rate the highest?"+
" 5. What year was the Robbery rate the lowest?"+
" 6. What was the total percentage change in Motor Vehicle Theft between 1998 and 2012?"+
" 7. What year was the Assault rate the highest?"+   
" 8. What year was the Assault rate the lowest?"+
" 0. Quit the program"+
" Enter your selection:";

public String getMenu(){//getter method
return showMenu;
}
  
}

====================================================================

//CrimeObject.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CrimeObject {
//Fields   
private short Year;
private int Population;
private int ViolentCrime;
private double ViolentRate;
private int Murder;
private double MurderRate;
private int Rape;
private double RapeRate;
private int Robbery;
private double RobberyRate;
private int Assault;
private double AssaultRate;
private int PropertyCrime;
private double PropertyCrimeRate;
private int Burglary;
private double BurglaryRate;
private int Larceny;
private double LarcenyRate;
private int VehicleTheft;
private double VehicleTheftRate;

public CrimeObject (short Year, int Population,
int ViolentCrime, double ViolentRate,
int Murder,double MurderRate,
int Rape, double RapeRate,
int Robbery,double RobberyRate,
int Assault, double AssaultRate,
int PropertyCrime, double PropertyCrimeRate,
int Burglary, double BurglaryRate,
int Larceny,double LarcenyRate,
int VehicleTheft, double VehicleTheftRate){
  
this.Year= Year;
this.Population = Population;
this.ViolentCrime = ViolentCrime;
this.ViolentRate = ViolentRate;
this.Murder = Murder;
this.MurderRate = MurderRate;
this.Rape = Rape;
this.RapeRate = RapeRate;
this.Robbery = Robbery;
this.RobberyRate = RobberyRate;
this.Assault = Assault;
this.AssaultRate = AssaultRate;
this.PropertyCrime = PropertyCrime;
this.PropertyCrimeRate = PropertyCrimeRate;
this.Burglary = Burglary;
this.BurglaryRate = BurglaryRate;
this.Larceny = Larceny;
this.LarcenyRate = LarcenyRate;
this.VehicleTheft = VehicleTheft;
this.VehicleTheftRate = VehicleTheftRate;
}
//getter methods
public short getYear(){
return this.Year;
}
public int getPopulation(){
return this.Population;
}
public int getViolentCrime(){
return this.ViolentCrime;
}
public double getMurderRate(){
return this.MurderRate;
}
public double getRobberyRate(){
return this.RobberyRate;
}
public double getVehicleTheftRate(){
return this.VehicleTheftRate;
}
public double getAssaultRate(){
return this.AssaultRate;
}
  
}

======================================================================

//TestCrime.java

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.time.*;


public class TestCrime {
public static void main(String[] args) throws IOException {
Instant start,stop; // create timers
start = Instant.now();//start timer
BufferedReader file;//create file
  
if(args.length != 1) {// command line parameter catch
System.err.println(" Error: Please incluce 1 command-line arguments" +
" Example: Crime.csv");
System.exit(1);
}
else{
DecimalFormat df2 = new DecimalFormat(".##");//outputing doubles
file = new BufferedReader(new FileReader(args[0]));//command line parameter   
CrimeData CrimeData = new CrimeData();//declare CrimeData List
Scanner Input = new Scanner(System.in);//declare Scanner
CrimeMenu Menu = new CrimeMenu();//declare menu
  
CrimeData.setCrimeData(file);//create CrimeData with cmdline argument file
System.out.println("Successfully Imported: "+ CrimeData.getLength() + " Rows of Data " );//verify data was imported
  
//Create additonal Variables
boolean flag = true;//for stopping the while loop
int userChoice;//for handing user input choice
  
System.out.println("********** Welcome to the US Crime Statistical Application **************************");
while(flag == true){
System.out.println(Menu.getMenu());//loops print menu
userChoice = Input.nextInt();
  
if(userChoice==0){ //checking for quit
flag = false;
  
}
if(userChoice==1)
{
System.out.println("********** Population Growth **************************");
for(int i=0; i<(CrimeData.getLength()-1);i++){//fomula for population growth
System.out.print(CrimeData.getYear(i) +"-"+CrimeData.getYear((i+1)) +": ");
System.out.println( df2.format((CrimeData.getGrowth(i))) +"%");
}
}
if(userChoice==2){   
System.out.println("********** Highest Murder Rates **************************");
CrimeObject high = CrimeData.getObject(0);
for(int i=0; i<(CrimeData.getLength()-1);i++){//equation for High Murder Rate
if((CrimeData.getObject(i).getMurderRate()) > (high.getMurderRate())){
high = CrimeData.getObject(i);
}
}
System.out.println("High Murder Rate " + high.getYear() + " : " + df2.format(high.getMurderRate()) +"%") ;
}
  
if(userChoice==3){   
System.out.println("********** Lowest Murder Rates **************************");
CrimeObject low = CrimeData.getObject(0);
for(int i=0; i<(CrimeData.getLength()-1);i++){//equation for Lowest Murder Rates
if((CrimeData.getObject(i).getMurderRate()) < (low.getMurderRate())){
low = CrimeData.getObject(i);
}
}
System.out.println("Low Murder Rate " + low.getYear() + " : " + df2.format(low.getMurderRate())+"%") ;
}
if(userChoice==4){   
System.out.println("********** Highest Robbery Rates **************************");
CrimeObject high = CrimeData.getObject(0);
for(int i=0; i<(CrimeData.getLength()-1);i++){//equation for Highest Robbery Rate
if((CrimeData.getObject(i).getRobberyRate()) > (high.getRobberyRate())){
high = CrimeData.getObject(i);
}
}
System.out.println("High Robbery Rate " + high.getYear() + " : " + df2.format(high.getRobberyRate())+"%") ;
}
  
  
if(userChoice==5){   
System.out.println("********** Lowset Robbery Rates **************************");
CrimeObject low = CrimeData.getObject(0);
for(int i=0; i<(CrimeData.getLength()-1);i++){//equation for Lowest Robbery Rate
if((CrimeData.getObject(i).getRobberyRate()) < (low.getRobberyRate())){
low = CrimeData.getObject(i);
}
}
System.out.println("Low Robbery Rate " + low.getYear() + " : " + df2.format(low.getRobberyRate())+"%") ;
}
if(userChoice==6){   
System.out.println("********** Change in Vehicle Theft Rate **************************");
CrimeObject first = CrimeData.getObject(0);
CrimeObject second = CrimeData.getObject(0);
for(int i=0; i<(CrimeData.getLength()-1);i++){//equation for change in Vechicle Theft
if((CrimeData.getObject(i).getYear())==(1998)){
first = CrimeData.getObject(i);
}
if((CrimeData.getObject(i).getYear())==(2012)){
second = CrimeData.getObject(i);
}
}
System.out.println("Change in Vehicle Theft Rate from 1998 to 2012: " + (first.getVehicleTheftRate() - second.getVehicleTheftRate()+"%" ));
}
if(userChoice==7){   
System.out.println("********** Highest Assault Rates **************************");
CrimeObject high = CrimeData.getObject(0);
for(int i=0; i<(CrimeData.getLength()-1);i++){//equation for Highest in Assault Rates
if((CrimeData.getObject(i).getAssaultRate()) > (high.getAssaultRate())){
high = CrimeData.getObject(i);
}
}
System.out.println("High Assault Rate " + high.getYear() + " : " + df2.format(high.getAssaultRate())+"%") ;
}
  
if(userChoice==8){   
System.out.println("********** Lowset Assault Rates **************************");
CrimeObject low = CrimeData.getObject(0);
for(int i=0; i<(CrimeData.getLength()-1);i++){//equation for lowest Assault rate
if((CrimeData.getObject(i).getAssaultRate()) < (low.getAssaultRate())){
low = CrimeData.getObject(i);
}
}
System.out.println("Low Assault Rate " + low.getYear() + " : " + df2.format(low.getAssaultRate())+"%") ;
}
  
}
}
stop = Instant.now();
System.out.println("Thank you for trying the US Crimes Statistics Program");
System.out.println("Elapsed time in seconds was: " + (Duration.between(start, stop).toMillis())/1000);//print durationg in seconds
}
  
}
  

============================================================