Question
using java

Develop the classes and interface represented in the following UML diagram: <cinterface >> Passenger 1 String name has passen
Note that the Person class includes both a first and last name, but the Passenger interface requires a full name. The field f
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Passenger.java
public interface Passenger {
String getName();
String getFareType();
}

===============================

// Person.java

public class Person implements Passenger {
private String firstName;
private String lastName;
private int age;
  
   /**
   * @param firstName
   * @param lastName
   * @param age
   */
   public Person(String firstName, String lastName, int age) {
       this.firstName = firstName;
       this.lastName = lastName;
       this.age = age;
   }

  
   @Override
   public String getName() {
       return firstName+" "+lastName;
   }

   @Override
   public String getFareType() {
       String str="";
       if(age<18)
       {
           str="Youth";
       }
       else if(age>=18 && age<=65)
       {
           str="Regular";
       }
       else
       {
           str="Senior";
       }
       return str;
   }
  

}

==============================

// Train.java

import java.util.ArrayList;

public class Train {
private ArrayList<Passenger> passengers;
private String name;
   /**
   * @param passengers
   * @param name
   */
   public Train(String name) {
       this.passengers = new ArrayList<Passenger>();
       this.name = name;
   }
  
   public String getName()
   {
       return name;
   }
   public void displayPassengers()
   {
       System.out.println("Passengers :");
       for(Passenger p: passengers)
       {
           System.out.println(p.getFareType()+"\t"+p.getName());
       }
   }
   public void addPassenger(Person p)
   {
       passengers.add(p);
   }

}

================================

// TrainDriver.java

public class TrainDriver {

   public static void main(String[] args) {

       Train t = new Train("Hogwarts Express");
       t.addPassenger(new Person("Harry", "Potter", 14));
       t.addPassenger(new Person("Hermlone", "Granger", 16));
       t.addPassenger(new Person("Ron", "Weasley", 12));
       t.addPassenger(new Person("Remus", "Lupin", 67));

       System.out.println("Train Name :" + t.getName());
       t.displayPassengers();

   }

}

================================

output:

Train Name :Hogwarts Express
Passengers :
Youth   Harry Potter
Youth   Hermlone Granger
Youth   Ron Weasley
Senior   Remus Lupin

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
using java Develop the classes and interface represented in the following UML diagram: <cinterface >> Passenger...
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
  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Complete the implementations of various classes in a Flight Management, We consider a database that stores...

    Complete the implementations of various classes in a Flight Management, We consider a database that stores information about passengers and flights. Each passenger record (e.g., String name, double TicketAmount, int flightID) can be uniquely identified by a String ID (e.g., ”e1”), whereas each flight record (e.g., String airline and String flight for flight name and airline name, respectively ) can be uniquely identified by an integer id. You must implement all methods in the FlightManagementSystem by manipulating the two attributes...

  • using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name...

    using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...

  • ********************Java Assigment******************************* 1. The following interface and classes have to do with the storage of information...

    ********************Java Assigment******************************* 1. The following interface and classes have to do with the storage of information that may appear on a mailing label. Design and implement each of the described classes below. public interface AddrLabelInterface { String getAttnName(); String getTitle(); // Mr., Mrs., etc. String getName(); String getNameSuffix(); // e.g., Jr., III String getProfessionalSuffix(); // O.D. (doctor of optometry) String getStreet(); String getSuiteNum(); String getCity(); String getState(); String getZipCode(); } Step 1 Create an abstract AddrLabel class that implements the...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one as...

    java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one assignment per team of two via Omnivox and NOT MIO.Assignments sent via MIO will be deducted marks. Assignments must be done alone or in groups and collaboration between individuals or groups is strictly forbidden. There will be a in class demo on June 1 make sure you are prepared, a doodle will be created to pick your timeslot. If you submit late, there...

  • Write a Java class named Employee to meet the requirements described in the UML Class Diagram...

    Write a Java class named Employee to meet the requirements described in the UML Class Diagram below: Note: Code added in the toString cell are not usually included in a regular UML class diagram. This was added so you can understand what I expect from you. If your code compiles cleanly, is defined correctly and functions exactly as required, the expected output of your program will solve the following problem: “An IT company with four departments and a staff strength...

  • Develop a set of classes for a college to use in various student service and personnel applications. Classes you need to design include the following: • Person — A Person contains the following fields...

    Develop a set of classes for a college to use in various student service and personnel applications. Classes you need to design include the following: • Person — A Person contains the following fields, all of type String: firstName, lastName, address, zip, phone. The class also includes a method named setData() that sets each data field by prompting the user for each value and a display method that displays all of a Person’s information on a single line at the...

  • Some java questions: 18. Consider the following class definitions public class TestAB public static void main (String a...

    Some java questions: 18. Consider the following class definitions public class TestAB public static void main (String args) A bl new B() в ь2 -new B() ; b1.х, А.у, Ь2.х, в.у); System.out.printf ("%d, Sd, %d, d\n", class A public int x = 2; public static int y = 4; public A () ( X=y- class Bextends A public int x = 32; public static int y = 45; public B ( x ++y What is the result of attempting to...

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