Given Trolley.java and Ride.java, I'm having some issues producing the correct outputs. In the driver below, I should be getting: ------Inspect Trolley----- false Trolley Trolley1 has driven 12 loops and has earned $26.40. This trolley is at Station D. Next up is Station E. true Trolley Trolley1 has driven 0 loops and has earned $26.40. This trolley is at Station D. Next up is Station E. true Trolley Trolley2 has driven 0 loops and has earned $60.00. This trolley is at Station 3. Next up is Station 1. -----Move Trolley----- Trolley Trolley1 has driven 1 loops and has earned $26.40. This trolley is at Station D. Next up is Station E. Trolley Trolley1 has driven 50 loops and has earned $26.40. This trolley is at Station B. Next up is Station C. Trolley Trolley2 has driven 81 loops and has earned $60.00. This trolley is at Station 3. Next up is Station 1. However, I'm getting false false false, leading to the wrong loops driven in 'Inspect Trolley' and in 'Move Trolley' my loops driven is skewed to 13, 61, and 102. All the money increments seem to be correct however. public class Driver { public static void main(String[] args) { System.out.println("\n-----Inspect Ride-----"); String[] inspect1 = new String[] {"asdfasd", "asjdfl", "tracks clear"}; String[] inspect2 = new String[] {"asdfasd", "asdfjk", "brakes oK", "gas tAnk not empty"}; String[] inspect3 = new String[] {"tracks clear", "sadfjh", "braKES ok", "brAKEs OK", "gas tank not EmPTY"}; System.out.println("\n-----Trolley Test-----"); String[] line1 = new String[] {"Station A", "Station B", "Station C", "Station D", "Station E"}; String[] line2 = new String[] {"Station 1", "Station 2", "Station 3"}; Trolley normalTrolley = new Trolley("Trolley1", 10, line1, 0); Trolley normalTrolley2 = new Trolley("Trolley2", 20, line2, 2); Trolley trolleyDiffConst = new Trolley("Trolley3", line1, 3); Trolley dupTrolley = new Trolley("Trolley1", 10, line1, 0); System.out.println(normalTrolley); System.out.println(normalTrolley2); System.out.println(trolleyDiffConst); System.out.println(dupTrolley); String[] waiting1 = new String[] {"C", "D", "E"}; String[] waiting2 = new String[] {"A", "A", "B", "C"}; System.out.println("\n-----Add Passengers-----"); System.out.println(normalTrolley.addPassengers(3, waiting1)); System.out.println(normalTrolley); System.out.println(normalTrolley.getPassengerList()); System.out.println(normalTrolley.addPassengers(5, waiting2)); System.out.println(normalTrolley); System.out.println(normalTrolley.getPassengerList()); System.out.println(normalTrolley.addPassengers(-5, waiting2)); System.out.println(normalTrolley); System.out.println(normalTrolley.getPassengerList()); System.out.println("Add all 20 passengers. Additional will not be added:"); String[] waiting4 = new String[] {"A", "A", "B", "C", "A", "A", "B", "C", "A", "A", "B", "C", "A", "A", "B", "C", "A", "A", "B", "C", "A"}; System.out.println(normalTrolley2.addPassengers(3, waiting4)); System.out.println(normalTrolley2); System.out.println(normalTrolley2.getPassengerList()); System.out.println("\n-----Remove then Add Passengers-----"); System.out.println(normalTrolley.removePassenger("A")); System.out.println(normalTrolley.removePassenger("A")); System.out.println(normalTrolley.removePassenger("A")); System.out.println(normalTrolley.addPassengers(5, waiting1)); System.out.println(normalTrolley); System.out.println(normalTrolley.getPassengerList()); System.out.println("\n-----Inspect Trolley-----"); System.out.println(normalTrolley.inspectRide(inspect1)); System.out.println(normalTrolley); System.out.println(normalTrolley.inspectRide(inspect2)); System.out.println(normalTrolley); System.out.println(normalTrolley2.inspectRide(inspect2)); System.out.println(normalTrolley2); System.out.println("\n-----Move Trolley-----"); normalTrolley.moveTrolley(5); System.out.println(normalTrolley); normalTrolley.moveTrolley(243); System.out.println(normalTrolley); normalTrolley2.moveTrolley(243); System.out.println(normalTrolley2); System.out.println("\n-----Double check equals-----"); System.out.println(normalTrolley.equals(dupTrolley));; } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Given Trolley.java and Ride.java, I'm having some issues producing the correct outputs.

In the driver below, I should be getting:

------Inspect Trolley-----

false

Trolley Trolley1 has driven 12 loops and has earned $26.40. This trolley is at Station D. Next up is Station E.

true

Trolley Trolley1 has driven 0 loops and has earned $26.40. This trolley is at Station D. Next up is Station E.

true

Trolley Trolley2 has driven 0 loops and has earned $60.00. This trolley is at Station 3. Next up is Station 1. -----Move Trolley-----

Trolley Trolley1 has driven 1 loops and has earned $26.40. This trolley is at Station D. Next up is Station E. Trolley Trolley1 has driven 50 loops and has earned $26.40. This trolley is at Station B. Next up is Station C. Trolley Trolley2 has driven 81 loops and has earned $60.00. This trolley is at Station 3. Next up is Station 1.


However, I'm getting false false false, leading to the wrong loops driven in 'Inspect Trolley' and in 'Move Trolley'
my loops driven is skewed to 13, 61, and 102. All the money increments seem to be correct however.

public class Driver {

public static void main(String[] args) {
System.out.println("\n-----Inspect Ride-----");
String[] inspect1 = new String[] {"asdfasd", "asjdfl", "tracks clear"};
String[] inspect2 = new String[] {"asdfasd", "asdfjk", "brakes oK", "gas tAnk not empty"};
String[] inspect3 = new String[] {"tracks clear", "sadfjh", "braKES ok", "brAKEs OK", "gas tank not EmPTY"};


System.out.println("\n-----Trolley Test-----");
String[] line1 = new String[] {"Station A", "Station B", "Station C", "Station D", "Station E"};
String[] line2 = new String[] {"Station 1", "Station 2", "Station 3"};
Trolley normalTrolley = new Trolley("Trolley1", 10, line1, 0);
Trolley normalTrolley2 = new Trolley("Trolley2", 20, line2, 2);
Trolley trolleyDiffConst = new Trolley("Trolley3", line1, 3);
Trolley dupTrolley = new Trolley("Trolley1", 10, line1, 0);
System.out.println(normalTrolley);
System.out.println(normalTrolley2);
System.out.println(trolleyDiffConst);
System.out.println(dupTrolley);

String[] waiting1 = new String[] {"C", "D", "E"};
String[] waiting2 = new String[] {"A", "A", "B", "C"};

System.out.println("\n-----Add Passengers-----");
System.out.println(normalTrolley.addPassengers(3, waiting1));
System.out.println(normalTrolley);
System.out.println(normalTrolley.getPassengerList());
System.out.println(normalTrolley.addPassengers(5, waiting2));
System.out.println(normalTrolley);
System.out.println(normalTrolley.getPassengerList());
System.out.println(normalTrolley.addPassengers(-5, waiting2));
System.out.println(normalTrolley);
System.out.println(normalTrolley.getPassengerList());
System.out.println("Add all 20 passengers. Additional will not be added:");
String[] waiting4 = new String[] {"A", "A", "B", "C", "A", "A", "B", "C", "A", "A", "B", "C", "A", "A", "B",
"C", "A", "A", "B", "C", "A"};
System.out.println(normalTrolley2.addPassengers(3, waiting4));
System.out.println(normalTrolley2);
System.out.println(normalTrolley2.getPassengerList());

System.out.println("\n-----Remove then Add Passengers-----");
System.out.println(normalTrolley.removePassenger("A"));
System.out.println(normalTrolley.removePassenger("A"));
System.out.println(normalTrolley.removePassenger("A"));
System.out.println(normalTrolley.addPassengers(5, waiting1));
System.out.println(normalTrolley);
System.out.println(normalTrolley.getPassengerList());

System.out.println("\n-----Inspect Trolley-----");
System.out.println(normalTrolley.inspectRide(inspect1));
System.out.println(normalTrolley);
System.out.println(normalTrolley.inspectRide(inspect2));
System.out.println(normalTrolley);
System.out.println(normalTrolley2.inspectRide(inspect2));
System.out.println(normalTrolley2);

System.out.println("\n-----Move Trolley-----");
normalTrolley.moveTrolley(5);
System.out.println(normalTrolley);
normalTrolley.moveTrolley(243);
System.out.println(normalTrolley);
normalTrolley2.moveTrolley(243);
System.out.println(normalTrolley2);

System.out.println("\n-----Double check equals-----");
System.out.println(normalTrolley.equals(dupTrolley));;

}

}
public class Trolley extends Ride {
private String[] stations;
private int currentStation;
public Trolley (String id, int runsSince Inspection, String[] stations, int currentStation) {
this.currentStation = currentStation;
}
public Trolley (String id, String[] stations, int currentStation) {
this(id, 0, stations, current Station);
super(id, runsSince Inspection, new String [20]);
this.stations = new String[stations.length];
for (int i = 0; i < stations.length; i++) {
this.stations[i] = new String(stations[i]);
}
}
// Methods
@Override
public boolean canRun(int numberOfRuns) {
return numberOfRuns >= 0;
}
@Override
public boolean inspectRide (String[] components) {
boolean gasTankNotEmpty = false;
boolean brakes0k = false;
}
for (String component : components) {
if (component.equalsIgnoreCase ("Tracks Clear")) {
}
gasTankNotEmpty = true;
} else if (component.equalsIgnoreCase("Brakes Ok")) {
brakes0k = true;
}
}
// Return true if both conditions are met, reset runsSince Inspection to
if (gasTankNotEmpty && brakes0k) {
runsSinceInspection = 0;
return true;
} else {
return false;
@Override
public double costPerPassenger (int numberOfStops) {
return (3.00* numberOfStops / stations.length) * 100.00 / 100.00;
}
@Override
public boolean addPassengers (int numberOfStops, String[] newPassengers) {
if (canRun(numberOfStops)) {
int stopsPerLoop=stations.length;
int loopsToComplete = numberOfStops / stopsPerLoop;
for (int i = 0; i < newPassengers.length; i++) {
if (canAddPassenger()) {
passengers [nextEmptySeat()] = newPassengers[i];
chargePassenger (numberOfStops);
}
move Trolley (numberOfStops);
return true;
} else {
}
return false;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (!super.equals(obj)) return false;
Trolley trolley = (Trolley) obj;
if (currentStation != trolley.currentStation) return false;
if (stations.length != trolley.stations.length) return false;
// Manually iterate over the arrays to check for equality
for (int i = 0; i < stations.length; i++) {
if (!stations[i].equals IgnoreCase (trolley.stations[i])) {
return false;
}
}
return true;
}
public void move Trolley(int numberOfStops) {
int stopsPerLoop=stations.length;
int loopsToComplete = numberOfStops / stopsPerLoop;
}
runsSince Inspection += loopsToComplete;
currentStation = (currentStation + numberOfStops) % stations.length;
@Override
public String toString() {
int nextStation Index = (currentStation + 1) % stations.length;
String formatted Earnings = String.format("%.2f", earnings);
return "Trolley " + id + " has driven " + runsSince Inspection + " loops and has earned $" + formatted Earnings +
". This trolley is at " + stations [currentStation] + ". Next up is " + stations [nextStationIndex] + ".";
}
private boolean canAddPassenger() {
for (String passenger : passengers) {
if (passenger == null) {
return true;
}
}
return false;
}
// Helper method to find the index of the next empty seat on the trolley
private int nextEmptySeat() {
for (int i = 0; i < passengers.length; i++) {
if (passengers[i] == null) {
return i;
}
}
return -1; // Should not happen if canAddPassenger is checked before calling this method
Transcribed Image Text:public class Trolley extends Ride { private String[] stations; private int currentStation; public Trolley (String id, int runsSince Inspection, String[] stations, int currentStation) { this.currentStation = currentStation; } public Trolley (String id, String[] stations, int currentStation) { this(id, 0, stations, current Station); super(id, runsSince Inspection, new String [20]); this.stations = new String[stations.length]; for (int i = 0; i < stations.length; i++) { this.stations[i] = new String(stations[i]); } } // Methods @Override public boolean canRun(int numberOfRuns) { return numberOfRuns >= 0; } @Override public boolean inspectRide (String[] components) { boolean gasTankNotEmpty = false; boolean brakes0k = false; } for (String component : components) { if (component.equalsIgnoreCase ("Tracks Clear")) { } gasTankNotEmpty = true; } else if (component.equalsIgnoreCase("Brakes Ok")) { brakes0k = true; } } // Return true if both conditions are met, reset runsSince Inspection to if (gasTankNotEmpty && brakes0k) { runsSinceInspection = 0; return true; } else { return false; @Override public double costPerPassenger (int numberOfStops) { return (3.00* numberOfStops / stations.length) * 100.00 / 100.00; } @Override public boolean addPassengers (int numberOfStops, String[] newPassengers) { if (canRun(numberOfStops)) { int stopsPerLoop=stations.length; int loopsToComplete = numberOfStops / stopsPerLoop; for (int i = 0; i < newPassengers.length; i++) { if (canAddPassenger()) { passengers [nextEmptySeat()] = newPassengers[i]; chargePassenger (numberOfStops); } move Trolley (numberOfStops); return true; } else { } return false; @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false; if (!super.equals(obj)) return false; Trolley trolley = (Trolley) obj; if (currentStation != trolley.currentStation) return false; if (stations.length != trolley.stations.length) return false; // Manually iterate over the arrays to check for equality for (int i = 0; i < stations.length; i++) { if (!stations[i].equals IgnoreCase (trolley.stations[i])) { return false; } } return true; } public void move Trolley(int numberOfStops) { int stopsPerLoop=stations.length; int loopsToComplete = numberOfStops / stopsPerLoop; } runsSince Inspection += loopsToComplete; currentStation = (currentStation + numberOfStops) % stations.length; @Override public String toString() { int nextStation Index = (currentStation + 1) % stations.length; String formatted Earnings = String.format("%.2f", earnings); return "Trolley " + id + " has driven " + runsSince Inspection + " loops and has earned $" + formatted Earnings + ". This trolley is at " + stations [currentStation] + ". Next up is " + stations [nextStationIndex] + "."; } private boolean canAddPassenger() { for (String passenger : passengers) { if (passenger == null) { return true; } } return false; } // Helper method to find the index of the next empty seat on the trolley private int nextEmptySeat() { for (int i = 0; i < passengers.length; i++) { if (passengers[i] == null) { return i; } } return -1; // Should not happen if canAddPassenger is checked before calling this method
public abstract class Ride {
protected final String id;
protected double earnings;
}
protected int runsSince Inspection;
protected String[] passengers;
public Ride (String id, int runsSince Inspection, String[] passengers) {
this.id = id;
this.earnings = 0;
this.runsSince Inspection = runsSince Inspection;
this.passengers = new String[passengers.length];
for (int i = 0; i < passengers.length; i++) {
this.passengers [i] passengers[i];
}
}
public Ride (String id, String[] passengers) {
this(id, 0, passengers);
}
public abstract boolean canRun(int number of Runs);
public abstract boolean inspectRide (String[] components);
public abstract double costPerPassenger (int numberOfStops);
public abstract boolean addPassengers (int numberOfStops, String[] newPassengers);
public String getPassengerList() {
String passengerList = "Passenger List for + id + ":\n";
for (String passenger
passengers) {
}
}
public final void chargePassenger (int numberOfStops) {
earnings += costPerPassenger (numberOfStops);
}
=
if (passenger != null) {
}
}
return passengerList.trim();
public boolean removePassenger (String passengerName) {
for (int i = 0; i < passengers.length; i++) {
if (passengers[i] != null && passengers [i].equals IgnoreCase (passengerName)) {
passengers[i] = null;
return true;
}
passengerList += passenger + "\n";
}
}
return false;
@Override
public boolean equals(Object obj) {
if (this ==
obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Ride other= (Ride) obj;
return id.equals(other.id) && runsSince Inspection == other.runsSince Inspection;
@Override
public String toString() {
return id + " has run " + runsSinceInspection + times and has earned $" + earnings
+
Transcribed Image Text:public abstract class Ride { protected final String id; protected double earnings; } protected int runsSince Inspection; protected String[] passengers; public Ride (String id, int runsSince Inspection, String[] passengers) { this.id = id; this.earnings = 0; this.runsSince Inspection = runsSince Inspection; this.passengers = new String[passengers.length]; for (int i = 0; i < passengers.length; i++) { this.passengers [i] passengers[i]; } } public Ride (String id, String[] passengers) { this(id, 0, passengers); } public abstract boolean canRun(int number of Runs); public abstract boolean inspectRide (String[] components); public abstract double costPerPassenger (int numberOfStops); public abstract boolean addPassengers (int numberOfStops, String[] newPassengers); public String getPassengerList() { String passengerList = "Passenger List for + id + ":\n"; for (String passenger passengers) { } } public final void chargePassenger (int numberOfStops) { earnings += costPerPassenger (numberOfStops); } = if (passenger != null) { } } return passengerList.trim(); public boolean removePassenger (String passengerName) { for (int i = 0; i < passengers.length; i++) { if (passengers[i] != null && passengers [i].equals IgnoreCase (passengerName)) { passengers[i] = null; return true; } passengerList += passenger + "\n"; } } return false; @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } Ride other= (Ride) obj; return id.equals(other.id) && runsSince Inspection == other.runsSince Inspection; @Override public String toString() { return id + " has run " + runsSinceInspection + times and has earned $" + earnings +
Expert Solution
steps

Step by step

Solved in 6 steps with 5 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education