We will read in the csv file that can be found here: https://github.com/washingt
ID: 3808419 • Letter: W
Question
We will read in the csv file that can be found here: https://github.com/washingtonpost/data-police-shootings The Washington Post is compiling a database of every fatal shooting in the United States by a police officer in the line of duty in 2015 and 2016. We will make an object to represent a record in this file and then read the data into an array of these objects. We can then answer questions regarding this data: 1. What was the percentage of records where the person was unarmed? 2. What percentage was Male? 3. How many shootings happened overall? How many were in California? Make a method to answer each of these questions.Explanation / Answer
Based on the above problem description I have analyzed it by writing by a java program
***********************
package com.sql.acadgild;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class ReadPoliceData {
public static void main(String[] args) throws MalformedURLException, IOException {
//List<Police> books = readBooksFromCSV("https://github.com/washingtonpost/data-police-shootings/blob/master/fatal-police-shootings-data.csv"); // let's print all the person read from CSV file for (Book b : books) { System.out.println(b); }
// URLConnection url = new URL( "https://github.com/washingtonpost/data-police-shootings/blob/master/fatal-police-shootings-data.csv" ).openConnection();
URL url;
url = new URL("https://github.com/washingtonpost/data-police-shootings/blob/master/fatal-police-shootings-data.csv");
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ( (inputLine = br.readLine()) != null){
List<Police> books=readBooksFromCSV(inputLine);
for (Police police : books) {
System.out.println(police);
}
}
//System.out.println(inputLine);
// BufferedReader br = new BufferedReader(new )
/*List<Police> books= readBooksFromCSV(input.toString());
for (Police police : books) {
System.out.println(police);
}
*/
/*try {
System.out.println(IOUtils.toString( input )) ;
} finally {
IOUtils.closeQuietly(input);
}*/
}
//Method to calculate the percentage of records
void percentageofrecords(String filename){
if(filename.contentEquals("unarmed")){
System.out.println("Percentage of records who was unarmed");
}
}
//Method to calculate percentage of male
void percentageofmale(String filename){
if(filename.contentEquals("M")){
System.out.println("Percentage of records WHO IS M");
}
}
//Shooting happend overall and how many were in california
void happendOverallandinHowamnyinCalifornia(String filename){
if(filename.contentEquals("SHOT")){
System.out.println("Percentage of records WHO SHOOTING HAPPEND OVERALL");
}
else if (filename.contentEquals("CA")) {
System.out.println("Number of people in california");
}
}
private static List<Police> readBooksFromCSV(String filename) {
// TODO Auto-generated method stub
List<Police> books = new ArrayList<>();
Path pathToFile = Paths.get(filename);
try (BufferedReader br = Files.newBufferedReader(pathToFile,
StandardCharsets.US_ASCII)) {
String line = br.readLine();
while(line!= null){
String[] attributes = line.split(",");
Police book = createPolice(attributes);
books.add(book);
line = br.readLine();
}
}catch (Exception e) {
// TODO: handle exception
}
return books;
}
private static Police createPolice(String[] metadata) {
// TODO Auto-generated method stub
int id= Integer.parseInt(metadata[0]);
String name= metadata[1];
String manner_of_death=metadata[3];
String armed=metadata[4];
int age= Integer.parseInt(metadata[5]);
String gender=metadata[6];
String race= metadata[7];
String city=metadata[8];
String state=metadata[9];
return new Police(id, name, manner_of_death, armed, age, gender, race, city, state);
}
}
class Police{
private int id;
private String name;
private String manner_of_death;
private String armed;
private int age;
private String gender;
private String race;
private String city;
private String state;
public Police(int id, String name, String manner_of_death,
String armed, int age, String gender, String race, String city,
String state) {
super();
this.id = id;
this.name = name;
this.manner_of_death = manner_of_death;
this.armed = armed;
this.age = age;
this.gender = gender;
this.race = race;
this.city = city;
this.state = state;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManner_of_death() {
return manner_of_death;
}
public void setManner_of_death(String manner_of_death) {
this.manner_of_death = manner_of_death;
}
public String getArmed() {
return armed;
}
public void setArmed(String armed) {
this.armed = armed;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "Police [id=" + id + ", name=" + name + ", manner_of_death="
+ manner_of_death + ", armed=" + armed + ", age=" + age
+ ", gender=" + gender + ", race=" + race + ", city=" + city
+ ", state=" + state + "]";
}
}
*********************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.