In this assignment you will practice creating classes and enumerations in the co
ID: 3802426 • Letter: I
Question
In this assignment you will practice creating classes and enumerations in the context of a larger system. Overall, we are building pieces of a bus route booking system that allows passengers to book travel buses that travel throughout the United States. The classes that we are constructing in this assignment will take care of representing the bus data in Java. The final booking system should be able to search for bus routes from a list of commercial bus types (e.g. BoltBus, Greyhound), and locate various routes between two bus stations.
Q1: Create a BusType enumeration. This is a datatype that represents bus types in our system. Include three values: Greyhound, BoltBus, and Megabus. This should be a single file containing the enum declaration. [2 points]
Q2: Create a smart BusStation enumeration. This is a datatype that represents bus stations in our system, and has the ability to change from bus station code and city name. Include five values: PHX, LAX, LVS, SAN, and SFO. Also add a static method called getBusStationCity. This should be a single file containing the enum declaration, with the method declaration nested inside. (Hint: The attached file, ExampleSmartEnum.java, contains an example of including a method into an enumeration.) [8 points]
getBusStationCity(...) will take a BusStation enumeration value as a parameter, and return a String containing the city where that bus station is located (e.g., "Phoenix", "Las Vegas", "San Diego", "San Francisco"). Use a switch statement. Have a default case that returns "Unknown City".
Q3: Create a Bus class. This class will represent a bus in our system. It should contain a constructor, an instance variable (a BusType enumeration) and two methods (getBusType, toString) [10 points]
The constructor will take a BusType to set up the BusType enumeration.
getBusType(): will return the BusType of the bus (e.g., BusType.Greyhound).
toString(): will return the name of the bus's bus type (e.g., "Greyhound"), followed by a star (" * ").
Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and minute), and 10 methods (see below). All methods but toString should be in terms of the 24 hour format. [30 points]
default constructor: Creates a Time object for 12:00AM.
overloaded constructor: Creates a Time object at a specific hour and minute.
getHour(): Returns an integer representing the hour of the Time object.
getMinute(): Returns an integer representing the minute of the Time object.
addHours(...): Updates the object by moving it forward a number of hours.
addMinute(...): Updates the object by moving it forward a number of minutes. (Hint: Be careful that you don't allow minutes to be more than 59.)
addTime(...): Updates the object by moving it forward by the hour and minute from another Time object.
getCopy(): Returns a new Time object that has the same hour and minute of the existing Time object.
isEarlierThan(...): Returns true if this Time object is earlier than another Time object.
isSameTime(...): Returns true if this Time object is the same as another Time object.
isLaterThan(...): Returns true if this Time object is later than another Time object.
toString(): 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.
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). [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.
This needs to be done in Java please
Explanation / Answer
ans 1:
public enum BusType {
Greyhound, BoltBus, Megabus
}
--------------------------------
ans 2:
enum BusStation {
PHX, LAX, LVS, SAN, SFO;
static String getBusStationCity(BusStation b) {
switch (b.name()) {
case "PHX":
return "Phoenix";
case "LAX":
return "Las Vegas";
case "LVS":
return "San Diego";
case "SAN":
return "San Francisco";
case "SFO":
return "San Francisco";
default:
return "Unknown City";
}
}
}
-----------------------------------------
ans 3:
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());
}
}
------------------------------------------------------------
ans 4:
package Mar20;
public class Time {
private int hour;
private int minute;
public Time() {
super();
this.hour = 0;
this.minute = 0;
}
public Time(int hour, int minute) {
super();
this.hour = hour;
this.minute = minute;
}
public void addHours(int hour) {
this.hour = this.hour + hour;
}
public void addMinute(int minute) {
this.minute = this.minute + minute;
if (this.minute > 59) {
this.minute = this.minute - 60;
this.hour = this.hour + 1;
if (this.minute > 59) {
addMinute(this.minute);
}
}
}
public boolean isEarlierThan(Time time) {
return (this.hour < time.hour)
|| (this.hour == time.hour && this.minute < time.minute);
}
public boolean isSameTime(Time time) {
return this.hour == time.hour && this.minute == time.minute;
}
public boolean isLaterThan(Time time) {
return (this.hour > time.hour)
|| (this.hour == time.hour && this.minute > time.minute);
}
public Time getCopy() {
return new Time(this.hour, this.minute);
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = minute;
}
@Override
public String toString() {
String s = "AM";
if (this.hour >= 12)
s = "PM";
int h = this.hour % 12;
return "Time : " + String.format("%02d", h) + " : "
+ String.format("%02d", this.minute) + " " + s;
}
public static void main(String[] args) {
Time time = new Time(2, 40);
time.addHours(3);
time.addMinute(55);
System.out.println(time.toString());
}
}
OUTPUT:
Time : 06 : 35 AM