Answer to Question #299285 in Java | JSP | JSF for Angge

Question #299285

Create a class hierarchy that will represent trips, including member variables to store trip details. Create member functions that will:

  • compute the cost of the trip, and
  • display the details of the trip: origin, destination, distance and cost


Study the sample output carefully so you can correctly implement the trip hierarchy. 

Input


5
1·Zamboanga·Cagayan·494·5
1·Tagbilaran·Panglao·20·10
2·Cebu·Tagbilaran·90·800
3·Manila·Davao·1500·4500
3·Clark·Iloilo·751·2300

Output


Trip·Constructor
Bus·Constructor
Bus·Trip:·Zamboanga·to·Cagayan,·494·km,·P5·base·fare
Trip·Constructor
Bus·Constructor
Bus·Trip:·Tagbilaran·to·Panglao,·20·km,·P10·base·fare
Trip·Constructor
Boat·Constructor
Boat·Trip:·Cebu·to·Tagbilaran,·90·km,·P800·base·fare
Trip·Constructor
Plane·Constructor
Plane·Trip:·Manila·to·Davao,·1500·km,·P4500·base·fare
Trip·Constructor
Plane·Constructor
Plane·Trip:·Clark·to·Iloilo,·751·km,·P2300·base·fare
Total·Cost·of·trips:·10950




1
Expert's answer
2022-02-18T07:31:41-0500


import java.util.*;


class Trip {
	private String origin;
	private String destination;
	protected int distance;
	protected int cost;


	public Trip(String origin, String destination, int distance, int cost) {
		System.out.println("Trip Constructor");
		this.origin = origin;
		this.destination = destination;
		this.distance = distance;
		this.cost = cost;
	}


	public String toString() {
		return this.origin + " to " + this.destination + ", " + this.distance + " km, P" + this.getCost()
				+ " base fare";
	}


	/**
	 * @return the cost
	 */
	public int getCost() {
		return cost;
	}


	public int calculateCostTrip() {
		return cost;
	}
}


class Bus extends Trip {


	public Bus(String origin, String destination, int distance, int cost) {
		super(origin, destination, distance, cost);
		System.out.println("Bus Constructor");
	}


	public String toString() {
		return "Bus Trip: " + super.toString();
	}


	@Override
	public int calculateCostTrip() {
		return cost * distance;
	}
}


class Boat extends Trip {


	public Boat(String origin, String destination, int distance, int cost) {
		super(origin, destination, distance, cost);
		System.out.println("Boat Constructor");
	}


	public String toString() {
		return "Boat Trip: " + super.toString();
	}


	@Override
	public int calculateCostTrip() {
		return super.calculateCostTrip();
	}
}


class Plane extends Trip {


	public Plane(String origin, String destination, int distance, int cost) {
		super(origin, destination, distance, cost);
		System.out.println("Plane Constructor");
	}


	public String toString() {
		return "Plane Trip: " + super.toString();
	}


	@Override
	public int calculateCostTrip() {
		int costTrip = super.calculateCostTrip();
		return costTrip + (int) (costTrip * 0.1);
	}
}


class App {


	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] types = new int[n];
		String[] origins = new String[n];
		String[] destinations = new String[n];
		int[] distances = new int[n];
		int[] costs = new int[n];


		int totalCostTrips = 0;
		in.nextLine();
		for (int i = 0; i < n; i++) {
			String values[] = in.nextLine().split(" ");
			types[i] = Integer.parseInt(values[0]);
			origins[i] = values[1];
			destinations[i] = values[2];
			distances[i] = Integer.parseInt(values[3]);
			costs[i] = Integer.parseInt(values[4]);


		}
		for (int i = 0; i < n; i++) {
			Trip trip = null;
			if (types[i] == 1) {
				trip = new Bus(origins[i], destinations[i], distances[i], costs[i]);


			}
			if (types[i] == 2) {
				trip = new Boat(origins[i], destinations[i], distances[i], costs[i]);
			}
			if (types[i] == 3) {
				trip = new Plane(origins[i], destinations[i], distances[i], costs[i]);
			}
			System.out.println(trip.toString());
			totalCostTrips += trip.calculateCostTrip();
		}
		System.out.println("Total Cost of trips: " + totalCostTrips);
		in.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS