1. Design a Java application that will read a file(Titanic.txt) containing data
ID: 664803 • Letter: 1
Question
1. Design a Java application that will read a file(Titanic.txt) 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 …)
j. Additional statistical results you add to enhance the functionality
2. 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 Titanic file.
b. Use a 2D array to store the Titanic data. (Hint: You will probably need to store the array as String values and then convert to other types as needed since some data is null)
c. You should create at least 2 Java classes – Titanic and TestTitanic. You are welcome to create additional classes if you want to further separate the functionality.
d. You should create separate methods for each of the required functionality. (e.g. getTotalPassengers() will return the total number of passengers on the Titanic.)
e. 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.
f. 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
g. 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 2 application should provide a prompt thanking the user for trying the Titanic program and providing the total time elapsed.
Here is sample run: java TestTitanic Titanic.txt
********** Welcome to the Titanic Statistical Application **************************
Enter the number of the question you want answered. Enter ‘Q’ to quit the program :
1. How many passengers were on the Titanic?
2. What percentage of passengers perished on the Titanic?
3. What percentage passengers survived the sinking of the Titanic?
4. What percentage of passengers survived for each of the three classes?
5. What percentage of passengers survived as a function of gender?
6. What specific passengers paid more than $200 for their tickets?
7. What specific passengers who were less than 10 years old perished on the titanic?
8. What specific passengers who were less than 10 years old survived the sinking of the titanic?
9. For each letter in the alphabet, how many passengers last names started with that letter?
Q. Quit the program
Enter your selection: 1
There were 1310 Passengers on the Titanic.
Enter the number of the question you want answered. Enter ‘Q’ to quit the program :
1.How many passengers were on the Titanic?
2. What percentage of passengers perished on the Titanic?
3. What percentage passengers survived the sinking of the Titanic?
4. What percentage of passengers survived for each of the three classes?
5. What percentage of passengers survived as a function of gender?
6. What specific passengers paid more than $200 for their tickets?
7. What specific passengers who were less than 10 years old perished on the titanic?
8. What specific passengers who were less than 10 years old survived the sinking of the titanic?
9. For each letter in the alphabet, how many passengers last names started with that letter?
Q. Quit the program
Enter your selection: 6
The following passengers paid more than $200 for their tickets:
Allen, Miss. Elisabeth Walton Astor, Col. John Jacob Astor, Mrs. John Jacob (Madeleine Talmadge Force) Baxter, Mr. Quigg Edmond Baxter, Mrs. James (Helene DeLaudeniere Chaput) Bidois, Miss. Rosalie Bird, Miss. Ellen Bowen, Miss. Grace Scott 3 Cardeza, Mr. Thomas Drake Martinez Cardeza, Mrs. James Warburton Martinez (Charlotte Wardle Drake) Chaudanson, Miss. Victorine Douglas, Mrs. Frederick Charles (Mary Helene Baxter) Endres, Miss. Caroline Louise Farthing, Mr. John Fortune, Miss. Alice Elizabeth Fortune, Miss. Ethel Flora Fortune, Miss. Mabel Helen Fortune, Mr. Charles Alexander Fortune, Mr. Mark Fortune, Mrs. Mark (Mary McDougald) Geiger, Miss. Amalie Keeping, Mr. Edwin Kreuchen, Miss. Emilie Lesurer, Mr. Gustave J Madill, Miss. Georgette Alexandra Robbins, Mr. Victor Robert, Mrs. Edward Scott (Elisabeth Walton McMillan) Ryerson, Master. John Borie Ryerson, Miss. Emily Borie Ryerson, Miss. Susan Parker "Suzette" Ryerson, Mr. Arthur Larned Ryerson, Mrs. Arthur Larned (Emily Maria Borie) Straus, Mr. Isidor Straus, Mrs. Isidor (Rosalie Ida Blun) Ward, Miss. Anna Widener, Mr. George Dunton Widener, Mr. Harry Elkins Widener, Mrs. George Dunton (Eleanor Elkins)
Enter the number of the question you want answered. Enter ‘Q’ to quit the program :
1. How many passengers were on the Titanic?
2. What percentage of passengers perished on the Titanic?
3. What percentage passengers survived the sinking of the Titanic?
4. What percentage of passengers survived for each of the three classes?
5. What percentage of passengers survived as a function of gender?
6. What specific passengers paid more than $200 for their tickets?
7. What specific passengers who were less than 10 years old perished on the titanic?
8. What specific passengers who were less than 10 years old survived the sinking of the titanic?
9. For each letter in the alphabet, how many passengers last names started with that letter?
Q. Quit the program
Enter your selection: Q
Thank you for trying the Titanic Program. Elapsed time in seconds was: 95
Link to Titanic.txt file needed:
https://drive.google.com/file/d/0B8lS-fBjMB37cWRrbFhyb25Dc28/view?usp=sharing
Explanation / Answer
// TitanicTraveller.java
import java.util.Scanner;
import java.util.regex.Pattern;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.text.NumberFormat;
public class TitanicTraveller
{
/**
* The TrvlrName of this TitanicTraveller.
*/
private String TrvlrName;
/**
* The class this TitanicTraveller was travelling in. This will be 1, 2, or 3.
*/
private int classLevel;
/**
* True iff this TitanicTraveller SurvivedAlive.
*/
private boolean SurvivedAlive;
/**
* Creates a TitanicTraveller with the given parameters.
*
* @param n the TrvlrName of the new TitanicTraveller
* @param c the class of the new TitanicTraveller, "1st", "2nd", or "3rd"
* @param s 1 if the TitanicTraveller SurvivedAlive, 0 otherwise
*/
void TitanicTraveller(String n, String c, int s)
{
TrvlrName = n;
switch (c.charAt(0))
{
case '1':
classLevel = 1;
break;
case '2':
classLevel = 2;
break;
default:
classLevel = 3;
break;
}
SurvivedAlive = (s == 1);
}
/**
* Returns a new TitanicTraveller containing data read from the given scanner. The scanner's
* data source should be such that the next input is a TitanicTraveller number, class (1st, 2nd, 3rd),
* survival flag (0 or 1) and TrvlrName, separated by pipe characters (|) with possibly extra data
* at the end of the line.
*
* @param scan the scanner to read a TitanicTraveller from
* @return the TitanicTraveller read
*/
public static TitanicTraveller scanTitanicTraveller(Scanner scan)
{
// there's no terribly compelling reason for this to be a
// class method instead of a constructor except to review
// defining and using class methods
// set delimiter to |
Pattern oldDelim = scan.delimiter();
scan.useDelimiter("\|");
// read data
int num = scan.nextInt(); // ignored
String classLevel = scan.next(); // 1st, 2nd, or 3rd
int SurvivedAliveFlag = scan.nextInt(); // 1 for SurvivedAlive; 0 otherwise
String TrvlrName = scan.next(); // TrvlrName
scan.nextLine(); // consume and ignore the remainder of the line
// restore old delimiter used by the scanner
scan.useDelimiter(oldDelim);
// create and return object
return new TitanicTraveller(TrvlrName, classLevel, SurvivedAliveFlag);
}
/**
* Returns the TrvlrName of this TitanicTraveller.
*
* @return the TrvlrName of this TitanicTraveller
*/
public String getTrvlrName()
{
return TrvlrName;
}
/**
* Returns the class this TitanicTraveller was travelling in. The class
* will be 1, 2, or 3.
*
* @return the class this TitanicTraveller was travelling in
*/
public int getClassLevel()
{
return classLevel;
}
/**
* Determines whether this TitanicTraveller SurvivedAlive.
*
* @return true iff this TitanicTraveller SurvivedAlive
*/
public boolean didSurvive()
{
return SurvivedAlive;
}
}
// TravellerVerify
class TravellerVerify
{
/**
* The Travellers on this manifest.
*/
private ArrayList<Traveller> list;
/**
* Creates a Traveller manifest using Travellers data read from
* the given scanner. The data the scanner reads should be in a
* format with the Traveller number, class (1st, 2nd, or 3rd),
* survival flag (0 or 1), and name, followed by any extra data to
* the end of the line.
*
* @param scan the scanner to read Travellers from
*/
public TravellerVerify(Scanner scan)
{
// create an empty list
list = new ArrayList<Traveller>();
// read Travellers from the scanner until there are no more
while (scan.hasNext())
{
Traveller p = Traveller.scanTraveller(scan);
list.add(p);
}
}
/**
* Returns the number of Travellers on this manifest.
*
* @return the number of passeengers on this manifest
*/
public int CountTravellers()
{
return list.size();
}
/**
* Returns the nubmer of Travellers on this list in the given class.
*
* @param class an integer
* @return the number of Travellers on this list in that class
*/
public int CountTravellersInClass(int c)
{
int count = 0;
for (Traveller p : list)
{
if (p.getClassLevel() == c)
{
count++;
}
}
return count;
}
/**
* Returns the number of survovors in the given class.
*
* @param class an integer
* @return the number of survivors on this list in that class
*/
public int countSurvivorsInClass(int c)
{
int count = 0;
for (Traveller p : list)
{
if (p.getClassLevel() == c && p.didSurvive())
{
count++;
}
}
return count;
}
}
class Examineverify
{
public static void main(String[] args) throws FileNotFoundException
{
NumberFormat fmt = NumberFormat.getPercentInstance();
fmt.setMinimumFractionDigits(1);
fmt.setMaximumFractionDigits(1);
TravellerVerify verify = new TravellerVerify(new Scanner(new File("titanic.txt")));
for (int c = 1; c <= 3; c++)
{
System.out.println("=== CLASS " + c + " ===");
int total = verify.countPassengersInClass(c);
int survivors = verify.countSurvivorsInClass(c);
double rate = (double)survivors/total;
System.out.println(survivors + " survivors out of " + total);
System.out.println(fmt.format(rate) + " survived");
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.