Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have wrote a code (based on various other methods seen here) that asks the user for an input (preset questions) and it gives an

I have wrote a code (based on various other methods seen here) that asks the user for an input (preset questions) and it gives an output based on java taking the information in a .csv file. below is the code but it wont recognize USCrimeClass as being a class with anything in it. What am I doing wrong?

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.Scanner;

/**

*

* @author

*/

public class TestUSCrime {

static Scanner input = new Scanner(System.in);

/**

*

* @param args

*/

public static void main(String[] args) {

long startTime = System.currentTimeMillis();

long endTime = 0;

System.out

.println("********** Welcome to the US Crime Statistical Application **************************");

if (args.length != 1) {

System.out.println("Usage: C:\\Users\\forknucklesdeep\\Documents\\NetBeansProjects\\TestUSCrime\\Crime.csv");

return;

}

USCrimeClass[] data = read(args[0]);

String choice;

while (true) {

String menu = " 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 the total percentage change in property crimes between 2011 and 2013? "

+ "8. What was the total percentage change in murder crimes between 2012 and 2013? "

+ "Q. Quit the program";

System.out.println(menu);

choice = getInput();

System.out.println();

switch (choice) {

case "1":

showPercentage(data);

break;

case "2":

System.out.println("The Murder rate was highest in "

+ murderRate(data));

break;

case "3":

System.out.println("The Murder rate was lowest in "

+ lowestMurderRate(data));

break;

case "4":

System.out.println("The Robbery rate was highest in "

+ highestRobberyRate(data));

break;

case "5":

System.out.println("The Robbery rate was lowest in "

+ lowestRobberyRate(data));

break;

case "6":

System.out

.println("Total Percentage change in motor vehicle theft between(1998-2012) is "

+ String.format("%.4f", chnage1998_2012(data))

+ "%");

break;

case "7":

System.out

.println("Total Percentage change in property crimes between 1994 and 1998 is "

+ String.format("%.4f", chnage1994_1998(data))

+ "%");

break;

case "8":

System.out

.println("Total Percentage change in murder crimes between 2012 and 2013 is "

+ String.format("%.4f", chnage2012_2013(data))

+ "%");

break;

case "Q":

System.out

.println("Thank you for trying the US Crimes Statistics Program.");

endTime = System.currentTimeMillis();

System.out.println("Elapsed time in seconds was: "

+ (endTime - startTime) / 1000);

return;

default:

System.out

.println("Error: Invalid choice selected!! Try again. ");

break;

}

}

}

/**

* function to display menu to user

*/

static void displayMenu() {

}

public static String getInput() {

String choice;

System.out.print(" Enter your selection: ");

choice = input.next();

return choice;

}

/**

* function returns the highest murder rate for a year

*

* @param data

* @return year with highest murder rate

*/

public static int murderRate(USCrimeClass[] data) {

int year = 0;

float maxRate = 0;

for (USCrimeClass crime : data) {

if (crime.getMurderRate() > maxRate) {

maxRate = crime.getMurderRate();

year = crime.getYear();

}

}

return year;

}

/**

* function returns the lowest murder rate for a year

*

* @param data

* @return year with lowest murder rate

*/

public static int lowestMurderRate(USCrimeClass[] data) {

int year = 0;

float minRate = data[0].getMurderRate();

for (USCrimeClass crime : data) {

if (crime.getMurderRate() < minRate) {

minRate = crime.getMurderRate();

year = crime.getYear();

}

}

return year;

}

/***

*

* @param data

* @return

*/

public static int highestRobberyRate(USCrimeClass[] data) {

int year = 0;

float maxRate = 0;

for (USCrimeClass crime : data) {

if (crime.getRobberyRate() > maxRate) {

maxRate = crime.getRobberyRate();

year = crime.getYear();

}

}

return year;

}

/****

*

* @param data

* @return

*/

public static int lowestRobberyRate(USCrimeClass[] data) {

int year = 0;

float minRate = data[0].getRobberyRate();

for (USCrimeClass crime : data) {

if (crime.getRobberyRate() < minRate) {

minRate = crime.getRobberyRate();

year = crime.getYear();

}

}

return year;

}

/**

* function calculates the percentage change in motor vehicle theft between

* 1998-2012

*

* @param data

* @return

*/

static float chnage1998_2012(USCrimeClass[] data) {

float change;

int motorVehicleTheftIn1998 = data[4].getMotorVehicleTheft();

int motorVehicleTheftIn2012 = data[18].getMotorVehicleTheft();

change = (float) (motorVehicleTheftIn2012 - motorVehicleTheftIn1998)

* 100 / motorVehicleTheftIn1998;

return change;

}

/***

*

* @param data

* @return

*/

public static float chnage2012_2013(USCrimeClass[] data) {

float change;

float rate2012 = data[18].getMurderRate();

float rate2013 = data[19].getMurderRate();

change = (rate2013 - rate2012) * 100 / rate2012;

return change;

}

/***

*

* @param data

* @return

*/

public static float chnage1994_1998(USCrimeClass[] data) {

float change;

int rate1994 = data[2].getPropertyCrime();

int rate1998 = data[6].getPropertyCrime();

change = (float) (rate1998 - rate1994) * 100 / rate1994;

return change;

}

/**

* function displays the percentage population for each consecutive year

*

* @param data

*/

static void showPercentage(USCrimeClass[] data) {

float growth;

for (int i = 0; i < data.length - 1; i++) {

growth = 100

* (float) (data[i + 1].getPopulation() - data[i]

.getPopulation()) / data[i].getPopulation();

System.out.println("[" + data[i].getYear() + "-"

+ data[i + 1].getYear() + "]: "

+ String.format("%.4f", growth) + "%");

}

}

/**

* function handles the choice given by the user

*

* @param data

*/

static void handleMenu(USCrimeClass[] data) {

}

/***

* Read file data

*

* @param filename

* @return

*/

public static USCrimeClass[] read(String filename) {

USCrimeClass[] stats = new USCrimeClass[20];

int count = 0;

String line;

try {

// read file

Scanner inputReader = new Scanner(new File("C:\\Users\\forknucklesdeep\\Documents\\NetBeansProjects\\TestUSCrime\\Crime.csv"));

// ignore column name

inputReader.nextLine();

while (inputReader.hasNext()) {

line = inputReader.nextLine();

String[] data = line.split(",");

stats[count] = new USCrimeClass(Integer.parseInt(data[0]));

stats[count].setPopulation(Integer.parseInt(data[1]));

stats[count].setMurderRate(Float.parseFloat(data[5]));

stats[count].setMotorVehicleTheft(Integer.parseInt(data[18]));

stats[count].setPropertyCrime(Integer.parseInt(data[12]));

stats[count].setRobberyRate(Float.parseFloat(data[9]));

count++;

}

} catch (FileNotFoundException e) {

System.out.println(e);

}

return stats;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

What is the output of the following code segment? for (int i=1;i

Answered: 1 week ago

Question

U11 Informing Industry: Publicizing Contract Actions 317

Answered: 1 week ago