Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a BusRoute class that uses the Bus and Time class. This class will repres

ID: 3804437 • Letter: C

Question

Create a BusRoute class that uses the Bus and Time class. This class will represent a bus route between two bus stations, using a specific Bus, and departing at a specific Time. It should contain a constructor, 7 instance variables (bus, bus number, cost, departure, duration, source, destination), and 9 methods (see below). [25 points] overloaded constructor: Creates a BusRoute object that is setup up with a Bus, a bus number, a cost, a departure Time, a duration time, a source BusStation, and a destination BusStation. getBus(): Returns the Bus that operates this bn rusoute. getNumber(): Returns the bus number as a String. getCost(): Returns the bus route cost. getDestination(): Returns the destination BusStation. getDeparture(): Returns the departure Time. getArrival(): Returns a Time object with the arrival time (computed from the departure time and duration). getSource(): Returns a Bus Station object for the departure location. toOverviewString(): Returns a String representing an overview of the bus route. Use NumberFormat to display the price. See the sample output for an example. toDetailedString(): Returns a String representing the bus route's detailed information. See the sample output for an example. Included below is an overall UML diagram that describes the three classes you will be constructing. It provides a useful summary of all of the methods you are expected to implement, and their corresponding types and visibility. Notice that one private method is listed here (formatDigits in Time) that isn't mentioned above. This is a method that was in our solution, you may not need it in your answer. Conversely, you may implement additional private methods that you find useful.

Bus type BusType Bus (b Bus Type) getBus Type() Bus Type toString0: String association Bus Bus Route bus Bus number String cost double departure Time duration int Bus Time hour int minute int Time Time(h: int, m: int) getHour nt getMinute nt addHours (h int void addMinutes (m nt void add Time (other :Time) void get Copy() Time isEarlier Than other Time) boolean ISSamelime other Time) boolean isLater Than (other Time): boolean forma Digits nt): String toString String association

Explanation / Answer

Bus.java

public class Bus {  
   public enum BusType {
Luxury, Normal;
   }
  
   private BusType type;

   public Bus(BusType type) {
       this.type = type;
   }

   public BusType getBusType() {
       return type;
   }
  
   public String toString() {
       return "BusType: " + type;
   }
}


Time.java:

public class Time {
   private int hour, minute;

   public Time() {
   }

   public Time(int hour, int minute) {
       this.hour = hour;
       if(minute >= 60) {
           minute -= 60;
           hour += 1;
       }
       this.minute = minute;
   }

   public int getHour() {
       return hour;
   }

   public int getMinute() {
       return minute;
   }

   public void addHours(int h) {
       hour += h;
   }

   public void addMinutes(int m) {
       minute += m;
       if(minute >= 60) {
           hour += 1;
           minute -= 60;
       }
   }
  
   public void addTime(Time other) {
       minute += other.getMinute();
       hour += other.getHour();
   }
   public Time getCopy() {
       Time t = new Time(hour, minute);
       return t;
   }

   boolean isEarlierThan(Time other) {
       if(hour < other.getHour()) {
           return true;
       } else if(hour > other.getHour()) {
           return false;
       } else {
           if(minute < other.getMinute())
               return true;
           else
               return false;
       }
   }
  
   boolean isSameTime(Time other) {
       if(hour == other.getHour() && minute== other.getMinute()) {
           return true;
       } else {
           return false;
       }
   }

   boolean isLargerThan(Time other) {
       if(hour > other.getHour()) {
           return true;
       } else if(hour < other.getHour()) {
           return false;
       } else {
           if(minute > other.getMinute())
               return true;
           else
               return false;
       }
   }
  
   public String toString() {
       return String.format("%02d:%02d", hour, minute);
   }
}


BusRoute.java:

import java.text.DecimalFormat;

public class BusRoute {
   private Bus bus;
   private Time departure;
   private String number;
   private int duration;
   private double cost;
   private String source, destination;
  
   BusRoute(Bus bus, String number, double cost, String departure,
           int duration, String source, String destination) {
       this.departure = new Time(Integer.parseInt(departure.split(":")[0]),
               Integer.parseInt(departure.split(":")[1]));
       this.number = number;
       this.cost = cost;
       this.bus = bus;
       this.duration = duration;
       this.destination = destination;
       this.source = source;
   }

   public Bus getBus() {
       return bus;
   }

   public Time getDeparture() {
       return departure;
   }
  
   public Time getArrival() {
       Time arrival = departure.getCopy();
       arrival.addHours(duration);
       return arrival;
   }

   public String getNumber() {
       return number;
   }

   public double getCost() {
       return cost;
   }

   public String getSource() {
       return source;
   }

   public String getDestination() {
       return destination;
   }
  
   public String toOverviewString(){
       StringBuilder sb = new StringBuilder();
       DecimalFormat df = new DecimalFormat("####.00");
       sb.append("Bus: " + bus);
       sb.append("Destination: " + destination);
       sb.append("Source: " + source);
       sb.append("Cost: " + df.format(cost));
       return sb.toString();
   }
  
   public String toDetailedString(){
       StringBuilder sb = new StringBuilder();
       DecimalFormat df = new DecimalFormat("####.00");
       sb.append("Bus: " + bus);
       sb.append("Destination: " + destination);
       sb.append("Source: " + source);
       sb.append("Cost: " + df.format(cost));
       sb.append("Departure: " + departure);
       sb.append("Arrival: " + getArrival());
       return sb.toString();
   }
}