Java Help with displaying an array list while using other classes

Click For Summary
SUMMARY

The discussion focuses on a Java class assignment involving the display of passenger information from an ArrayList within an Aircraft class. The user successfully retrieves aircraft information but struggles to format and display passenger details correctly. Key suggestions include using enhanced for-loops and streams for traversing the passenger list, as well as renaming the variable from "passenger" to "passengers" for clarity. The user is advised to implement these changes to achieve the desired output without brackets.

PREREQUISITES
  • Understanding of Java ArrayLists
  • Familiarity with Java class structures and constructors
  • Knowledge of Java methods and return types
  • Basic understanding of Java loops and iteration techniques
NEXT STEPS
  • Learn about Java ArrayList traversal techniques, including enhanced for-loops and streams
  • Explore Java method overloading and constructors for better class design
  • Research best practices for naming conventions in Java to improve code readability
  • Investigate Java's String formatting methods for cleaner output presentation
USEFUL FOR

Java developers, students working on object-oriented programming assignments, and anyone looking to improve their skills in managing collections and class interactions 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: 112
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! :-)
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
838
  • · Replies 9 ·
Replies
9
Views
3K