Design a Java application that will read a file containing data related to the p
ID: 643171 • Letter: D
Question
Design a Java application that will read a file containing data related to the
passengers on the Titanic. The description of the file is shown below. The application should
provide statistical results on the passengers including:
a. Total number of passengers on the Titanic
b. Total number of passengers who perished on the Titanic
c. Total number of Passengers who survived the sinking of the Titanic
d. Number of passengers who survived the sinking of the Titanic as a function of the
passenger class (e.g. 1,2,3)
e. Number of passengers who survived the sinking of the Titanic as a function of the
passenger gender (e.g., male, female)
f. A list of the names of passengers who paid greater than $200 for their tickets
g. A list of the names of passengers who were less than 10 years old who survived the
sinking of the Titanic
h. A list of the names of passengers who were less than 10 years old who perished on the
Titanic
i. The count of the number of passengers as a function of the first letter of their last name.
(e.g., A: 13, B:33
Explanation / Answer
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class Titanic {
public class MyObjectFileStore {
public void storeObject(Titanic Tit){
OutputStream ops = null;
ObjectOutputStream objOps = null;
try{
ops = new FileOutputStream("titanic.txt");
objOps = new ObjectOutputStream(ops);
objOps.writeObject(Tit);
objOps.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(objOps != null) objOps.close();
} catch (Exception ex){
}
}
}
public void displayObjects(){
InputStream fileIs = null;
ObjectInputStream objIs = null;
try{
fileIs = new FileInputStream("titanic.txt");
objIs = new ObjectInputStream(fileIs);
Titanic tit = (Titanic) objIs.readObject();
System.out.println(tit);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e){
e.printStackTrace();
} finally{
try{
if(objIs != null) objIs.close();
} catch (Exception ex){
}
}
}
public void main (String a[]){
MyObjectFileStore mof = new MyObjectFileStore();
// Titanic t1 = new Titanic();
// mof.storeObject(t1);
// mof.displayObjects();
}
}
class Titanic1 implements Serializable{
private String name;
private int passengers;
private int perished;
private int survived;
private int passengerClass1;
private int passengerClass2;
private int passengerClass3;
private String gender;
private int maximum;
private int ageMax10;
private int ageMin10;
private String firstLetter;
public Titanic1(String name, int passengers, int perished, int survived, int passengerClass1,
int passengerClass2, int passengerClass3, String gender, int maximum,
int agemax10, int agemin10, String firstLetter){
this.name = name;
this.passengers = passengers;
this.survived = survived;
this.passengerClass1 = passengerClass1;
this.passengerClass2 = passengerClass2;
this.passengerClass3 = passengerClass3;
this.gender = gender;
this.maximum = maximum;
this.ageMax10 = ageMax10;
this.ageMin10 = ageMin10;
this.firstLetter = firstLetter;
}
public String toString(){
return name +"=="+passengers+"=="+survived+"=="+passengerClass1
+"=="+passengerClass2+"=="+passengerClass3+"=="+gender
+"=="+maximum+"=="+ageMax10+"=="+ageMin10+"=="+firstLetter;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassengers() {
return passengers;
}
public void setPassengers(int passengers) {
this.passengers = passengers;
}
public int getPassengerClass1(){
return passengerClass1;
}
public void setPassengerClass1(int passengerClass1){
this.passengerClass1 = passengerClass1;
}
public int getPassengerClass2(){
return passengerClass2;
}
public void setPassengerClass2(int passengerClass2){
this.passengerClass2 = passengerClass2;
}
public int getPassengerClass3(){
return passengerClass3;
}
public void setPassengerClass3(int passengerClass3){
this.passengerClass3 = passengerClass3;
}
public int getMaximum(){
return maximum;
}
public void setMaximum (int maximum) {
this.maximum = maximum;
}
public int getAgeMax10(){
return ageMax10;
}
public void setAgeMax10 (int ageMax10) {
this.ageMax10 = ageMax10;
}
public int getAgeMin10(){
return ageMin10;
}
public void setAgeMain10 (int ageMin10) {
this.ageMin10 = ageMin10;
}
public String getFirstLetter(){
return firstLetter;
}
public void setFirstLetter (String firstLetter){
this.firstLetter = firstLetter;
}
}
}