Java Java Help with displaying an array list while using other classes

AI Thread Summary
The discussion revolves around a programming assignment where the user is struggling to print values from a passenger array within their Aircraft class. The current output successfully displays aircraft information but fails to include detailed passenger information in the desired format. The user has attempted various methods to access passenger data, including calling methods from the Aircraft and Passenger classes, but has not achieved the expected results. Key suggestions from the community include using different traversal methods for the passenger list, such as enhanced for-loops or streams, to format the output correctly. Additionally, there is advice to rename the variable from "passenger" to "passengers" to better reflect its nature as a collection. The user is encouraged to ensure that the passenger information is properly concatenated and formatted to eliminate unwanted brackets in the output. Overall, the focus is on troubleshooting class interactions and improving data presentation in Java.
smilesofmiles
Messages
18
Reaction score
0
This is a class assignment that has been puzzling me for a few hours. I need this to print out the values from the passenger array. My code so far prints the values from my aircraft class perfectly but not from my passenger class. Please see my output below:

View attachment 6401

How do I make the output display this instead?

Information regarding AirCraft: 2000 parsecs 4 Passenger1[The Twilight Zone, First Class Bunker] Passenger2 [ Bermuda Triangle, Near the Bathroom] Passenger3 [North Korea, Windowseat blocked by Airplane Wing] Passenger4 [Japan, Behind screaming infant].

Preferably without the ugly brackets haha


Things I've tried:


Code:
//In my main class
airCraft.getInfo();
airCraft.getInfo().getPassengerInfo();
airCraft.getPassengerInfo().getInfo();
airCraft.getPassengferInfo();
System.out.println(airCraft.getInfo().getPassengerIno());
System.out.println(airCraft[0].getInfo()); <-- I know it's not an array but I was desperate!
// In my passenger class

   public String getInfo(){
       return ("Information regarding AirCraft: " + maxSpeed + " " + maxPassengers + " " + passenger.getPassengerInfo());
   }
//Other things I've tried
//Making sure I have the correct number of variables in each constructor, Double checking my syntax for arraylists
//Looking up youtube videos on arraylists and classes
//Re-reading my textbook
//Taking a break for hot chocolate

MY ENTIRE CODE SEPARATED BY CLASSES

Code:
//CLASS APP
import java.util.ArrayList;public class App {

    public static void main(String[] args) {
        
        ArrayList<Passenger> passenger = new ArrayList();
  
        passenger.add(new Passenger("The Twilight Zone", "First Class Bunker"));
        passenger.add(new Passenger("Bermuda Triangle", "Near the Bathroom"));
        passenger.add(new Passenger("North Korea", "Windowseat blocked by Airplane Wing"));
        passenger.add(new Passenger("Japan", "Behind screaming infant"));
        //I hate flying :-D
        
        AirCraft airCraft = new AirCraft("2000 parsecs", 4, passenger);
        
        System.out.println(airCraft.getInfo());
        
        
    }
    
}
Code:
//CLASS AIRCRAFT

import java.util.ArrayList;

public class AirCraft {
    //AKA Constructor hint
    //Each Aircraft contains one Pilot, one Stewardess, and four Passenger
    //maxSpeed, maxPassengers,]\
    private String maxSpeed;
    private int maxPassengers;
    private ArrayList<Passenger> passenger; 
    
    public AirCraft(String maxSpeed, int maxPassengers, ArrayList<Passenger> passenger){
        this.maxSpeed = maxSpeed;
        this.maxPassengers = maxPassengers;
        this.passenger = passenger;
    }

 
   public String getMaxSpeed() 
   {
       return this.maxSpeed;
   }
   public void setMaxSpeed(String maxSpeed) 
   {
       this.maxSpeed = maxSpeed;
   }
   public int getMaxPassengers(){
       return this.maxPassengers;
   }
   public void setMaxPassengers(int maxPassengers){
       this.maxPassengers = maxPassengers;
   }
   public void getPassenger (ArrayList<Passenger> passenger){
       this.passenger = passenger;
   }
   public ArrayList<Passenger> setPassenger(){
       return this.passenger;
   }
   public String getInfo(){
       return ("Information regarding AirCraft: " + maxSpeed + " " + maxPassengers + " " + passenger);
   }
   

}
Code:
//CLASS PASSENGER
import java.util.ArrayList;public class Passenger {
    //Must have two attributes
    //finalDestination, seatingSection
    private String finalDestination;
    private String seatingSection;

    
    Passenger(String finalDestination, String seatingSection){
        this.finalDestination = finalDestination;
        this.seatingSection = seatingSection;
        
    }
    public String getFinalDestination()
    {
        return this.finalDestination;
    }
    public void setFinalDestination(String finalDestination){
        this.finalDestination = finalDestination;
    }
    public String getSeatingSection(){
        return this.seatingSection;
    }
    public void setSeatingSection(String seatingSection){
        this.seatingSection = seatingSection;
    }
    public String getPassengerInfo(){
        return (finalDestination + " " + seatingSection + " ");
    }
}

Some background:
I need to make all of these classes for my assignment.
An Aircraft class
A Pilot class
A Passenger class
A Stewardess class
A Suitcase class
An Address class
A Map class

With each Aircraft containing one Pilot, one Stewardess, and four Passengers.

I decided to take it slow and just create the first two classes, Aircraft and Passenger. I am trouble shooting them right now but can't get them to print :-(

Any help, or push int the right direction would be greatly appreciated!
Thank you!
 

Attachments

  • aircraftproblem.JPG
    aircraftproblem.JPG
    6.6 KB · Views: 103
Technology news on Phys.org
There are many ways to traverse a list in Java. Here are several examples.

Code:
for (Passenger p : passengers)
  System.out.printf("%s, ", p.getPassengerInfo());

for (int i = 0; i < passengers.size(); i++)
  System.out.printf("%s, ", passengers.get(i).getPassengerInfo());

for (Iterator<Passenger> it = passengers.iterator(); it.hasNext(); )
  System.out.printf("%s, ", it.next().getPassengerInfo());

passengers.stream().forEach(e -> System.out.printf("%s, ", e.getPassengerInfo()));

You can read about them in the tutorial by Oracle, section "Traversing Collections".

I would recommend renaming [m]passenger[/m] to [m]passengers[/m] to indicate that this is a collection and not a single passenger.
 
Thank you for the advice and the link! :-)
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top