CompSci 251: Assignment 4 JAVA Due 2/27, 2017 10:00am Topics Covered: Writing cl
ID: 3798068 • Letter: C
Question
CompSci 251: Assignment 4 JAVA
Due 2/27, 2017 10:00am
Topics Covered: Writing classes; References; Equality
1 Introduction
B.O.S.S. is a non-emergency service designed to provide a safe ride around the UW-Milwaukee campus. B.O.S.S. runs seven days a week when school is in session and there is no cost at the point of use for currently enrolled UWM students.
2 What you will do
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 don 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
1
BossRoadTripDriver
+ 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
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.
3 Sample Runs
Goodbye!
2
Goodbye!
Goodbye!
Template:
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 don 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”
Explanation / Answer
import java.util.Scanner;
public class BossRoadTrip
{
private String start;
private String destination;
private int minutes;
public BossRoadTrip(){}
public BossRoadTrip(String s,String d)
{
start = s;
destination = d;
minutes = 1;
}
public BossRoadTrip(String s,String d, int m)
{
start = s;
destination = d;
minutes = m;
}
public String getStart()
{
return start;
}
public String getDestination()
{
return destination;
}
public int getMinutes()
{
return minutes;
}
// adds additional minutes to trip, mins supposed to be positive, if not do not add
public void lengthen(int min){
if(min > 0){
minutes+=min;
}
}
//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
public boolean shorten(int min){
if(min < 0 && minutes-1 > min){
minutes-=min;
return true;
}else{
return false;
}
}
// true if all instance variables are equal
public boolean equals(BossRoadTrip other){
if(other.start == start && other.destination == destination && other.minutes == minutes){
return true;
}
else{
return false;
}
}
// used to print trip data. Notice that it prints minute or minutes
public String toString(){
return start +" to "+ destination + ", "+ minutes + " minutes";
}
}
// Driver class to handle input output to the console
public class BossRoadTripsDriver
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to B.O.S.S Road trip program ");
int minutes;
BossRoadTrip rT1 = createRoadtrip(scanner,1);
BossRoadTrip rT2 = createRoadtrip(scanner,2);
String tripDetails = rT1.toString();
System.out.println("B.O.S.S RoadTrip 1: "+ tripDetails);
tripDetails = rT2.toString();
System.out.println("B.O.S.S RoadTrip 2: "+ tripDetails);
modifyTripLength(scanner,1,rT1);
modifyTripLength(scanner,2,rT2);
tripDetails = rT1.toString();
System.out.println("B.O.S.S RoadTrip 1: "+ tripDetails);
tripDetails = rT2.toString();
System.out.println("B.O.S.S RoadTrip 2: "+ tripDetails);
if(rT1.equals(rT2)){
System.out.println("The B.O.S.S RoadTrips are equal");
}
else{
System.out.println("The B.O.S.S RoadTrips are not equal");
}
BossRoadTrip bT = combine(rT1,rT2);
if(null != bT){
System.out.println("The two B.O.S.S RoadTrips can be combined.");
tripDetails = bT.toString();
System.out.println("B.O.S.S RoadTrip 2: "+ tripDetails);
}else{
System.out.println("The two B.O.S.S RoadTrips cannot be combined.");
}
System.out.println(" GoodBye!");
}
// returns a new BossRoadTrip that combines first and second
// if trips cant be combined, return value is null
public static BossRoadTrip combine(BossRoadTrip first, BossRoadTrip second){
String start1 = first.getStart();
String destination1 = first.getDestination();
int min1 = first.getMinutes();
String start2 = second.getStart();
String destination2 = second.getDestination();
int min2 = second.getMinutes();
if(destination1 == start2){
BossRoadTrip bT = new BossRoadTrip(start1,destination2,(min1+min2));
return bT;
}
else
{
return null;
}
}
//prompts the user for a series of values to define a new BossRoadTrip object; then
//return the object created
public static BossRoadTrip createRoadtrip(Scanner stdIn, int tripNumber){
String start, destination;
int minutes;
System.out.print("Enter start of B.O.S.S RoadTrip "+tripNumber + ": ");
start = stdIn.nextLine();
System.out.print("Enter destination of B.O.S.S RoadTrip "+tripNumber + ": ");
destination = stdIn.nextLine();
System.out.print("Enter minutes of B.O.S.S RoadTrip "+tripNumber +": ");
minutes = stdIn.nextInt();
BossRoadTrip rT = new BossRoadTrip(start,destination,minutes);
return rT;
}
//prompts the user for an integer to modify the trips 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
public static BossRoadTrip modifyTripLength(Scanner stdIn, int tripNumber, BossRoadTrip trip){
int minutes;
System.out.println("Adjust length of B.O.S.S RoadTrip "+ tripNumber +" (zero for no change):");
minutes = stdIn.nextInt();
if(minutes>0){
trip.lengthen(minutes);
}
else{
trip.shorten(minutes);
}
return trip;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.