The Question involves using the code shown in this image: http://i617.photobucket.com/albums/tt253/bboyDP/cmsc.jpg?t=1257646617
I am not sure or don’t know how to answer these questions… Please help~
# What is the purpose of the instance variable nrVehicles in the Lot class?
To keep track of the number of Vehicles currently in the Parking Lot?
# Identify the common coding mistake in Lot’s park method. How would you correct this coding error?
I’m not sure….
# In the Vehicle constructor, what is the purpose of { this( 4 ); } ?
Is it calling it’s parent’s class constructor, Vehicle, and thus instantiating that it is a vehicle with 4 wheels?
# In the MotorCycle constructor, what is the purpose of { super( nrWheels); }?
Is it referencing the parent class, Vehicle, and changing the number of wheels to the one given as a parameter?
# Car’s toString() methods invokes getWheels(), even though getWheels( ) is not defined in the Car class. How is this possible?
Through Polymorphism because the Car class obtains the Vehicle’s toString Method?
# Write the copy constructor for the Vehicle class.
I’m not sure how..
# Write the copy constructor for the Car class.
I’m not sure how..
# Write the clone( ) method for the Car class.
I’m not sure how..
# Write the copy constructor for the Lot class.
I’m not sure how..
Thank you for the help!
1. What is the purpose of the instance variable nrVehicles in the Lot class?
Your are correct.
2.Identify the common coding mistake in Lot’s park method. How would you correct this coding error?
It doesn’t check for the maximum capacity (20), it should validate first if nrVehicles is less than
20 before allowing the vehicle to be parked.
3. In the Vehicle constructor, what is the purpose of { this( 4 ); } ?
Your wrong.
It calls another constructor of the same Vehicle class to initialize the wheels.
4. # In the MotorCycle constructor, what is the purpose of { super( nrWheels); }?
Your wrong.
Calling it’s parent’s class constructor, Vehicle, and thus instantiating that
it is a MotorCycle with nrWheels number of wheels.
5. # Car’s toString() methods invokes getWheels(), even though getWheels( ) is not defined in the Car class. How is this possible?
Your wrong.
Through Inheritance the Car obtains the method getWheels() from the Vehicle parent class
6. Write the copy constructor for the Vehicle class.
public Vehicle(Vehicle copy){
//Just copy the properties of the passed Vehicle object.
setWheels(copy.getWheels());
}
7. Write the copy constructor for the Car class.
public Car(Car copy){
super(copy);
}
8. Write the clone( ) method for the Car class.
protected Object clone()
throws CloneNotSupportedException {
Car car = new Car();
car.setWheels(nrWheels);
return car;
}
9. Write the copy constructor for the Lot class.
public Lot(Lot lot){
//TODO: Provide these
//methods for the lot in order
//to do the copying.
setNParked(lot.getNParked());
setVehicles(lot.getVehicles());
}
Hope this helps my friend.