c) costPerPassenger i) 11) a) addPassengers i) ii) 111) iv) v) vi) Takes in an int number of stops and returns a double representing the cost for the passenger to ride the specified number of stops. Use the descriptions for the rate and photoFee fields to determine the cost per passenger. 1) 2) vii) double rate the cost of one run double photoFees - the fees to purchase the mandatory photo package o (a) A passenger may ride the roller coaster multiple times and the fee must only be paid once. Takes in an int number of stops and a String[] representing the passengers in a group. The roller coaster should be able to travel the requested number of runs without exceeding maxNumRuns, or the passengers cannot be added. For each passenger, add them once to the passengers array, starting from the lowest available index. 1) Only add the passengers if there are enough seats for ALL of the input passengers on the roller coaster AND if the roller coaster can complete the number of stops specified! A seat is available if the corresponding index in the array does not already have a passenger assigned to it. What value is used to represent an empty seat? 2) 3) Do not assume that passengers already on the roller coaster will always be at the front of the array; empty seats may be spread throughout the array. Note: Duplicate passenger names are allowed. 4) If all passengers in the group fit on the roller coaster, charge each passenger for their ride. Assume that each passenger is riding for the same number of stops. (Charge the new passengers added, not the ones already riding) 1) Reuse code. What method have we already created to charge a passenger? 2) Increase runsSinceInspection. No matter how many passengers there are, runs Since Inspection should only be increased once per stop. Returns true if the entire group of passengers can fit on the roller coaster and the rollercoaster can travel the specified number of stops. Returns false if any passenger in the group cannot fit on the roller coaster or the rollercoaster cannot travel the given number of stops.
Given the following code and instructions:
I'm having trouble implementing these last 2 methods.
Method for finding empty seats in the code already if it helps the process.
Ride.Java contains:
public abstract boolean inspectRide(String[] components);
public abstract double costPerPassenger(int numberOfStops);
public abstract boolean addPassengers(int numberOfStops, String[] newPassengers);
Step by step
Solved in 3 steps
After implementing the suggested fix as described in the 'Follow-Up Questions', I'm still getting
-----Add Passengers-----
true
Roller Coaster Roller1 has run 10 times and has earned $363.75. It can only run 40 more times. It costs $32.43 per ride and there is a one-time photo fee of $40.32.
true
Roller Coaster Roller2 has run 15 times and has earned $0.00. It can only run 5 more times. It costs $12.43 per ride and there is a one-time photo fee of $4.00.
Instead of:
true
Roller Coaster Roller1 has run 10 times and has earned $607.41. It can only run 40 more times. It costs $32.43 per ride and there is a one-time photo fee of $40.32.
true
Roller Coaster Roller2 has run 15 times and has earned $132.30. It can only run 5 more times. It costs $12.43 per ride and there is a one-time photo fee of $4.00
I'm still having a bit of trouble implementing the charging inside the addPassenger.
Where "In addPassengers(...), if passenger from the input array gets added to the passengers array, they should be charged (use the method that charges a single passenger to charge each one)."
Using the Driver:
public static void main(String[] args) {
System.out.println("-----Roller Coaster Test-----");
RollerCoaster normalRoller = new RollerCoaster("Roller1", 5, new String[]{null, "A", "B", null, "C", "D",
null},
32.43, 40.32, 50);
RollerCoaster normalRoller2 = new RollerCoaster("Roller2", 10, new String[]{"A", "A", null, "B", null, "A",
"B"}, 12.43, 4.0, 20);
RollerCoaster brokenRoller = new RollerCoaster("Roller3", 50, new String[]{null, null, null}, 10.0, 10.0, 50);
RollerCoaster rollerDiffConst = new RollerCoaster("Roller4", 10, 100);
RollerCoaster rollerDiffConst2 = new RollerCoaster("Roller5");
RollerCoaster dupRoller = new RollerCoaster("Roller1", 5, new String[]{null, "A", "B", null, "C", "D",
null},
32.43, 40.32, 50);
System.out.println("\n-----Add Passengers-----");
String[] waiting1 = new String[]{"C", "D", "E"};
String[] waiting2 = new String[]{"A", "A", "B", "C"};
String[] waiting3 = new String[]{"A", "A"};
System.out.println(normalRoller.addPassengers(5, waiting1));
System.out.println(normalRoller);
System.out.println(normalRoller2.addPassengers(5, waiting2));
System.out.println(normalRoller2);
System.out.println(normalRoller2.addPassengers(5, waiting3));
System.out.println(normalRoller2);
System.out.println(brokenRoller.addPassengers(5, waiting3));
System.out.println(brokenRoller);
System.out.println(rollerDiffConst.addPassengers(-5, waiting1));
System.out.println(rollerDiffConst);
System.out.println(rollerDiffConst2.addPassengers(1000, waiting1));
System.out.println(rollerDiffConst2);
}
}
I should be getting the following results for the 2 true cases:
true Roller Coaster Roller1 has run 10 times and has earned $607.41. It can only run 40 more times. It costs $32.43 per ride and there is a one-time photo fee of $40.32.
true Roller Coaster Roller2 has run 15 times and has earned $132.30. It can only run 5 more times. It costs $12.43 per ride and there is a one-time photo fee of $4.00.
But I've been getting 363.75 for the first case and 0.00 for the 2nd. I've attached both completed programs.