Part A.Create a class called Trip with Private Attributes: tripID (int) location
ID: 3700798 • 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 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
Java code for the given requirement:
Trip class code:
public class Trip
{
//Private Attributes:
private int tripID;
private String location;
private double pricePerPerson;
//Public Methods:
//argument constructor
public Trip(int tripID, String location, double pricePerPerson)
{
super();
this.tripID = tripID;
this.location = location;
this.pricePerPerson = pricePerPerson;
}
public void display()
{
System.out.println("Trip id:"+tripID+", Location: "+location+", Price Per Person: "+pricePerPerson);
}
public double computeCost(int numOfPeopleComingin)
{
return (numOfPeopleComingin*pricePerPerson);
}
}
Cruise class code:
public class Cruise extends Trip
{
//Private Attributes:
private int portsOfCalls;
private double fees;
//Public Methods:
//Argument constructor
public Cruise(int tripID, String location, double pricePerPerson, int portsOfCalls, double fees)
{
//sends the appropriate variables to the super class constructor.
super(tripID, location, pricePerPerson);
//initializes the Cruise attributes
this.portsOfCalls = portsOfCalls;
this.fees = fees;
}
public void display()
{
super.display();
System.out.println("Number of ports the cruise will stop in:"+portsOfCalls);
System.out.println("Fees for the cruise:"+fees);
}
public double computeCost(int numberOfPeople)
{
double tripCost= super.computeCost(numberOfPeople);
double cruiseCost=numberOfPeople*fees;
return (tripCost+cruiseCost);
}
}
RunCuise class code:
import java.util.Scanner;
public class RunCuise
{
public static void main(String[] args)
{
//Declare and initialize variables you will need to initialize a Cruise object.
Cruise cruise=null;
//Create an object of the Cruise class and send the appropriate arguments to the constructor.
cruise = new Cruise(34901,"Poland",5500,6,1700);
//Display the Cruise information by calling the display method.
cruise.display();
//scanner class object to read inputs from user
Scanner sc= new Scanner(System.in);
//Prompt the user to enter how many people will be going on the trip,
System.out.println("How many people will be going on the trip?:");
int numOfPpl=sc.nextInt();
System.out.println("Cruise cost: "+cruise.computeCost(numOfPpl));
}
}
Output:
Trip id:34901, Location: Poland, Price Per Person: 5500.0
Number of ports the cruise will stop in:6
Fees for the cruise:1700.0
How many people will be going on the trip?:
3
Cruise cost: 21600.0