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

I try running this codes on NetBean but I encounter alot of errors: Please can s

ID: 672009 • Letter: I

Question

I try running this codes on NetBean but I encounter alot of errors: Please can some one help identify them for me.

import java.io.*;
import java.util.*;

/*
Since You have not provide me text file I am Assuming its format
First_Name Last_Name Sex Price class Age perish_survive
First_Name -> String
Last_Name -> String
gender -> String (Male or Female)
Price -> Int;
Class(1,2,3) -> Int;
Age -> Int;
perish_survive -> Int(1 if survive otherwise 0);
*/

class Passenger{
String first_name;
String last_name;
String sex;
int price;
int class;
int age;
int perish;
public Passenger(String f,String l,String s,int p_1,int c,int a,int p_2){
first_name = f;
last_name = l;
sex = s;
price = p_1;
class = c;
age = a;
perish = p_2;
}
}

class main{
// return the total number of passengers on the Titanic
public static int getTotalPassengers(ArrayList<Passenger> pass){
return pass.size();
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String file_name = args[0];
BufferedReader br = null;
// Array to store the Titanic data.
ArrayList<Passenger> pass = new ArrayList<Passenger>();
try{
//Command Line arguments to send in the name of the Titanic file.
br = new BufferedReader(new FileReader(args[0]));
String line = br.readLine();
String[] str;
while (line != null){
str = line.split(" ");
String first = str[0];
String last = str[1];
String sex = str[2];
int price = Interger.parseInt(str[3]);
int class = Interger.parseInt(str[4]);
int age = Interger.parseInt(str[5]);
int perish = Interger.parseInt(str[6]);
Passenger p = new Passenger(first,last,price,class,age,perish);
pass.add(p);
line = br.readLine();
}
}
catch(IOException e){
e.printStackTrace();
}
while(true){
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 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 you selection : ");
int n = Integer.parseInt(sc.nextLine());
if (n == 1){
System.out.println("There were "+getTotalPassengers(pass)+" Passengers on the Titanic");
}
  
else if (n == 2){
float total = 0.0;
for (int i = 0; i < getTotalPassengers(pass); i++){
if (pass.get(i).perish == 0)
total++;
}
System.out.println("percentage of passengers perished on the Titanic is : "+((total*100)/getTotalPassengers(pass)));
}
  
else if (n == 3){
float total = 0.0;
for (int i = 0; i < getTotalPassengers(pass); i++){
if (pass.get(i).perish == 1)
total++;
}
System.out.println("percentage of passengers survived on the Titanic is : "+((total*100)/getTotalPassengers(pass)));
}
  
else if (n == 4){
float total_1 = 0.0,total_2 = 0.0,total_3 = 0.0;
for (int i = 0; i < getTotalPassengers(pass); i++){
if (pass.get(i).perish == 1){
if (pass.get(i).class == 1) total_1++;
else if (pass.get(i).class == 2) total_2++;
else total_3++;
}
}
System.out.println("percentage of passengers of class 1 perished on the Titanic is : "+((total_1*100)/getTotalPassengers(pass)));
System.out.println("percentage of passengers of class 2 perished on the Titanic is : "+((total_2*100)/getTotalPassengers(pass)));
System.out.println("percentage of passengers of class 3 perished on the Titanic is : "+((total_3*100)/getTotalPassengers(pass)));
}

else if (n == 5){
float total = 0.0;
for (int i = 0; i < getTotalPassengers(pass); i++){
if (pass.get(i).sex.charAt(0) == 'm' || pass.get(i).sex.charAt(0) == 'M')
total++;
}
double per = total*100)/getTotalPassengers(pass);
System.out.println("percentage of Male survived on the Titanic is : "+per);
System.out.println("percentage of Female survived on the Titanic is : "+(100-per));
}

else if (n == 6){
System.out.println("The following passengers paid more than $200 for their tickets:");
for (int i = 0; i < getTotalPassengers(pass); i++){
if (pass.get(i).price > 200)
System.out.println(pass.get(i).last_name+","+pass.get(i).first_name);
}
}

else if (n == 7){
float total = 0.0;
System.out.println("passengers who were less than 10 years old perished on the titanic:");
for (int i = 0; i < getTotalPassengers(pass); i++){
if (pass.get(i).perish == 0 && pass.get(i).age < 10)
System.out.println(pass.get(i).last_name+","+pass.get(i).first_name);
}
}

else if (n == 8){
float total = 0.0;
System.out.println("passengers who were less than 10 years old survived on the titanic:");
for (int i = 0; i < getTotalPassengers(pass); i++){
if (pass.get(i).perish == 1 && pass.get(i).age < 10)
System.out.println(pass.get(i).last_name+","+pass.get(i).first_name);
}
}

else if (n == 9){
char[] all = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for (int i = 0; i < 26; i++){
char ch = all[i];
System.out.println("Passenger whose last name start with "+ch+" are :");
for (int i = 0; i < getTotalPassengers(pass); i++){
String last = pass.get(i).last_name.toLowerCase();
if (last.charAt(i) == ch)
System.out.println(pass.get(i).last_name+","+pass.get(i).first_name);
}
}
}
else{
break;
}
}
}
}

Explanation / Answer

import java.io.*;
import java.util.*;

/*
Since You have not provide me text file I am Assuming its format
First_Name Last_Name Sex Price class Age perish_survive
First_Name -> String
Last_Name -> String
gender -> String (Male or Female)
Price -> Int;
Class(1,2,3) -> Int;
Age -> Int;
perish_survive -> Int(1 if survive otherwise 0);
*/
class Passenger {
   String first_name;
   String last_name;
   String sex;
   int price;
   int classVal;
   int age;
   int perish;

   public Passenger(String f, String l, String s, int p_1, int c, int a,
           int p_2) {
       first_name = f;
       last_name = l;
       sex = s;
       price = p_1;
       classVal = c;
       age = a;
       perish = p_2;
   }
}

class mainClass {
   // return the total number of passengers on the Titanic
   public static int getTotalPassengers(ArrayList<Passenger> pass) {
       return pass.size();
   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String file_name = args[0];
       BufferedReader br = null;
       // Array to store the Titanic data.
       ArrayList<Passenger> pass = new ArrayList<Passenger>();
       try {
           // Command Line arguments to send in the name of the Titanic file.
           br = new BufferedReader(new FileReader(args[0]));
           String line = br.readLine();
           String[] str;
           while (line != null) {
               str = line.split(" ");
               String first = str[0];
               String last = str[1];
               String sex = str[2];
               int price = Integer.parseInt(str[3]);
               int classVal = Integer.parseInt(str[4]);
               int age = Integer.parseInt(str[5]);
               int perish = Integer.parseInt(str[6]);
               Passenger p = new Passenger(first, last, sex, price, classVal,
                       age, perish);
               pass.add(p);
               line = br.readLine();
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
       while (true) {
           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 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 you selection : ");
           int n = Integer.parseInt(sc.nextLine());
           if (n == 1) {
               System.out.println("There were " + getTotalPassengers(pass)
                       + " Passengers on the Titanic");
           }

           else if (n == 2) {
               float total = 0.0f;
               for (int i = 0; i < getTotalPassengers(pass); i++) {
                   if (pass.get(i).perish == 0)
                       total++;
               }
               System.out
                       .println("percentage of passengers perished on the Titanic is : "
                               + ((total * 100) / getTotalPassengers(pass)));
           }

           else if (n == 3) {
               float total = 0.0f;
               for (int i = 0; i < getTotalPassengers(pass); i++) {
                   if (pass.get(i).perish == 1)
                       total++;
               }
               System.out
                       .println("percentage of passengers survived on the Titanic is : "
                               + ((total * 100) / getTotalPassengers(pass)));
           }

           else if (n == 4) {
               float total_1 = 0.0f, total_2 = 0.0f, total_3 = 0.0f;
               for (int i = 0; i < getTotalPassengers(pass); i++) {
                   if (pass.get(i).perish == 1) {
                       if (pass.get(i).classVal == 1)
                           total_1++;
                       else if (pass.get(i).classVal == 2)
                           total_2++;
                       else
                           total_3++;
                   }
               }
               System.out
                       .println("percentage of passengers of class 1 perished on the Titanic is : "
                               + ((total_1 * 100) / getTotalPassengers(pass)));
               System.out
                       .println("percentage of passengers of class 2 perished on the Titanic is : "
                               + ((total_2 * 100) / getTotalPassengers(pass)));
               System.out
                       .println("percentage of passengers of class 3 perished on the Titanic is : "
                               + ((total_3 * 100) / getTotalPassengers(pass)));
           } else if (n == 5) {
               float total = 0.0f;
               for (int i = 0; i < getTotalPassengers(pass); i++) {
                   if (pass.get(i).sex.charAt(0) == 'm'
                           || pass.get(i).sex.charAt(0) == 'M')
                       total++;
               }
               double per = (total * 100) / getTotalPassengers(pass);
               System.out
                       .println("percentage of Male survived on the Titanic is : "
                               + per);
               System.out
                       .println("percentage of Female survived on the Titanic is : "
                               + (100 - per));
           } else if (n == 6) {
               System.out
                       .println("The following passengers paid more than $200 for their tickets:");
               for (int i = 0; i < getTotalPassengers(pass); i++) {
                   if (pass.get(i).price > 200)
                       System.out.println(pass.get(i).last_name + ","
                               + pass.get(i).first_name);
               }
           } else if (n == 7) {
               float total = 0.0f;
               System.out
                       .println("passengers who were less than 10 years old perished on the titanic:");
               for (int i = 0; i < getTotalPassengers(pass); i++) {
                   if (pass.get(i).perish == 0 && pass.get(i).age < 10)
                       System.out.println(pass.get(i).last_name + ","
                               + pass.get(i).first_name);
               }
           } else if (n == 8) {
               float total = 0.0f;
               System.out
                       .println("passengers who were less than 10 years old survived on the titanic:");
               for (int i = 0; i < getTotalPassengers(pass); i++) {
                   if (pass.get(i).perish == 1 && pass.get(i).age < 10)
                       System.out.println(pass.get(i).last_name + ","
                               + pass.get(i).first_name);
               }
           } else if (n == 9) {
               char[] all = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                       'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                       'u', 'v', 'w', 'x', 'y', 'z' };
               for (int i = 0; i < 26; i++) {
                   char ch = all[i];
                   System.out.println("Passenger whose last name start with "
                           + ch + " are :");
                   for (int j = 0; j < getTotalPassengers(pass); j++) {
                       String last = pass.get(j).last_name.toLowerCase();
                       if (last.charAt(j) == ch)
                           System.out.println(pass.get(i).last_name + ","
                                   + pass.get(i).first_name);
                   }
               }
           } else {
               break;
           }
       }
   }
}

Errors :

1. class is used as identifier

2. for the float variables are assigned double

3. Integer Wrapper class spell mistake