Java Help with displaying an array list while using other classes

In summary, I was able to get the values from my Aircraft class but not my Passenger class. I am not sure how to make the output display this instead.
  • #1
smilesofmiles
19
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: 55
Technology news on Phys.org
  • #2
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.
 
  • #3
Thank you for the advice and the link! :-)
 

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

1. How do I display an array list in Java?

To display an array list in Java, you can use a for loop to iterate through the elements in the list and print them out individually. Alternatively, you can use the built-in toString() method to print out the entire list at once.

2. Can I display an array list while using other classes in Java?

Yes, you can display an array list while using other classes in Java. You can either pass the array list as a parameter to the other class or create an instance of the class and access the array list through that instance.

3. How do I access an array list from another class in Java?

To access an array list from another class in Java, you can use the get() method to retrieve a specific element, or you can use the size() method to determine the size of the array list and iterate through it using a for loop.

4. Is it possible to modify an array list while using other classes in Java?

Yes, it is possible to modify an array list while using other classes in Java. You can use methods such as add(), remove(), and set() to add, remove, or update elements in the array list from the other class.

5. How can I ensure that my array list is displayed correctly while using other classes in Java?

To ensure that your array list is displayed correctly while using other classes in Java, make sure to properly initialize and populate the array list before passing it to the other class. Also, use proper error handling techniques to avoid any unexpected errors or exceptions.

Similar threads

  • Programming and Computer Science
Replies
4
Views
848
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
690
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
794
Back
Top