You need to complete part I to be able to do part II. Part I Various publication
ID: 3865560 • Letter: Y
Question
You need to complete part I to be able to do part II.
Part I
Various publications can be described as follows:
- A PublicTransportation class with the following: ticket price (double type) and number of stops (int type).
- A CityBus is a PublicTransportation that in addition has the following: an route number (long type), an begin operation year (int type), a line name (String type), and driver(s)name (String type).
- A Tram is a CityBus that in addition has the following: maximum speed (int type), which indicates the maximum speed that this Citybus could reach.
- A Metro is a CityBus that in addition it has the following: number of vehicules (int type) and the name of the city (String type), such as Montreal, Toronto, etc.
- A Ferry is a PublicTransportation that in addition has the following: build year (int type) and Ship name (String type).
- An AirCraft is a PublicTransportation that in addition has the following: class type (enumeration type that can be: Helicopter, Airline, Glider or Balloon), and maintenance type (enumeration type that can be: Weekly, Monthly, or Yearly).
Part I:
Write the implementation of the above mentioned classes using inheritance and according to the specifications and requirements given below:
• You must have 4 different Java packages for the classes. The first package will include the PublicTransportation class. The second package will include the CityBus, Tram and Metro classes. The third package will include the Ferry class and the last package will include the AirCraft class.
- For each of the classes, you must have at least three constructors, a default constructor, a parametrized constructor (which will accept enough parameters to initialize ALL the attributes of the created object from this class) and a copy constructor. For instance the parametrized constructor of the Tram class must accept 7 parameters to initialize the price, the number of stops, the route number, the begin operation year, the line name, the driver(s)name, and maximum speed.
An object creation using the default constructor must trigger the default constructor of its ancestor classes, while creation using parametrized constructors must trigger the parametrized constructors of the ancestors.
For each of the classes, you must include at least the following methods: accessors, mutators, toString() and equals() methods (notice that you are always overriding the last two methods).
- The toString() method must display clear description and information of the object (i.e “This Tram has 23 stops, and costs 17$. It’s maximun speed is 70km/h.....”).
- The equals() method must first check if the passed object (to compare to) is null and if it is of a different type than the calling object. The method would clearly return false if any of these conditions is true; otherwise the comparison of the attributes are conducted to see if the two objects are actually equal. You need to add a comment indicating how effective these null verifications inside the method will be in relation to protecting your program from crashing!
For all classes, with the exception of the CityBus class, you are required to use the appropriate packages access rights. For the CityBus class for example, you are required to use protected access rights.
When accessing attributes from a base class, you must take full advantage of the permitted rights. For instance, if you can directly access an attribute by name from a base class, then you must do so instead of using a public method from that base class to access the attribute.
3. Write a driver program ( that contains the main() method) that would utilize all of your classes. The driver class can be in a separate package or in any of the already existing four packages. In the main() method you must:
Create various objects from the 6 classes, and display all their information using the toString() method.
Test the equality of some to the created objects using the equals() method.
Create an array of 10 PublicTransportation objects and fill that array with various objects from the 6 classes (each class must have at least one entry in that array).
Trace(search) that array to find the object that is least expensive (has lowest price) and the one that is most expensive. Display all information of these two objects along with their location (index) in the array.
Part II
For the requirements of this part, you need to modify your implementation from Part I as follows:
The classes from part I, must now have the most restrictive (secure/protective) access rights to their attributes. Adjust your implementation from Part I accordingly, and comment on the decision about using restricted access (i.e. what are the tradeoffs).
In the driver program of this part, you need to add to the one from part I, another static method (added it above the main()method), called copyCityBuss. The method will take as imput an array of PublicTransportation (an array of any size) and returns an array of PublicTransportation. That is to say, the method needs to create an array of the same length as the passed paramter one, copy all CityBuss from the passed array to a new array then return it. Your copy of the objects must use the copy constructors of the different listed classes.
In the driver program, create an array of 12 objects (must have at least one from each of the classes), then call the copyCityBuss() method to create a copy of the that array.
Display the contents of both arrays, then add some comments indicating whether or not the coping is correct. If not; you need to explain why it has not been sucessful or as you might expected.
Explanation / Answer
Given below is the code for the question. I have used dummy values in driver for part1 and part2. Please use some menaingful values according to your location. Please do rate the answer if it helped.
PublicTransport.java
package transport.base;
public class PublicTransportation {
protected double ticketPrice;
protected int numStops;
public PublicTransportation()
{
}
public PublicTransportation(double tktprice, int noStops)
{
ticketPrice = tktprice;
numStops = noStops;
}
public PublicTransportation(PublicTransportation other)
{
this.ticketPrice = other.ticketPrice;
this.numStops = other.numStops;
}
public double getTicketPrice() {
return ticketPrice;
}
public void setTicketPrice(double ticketPrice) {
this.ticketPrice = ticketPrice;
}
public int getNumStops() {
return numStops;
}
public void setNumStops(int numStops) {
this.numStops = numStops;
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if(obj instanceof PublicTransportation)
{
PublicTransportation pt = (PublicTransportation)obj;
return (ticketPrice == pt.ticketPrice && numStops == pt.numStops);
}
else
return false;
}
@Override
public String toString() {
return "This PublicTransport has " + numStops + " stops, and costs $" + String.format("%.2f", ticketPrice) +"."+" ";
}
}
CityBus.java
package transport.type1;
import transport.base.PublicTransportation;
public class CityBus extends PublicTransportation {
protected long routeNo;
protected int beginYear;
protected String lineName;
protected String driverName;
public CityBus(){
super();
}
public CityBus(double tktPrice, int stops, long route, int year, String line, String driver){
super(tktPrice, stops);
routeNo = route;
beginYear = year;
lineName = line;
driverName = driver;
}
public CityBus(CityBus other)
{
super(other);
routeNo = other.routeNo;
beginYear = other.beginYear;
lineName = other.lineName;
driverName = other.driverName;
}
public long getRouteNo() {
return routeNo;
}
public void setRouteNo(long routeNo) {
this.routeNo = routeNo;
}
public int getBeginYear() {
return beginYear;
}
public void setBeginYear(int beginYear) {
this.beginYear = beginYear;
}
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
@Override
public boolean equals(Object obj) {
if(super.equals(obj) && obj instanceof CityBus)
{
CityBus cb = (CityBus)obj;
return (routeNo == cb.routeNo && lineName.equals(cb.lineName) &&
beginYear == cb.beginYear && driverName.equals(cb.driverName));
}
else
return false;
}
@Override
public String toString() {
String str = super.toString().replace("PublicTransport", "CityBus") ;
str += "It was started in " + beginYear + " and its route no. is " + routeNo + " and ";
str += "line is " + lineName +". It's driven by " + driverName +"." +" ";
return str;
}
}
Tram.java
package transport.type1;
public class Tram extends CityBus {
protected int maxSpeed;
public Tram()
{
super();
}
public Tram(double tktPrice, int stops, long route, int year, String line, String driver, int maxSpd)
{
super(tktPrice, stops, route, year, line, driver);
this.maxSpeed = maxSpd;
}
public Tram(Tram other)
{
super(other);
this.maxSpeed = other.maxSpeed;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
@Override
public boolean equals(Object obj) {
if(super.equals(obj) && obj instanceof Tram)
return maxSpeed == ((Tram)obj).maxSpeed;
else
return false;
}
@Override
public String toString() {
String str = super.toString().replace("CityBus", "Tram");
str += "It's maximum speed is " + maxSpeed + " km/h" +" ";
return str;
}
}
Metro.java
package transport.type1;
public class Metro extends CityBus {
protected int noOfVehicles;
protected String city;
public Metro()
{
super();
}
public Metro(double tktPrice, int stops, long route, int year, String line, String driver, int vehicles, String cty)
{
super(tktPrice, stops, route, year, line, driver);
this.noOfVehicles = vehicles;
this.city = cty;
}
public Metro(Metro other)
{
super(other);
this.noOfVehicles = other.noOfVehicles;
this.city = other.city;
}
public int getNoOfVehicles() {
return noOfVehicles;
}
public void setNoOfVehicles(int noOfVehicles) {
this.noOfVehicles = noOfVehicles;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public boolean equals(Object obj) {
if(super.equals(obj) && obj instanceof Metro)
{
Metro m = (Metro) obj;
return noOfVehicles == m.noOfVehicles && city.equals(m.city);
}
else
return false;
}
@Override
public String toString() {
String str = super.toString().replace("CityBus", "Metro") ;
str += "It has " + noOfVehicles + " vehicles and travels to " + city +" ";
return str;
}
}
Ferry.java
package transport.type2;
import transport.base.PublicTransportation;
public class Ferry extends PublicTransportation {
protected int buildYear;
protected String shipName;
public Ferry()
{
super();
}
public Ferry(double tktprice, int noStops, int year, String name)
{
super(tktprice, noStops);
this.buildYear = year;
this.shipName = name;
}
public Ferry(Ferry other)
{
super(other);
this.buildYear = other.buildYear;
this.shipName = other.shipName;
}
public int getBuildYear() {
return buildYear;
}
public void setBuildYear(int buildYear) {
this.buildYear = buildYear;
}
public String getShipName() {
return shipName;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
@Override
public boolean equals(Object obj) {
if(super.equals(obj) && obj instanceof Ferry)
{
Ferry f = (Ferry)obj;
return buildYear == f.buildYear && shipName.equals(f.shipName);
}
else
return false;
}
@Override
public String toString() {
String str = super.toString().replace("PublicTransport", "Ferry");
str += "It is named " + shipName + " and was built in " + buildYear +" ";
return str;
}
}
AirCraft.java
package transport.type2;
import transport.base.PublicTransportation;
public class AirCraft extends PublicTransportation {
public enum ClassType {Helicopter, Airline, Glider, Balloon};
public enum MaintenanceType {Weekly, Monthly, Yearly};
protected ClassType classType;
protected MaintenanceType maintenanceType;
public AirCraft()
{
super();
}
public AirCraft(double tktprice, int noStops, ClassType ctype, MaintenanceType mType)
{
super(tktprice, noStops);
this.classType = ctype;
this.maintenanceType = mType;
}
public AirCraft(AirCraft other)
{
super(other);
this.classType = other.classType;
this.maintenanceType = other.maintenanceType;
}
public ClassType getClassType() {
return classType;
}
public void setClassType(ClassType classType) {
this.classType = classType;
}
public MaintenanceType getMaintenanceType() {
return maintenanceType;
}
public void setMaintenanceType(MaintenanceType maintenanceType) {
this.maintenanceType = maintenanceType;
}
@Override
public boolean equals(Object obj) {
if(super.equals(obj) && obj instanceof AirCraft)
{
AirCraft ac = (AirCraft)obj;
return classType == ac.classType && maintenanceType == ac.maintenanceType;
}
else
return false;
}
@Override
public String toString() {
String ac = "";
switch(classType)
{
case Helicopter:
ac = "Helicopter";
break;
case Airline:
ac = "Airline";
break;
case Glider:
ac = "Glider";
break;
case Balloon:
ac = "Balloon";
break;
}
String str = super.toString().replace("PublicTransport", ac) ;
str += "It needs ";
switch(maintenanceType)
{
case Weekly:
str += "Weekly ";
break;
case Monthly:
str += "Monthly ";
break;
case Yearly:
str += "Yearly ";
break;
}
str += " maintenance." +" ";
return str;
}
}
TransportDriverPart1.java
package transport;
import transport.base.PublicTransportation;
import transport.type1.CityBus;
import transport.type1.Metro;
import transport.type1.Tram;
import transport.type2.AirCraft;
import transport.type2.Ferry;
import transport.type2.AirCraft.ClassType;
import transport.type2.AirCraft.MaintenanceType;
public class TransportDriverPart1 {
private static void printLowest(PublicTransportation trans[])
{
int minIdx = 0;
for(int i = 1; i < trans.length; i++)
if(trans[i].getTicketPrice() < trans[minIdx].getTicketPrice())
minIdx = i;
System.out.println(" The lowest ticket price is at index " + minIdx + " for " + trans[minIdx]);
}
private static void printHighest(PublicTransportation trans[])
{
int maxIdx = 0;
for(int i = 1; i < trans.length; i++)
if(trans[i].getTicketPrice() > trans[maxIdx].getTicketPrice())
maxIdx = i;
System.out.println(" The highest ticket price is at index " + maxIdx + " for " + trans[maxIdx]);
}
public static void main(String[] args) {
PublicTransportation publicTrans = new PublicTransportation(5, 8);
CityBus bus = new CityBus(5, 5, 111, 2000, "citybus line1", "john");
Tram tram = new Tram(7, 8, 222, 2001, "tram line1" , "bob", 70);
Metro metro = new Metro(10, 10, 333, 2002, "metros line1" , "peter", 4, "Toronto");
Ferry ferry = new Ferry(50, 5, 2004, "Ship1");
AirCraft aircraft = new AirCraft(100, 2, ClassType.Helicopter, MaintenanceType.Weekly);
System.out.println(publicTrans);
System.out.println(bus);
System.out.println(tram);
System.out.println(metro);
System.out.println(ferry);
System.out.println(aircraft);
CityBus bus2 = new CityBus(5, 5, 111, 2000, "citybus line1", "john");
if(bus.equals(bus2))
System.out.println("the 2 city bus are equal");
else
System.out.println("the 2 city bus are not equal");
PublicTransportation[] transArr = new PublicTransportation[10];
transArr[0] = bus;
transArr[1] = tram;
transArr[2] = metro;
transArr[3] = ferry;
transArr[4] = aircraft;
transArr[5] = new CityBus(6, 5, 666, 2000, "line2", "michael");
transArr[6] = new Tram(9, 8, 777, 2001, "tram line1" , "bob", 70);
transArr[7] = new CityBus(4, 5, 888, 2000, "line2", "michael");
transArr[8] = new CityBus(2, 5, 999, 2000, "line2", "michael");
transArr[9] = new CityBus(2, 5, 1010, 2000, "line2", "michael");
printLowest(transArr);
printHighest(transArr);
}
}
output
This PublicTransport has 8 stops, and costs $5.00.
This CityBus has 5 stops, and costs $5.00.
It was started in 2000 and its route no. is 111 and line is citybus line1. It's driven by john.
This Tram has 8 stops, and costs $7.00.
It was started in 2001 and its route no. is 222 and line is tram line1. It's driven by bob.
It's maximum speed is 70 km/h
This Metro has 10 stops, and costs $10.00.
It was started in 2002 and its route no. is 333 and line is metros line1. It's driven by peter.
It has 4 vehicles and travels to Toronto
This Ferry has 5 stops, and costs $50.00.
It is named Ship1 and was built in 2004
This Helicopter has 2 stops, and costs $100.00.
It needs Weekly maintenance.
the 2 city bus are equal
The lowest ticket price is at index 8 for
This CityBus has 5 stops, and costs $2.00.
It was started in 2000 and its route no. is 999 and line is line2. It's driven by michael.
The highest ticket price is at index 4 for
This Helicopter has 2 stops, and costs $100.00.
It needs Weekly maintenance.
TransportDriverPart2.java
package transport;
import transport.base.PublicTransportation;
import transport.type1.CityBus;
import transport.type1.Metro;
import transport.type1.Tram;
import transport.type2.AirCraft;
import transport.type2.Ferry;
import transport.type2.AirCraft.ClassType;
import transport.type2.AirCraft.MaintenanceType;
public class TransportDriverPart2 {
private static void printLowest(PublicTransportation[] trans)
{
int minIdx = 0;
for(int i = 1; i < trans.length; i++)
if(trans[i].getTicketPrice() < trans[minIdx].getTicketPrice())
minIdx = i;
System.out.println(" The lowest ticket price is at index " + minIdx + " for " + trans[minIdx]);
}
private static void printHighest(PublicTransportation[] trans)
{
int maxIdx = 0;
for(int i = 1; i < trans.length; i++)
if(trans[i].getTicketPrice() > trans[maxIdx].getTicketPrice())
maxIdx = i;
System.out.println(" The highest ticket price is at index " + maxIdx + " for " + trans[maxIdx]);
}
private static PublicTransportation[] copyAll(PublicTransportation[] trans)
{
PublicTransportation[] copy = new PublicTransportation[trans.length];
for(int i = 0; i < trans.length; i++)
{
if(trans[i] instanceof Tram)
copy[i] = new Tram((Tram)trans[i]);
else if(trans[i] instanceof Metro)
copy[i] = new Metro((Metro)trans[i]);
else if(trans[i] instanceof Ferry)
copy[i] = new Ferry((Ferry)trans[i]);
else if(trans[i] instanceof AirCraft)
copy[i] = new AirCraft((AirCraft)trans[i]);
else if(trans[i] instanceof CityBus)
copy[i] = new CityBus((CityBus)trans[i]);
else if(trans[i] instanceof PublicTransportation)
copy[i] = new PublicTransportation(trans[i]);
}
return copy;
}
private static void print(PublicTransportation[] trans)
{
for(int i = 0; i < trans.length; i++)
System.out.println(trans[i].toString());
}
public static void main(String[] args) {
PublicTransportation publicTrans = new PublicTransportation(5, 8);
CityBus bus = new CityBus(5, 5, 111, 2000, "citybus line1", "john");
Tram tram = new Tram(7, 8, 222, 2001, "tram line1" , "bob", 70);
Metro metro = new Metro(10, 10, 333, 2002, "metros line1" , "peter", 4, "Toronto");
Ferry ferry = new Ferry(50, 5, 2004, "Ship1");
AirCraft aircraft = new AirCraft(100, 2, ClassType.Helicopter, MaintenanceType.Weekly);
System.out.println(publicTrans);
System.out.println(bus);
System.out.println(tram);
System.out.println(metro);
System.out.println(ferry);
System.out.println(aircraft);
CityBus bus2 = new CityBus(5, 5, 111, 2000, "citybus line1", "john");
if(bus.equals(bus2))
System.out.println("the 2 city bus are equal");
else
System.out.println("the 2 city bus are not equal");
PublicTransportation[] transArr = new PublicTransportation[10];
transArr[0] = bus;
transArr[1] = tram;
transArr[2] = metro;
transArr[3] = ferry;
transArr[4] = aircraft;
transArr[5] = new CityBus(6, 5, 666, 2000, "line2", "michael");
transArr[6] = new Tram(9, 8, 777, 2001, "tram line1" , "bob", 70);
transArr[7] = new CityBus(4, 5, 888, 2000, "line2", "michael");
transArr[8] = new CityBus(2, 5, 999, 2000, "line2", "michael");
transArr[9] = new CityBus(2, 5, 1010, 2000, "line2", "michael");
System.out.println("The array of transports contains ");
print(transArr);
System.out.println("=====================================");
printLowest(transArr);
printHighest(transArr);
System.out.println("=====================================");
System.out.println("Copying the transport array using copy constructor...");
PublicTransportation[] copied = copyAll(transArr);
System.out.println("The copied array is");
print(copied);
}
}
outptu
This PublicTransport has 8 stops, and costs $5.00.
This CityBus has 5 stops, and costs $5.00.
It was started in 2000 and its route no. is 111 and line is citybus line1. It's driven by john.
This Tram has 8 stops, and costs $7.00.
It was started in 2001 and its route no. is 222 and line is tram line1. It's driven by bob.
It's maximum speed is 70 km/h
This Metro has 10 stops, and costs $10.00.
It was started in 2002 and its route no. is 333 and line is metros line1. It's driven by peter.
It has 4 vehicles and travels to Toronto
This Ferry has 5 stops, and costs $50.00.
It is named Ship1 and was built in 2004
This Helicopter has 2 stops, and costs $100.00.
It needs Weekly maintenance.
the 2 city bus are equal
The array of transports contains
This CityBus has 5 stops, and costs $5.00.
It was started in 2000 and its route no. is 111 and line is citybus line1. It's driven by john.
This Tram has 8 stops, and costs $7.00.
It was started in 2001 and its route no. is 222 and line is tram line1. It's driven by bob.
It's maximum speed is 70 km/h
This Metro has 10 stops, and costs $10.00.
It was started in 2002 and its route no. is 333 and line is metros line1. It's driven by peter.
It has 4 vehicles and travels to Toronto
This Ferry has 5 stops, and costs $50.00.
It is named Ship1 and was built in 2004
This Helicopter has 2 stops, and costs $100.00.
It needs Weekly maintenance.
This CityBus has 5 stops, and costs $6.00.
It was started in 2000 and its route no. is 666 and line is line2. It's driven by michael.
This Tram has 8 stops, and costs $9.00.
It was started in 2001 and its route no. is 777 and line is tram line1. It's driven by bob.
It's maximum speed is 70 km/h
This CityBus has 5 stops, and costs $4.00.
It was started in 2000 and its route no. is 888 and line is line2. It's driven by michael.
This CityBus has 5 stops, and costs $2.00.
It was started in 2000 and its route no. is 999 and line is line2. It's driven by michael.
This CityBus has 5 stops, and costs $2.00.
It was started in 2000 and its route no. is 1010 and line is line2. It's driven by michael.
=====================================
The lowest ticket price is at index 8 for
This CityBus has 5 stops, and costs $2.00.
It was started in 2000 and its route no. is 999 and line is line2. It's driven by michael.
The highest ticket price is at index 4 for
This Helicopter has 2 stops, and costs $100.00.
It needs Weekly maintenance.
=====================================
Copying the transport array using copy constructor...
The copied array is
This CityBus has 5 stops, and costs $5.00.
It was started in 2000 and its route no. is 111 and line is citybus line1. It's driven by john.
This Tram has 8 stops, and costs $7.00.
It was started in 2001 and its route no. is 222 and line is tram line1. It's driven by bob.
It's maximum speed is 70 km/h
This Metro has 10 stops, and costs $10.00.
It was started in 2002 and its route no. is 333 and line is metros line1. It's driven by peter.
It has 4 vehicles and travels to Toronto
This Ferry has 5 stops, and costs $50.00.
It is named Ship1 and was built in 2004
This Helicopter has 2 stops, and costs $100.00.
It needs Weekly maintenance.
This CityBus has 5 stops, and costs $6.00.
It was started in 2000 and its route no. is 666 and line is line2. It's driven by michael.
This Tram has 8 stops, and costs $9.00.
It was started in 2001 and its route no. is 777 and line is tram line1. It's driven by bob.
It's maximum speed is 70 km/h
This CityBus has 5 stops, and costs $4.00.
It was started in 2000 and its route no. is 888 and line is line2. It's driven by michael.
This CityBus has 5 stops, and costs $2.00.
It was started in 2000 and its route no. is 999 and line is line2. It's driven by michael.
This CityBus has 5 stops, and costs $2.00.
It was started in 2000 and its route no. is 1010 and line is line2. It's driven by michael.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.