Part A.Create a class called Trip with Private Attributes: tripID (int) location
ID: 3686733 • Letter: P
Question
Part A.Create a class called Trip with
Private Attributes: tripID (int)
location (String)
pricePerPerson (double)
Public Methods:
constructor: Receives values for the attributes as parameters and initializes them.//argument constructor
display: Displays the trip information, including tripID, location, and pricePerPerson.
computeCost: This method takes an integer parameter to represent the number of people coming on the trip, and returns the computed cost using the number of people and pricePerPerson attribute.
Part B. Complete the implementation for a class called Cruise, described below. The Cruise class inherits the class Trip and represents a more specific type of trip package that has a cruise. It has additional attributes to include a list of ports of call, as well as an attribute for cruise fees.
The Cruise class should have the following:
Private Attributes: portsOfCalls(int):number of ports the cruise will stop in
fees (double): fees for the cruise
Public Methods:
constructor: receives all Trip and Cruise attributes as parameters. This constructor initializes the Cruise attributes, and sends the appropriate variables to the super class constructor.
display: Displays the information in the Trip and Cruise classes. (We will need to call a method in the super class within this method.)
computeCost: This method takes an integer parameter for the number of people and computes the cost, which now includes the cruise fees. (We will need to call a method in the super class within this method).
Part C
Completing a main method Complete the main method in the below RunCuise class to demo the Cruise class. Declare and initialize variables you will need to initialize a Cruise object. Create an object of the Cruise class and send the appropriate arguments to the constructor. Display the Cruise information by calling the display method. Prompt the user to enter how many people will be going on the trip, and display the cruise cost.
Explanation / Answer
import java.util.*;
class Trip
{
private int tripId;
private String location;
private double pricePerPerson;
Trip(int tripId,String location,double price)
{
this.tripId=tripId;
this.location=location;
this.pricePerPerson=price;
}
public void display()
{
System.out.println(" Trip Info:");
System.out.println("Trip ID: "+tripId+ " Location: "+location+" Price Per Person: "+pricePerPerson);
}
public double computeCost(int n)
{
return n*pricePerPerson;
}
}
class Cruise extends Trip
{
private int portsOfCalls;
private double fees;
Cruise(int tripId,String location,double price,int portOfCalls,double fees)
{
super(tripId,location,price);
this.fees=fees;
this.portsOfCalls=portOfCalls;
}
public void display()
{
super.display();
System.out.println(" Cruise Info:");
System.out.print("Port Stops: "+ portsOfCalls+" Fees: "+fees);
}
public double computeCost(int n)
{
return n*fees;
}
}
public class HelloWorld
{
public static void main(String[] args)
{
Cruise c=new Cruise(21,"21 Street",21000,21,2100);
c.display();
Scanner read=new Scanner(System.in);
System.out.print(" Enter no of peploe going to trip: ");
System.out.println("Total trip Cost: "+ c.computeCost(read.nextInt()));
}
}