Uploaded by Mohamed Ibrahim

Quiz 1 (group 2)

advertisement
CANADIAN INTERNATIONAL COLLEGE(CIC)
‫المعهد الكندي العالي لإلدارة‬
Course Name: Programming 3
Date: 14/03/2023
Coordinator: Dr.\ Manar Mahmoud Elshazly
Course Code:
Name: ……………………………….………….
ID: ……...………...
Quiz 1
a. Create an Vehicle class, which is the abstract superclass of all vehicles.
1. Declare a protected integer attribute called wheels, which records the number of wheels
for this vehicle.
2. Define a protected constructor that initializes the wheels attribute.
3. Declare an abstract method called moveforward.
4. Declare a concrete method called movebackward that prints out something about how the
vehicle movebackward.
b. Create a Bicycle Class
1. The Bicycle class extends the Vehicle class
2. Define a default constructor that calls the superclass constructor to specify that all
Bicycle have two wheels only.
3. Implement the moveforward method.
c. Create a Drivable interface specified by the UML diagram below.
d. Create the Car class that extends Vehicle and implements Drivable.
1. This class must include a string attribute to store the model of the car.
2. Define a constructor that takes one string parameter that specifies the Car’s model. This
constructor must also call the superclass constructor to specify that all cars have four
wheels.
3. Define another constructor that takes no parameters. Have this constructor call the
previous constructor (using this keyword) and pass an empty string as the argument.
4. Implement the Drivable interface methods.
5. Implement the movebackward method.
CANADIAN INTERNATIONAL COLLEGE(CIC)
‫المعهد الكندي العالي لإلدارة‬
<<interface>>
Drivable
Vehicle
#wheels:int
# Vehicle(wheels:int)
+moveforward()
+movebackward()
+StartEngine():boolean
+StopEngine():boolean
+Accelerate(Speed:int)
Car
Truck
+Car(model:String)
+Car()
+StartEngine():boolean
+StopEngine():boolean
+Accelerate(Speed:int)
+moveforward()
+Truck()
+StartEngine():boolean
+StopEngine():boolean
+Accelerate(Speed:int)
+moveforward()
+movebackward()
Bicycle
# Bicycle()
+moveforward()
e. Create the Truck class. Override the Vehicle methods to specify that truck cannot move
backward and have 8 wheels.
f. Create a TestVehicle program. Have the main method created and manipulate the instance of
the classes you created above: Start with:
1. Creating instances of Car, Bicycle, Truck.
2. Experimenting on: Calling methods in each object
3. Using polymorphism
4. Using super to call superclass methods.
5. If you can add comments that would be very much appreciated.
Best Wishes,
Dr. Manar Elshazly
Download