I\'m am trying to write a code that helps people get off of a boat. I have a men
ID: 3623722 • Letter: I
Question
I'm am trying to write a code that helps people get off of a boat. I have a menu where the user can choose 1 to add more people to a list, 2 to release the people in the list to a lifeboat, and 3 to exit. I have to create the classes and methods.I've created a class that prioritizes people when they are added to the list, then sorts them using Arrays.sort, and I'm trying to create a class that will gather those sorted people and allow a certain amount of them to get onto the lifeboat( how ever many the lifeboat can fit). This is what i need to happen when the user chooses option 2. I am very stuck. 1 seems to work well enough, but I can't go from there. I've commented my code to help show what I was TRYING to do....
package evacuation;
import java.io.*;
import java.util.*;
public class Evacuation
{
static Evacuee newPerson(Scanner scan1) //what happens when i press 1
{
System.out.println("What is the evacuee's first name? ");
String firstn = scan1.next();
System.out.println("What is the evacuee's last name? ");
String lastn = scan1.next();
System.out.println("Is the evacuee 'male' or 'female'");
boolean sex = scan1.next() != null; //not sure if this is gonna do what I need it to...
System.out.println("What is the evacuee's age");
int age = scan1.nextInt();
System.out.println("What is the evacuee's annual income");
int income = scan1.nextInt();
return new Evacuee(firstn, lastn, sex, age, income);
}
static void waitList(Scanner scan1, int numPeople) // reads in a specific (numPeople) of ppl
{
Evacuee[] listArray = new Evacuee[numPeople]; //listArray is array of Evacuees
for(int i = 0; i <numPeople; i++)
listArray[i] = newPerson(scan1); // set each Evacuee to each index in listArray
System.out.println("These Are The Following People In The List...");
//System.out.println();
//System.out.println();
for(int i = 0; i < listArray.length; i++) //hopefully goes through the array and prints the Evacuees
{
System.out.println(listArray[i]);
}
System.out.println();
System.out.println();
Arrays.sort(listArray);
System.out.println("These Are The People In Prioritized Order...");
for(int i = 0; i < listArray.length; i++)
{
System.out.println(listArray[i]);
}
}
//I need to create this which is what would happen wwhen option 2 is chosen
//what I'm trying to do: read in the sorted array listArray and print out the number of Evacuees
//in the array up to a certain number. The number of people I want to print out from listArray
//is boatSize number of people. But an error i get is that listArray cannot be resolved to a variable
static void lifeBoat(Scanner scan1)
{
System.out.println("How many people can the lifeboat hold? "); //where the confusion starts.
int boatSize = scan1.nextInt();
System.out.println();
// Evacuee[] boatArray = new Evacuee[boatSize];
// for(int i = 0; i < boatSize; i++)
// boatArray[i] = Arrays.sort(listArray);
}
public static void main(String[] args)
{
System.out.println("Welcome! You're Probably About To Die! MAKE IT QUICK...");
System.out.println("1. Add Person To List...");
System.out.println();
System.out.println("2. Release Lifeboat...");
System.out.println();
System.out.println("3. Exit Program And SAVE YOURSELF!!!");
Scanner scan0 = new Scanner(System.in);
int choice = scan0.nextInt();
switch (choice)
{
case 1:
{
Scanner scan1 = new Scanner(System.in);
waitList(scan1, 2);
}
break;
case 2:
{
}
break;
case 3: System.exit(1);
}
}
}
Explanation / Answer
see you messages and please rate - thanks
static void lifeBoat(Evacuee boatArray[], Evacuee listArray[],int boatSize,Scanner scan1)
{
Arrays.sort(listArray);
for(int i = 0; i < boatSize; i++)
boatArray[i] =listArray[i];
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.