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

This assignment demonstrates your understanding of the concepts from the CMIS 14

ID: 3856419 • 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

Edit the Crime.cvs file accodingly


TestCrime.java
--------------------------
/** File: TestCrime.java
* Purpose: Display menu to user and output data from various classes
*/

import java.util.Scanner;

public class TestCrime {
    public static void main(String[] args){
        try {
            //fields
            Long startTime, endTime, elapsedTime;
            String userInput;

            /** Create and instantiate USCrimeClass object
             * pass command line argument to CsvToArray.getArray static method to convert csv to 2d array
             * pass the returned array to the constructor as a parameter
             */
            USCrimeClass usCrimes = new USCrimeClass(CsvToArray.getArray(args[0]));


            //set start time
            startTime = System.nanoTime();

            //setup scanner
            Scanner stdin = new Scanner(System.in);

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

            //print menu
            // loop while userInput is not equal to "Q" or "q"
            while (true) {
                //Call Menu class static method getMenu and print
                System.out.println(Menu.getMenu());
                //take user input
                userInput = stdin.nextLine();

                //take action based on user choice
                switch (userInput.toLowerCase()) {
                    case "1": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getPercentChanged("Population","1994","1995"));
                        System.out.println(usCrimes.getPercentChanged("Population","1995","1996"));
                        System.out.println(usCrimes.getPercentChanged("Population","1996","1997"));
                        System.out.println(usCrimes.getPercentChanged("Population","1997","1998"));
                        System.out.println(usCrimes.getPercentChanged("Population","1998","1999"));
                        System.out.println(usCrimes.getPercentChanged("Population","1999","2000"));
                        System.out.println(usCrimes.getPercentChanged("Population","2000","2001"));
                        System.out.println(usCrimes.getPercentChanged("Population","2001","2002"));
                        System.out.println(usCrimes.getPercentChanged("Population","2002","2003"));
                        System.out.println(usCrimes.getPercentChanged("Population","2003","2004"));
                        System.out.println(usCrimes.getPercentChanged("Population","2004","2005"));
                        System.out.println(usCrimes.getPercentChanged("Population","2005","2006"));
                        System.out.println(usCrimes.getPercentChanged("Population","2006","2007"));
                        System.out.println(usCrimes.getPercentChanged("Population","2007","2008"));
                        System.out.println(usCrimes.getPercentChanged("Population","2008","2009"));
                        System.out.println(usCrimes.getPercentChanged("Population","2009","2010"));
                        System.out.println(usCrimes.getPercentChanged("Population","2010","2011"));
                        System.out.println(usCrimes.getPercentChanged("Population","2011","2012"));
                        System.out.println(usCrimes.getPercentChanged("Population","2012","2013"));
                        break;
                    case "2": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getMinMax("Murder and nonnegligent manslaughter rate","highest"));
                        break;
                    case "3": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getMinMax("Murder and nonnegligent manslaughter rate","lowest"));
                        break;
                    case "4": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getMinMax("Robbery rate","highest"));
                        break;
                    case "5": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getMinMax("Robbery rate","lowest"));
                        break;
                    case "6": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getPercentChanged("Motor Vehicle Theft","1998","2012"));
                        break;
                    case "7": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getValueWhen("Population","Violent crime rate","highest"));
                        break;
                    case "8": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getValueWhen("Violent crime rate", "Burglary rate","highest"));
                        break;
                    case "9": System.out.println(" you have chosen: " + userInput + " ");
                        System.out.println(usCrimes.getAllData());
                        break;
                    case "q"://end time and calculate duration of run in seconds
                        System.out.println(" Thank you for trying the US Crimes Statistics Program");
                        endTime = System.nanoTime(); //LocalTime.now().toSecondOfDay();
                        elapsedTime = (endTime - startTime) / 1000000000;
                        System.out.println(" Elapsed time in seconds was: " + elapsedTime);
                        //exit program without error
                        System.exit(0);
                        break;
                    default: System.out.println(" Invalid selection. Please choose again.");
                        break;
                }   //end switch

            }   //end while loop

        } catch (ArrayIndexOutOfBoundsException oob) {
            System.out.println(" There was an issue. Please make sure you specify a file.");
            System.exit(1);
        } catch (Exception e) {
            System.out.println(" There is something wrong and I don't know what it could be.");
            System.exit(1);
        }

    }   //end main method
} //end TestCrime class
---------------------------------------------------------------
USCrimeClass.java
---------------------------------
/** File: USCrimeClass.java
* Purpose: Read in and store data.
*/
public class USCrimeClass {

    // ---------- fields ----------
    final int COLUMN = 20;
    final int ROW = 21;
    String crimeData2D[][] = new String[ROW][COLUMN];

    // ---------- Constructors ----------
    public USCrimeClass(String crime2darray[][]) {
        this.crimeData2D = crime2darray;
    }

    // --------- getMinMax Method ---------
    protected String getMinMax(String heading, String highestOrLowest) {
        // ---------- Fields ----------
        int rowIndex = 0;
        int columnIndex;
        final int YEARCOLUMNINDEX = 0;
        //returnFields
        String returnYear;
        String returnValue;
        String results = "AHHHHH";

        //set columnIndex to matching heading
        columnIndex = ArrayUtils.getHeadingIndex(this.crimeData2D, heading);

        switch (highestOrLowest){
            case "highest":
                //based on columnIndex set rowIndex to the highest value
                rowIndex = ArrayUtils.getMaxValueRowIndex(this.crimeData2D, columnIndex);
                break;
            case "lowest":
                //based on columnIndex set rowIndex to the lowest value
                rowIndex = ArrayUtils.getMinValueRowIndex(this.crimeData2D, columnIndex);
                break;
            default: System.out.println("you are dumb, high or low?");
                break;
        }

        //based on rowIndex get the year for that row
        returnYear = crimeData2D[rowIndex][YEARCOLUMNINDEX];
        returnValue = crimeData2D[rowIndex][columnIndex];

        results = "The " + highestOrLowest + " " + heading + " was at " + returnValue + " in " + returnYear + ".";
        return results;
    }   //end getMinMax method


    // --------- percentageChange method ----------
    public String getPercentChanged(String heading, String yearOne, String yearTwo) {
        // ---------- Fields ----------
        int firstRowIndex;
        int columnIndex;
        int secondRowIndex;
        String returnValue;

        //search for the first year's row
        firstRowIndex = ArrayUtils.getYearIndex(this.crimeData2D, yearOne);
        //search for the second year's row
        secondRowIndex = ArrayUtils.getYearIndex(this.crimeData2D, yearTwo);
        //search for heading value and set column
        columnIndex = ArrayUtils.getHeadingIndex(this.crimeData2D, heading);

        //Calculations
        double firstStat = Double.parseDouble(crimeData2D[firstRowIndex][columnIndex]);
        double secondStat = Double.parseDouble(crimeData2D[secondRowIndex][columnIndex]);
        double difference = ((secondStat - firstStat) / firstStat) * 100;

        returnValue ="The percentage change for " + heading + " from " + yearOne + " to " + yearTwo + " was " +
                difference + "%.";
        return returnValue;
    }   //end perChange method


    // --------- getValueWhen method ----------
    public String getValueWhen(String firstHeading, String secondHeading, String highestOrLowest) {
        // ---------- Fields ----------
        int rowIndex = 0;
        int firstColumnIndex;
        int secondColumnIndex;
        final int YEARCOLUMNINDEX = 0;
        //returnFields
        String returnYear;
        String returnValue;
        String results = "AHHHH";


        //search for first heading value and set firstColumnIndex
        firstColumnIndex = ArrayUtils.getHeadingIndex(this.crimeData2D, firstHeading);

        //search for first heading value and set secondColumnIndex
        secondColumnIndex = ArrayUtils.getHeadingIndex(this.crimeData2D, secondHeading);

        //determine min/max value in secondColumnIndex and return rowIndex
        switch (highestOrLowest){
            case "highest":
                //based on columnIndex set rowIndex to the highest value
                rowIndex = ArrayUtils.getMaxValueRowIndex(this.crimeData2D, secondColumnIndex);
                break;
            case "lowest":
                //based on columnIndex set rowIndex to the lowest value
                rowIndex = ArrayUtils.getMinValueRowIndex(this.crimeData2D, secondColumnIndex);
                break;
            default: System.out.println("you are dumb, high or low?");
                break;
        }

        //sets up return values
        returnValue = crimeData2D[rowIndex][firstColumnIndex];
        returnYear = crimeData2D[rowIndex][YEARCOLUMNINDEX];
        results = "The " + firstHeading + " was at " + returnValue + " when " + secondHeading +
                " was at it's " + highestOrLowest + " in " + returnYear + ".";

        return results;
    }   //end getValueWhen method

    //Additional Methods go here

    //This section prints the 2d array -- for verification
    public StringBuilder getAllData() {
        //This section prints the 2d array -- for verification
        //sets the row
        StringBuilder allData = new StringBuilder("********** US Crime Statistical Data ********** ");
        for (int i = 0; i < ROW; i++) {
            //sets the columns
            for (int j = 0; j < COLUMN; j++) {
                allData.append(crimeData2D[i][j] + " ");
            }
            allData.append(" ");
        }

        return allData;
    }   //end getAllData method


} //end USCrimeClass
---------------------------------------------------------------
Menu.java
---------------------------------
/** File: Menu.java
* Purpose: return menu as a string when called
*/
public class Menu {
    public static String getMenu() {
        //instantiate and read menu into string variable
        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 Population when the Violent crime rate was the highest? " +
                "8. What was Violent crime rate when the Burglary rate was the highest? " +
                "9. View a table of all statistical data. " +
                "Q. Quit the program " +
                "Enter your selection:" +
                "";

        return menu;
    }   // end getMenu method
} // end Menu class
---------------------------------------------------------------
ArrayUtils.java
---------------------------------

/** File: Menu.java
* Purpose: provides various utilities to find array indexes based on some search criteria
*/
public class ArrayUtils {

    // ----------- fields ----------
    private static final int YEARCOLUMNINDEX = 0;
    private static final int HEADINGROWINDEX = 0;


    //getHeadingIndex - column
    public static int getHeadingIndex(String[][] array, String heading){
        int columnIndex = 0;

        for (int i = 1; i < 20; i++){
            if (array[HEADINGROWINDEX][i].equals(heading)){
                columnIndex = i;
            }
        }
        return columnIndex;
    }   //end getHeadingIndex method

    //getYearIndex - row
    public static int getYearIndex(String[][] array, String year){
        int rowIndex = 0;

        for (int i = 1; i < 21; i++){
            if (array[i][YEARCOLUMNINDEX].equals(year)){
                rowIndex = i;
            }
        }
        return rowIndex;
    }   //end getHeadingIndex method

    //getMaxValueRowIndex
    public static int getMaxValueRowIndex(String[][] array, int columnIndex){
        double maxValue = 0;
        double value;
        int rowIndex = 0;

        for (int i = 1; i < 21; i++){
            value = Double.parseDouble(array[i][columnIndex]);
            if (maxValue < value) {
                maxValue = value;
                rowIndex = i;
            }
        }
        return rowIndex;
    }   //end getMaxValueRowIndex method

    //getMinValueRowIndex
    public static int getMinValueRowIndex(String[][] array, int columnIndex){
        double minValue = 1000000000;
        double value;
        int rowIndex = 0;

        for (int i = 1; i < 21; i++){
            value = Double.parseDouble(array[i][columnIndex]);
            if (minValue > value) {
                minValue = value;
                rowIndex = i;
            }
        }
        return rowIndex;
    }   //end getMinValueRowIndex method


} //end ArraysUtils class
---------------------------------------------------------------
CsvToArray.java
---------------------------------

/**File: USCrimeClass.java
* Purpose: take csv filename passed and read csv into a 2d array and return array
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CsvToArray {
    public static String[][] getArray (String filename){
        //fields
        BufferedReader br = null;
        final int COLUMN = 20;
        final int ROW = 21;
        String array[][] = new String[ROW][COLUMN];

        //Read the CSV file
        //Setup try/catch/finally blocks for exception handling
        try {
            //setup input reader
            String currentLine;
            br = new BufferedReader(new FileReader(filename));

            //while loop to read input, provide output and count
            int count = 0;
            while ((currentLine = br.readLine()) != null) {
                for (int i = count; i < ROW; i++) {
                    array[i] = currentLine.split(",");
                }
                count++;
            }
        } catch (IOException e) {
            //output exception stack
            System.out.println(" There was an issue. Please make sure you have the correct file.");
            System.exit(1);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                System.out.println(" There was an issue.");
                System.exit(1);
            }
        }

        return array;
    }
}