Question

In: Computer Science

Intro Topics Covered: Writing classes; References; Equality B.O.S.S. is a service designed to provide a safe...

Intro Topics Covered: Writing classes; References; Equality

B.O.S.S. is a service designed to provide a safe ride around campus. It runs seven days a week while school is in session and there is no cost to use this service.

For this assignment you will write a BossRoadTrip class and a driver class called BossRoadTripDriver. A BossRoadTrip object represents a trip that someone might take between two locations over couple of minutes. Here is the UML diagram for the BossRoadTrip class:

BossRoadTrip

- start: String

- destination: String

- minutes: int // at least 1

+ BossRoadTrip(start: String, destination: String) // minutes = 1, must call other constructor

+ BossRoadTrip(start: String, destination: String, mins: int)

+ getStart() : String

+ getDestination() : String

+ getMinutes() : int

+ lengthen(mins: int) : void // adds additional minutes to trip, mins supposed to be positive, if not do not add

+ shorten(mins: int):boolean //reduces minutes of trip, returns true if successful, false otherwise. //mins supposed to be negative, if not do not update the minutes instance variable. //if mins that will be taken from the trip will make minutes <1, you //should not update the trip length and you should return false

+ equals(other: Object) : boolean // true if all instance variables are equal

+ toString() : String // used to print trip data. Notice that it prints minute or “minutes”

The UML diagram for the BossRoadTripDriver class shown below. You must implement these classes the way that we specify. (Note: The comments in the UML class diagrams are not a standard part of UML. It just happens to be a convenient place to document some of how the methods should work.)

The BossRoadTripDriver combine() method needs a bit of explanation. It takes two BossRoadTrip objects as parameters and returns the combination of the two. This only makes sense if the destination of the first BossRoadTrip is the start of the second BossRoadTrip. When this is the case, the method returns a new BossRoadTrip whose start comes from “first” and destination comes from “second” and whose minutes are the sum of minutes from “first” and “second”.

When the two trips don’t match correctly, you signal this fact by returning a null reference. (There are better ways to handle this, but we havent covered the techniques yet.) You need to remember that the null value represents an undefined object. So, if try to call a method using null, your program will crash. This means that your main() method will have to check the return value of combine() to see whether it is null. Your driver program will ask the user to enter two BossRoadTrips. Then, it will ask if the user wants to adjust their trip duration(minutes). It will then compare for equality and try to combine them.

Boss Road Trip Driver

+ static main(String[] args) : void

+ static combine(first: BossRoadTrip, second:BossRoadTrip) : BossRoadTrip // returns a new BossRoadTrip that combines first and second // if trips can’t be combined, return value is null

+static createRoadTrip(Scanner stdIn, int tripNumber):BossRoadTrip //prompts the user for a series of values to define a new BossRoadTrip object; then //return the object created

+static modifyTripLength(stdIn: Scanner, tripNumber: int, trip: BossRoadTrip): void //prompts the user for an integer to modify the trip’s minutes variable; if the //user provides an integer >0, it calls the lengthen method on trip object, //and if <0 it calls the shorten method

Sample Run:

Welcome to the B.O.S.S RoadTrip Program

Enter start of B.O.S.S RoadTrip 1: Union

Enter destination of B.O.S.S RoadTrip 1: 2000 Frederick ave

Enter minutes for the B.O.S.S RoadTrip 1: 5

Enter start of B.O.S.S RoadTrip 2: 2000 Frederick ave

Enter destination of B.O.S.S RoadTrip 2: Brady Street

Enter minutes for the B.O.S.S RoadTrip 2: 10

B.O.S.S RoadTrip 1: (Union to 2000 Frederick ave, 5 minutes)

B.O.S.S RoadTrip 2: (2000 Frederick ave to Brady Street, 10 minutes)

Adjust length of B.O.S.S RoadTrip 1 (zero for no change): 0

Adjust length of B.O.S.S RoadTrip 2 (zero for no change): 0

B.O.S.S RoadTrip 1: (Union to 2000 Frederick ave, 5 minutes)

B.O.S.S RoadTrip 2: (2000 Frederick ave to Brady Street, 10 minutes)

The B.O.S.S RoadTrips are not equals.

The two B.O.S.S RoadTrips can be combined.

The new B.O.S.S RoadTrip is: (Union to Brady Street, 15 minutes)

Goodbye!

Welcome to the B.O.S.S RoadTrip Program

Enter start of B.O.S.S RoadTrip 1: Union

Enter destination of B.O.S.S RoadTrip 1: Walmart

Enter minutes for the B.O.S.S RoadTrip 1: 10

Enter start of B.O.S.S RoadTrip 2: Union

Enter destination of B.O.S.S RoadTrip 2: Walmart

Enter minutes for the B.O.S.S RoadTrip 2: 10

B.O.S.S RoadTrip 1: (Union to Walmart, 10 minutes)

B.O.S.S RoadTrip 2: (Union to Walmart, 10 minutes)

Adjust length of B.O.S.S RoadTrip 1 (zero for no change): 0

Adjust length of B.O.S.S RoadTrip 2 (zero for no change): 0

B.O.S.S RoadTrip 1: (Union to Walmart, 10 minutes)

B.O.S.S RoadTrip 2: (Union to Walmart, 10 minutes)

The B.O.S.S RoadTrips are equals.

The two B.O.S.S RoadTrips cannot be combined.

Goodbye!

Welcome to the B.O.S.S RoadTrip Program

Enter start of B.O.S.S RoadTrip 1: Union

Enter destination of B.O.S.S RoadTrip 1: Walgreens

Enter minutes for the B.O.S.S RoadTrip 1: 5

Enter start of B.O.S.S RoadTrip 2: PicknSave

Enter destination of B.O.S.S RoadTrip 2: Union

Enter minutes for the B.O.S.S RoadTrip 2: 10

B.O.S.S RoadTrip 1: (Union to Walgreens, 5 minutes)

B.O.S.S RoadTrip 2: (PicknSave to Union, 10 minutes)

Adjust length of B.O.S.S RoadTrip 1 (zero for no change): 2

Adjust length of B.O.S.S RoadTrip 2 (zero for no change): 6

B.O.S.S RoadTrip 1: (Union to Walgreens, 7 minutes)

B.O.S.S RoadTrip 2: (PicknSave to Union, 16 minutes)

The B.O.S.S RoadTrips are not equals.

The two B.O.S.S RoadTrips cannot be combined.

Goodbye!

Solutions

Expert Solution

/**
 * @fileName :BossRoadTrip.java
 * @author ravi
 * @since 27-02-2017
 **/
public class BossRoadTrip {
    private String start;
    private String destination;
    private int minutes;

    /**
     *
     * @param start
     * @param destination
     */
    public BossRoadTrip(String start, String destination) {
        this(start,destination,1);
    }

    /**
     *
     * @param start
     * @param destination
     * @param minutes
     */
    public BossRoadTrip(String start, String destination, int minutes) {
        this.start = start;
        this.destination = destination;
        this.minutes = minutes;
    }

    public String getStart() {
        return start;
    }

    public String getDestination() {
        return destination;
    }

    public int getMinutes() {
        return minutes;
    }

    public void lengthen(int mins){

        if(mins>0){
            this.minutes+=mins;
        }
    }

    public boolean shorten(int mins){
        if(mins<0&& (this.minutes+mins)>=1){
            this.minutes=+mins;
            return true;
        }
        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BossRoadTrip)) return false;

        BossRoadTrip that = (BossRoadTrip) o;

        if (minutes != that.minutes) return false;
        if (!start.equals(that.start)) return false;
        return destination.equals(that.destination);

    }

    @Override
    public int hashCode() {
        int result = start.hashCode();
        result = 31 * result + destination.hashCode();
        result = 31 * result + minutes;
        return result;
    }

    @Override
    public String toString() {
        return  "("+start + " to "+ destination +", "+ minutes +"minutes)";
    }
}
/**
 * @fileName :BossRoadTripDriver.java
 **/


import java.util.Scanner;

/**
 * @author
 * @since 27-02-2017
 **/
public class BossRoadTripDriver {

    /**
     * It takes two BossRoadTrip objects as parameters and returns the combination of the two.
     * This only makes sense if the destination of the first BossRoadTrip is the start of the second BossRoadTrip.
     * When this is the case, the method returns a new BossRoadTrip whose start comes from “first”
     * and destination comes from “second” and whose minutes are the sum of minutes from “first” and “second”
     *
     * @param first
     * @param second
     * @return
     */
    public static BossRoadTrip combine(BossRoadTrip first, BossRoadTrip second) {
        BossRoadTrip combinedTrip = null;
        if (first.getDestination().equalsIgnoreCase(second.getStart())) {
            combinedTrip = new BossRoadTrip(first.getStart(), second.getDestination(), first.getMinutes() + second.getMinutes());
        }
        return combinedTrip;
    }

    /**
     * Prompts the user for a series of values to define a new BossRoadTrip object
     *
     * @param stdIn
     * @param tripNumber
     * @return the new BossRoadTrip object
     */
    public static BossRoadTrip createRoadTrip(Scanner stdIn, int tripNumber) {
        System.out.print("Enter start of B.O.S.S RoadTrip " + tripNumber + ":");
        String start = stdIn.nextLine();
        System.out.print("Enter Destination of B.O.S.S RoadTrip " + tripNumber + ":");
        String destination = stdIn.nextLine();
        System.out.print("Enter Minutes of B.O.S.S RoadTrip " + tripNumber + ":");
        int minutes = stdIn.nextInt();
        stdIn.nextLine();
        System.out.println();
        return new BossRoadTrip(start, destination, minutes);
    }

    /**
     * prompts the user for an integer to modify the trip’s minutes variable
     * if the ser provides an integer >0, it calls the lengthen method on trip object,
     * and if <0 it calls the shorten method
     *
     * @param stdIn
     * @param tripNumber
     * @param trip
     */
    public static void modifyTripLength(Scanner stdIn, int tripNumber, BossRoadTrip trip) {
        System.out.print("Adjust length of B.O.S.S RoadTrip " + tripNumber + " (zero for no change):");
        int minutes = stdIn.nextInt();
        stdIn.nextLine();
        if (minutes > 0) {
            trip.lengthen(minutes);
        } else {
            trip.shorten(minutes);
        }
    }

    public static void main(String[] args) {
        System.out.println("Welcome to the B.O.S.S RoadTrip Program");
        Scanner sc = new Scanner(System.in);
        BossRoadTrip trip1 = createRoadTrip(sc, 1);
        BossRoadTrip trip2 = createRoadTrip(sc, 2);

        System.out.println("B.O.S.S RoadTrip 1:" + trip1);
        System.out.println("B.O.S.S RoadTrip 2:" + trip2);

        System.out.println();
        modifyTripLength(sc, 1, trip1);
        modifyTripLength(sc, 2, trip2);

        System.out.println();
        System.out.println("B.O.S.S RoadTrip 1:" + trip1);
        System.out.println("B.O.S.S RoadTrip 2:" + trip2);

        System.out.println();
        if (!trip1.equals(trip2)) {
            System.out.println("The B.O.S.S RoadTrips are not equals.");
            BossRoadTrip combinedTrip = combine(trip1, trip2);
            if (combinedTrip != null) {
                System.out.println("The two B.O.S.S RoadTrips can be combined.");
                System.out.println("The new B.O.S.S RoadTrip is:" + combinedTrip);
            } else {
                System.out.println("The two B.O.S.S RoadTrips can not be combined.");
            }

        } else {
            System.out.println("The B.O.S.S RoadTrips are equals.");
            System.out.println("The two B.O.S.S RoadTrips cannot be combined.");

        }
        System.out.println("Goodbye!");
    }
}

output:


Related Solutions

A rigid pavement is to be designed to provide a service life of 20 years and...
A rigid pavement is to be designed to provide a service life of 20 years and has an initial PSI of 4.4 and a TSI of 2.5. The modulus of subgrade reaction is determined to be 300 lb/in3. For design, the daily car, pickup truck, and light van traffic is 30,000; and the daily truck traffic consists of 300 passes of single-unit trucks with single and tandem axles, and 400 passes of tractor semi-trailer trucks with single, tandem, and triple...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT