Question

PLEASE I NEED HELP WITH THIS JAVA CODE Homework 3-3 Create a Driver class to use...

PLEASE I NEED HELP WITH THIS JAVA CODE

Homework 3-3

Create a Driver class to use your Airplane and Passenger classes and create instances of them.

In the main method do the following:

  1. Create an Airplane that will store up to 100 Passengers

  2. Create 5 Passenger Objects with the details specified in the table below

  3. Add the 5 Passenger objects to the Airplane

  4. Call the printDetails method from the Airplane to print all the Airplane and Passenger details.

variable name

name

birthYear

weight

gender

p1

Albert

1879

198.5

‘m’

p2

Grace

1906

105

‘f’

p3

Tim

1955

216.3

‘m’

p4

Steve

1955

160

‘m’

p5

Sergey

1973

165.35

‘m’







      

This work must be completed in your textbook ZYBooks -- CMP-168: Programming Methods II

No other forms of submission will be accepted.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


/*The java program that tests the class Airplane by adding the passengers to the airplane object
* and then display the passenger objects to console output window.*/
//Tester.java

public class Tester {
   public static void main(String[] args) {
  
       //create an instance of Airplane with size,100
       Airplane airplane=new Airplane(100);
       //add passenger objects to the airplane
       airplane.addPassenger(new Passenger("Albert", 1879, 198.5, 'm'));
       airplane.addPassenger(new Passenger("Grace", 1906, 105, 'f'));
       airplane.addPassenger(new Passenger("Tim", 1955, 216.3, 'm'));
       airplane.addPassenger(new Passenger("Steve", 1955, 160, 'm'));
       airplane.addPassenger(new Passenger("Sergey", 1973, 165.35, 'm'));      
       //call display method to print the passengers to console output
       airplane.display();
   }
}
--------------------------------------------------------------------------------------------------------------


//Airplane.java
public class Airplane
{
   private Passenger passengers[];
   private int count;
   //constructor with size
   public Airplane(int size)
   {
       passengers=new Passenger[size];
       count=0;
   }
   //Add passenger to the passengers array
   public void addPassenger(Passenger p)
   {
       passengers[count]=p;
       count++;
   }
   //Method to display the passengers
   public void display()
   {
       System.out.printf("%-20s%-10s%-10s%-10s\n",
               "Name","Year","Weight","Gender");
       for (int i = 0; i < count; i++)
       {
           System.out.println(passengers[i].toString());
       }
   } //end of the method ,display
} //end of the class

--------------------------------------------------------------------------------------------------------------

//Passenger.java
public class Passenger
{
   private String name;
   private int year;
   private double weight;
   private char gender;
   /*constructor with the arguments*/
   public Passenger(String name, int year, double weight, char gender) {
       this.name = name;
       this.year = year;
       this.weight = weight;
       this.gender = gender;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }
   public double getWeight() {
       return weight;
   }
   public void setWeight(double weight) {
       this.weight = weight;
   }
   public char getGender() {
       return gender;
   }
   public void setGender(char gender) {
       this.gender = gender;
   }
  
   @Override
   public String toString() {
         
       return String.format("%-20s%-10d%-10.2f%-5c",
               name,year,weight,gender);
   }  
}

--------------------------------------------------------------------------------------------------------------

sample output:

Name Albert Grace Tim Steve Year 1879 1906 1955 1955 1973 Weight Gender 198.50 m 105.00f 216.30 m 160.00 m 165.35 m Sergey

Add a comment
Know the answer?
Add Answer to:
PLEASE I NEED HELP WITH THIS JAVA CODE Homework 3-3 Create a Driver class to use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT