I\'ve finished the other classes but I\'m having trouble with this last one. Q5:
ID: 3803204 • Letter: I
Question
I've finished the other classes but I'm having trouble with this last one.
Q5: 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).
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.
Driver:
public static void testBusRoute() {
System.out.println("==testBusRoute()=="); // SEE BusRouteManager.java!!!
/*
//Test 1: create bus routes using different settings
BusRoute r1 = new BusRoute(new Bus(BusType.Greyhound),
"594", 45,
new Time(10, 50), 230,
BusStation.PHX, BusStation.YUM);
BusRoute r2 = new BusRoute(new Bus(BusType.Megabus),
"205", 46,
new Time(11, 5),
360,
BusStation.LAX,
BusStation.LVS);
BusRoute r3 = new BusRoute(new Bus(BusType.Greyhound),
"135", 75,
new Time(8, 20),
440,
BusStation.PHX,
BusStation.SAN);
BusRoute r4 =new BusRoute(new Bus(BusType.BoltBus),
"228", 50,
new Time(7, 10),
192,
BusStation.SAN,
BusStation.LAX);
BusRoute r5 = new BusRoute(new Bus(BusType.BoltBus),
"201", 65,
new Time(17, 50),
746,
BusStation.SAN,
BusStation.SFO);
System.out.println(r1.toDetailedString());
System.out.println();
System.out.println(r1.toOverviewString());
System.out.println();
System.out.println();
System.out.println(r5.toDetailedString());
System.out.println();
System.out.println(r5.toOverviewString());
*/
}
Bus Class:
public class Bus {
private BusType busType;
public Bus(BusType busType) {
super();
this.busType = busType;
}
public BusType getBusType() {
return this.busType;
}
@Override
public String toString() {
return "Bus [busType=" + busType + "]";
}
public static void main(String[] args) {
Bus bus = new Bus(BusType.Greyhound);
System.out.println(bus.toString());
}
}
Time Class:
package bus;
public class Time {
//Instance variables
private int hour;
private int minute;
/**
* Default constructor
*/
public Time() {
this.hour = 0;
this.minute = 0;
}
/**
* Parameterized constructor
* @param hour
* @param minute
*/
public Time(int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
/**
* @return the hour
*/
public int getHour() {
return hour;
}
/**
* @return the minute
*/
public int getMinute() {
return minute;
}
/**
* Updates the object by moving it forward a number of hours.
* @param hour
*/
public void addHours(int hour) {
this.hour += hour;
}
/**
* Updates the object by moving it forward a number of minutes.
* @param minute
*/
public void addMinutes(int minute) {
this.minute += minute;
while (this.minute > 60) {
this.minute -= 60;
this.hour += 1;
}
}
/**
* Updates the object by moving it forward by the hour and minute from another Time object.
*/
public void addTime(Time another) {
addMinutes(another.getMinute());
addHours(another.getHour());
}
/**
* Returns a new Time object that has the same hour and minute of the existing Time object.
*/
public Time getCopy() {
Time time = new Time();
time.hour = this.hour;
time.minute = this.minute;
return time;
}
/**
* Returns true if this Time object is earlier than another Time object.
*/
public boolean isEarlierThan(Time another) {
if(this.hour < another.hour)
return true;
else if((this.hour == another.hour) && (this.minute < another.minute))
return true;
return false;
}
/**
* Returns true if this Time object is the same as another Time object.
*/
public boolean isSameTime(Time another) {
if((this == another) || ((this.hour == another.hour) && (this.minute == another.minute))) {
return true;
}
return false;
}
/**
* Returns true if this Time object is later than another Time object.
*/
public boolean isLaterThan(Time another) {
return (!isEarlierThan(another) && !isSameTime(another));
}
/**
* Returns a string representing the Time object.
* Uses 12 hour AM/PM format and pads minutes to be two digits. See the sample output for an example.
*/
public String toString() {
return ((this.hour > 12) ? (this.hour - 12) : this.hour) + ":" +
((this.minute < 10) ? ("0" + this.minute) : this.minute) +
((this.hour >= 12) ? " PM" : " AM");
}
}
Explanation / Answer
//Am pasting the complete code, since I have added enum in few places according to need. pease modify as required
package chegg.march;
import chegg.march.BusRoute.BusStation;
/**
*
* @author Sam
*/
public class BusDriver {
public static void testBusRoute() {
System.out.println("==testBusRoute()=="); // SEE BusRouteManager.java!!!
//Test 1: create bus routes using different settings
BusRoute r1 = new BusRoute(new Bus(Bus.BusType.Greyhound),
"594", 45,
new Time(10, 50), 230,
BusStation.PHX, BusStation.YUM);
BusRoute r2 = new BusRoute(new Bus(Bus.BusType.Megabus),
"205", 46,
new Time(11, 5),
360,
BusStation.LAX,
BusStation.LVS);
BusRoute r3 = new BusRoute(new Bus(Bus.BusType.Greyhound),
"135", 75,
new Time(8, 20),
440,
BusStation.PHX,
BusStation.SAN);
BusRoute r4 =new BusRoute(new Bus(Bus.BusType.BoltBus),
"228", 50,
new Time(7, 10),
192,
BusStation.SAN,
BusStation.LAX);
BusRoute r5 = new BusRoute(new Bus(Bus.BusType.BoltBus),
"201", 65,
new Time(17, 50),
746,
BusStation.SAN,
BusStation.SFO);
System.out.println(r1.toDetailedString());
System.out.println();
System.out.println(r1.toOverviewString());
System.out.println();
System.out.println();
System.out.println(r5.toDetailedString());
System.out.println();
System.out.println(r5.toOverviewString());
}
}
class BusRoute {
Bus bus;
String busNumber;
int cost;
Time dept;
int duration;
BusStation source;
BusStation destination;
public BusRoute(Bus bus, String busNumber, int cost, Time dept, int duration, BusStation source, BusStation destination) {
this.bus = bus;
this.busNumber = busNumber;
this.cost = cost;
this.dept = dept;
this.duration = duration;
this.source = source;
this.destination = destination;
}
enum BusStation {
SAN,
SFO,
LAX,
PHX,
LVS,
YUM
}
public Bus getBus() {
return bus;
}
public String getBusNumber() {
return busNumber;
}
public int getCost() {
return cost;
}
public Time getDept() {
return dept;
}
public int getDuration() {
return duration;
}
public BusStation getSource() {
return source;
}
public Time getArrival() {
Time arrival = dept.getCopy();
arrival.addMinutes(duration%60);
arrival.addHours(duration/60);
return arrival;
}
public String toOverviewString() {
/* Create string as given in example*/
return null;
}
public String toDetailedString() {
/*Create String as given in example*/
return null;
}
}
//Bus Class:
class Bus {
public enum BusType {
Greyhound,
BoltBus,
Megabus;
}
private BusType busType;
public Bus(BusType busType) {
super();
this.busType = busType;
}
public BusType getBusType() {
return this.busType;
}
@Override
public String toString() {
return "Bus [busType=" + busType + "]";
}
public static void main(String[] args) {
Bus bus = new Bus(BusType.Greyhound);
System.out.println(bus.toString());
}
}
//Time class
class Time
{
//Variables for Time
int timeMinute;
int timeHour;
//Default-constructor
public Time()
{
timeMinute=0;
timeHour=0;
}
//Overloaded constructor
public Time(int hh,int mm)
{
timeMinute=mm;
timeHour=hh;
}
//Return hour
public int getHour()
{
//return timeHour
return timeHour;
}
//return minute
public int getMinute()
{
//return timeMinute
return timeMinute;
}
//adds aHrs to timeHour
public void addHours(int aHrs)
{
timeHour=timeHour+aHrs;
}
//adds aMts to timeMinute
public void addMinutes(int aMts)
{
timeMinute=timeMinute+aMts;
if(timeMinute>59)
{
addHours(1);
timeMinute=timeMinute-60;
}
}
//Add this time to t2
public void addTime(Time t2)
{
addHours(t2.getHour());
addMinutes(t2.getMinute());
}
//Return copy of this time
public Time getCopy()
{
//Return new time object
return new Time(this.getMinute(),this.getHour());
}
//Return true if this time > t2
public boolean isEarlierThan(Time t2)
{
if(this.getHour()>t2.getHour())
return true;
return false;
}
//Return true if this time=t2
public boolean isSameTime(Time t2)
{
if((this.getHour()==t2.getHour())&&(this.getMinute()== t2.getMinute()))
return true;
return false;
}
//Return true if this time>t2
public boolean isLaterThan(Time t2)
{
if(this.getHour()<t2.getHour())
return true;
return false;
}
//overridden toString
public String toString()
{
String tt="";
if(timeMinute<10)
tt="0";
if(timeHour==0)
return "12 :"+tt+timeMinute+"AM";
else if(timeHour<12)
return timeHour+":"+tt+timeMinute+"AM";
else if(timeHour==12)
return timeHour+":"+tt+timeMinute+"PM";
return (timeHour-12)+":"+tt+timeMinute+"PM";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.