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

Before attempting this project, be sure you have completed all of the reading as

ID: 3674217 • Letter: B

Question

Before attempting this project, be sure you have completed all of the reading assignments, hands-on labs, discussions, and assignments to date. 1. (25 points) Design, implement and test 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. Percentage of passengers who perished on the Titanic

c. Percentage of Passengers who survived the sinking of the Titanic

d. Percentage passengers who survived the sinking of the Titanic as a function of the passenger class (e.g. 1,2,3)

e. A list of the names of passengers who were less than 10 years old who perished on the Titanic

f. The count of the number of passengers as a function of the first letter of their last name. (e.g., A: 13, B:33 …)

g. At least one additional statistical results you add to enhance the functionality 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 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 2 ********** 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 specific passengers who were less than 10 years old survived the sinking of the titanic? 6. 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 specific passengers who were less than 10 years old survived the sinking of the titanic? 6. 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

Explanation / Answer

titanic.txt

1   1   Allen, Miss. Elisabeth Walton   female   29   211.3375
1   1   Allison, Master. Hudson Trevor   male   0.9167   151.5500
1   0   Allison, Miss. Helen Loraine   female   2   151.5500
1   0   Allison, Mr. Hudson Joshua Creighton   male   30   151.5500
1   0   Allison, Mrs. Hudson J C (Bessie Waldo Daniels)   female   25   151.5500
1   1   Anderson, Mr. Harry   male   48   26.5500
1   1   Andrews, Miss. Kornelia Theodosia   female   63   77.9583
1   0   Andrews, Mr. Thomas Jr   male   39   0.0000
1   1   Appleton, Mrs. Edward Dale (Charlotte Lamson)   female   53   51.4792
1   0   Artagaveytia, Mr. Ramon   male   71   49.5042
1   0   Astor, Col. John Jacob   male   47   227.5250
1   1   Astor, Mrs. John Jacob (Madeleine Talmadge Force)   female   18   227.5250
1   1   Aubart, Mme. Leontine Pauline   female   24   69.3000
1   1   Barber, Miss. Ellen Nellie   female   26   78.8500
1   1   Barkworth, Mr. Algernon Henry Wilson   male   80   30.0000
1   0   Baumann, Mr. John D   male       25.9250
1   0   Baxter, Mr. Quigg Edmond   male   24   247.5208
1   1   Baxter, Mrs. James (Helene DeLaudeniere Chaput)   female   50   247.5208
1   1   Bazzani, Miss. Albina   female   32   76.2917
1   0   Beattie, Mr. Thomson   male   36   75.2417
1   1   Beckwith, Mr. Richard Leonard   male   37   52.5542
1   1   Beckwith, Mrs. Richard Leonard (Sallie Monypeny)   female   47   52.5542
1   1   Behr, Mr. Karl Howell   male   26   30.0000
1   1   Bidois, Miss. Rosalie   female   42   227.5250
1   1   Bird, Miss. Ellen   female   29   221.7792
1   0   Birnbaum, Mr. Jakob   male   25   26.0000
1   1   Bishop, Mr. Dickinson H   male   25   91.0792
1   1   Bishop, Mrs. Dickinson H (Helen Walton)   female   19   91.0792
1   1   Bissette, Miss. Amelia   female   35   135.6333
1   1   Bjornstrom-Steffansson, Mr. Mauritz Hakan   male   28   26.5500
1   0   Blackwell, Mr. Stephen Weart   male   45   35.5000
1   1   Blank, Mr. Henry   male   40   31.0000
1   1   Bonnell, Miss. Caroline   female   30   164.8667
1   1   Bonnell, Miss. Elizabeth   female   58   26.5500
1   0   Borebank, Mr. John James   male   42   26.5500
1   1   Bowen, Miss. Grace Scott   female   45   262.3750
1   1   Bowerman, Miss. Elsie Edith   female   22   55.0000
1   1   Bradley, Mr. George (George Arthur Brayton)   male       26.5500
1   0   Brady, Mr. John Bertram   male   41   30.5000
1   0   Brandeis, Mr. Emil   male   48   50.4958
1   0   Brewe, Dr. Arthur Jackson   male       39.6000
1   1   Brown, Mrs. James Joseph (Margaret Tobin)   female   44   27.7208
1   1   Brown, Mrs. John Murray (Caroline Lane Lamson)   female   59   51.4792
1   1   Bucknell, Mrs. William Robert (Emma Eliza Ward)   female   60   76.2917
1   1   Burns, Miss. Elizabeth Margaret   female   41   134.5000
1   0   Butt, Major. Archibald Willingham   male   45   26.5500
1   0   Cairns, Mr. Alexander   male       31.0000
1   1   Calderhead, Mr. Edward Pennington   male   42   26.2875
1   1   Candee, Mrs. Edward (Helen Churchill Hungerford)   female   53   27.4458
1   1   Cardeza, Mr. Thomas Drake Martinez   male   36   512.3292
1   1   Cardeza, Mrs. James Warburton Martinez (Charlotte Wardle Drake)   female   58   512.3292
1   0   Carlsson, Mr. Frans Olof   male   33   5.0000
1   0   Carrau, Mr. Francisco M   male   28   47.1000
1   0   Carrau, Mr. Jose Pedro   male   17   47.1000
1   1   Carter, Master. William Thornton II   male   11   120.0000
1   1   Carter, Miss. Lucile Polk   female   14   120.0000
1   1   Carter, Mr. William Ernest   male   36   120.0000
1   1   Carter, Mrs. William Ernest (Lucile Polk)   female   36   120.0000
1   0   Case, Mr. Howard Brown   male   49   26.0000
1   1   Cassebeer, Mrs. Henry Arthur Jr (Eleanor Genevieve Fosdick)   female       27.7208
1   0   Cavendish, Mr. Tyrell William   male   36   78.8500
1   1   Cavendish, Mrs. Tyrell William (Julia Florence Siegel)   female   76   78.8500
1   0   Chaffee, Mr. Herbert Fuller   male   46   61.1750
1   1   Chaffee, Mrs. Herbert Fuller (Carrie Constance Toogood)   female   47   61.1750
1   1   Chambers, Mr. Norman Campbell   male   27   53.1000
1   1   Chambers, Mrs. Norman Campbell (Bertha Griggs)   female   33   53.1000
1   1   Chaudanson, Miss. Victorine   female   36   262.3750
1   1   Cherry, Miss. Gladys   female   30   86.5000
1   1   Chevre, Mr. Paul Romaine   male   45   29.7000
1   1   Chibnall, Mrs. (Edith Martha Bowerman)   female       55.0000
1   0   Chisholm, Mr. Roderick Robert Crispin   male       0.0000
1   0   Clark, Mr. Walter Miller   male   27   136.7792
1   1   Clark, Mrs. Walter Miller (Virginia McDowell)   female   26   136.7792
1   1   Cleaver, Miss. Alice   female   22   151.5500
1   0   Clifford, Mr. George Quincy   male       52.0000
1   0   Colley, Mr. Edward Pomeroy   male   47   25.5875
1   1   Compton, Miss. Sara Rebecca   female   39   83.1583
1   0   Compton, Mr. Alexander Taylor Jr   male   37   83.1583
1   1   Compton, Mrs. Alexander Taylor (Mary Eliza Ingersoll)   female   64   83.1583
1   1   Cornell, Mrs. Robert Clifford (Malvina Helen Lamson)   female   55   25.7000
1   0   Crafton, Mr. John Bertram   male       26.5500
1   0   Crosby, Capt. Edward Gifford   male   70   71.0000
1   1   Crosby, Miss. Harriet R   female   36   71.0000
1   1   Crosby, Mrs. Edward Gifford (Catherine Elizabeth Halstead)   female   64   26.5500
1   0   Cumings, Mr. John Bradley   male   39   71.2833
1   1   Cumings, Mrs. John Bradley (Florence Briggs Thayer)   female   38   71.2833
1   1   Daly, Mr. Peter Denis    male   51   26.5500
1   1   Daniel, Mr. Robert Williams   male   27   30.5000
1   1   Daniels, Miss. Sarah   female   33   151.5500
1   0   Davidson, Mr. Thornton   male   31   52.0000
1   1   Davidson, Mrs. Thornton (Orian Hays)   female   27   52.0000
1   1   Dick, Mr. Albert Adrian   male   31   57.0000
1   1   Dick, Mrs. Albert Adrian (Vera Gillespie)   female   17   57.0000
1   1   Dodge, Dr. Washington   male   53   81.8583
1   1   Dodge, Master. Washington   male   4   81.8583
1   1   Dodge, Mrs. Washington (Ruth Vidaver)   female   54   81.8583
1   0   Douglas, Mr. Walter Donald   male   50   106.4250
1   1   Douglas, Mrs. Frederick Charles (Mary Helene Baxter)   female   27   247.5208
1   1   Douglas, Mrs. Walter Donald (Mahala Dutton)   female   48   106.4250
1   1   Duff Gordon, Lady. (Lucille Christiana Sutherland) (Mrs Morgan)   female   48   39.6000
1   1   Duff Gordon, Sir. Cosmo Edmund (Mr Morgan)   male   49   56.9292
1   0   Dulles, Mr. William Crothers   male   39   29.7000
1   1   Earnshaw, Mrs. Boulton (Olive Potter)   female   23   83.1583
1   1   Endres, Miss. Caroline Louise   female   38   227.5250
1   1   Eustis, Miss. Elizabeth Mussey   female   54   78.2667
1   0   Evans, Miss. Edith Corse   female   36   31.6792
1   0   Farthing, Mr. John   male       221.7792
1   1   Flegenheim, Mrs. Alfred (Antoinette)   female       31.6833
1   1   Fleming, Miss. Margaret   female       110.8833
1   1   Flynn, Mr. John Irwin (Irving)   male   36   26.3875
1   0   Foreman, Mr. Benjamin Laventall   male   30   27.7500
1   1   Fortune, Miss. Alice Elizabeth   female   24   263.0000
1   1   Fortune, Miss. Ethel Flora   female   28   263.0000
1   1   Fortune, Miss. Mabel Helen   female   23   263.0000
1   0   Fortune, Mr. Charles Alexander   male   19   263.0000
1   0   Fortune, Mr. Mark   male   64   263.0000
1   1   Fortune, Mrs. Mark (Mary McDougald)   female   60   263.0000
1   1   Francatelli, Miss. Laura Mabel   female   30   56.9292
1   0   Franklin, Mr. Thomas Parham   male       26.5500
1   1   Frauenthal, Dr. Henry William   male   50   133.6500

TestTitanic.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestTitanic {

    // Method that builds a 2D array from the txt file
    public void filler(String[] args) {

        // Try-catch is needed for passing args to method
        try {
            // Turns the txt file into one large string
            String content = new Scanner(new File(args[0]))
                    .useDelimiter("\Z") // To the end of the file
                    .next();
            String[] rows = content.split(" "); // Breaks up by new line
            // Creates a 2D array as "long" as the input file
            String[][] titanic = new String[rows.length][];

            // Fills the 2D array
            for (int i = 0; i < rows.length; i++) {
                titanic[i] = rows[i].split("\t");
            }

            // Creates a new Titanic object
            Titanic titanicObject = new Titanic(titanic);
            // Calls the passToMenu method from Titanic class
            titanicObject.passToMenu();

        } catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }

    }

    public static void main(String[] args) {

        // Create an object from this class
        TestTitanic runner = new TestTitanic();
        // Calls the program method to fill the array
        runner.filler(args);

    }
}


Titanic.java


import java.text.DecimalFormat;
import java.time.LocalTime;
import java.util.*;


public class Titanic {

    // Private 2D array for the data
    private String[][] titanicData;
    private LocalTime startTime;
    private LocalTime stopTime;

    // Default constructor, accepts 2D array
    public Titanic(String[][] data) {
        this.titanicData = data;
        // Initializes startTime
        this.startTime = LocalTime.now();
    }

    // Creates a menu object and begins the prompts for user input
    public void passToMenu() {
        Menu menu = new Menu(this.titanicData);
        menu.input();
    }

   // 1 row = 1 passenger
    public void totalPassengers() {
        System.out.println(" The total number of passengers on the Titanic was: "
            + titanicData.length);
    }

    // Calculates percent of position 1 = 0 / total
    public void perishedPercent() {
        // DecimalFormat to clean up the output value %##.##
        DecimalFormat df = new DecimalFormat("#.00");
        // counter needs to be a double for percentage to work
        double counter = 0;
        // For each row in titanicData...
        for (String[] row : titanicData) {
            // Test if the 2nd [1] position is a string of "0"
            if (row[1].equals("0")) {
                counter++;
            }
        }
        // Sets the counter to the percent of the total
        counter = (counter / titanicData.length) * 100;
        System.out.println(" " + df.format(counter) + "% of people aboard the" +
                " Titanic perished.");
    }

    // Same as perishedPercent but for position 1 = 1 / total
    public void survivedPercent() {
        // DecimalFormat to clean up the output value %##.##
        DecimalFormat df = new DecimalFormat("#.00");
        double counter = 0;
        for (String[] row : titanicData) {
            if (row[1].equals("1")) {
                counter++;
            }
        }
        counter = (counter / titanicData.length) * 100;
        System.out.println(" " + df.format(counter) + "% of people aboard the" +
                " Titanic survived.");
    }

    // Calculates percent of survivors separated by position 0
    public void survivedByClass() {
        // DecimalFormat to clean up the output value %##.##
        DecimalFormat df = new DecimalFormat("#.00");
        double firstClass = 0;
        double secondClass = 0;
        double thirdClass = 0;
        for (String[] row : titanicData) {
            // Tests for position [0] equals a string value 1, 2, or 3
            if (row[0].equals("1")) {
                firstClass++;
            } else if (row[0].equals("2")) {
                secondClass++;
            } else if (row[0].equals("3")) {
                thirdClass++;
            }
        }
        // Refactor counters to be percentages
        firstClass = (firstClass / titanicData.length) * 100;
        secondClass = (secondClass / titanicData.length) * 100;
        thirdClass = (thirdClass / titanicData.length) * 100;
        System.out.println(" " + df.format(firstClass) + "% of people aboard the" +
                " Titanic were First Class citizens.");
        System.out.println(df.format(secondClass) + "% of people aboard the " +
                "Titanic were Second Class citizens.");
        System.out.println(df.format(thirdClass) + "% of people aboard the " +
                "Titanic were Third Class citizens.");
    }

    // Same as survivedByClass but for position 3 in each row
    public void survivedByGender() {
        // DecimalFormat to clean up the output value %##.##
        DecimalFormat df = new DecimalFormat("#.00");
        double male = 0;
        double female = 0;
        for (String[] row : titanicData) {
            if (row[3].equals("male")) {
                male++;
            } else if (row[3].equals("female")) {
                female++;
            }
        }
        male = (male / titanicData.length) * 100;
        female = (female / titanicData.length) * 100;
        System.out.println(" " + df.format(male) + "% of people aboard the" +
                " Titanic were male.");
        System.out.println(df.format(female) + "% of people aboard the" +
                " Titanic were female.");
    }

    // Method displays position 2 of rows which position 5 > 200
    public void rippedOff() {
        System.out.println(" The following passengers paid more than $200" +
                " for their tickets:");
        // For each row in titanicData..
        for (String[] row : titanicData) {
            // Try-Catch to handle null values
            try {
                // Test if position 5 is greater than 200
                if (Double.parseDouble(row[5]) > 200) {
                    // Print that row's position 2
                    System.out.println(row[2]);
                }
            } catch (Exception e) {
                //Continues if null string is found.
            }
        }
    }

    // Method displays position 2 of rows where position 1 is 0 and 4 < 10
    public void perishedUnderTen() {
        System.out.println(" The following passengers were less than 10" +
                " years old and perished:");
        for (String[] row : titanicData) {
            // Try-Catch for null values
            try {
                // Test if position 1 is 0
                if (row[1].equals("0")) {
                    // And tests if position 4 is less than 10
                    if (Double.parseDouble(row[4]) < 10) {
                        // Prints that row's position 2
                        System.out.println(row[2]);
                    }
                }
            } catch (Exception e) {
                //Continues if null string is found.
            }
        }
    }

    // Same as perishedUnderTen but position 1 equals 1 for survivors
    public void survivedUnderTen() {
        System.out.println(" The following passengers were less than 10" +
                " years old and survived:");
        for (String[] row : titanicData) {
            try {
                if (row[1].equals("1")) {
                    if (Double.parseDouble(row[4]) < 10) {
                        System.out.println(row[2]);
                    }
                }
            } catch (Exception e) {
                //Continues if null string is found.
            }
        }
    }

    // Method that displays a count by each passenger's first letter
    public void totalByLetter() {
        // Create an array list of Characters
        ArrayList<Character> letters = new ArrayList<>();
        for (int i = 0; i < titanicData.length; i++) {
            // Adds to list, from titanicData, as Upper Case, the first letter
            letters.add(titanicData[i][2].toUpperCase().charAt(0));
        }

        System.out.println(" For each first letter of their last names," +
                " here is a count of the passengers:");

        // Create a set of characters using the letters
        Set<Character> chars = new HashSet<>(letters);
        // For each character in the chars set...
        for (Character ch : chars) {
            // Returns the number of elements in letters equal to the ch object
            System.out.println(ch + ": " + Collections.frequency(letters, ch));
        }
    }

    // Method used to quit
    public void quitProgram() {
        System.out.println(" Thank you for trying the Titanic Program.");
        // Initializes stopTime
        this.stopTime = LocalTime.now();
        System.out.println(" Elapsed time in seconds was: " +
                // Gets the time between startTime and stopTime
            java.time.Duration.between(this.startTime, this.stopTime)
                .getSeconds()); // Converted to seconds
    }

}


Menu.java
import java.util.Scanner;

public class Menu extends Titanic {

    // Private data fields specific to Menu
    private static Boolean runState = true;
    private Scanner input = new Scanner(System.in);
    private String choice;

    // Default constructor
    public Menu(String[][] data) {
        super(data);
    }

    // Method that displays the menu in the command-line
    public void showMenu() {
        System.out.println(" *********Welcome to the Titanic Statistical Application" +
                "************************** ");
        System.out.println("Enter the number of the question you want answered. " +
                "Enter 'Q' to quit the program: ");
        System.out.println("1. How many passengers were on the Titanic?");
        System.out.println("2. What percentage of passengers perished on the Titanic?");
        System.out.println("3. What percentage of passengers survived the sinking of" +
                "the Titanic?");
        System.out.println("4. What percentage of passengers survived for each of the " +
                "three classes?");
        System.out.println("5. What percentage of passengers survived as a function " +
                "of gender?");
        System.out.println("6. What specific passengers paid more than $200 for their" +
                " tickets?");
        System.out.println("7. What specific passengers who were less than 10 years old" +
                " perished on the Titanic?");
        System.out.println("8. What specific passengers who were less than 10 years old" +
                " survived the sinking of the titanic?");
        System.out.println("9. For each letter in the alphabet, how many passengers last" +
                " names started with that letter?");
        System.out.println("Q. Quit the program.");
        System.out.print(" Enter your selection: ");
    }

    // Method that places the menu in a do/while loop
    public void input() {
        do {
            showMenu();
            choice = input.next().toLowerCase();
            // Runs methods from Titanic class based on user input
            switch (choice) {
                case "q" : quitProgram();
                    runState = false;
                    break;
                case "1" : totalPassengers();
                    break;
                case "2" : perishedPercent();
                    break;
                case "3" : survivedPercent();
                    break;
                case "4" : survivedByClass();
                    break;
                case "5" : survivedByGender();
                    break;
                case "6" : rippedOff();
                    break;
                case "7" : perishedUnderTen();
                    break;
                case "8" : survivedUnderTen();
                    break;
                case "9" : totalByLetter();
                    break;
                default:
                    System.out.println(" Please enter a valid choice.");
                    break;
            }

            // When runState turns false, this method ends
        } while (runState);

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote