US Crime Rates in Java

US Crime Rates in Java Homework Sample

Calculate the percentage change of the population each year. Find the years where the maximum and minimum murder rate occurred and the years with the highest and lowest robbery rates. You should display a menu to select between the statistics to be displayed, and when you Quit the program it should display the total time elapsed. The program should read in the crime statistics csv file, line by line and convert it into a class instance. For more Java programming assignment help contact us for a quote.

Solution:

Crimes.java

package testcrimes;

import java.io.*;
import java.util.*;

public class Crimes {
//field to store all the data coming from the file

private ArrayList<USCrime> data;

/*

* @constructor to fill the attribute with the data on the file

*/
public Crimes(String fileName) {

//initialize data
data = new ArrayList<USCrime>();
//stream reader to read information from the csv file

InputStreamReader isr;

try {

isr = new InputStreamReader(new FileInputStream(fileName));

BufferedReader in = new BufferedReader(isr);

//we call the method responsible of reading the data and parsing and storing it in data
initiateData(in);

in.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/*

* @paramBufferReader from the file containing the data

* @throws IOException if

*/
private void initiateData(BufferedReader in) throws IOException {

//a string to store each line of the file
String line;

//we skip the first line because it doesn’t contain data
in.readLine();

//we iterate over the lines of the file while we didn’t get to the end of it
while ((line = in.readLine()) != null) {

//we parse the line to a USCrime object
USCrime usCrime = parseUSCrime(line);

data.add(usCrime);

}

}

private USCrime parseUSCrime(String line) {

String[] lineParts = line.split(“,”);

int year = Integer.parseInt(lineParts[0]);

int population = Integer.parseInt(lineParts[1]);

float murderRate = Float.parseFloat(lineParts[2]);

float robberyRate = Float.parseFloat(lineParts[3]);

return new USCrime(year, population, murderRate, robberyRate);

}

public int getYearWithHighestMurder() {

int highestYear = data.get(0).getYear();

float max = data.get(0).getMurderRate();

for (USCrime crime : data) {

float currentRate = crime.getMurderRate();

if (currentRate > max) {

highestYear = crime.getYear();

max = currentRate;

}

}
return highestYear;

}

public int getYearWithLowestMurder() {

int lowestYear = data.get(0).getYear();

float min = data.get(0).getMurderRate();

for (USCrime crime : data) {

float currentRate = crime.getMurderRate();

if (currentRate < min) {

lowestYear = crime.getYear();

min = currentRate;

}

}
return lowestYear;

}

public int getYearWithHighestRobery() {

int highestYear = data.get(0).getYear();

float max = data.get(0).getRobberyRate();

for (USCrime crime : data) {

float currentRate = crime.getRobberyRate();

if (currentRate > max) {

highestYear = crime.getYear();

max = currentRate;

}

}
return highestYear;

}

public int getYearWithLowestRobery() {

int lowestYear = data.get(0).getYear();

float min = data.get(0).getRobberyRate();

for (USCrime crime : data) {

float currentRate = crime.getRobberyRate();

if (currentRate < min) {

lowestYear = crime.getYear();

min = currentRate;

}

}
return lowestYear;

}

public double[] getPopulationGrowth() {
double growths[] = new double[data.size() – 1];

for (int i = 0; i < data.size() – 2; i++) {

int actualPopulation = data.get(i).getPopulation();

int nextPopulation = data.get(i + 1).getPopulation();

double growth = (double) (nextPopulation – actualPopulation) / actualPopulation;

growths[i] = 100 * growth;

}
return growths;

}

public int getFirstYear() {

return data.get(0).getYear();

}
}

TestCrimes.java

package testcrimes;

import java.util.*;

public class TestCrimes {

private static long elapsedTime;

public static void main(String[] args) {

long startTime = System.currentTimeMillis();

long endTime;

Scanner input = new Scanner(System.in);
Crimes crimes=null;
try{
// class for calculation
crimes = new Crimes(args[0]);
}catch(Exception e)
{
System.out.println(“No filename specified. Try running this program using the command prompt.”);
System.out.println(“Recommended: java -jar TestCrimes.jar <file_name>”);
return;
}

printWelcomeMessage();

char req;

do {
printRequestMessage();

req = input.next().charAt(0);
int result;

switch (req) {

case ‘1’:
double[] percentage = crimes.getPopulationGrowth();
for (int i = 0; i < percentage.length; i++) {
int from = crimes.getFirstYear() + i;
int to = from + 1;
System.out.printf(“Population growth pourcentage from ”
+ from + ” to ” + to + ” is: %.4f” ,percentage[i]);
System.out.println(“%”);
}
break;

case ‘2’:
result = crimes.getYearWithHighestMurder();
System.out.println(“The Murder rate was highest in ” + result);

break;

case ‘3’:
result = crimes.getYearWithLowestMurder();

System.out.println(“The Murder rate was lowest in ” + result);
break;

case ‘4’:
result = crimes.getYearWithHighestRobery();

System.out.println(“The Robbery rate was highest in ” + result);
break;

case ‘5’:
result = crimes.getYearWithLowestRobery();

System.out.println(“The Robbery rate was lowest in ” + result);
break;

case ‘Q’:
break;
}
} while (req != ‘Q’);
input.close();

endTime = System.currentTimeMillis();

elapsedTime = (endTime – startTime) / 1000;
printExitMessage();
}

private static void printWelcomeMessage() {

System.out.println(“********** Welcome to the US Crime Statistical Application * * * * * * * * * * * * * * * * * * * * * * * * * *”);

}

private static void printRequestMessage() {

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(“Q. Quit the program”);
}

private static void printExitMessage() {
System.out.println(“Thank you for trying the US Crimes Statistics Program.”);

System.out.println(“Elapsed time in seconds was: ” + elapsedTime);
}
}

USCrime.java

package testcrimes;

public class USCrime {

private int year;

private int population;

private float murderRate;

private float robberyRate;

public USCrime(int year, int population, float murderRate, float robberyRate) {

this.year = year;

this.population = population;

this.murderRate = murderRate;

this.robberyRate = robberyRate;

}
// Setter and getter methods for variables

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 float getMurderRate() {
return murderRate;

}

public void setMurderRate(float murderRate) {

this.murderRate = murderRate;

}

public float getRobberyRate() {
return robberyRate;

}

public void setRobberyRate(float robberyRate) {

this.robberyRate = robberyRate;

}
}