For this exercise, you will need to design two classes, Wagon and Passenger, wit
ID: 3641073 • Letter: F
Question
For this exercise, you will need to design two classes,Wagon and Passenger, with data fields and/or methods,
as described in the next slides.
Wagon:
? id
? A private integer instance variable that contains the unique
label for this wagon
? numberOfWagons
? A private integer class variable that keeps track of the
number of wagons that have been created so far
? passengers
? A private instance array that contains the Passengers
currently on this Wagon
? public Wagon()
? A no-arg constructor that creates a Wagon with unique id
and default capacity (number of seats) = 5.
Wagon:
? public Wagon(int capacity)
? A constructor that takes in the Wagon's capacity (number
of seats) as an argument and creates a Wagon with a
unique id
? public int getNumberOfAvailableSeats()
? A method that returns the number of available seats in the
current wagon
? addPassenger(Passenger p)
? A method that adds this passenger to the current wagon
? Any necessary public methods to retrieve a Wagon's ID
or passengers
Passenger:
? name
? A private instance variable that stores the Passenger's
name
? public Passenger()
? A no-arg constructor that creates a Passenger with the
default name
Explanation / Answer
Please rate...
Save the tree classes in 3 different files in the same directory...
The class Wagon in file "Wagon.java"
The class Passenger in file "Passenger.java"
The class WagonTest( which has the main function) in file "WagonTest.java"
The classes are separted by "====================".....
public class Passenger
{
private String name;
public Passenger()
{
name="Anonymous";
}
public Passenger(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
//=====================================================
public class Wagon
{
private int id;
private int numberOfWagons;
private Passenger passengers[];
public Wagon()
{
id=0;
numberOfWagons=0;
passengers=new Passenger[0];
}
public Wagon(int capacity)
{
int i;
id++;
numberOfWagons++;
passengers=new Passenger[capacity];
for(i=0;i<capacity;i++)
{
passengers[i]=new Passenger();
}
}
public int getNumberOfAvailableSeats()
{
int i,c=0;
for(i=0;i<passengers.length;i++)
{
if("Anonymous".equals(passengers[i].getName()))c++;
}
return (c);
}
public void addPassenger(Passenger p)
{
if(getNumberOfAvailableSeats()!=0)
{
passengers[passengers.length-this.getNumberOfAvailableSeats()]=p;
}
}
public int getid()
{
return id;
}
public int getnow()
{
return numberOfWagons;
}
public int getpl()
{
return passengers.length;
}
public String getp(int i)
{
return passengers[i].getName();
}
}
//=========================================================
import java.util.Scanner;
public class WagonTest
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
Scanner s1=new Scanner(System.in);
Scanner s2=new Scanner(System.in);
int a,t,h;
String na;
System.out.println(" Enter the number of wagons to be created: ");
a=s.nextInt();
Wagon[] wob=new Wagon[a];
int i,n,j;
n=a;
for(i=0;i<n;i++)
{
System.out.println("Enter the capacity of the wagon #"+(i+1)+": ");
a=s.nextInt();
wob[i]=new Wagon(a);
}
while(true)
{
System.out.println("Enter the number of passengers to be sitting together: ");
a=s.nextInt();
h=0;
for(i=0;i<n;i++)
{
if(a<=wob[i].getNumberOfAvailableSeats())
{
h=1;
System.out.println("A wagon with "+a+" seats has been found");
for(j=0;j<a;j++)
{
System.out.println("Enter name of passenger #"+(j+1));
na=s1.nextLine();
Passenger pob=new Passenger(na);
wob[i].addPassenger(pob);
}
break;
}
}
if(h==0){
System.out.println("All the passengers cannot fit together");
}
System.out.println("Would you like to continue[y-yes,n-no]: ");
na=s2.nextLine();
if(na.toLowerCase().equals("n"))
{
System.out.println("Details:");
for(i=0;i<n;i++)
{
System.out.println("In wagon #"+(i+1));
System.out.println("Names:");
t=wob[i].getpl();
for(j=0;j<t;j++)
{
System.out.println(wob[i].getp(j));
}
}
System.exit(1);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.