Exercise #1
Design and implement a class called JUTCBus that will store data about an urban bus. Data for a bus includes:
- Bus number (a number in the range 1 to 99)
- Capacity (seating capacity of the bus)
- Driver (name of the bus driver)
- Speed (average speed, in miles per hour, at which the bus travels)
Your implementation should include:
a) A constructor which initializes all instance variables
b) Getter method for each instance attribute
c) A method called setDriver which accepts the name of a driver and assigns the driver to the bus.
d) A toString() method which should return a string with the format:
Bus Information:
Bus number : __
Capacity: ___ seats
Driver: _____
Speed: _____
JUTCBus Class:
public class JUTCBus {
private int bus_number;
private int capacity;
private String driver;
private String speed;
public JUTCBus(int b_num, int cap, String dri, String sp)
{
bus_number = b_num;
capacity = cap;
driver = dri;
speed = sp;
}
public int getBus_number()
{
return bus_number;
}
public int getCapacity()
{
return capacity;
}
public String getDriver()
{
return driver;
}
public String getSpeed()
{
return speed;
}
public void setDriver(String dri_name)
{
driver = dri_name;
}
public String toString()
{
String result;
result = "Bus Information: "+
"\n\t\tBus number : "+bus_number+
"\n\t\tCapacity: "+capacity+" seats"+
"\n\t\tDriver: "+driver+
"\n\t\tSpeed: "+speed;
return result;
}
}
Exercise #2
Design and implement a class called Route that will store data about a JUTC bus route. Data for a JUTC bus route includes:
- Location at which route starts (e.g. “HWT”)
- Location at which route terminates (e.g. “Papine”)
- Distance from start to end of route in miles
- Bus that is assigned to the route
- Total fares per trip ( the maximum amount of money that can be collected per trip)
Your implementation should include:
a) A constructor which initializes all instance variables. All values (except the total fares/trip) should be accepted as parameters. The constructor should set the total fares per trip to 80.00^capacity of the bus.
b) A toString()method which s should return a string with the format:
JUTC Bus Route:
Route #:____
From:___ to _____
Distance: ______
Bus Information:
Bus Number :______
Driver: __________
No. of Seats: _______
Speed:_______
Max. Fares/Trip:__________
Exercise #3
In the Route class, define the following methods that will calculate the amount of money that can be collected in fares per trip:
void estimateFares(double fare)
- Updates the total fares to be collected per trip as simply the capacity of the bus times fare.
void estimateFares(double fare1, double fare2, double pctChild)
- Updates the total fares per trip to: capacity * ((1- pctChild)*fare1 + pctChild*fare2))
Route class:
import java.text.NumberFormat;
public class Route {
private String route_start;
private String route_terminate;
private String distance;
private JUTCBus bus;
private double fare;
private String route_num;
private NumberFormat nf;
public Route(String r_num, String r_start,String r_terminate,String dis,JUTCBus b)
{
route_start = r_start;
route_terminate = r_terminate;
distance = dis;
bus = b;
fare = 80.00;
route_num = r_num;
}
public double getFare()
{
return fare;
}
public void estimateFares(double f)
{
fare = f * bus.getCapacity();
}
public void estimateFares(double fare1, double fare2, double pctChild)
{
fare = bus.getCapacity() * ((1- pctChild)*fare1 + pctChild*fare2);
}
public String toString()
{
String result;
nf = NumberFormat.getCurrencyInstance();
result = "\n\n\tJUTC Bus Route: "+
"\n\tRoute #: "+route_num+
"\n\tFrom: "+route_start+" to "+route_terminate+
"\n\tDistance: "+distance+
"\n\t"+bus.toString()+
"\n\tMax. Fares/Trip: "+nf.format(fare);
return result;
}
}
Exercise #4
Implement a driver class, called Dispatcher, that will do the following:
a. Declare and initialize a list of routes.
b. Add up to 5 routes of your choice
c. Use the first version of the estimateFares method to update the second route in the list.
d. Use the second version of the estimateFares method to update the fourth route in the list.
e. Use a while loop to display all routes.
f. Compute a grand total of fares/trip.
Dispatcher:
import java.util.*;
import java.text.NumberFormat;
public class Dispatcher {
public static void main(String[] args) {
double grand_total = 0;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
JUTCBus bus1 = new JUTCBus(7446,50, "John Brown", "105mph");
Route rou1 = new Route("18978", "Union","Papine","6 miles",bus1);
JUTCBus bus2 = new JUTCBus(7698,60, "Paul Smith", "105mph");
Route rou2 = new Route("68945", "Ring Road","Papine","5 miles",bus2);
JUTCBus bus3 = new JUTCBus(9875,80, "Karl Buck", "105mph");
Route rou3 = new Route("65423", "Ring Road","Half Way Tree","14 miles",bus3);
JUTCBus bus4 = new JUTCBus(9878,85, "Tom Clark", "115mph");
Route rou4 = new Route("69871", "Union","Half Way Tree","19 miles",bus4);
JUTCBus bus5 = new JUTCBus(3256,95, "Pat Smith", "115mph");
Route rou5 = new Route("97851", "Union","Down Town","25 miles",bus5);
ArrayList<Route> routes = new ArrayList<>();
routes.add(rou1);
routes.add(rou2);
routes.add(rou3);
routes.add(rou4);
routes.add(rou5);
routes.get(1).estimateFares(100);
routes.get(3).estimateFares(80, 100, 50);
int index = 0;
while(index < routes.size())
{
System.out.print(routes.get(index).toString());
index++;
}
for(Route r : routes)
{
grand_total+=r.getFare();
}
System.out.print("\n\nGrand total of fares: "+formatter.format(grand_total));
}
}