1) Given the following class diagram, write statements to answer the following q
ID: 3839476 • Letter: 1
Question
1)Given the following class diagram, write statements to answer the following questions. (6 points)
Vehicle:
-numOfWheels : int
-maxSpeed : double
+getNumOfWheels(): int
+getMaxSpeed(): double
+setNumOfWheels(int)
+setMaxSpeed(double)
+toString(): String
MotorVehicle:
-engineSize: double
+getEngineSize(): double
+setEngineSize(double)
+calculateMaxSpeed()
+toString(): String
7.1) create a MotorVehicle object named mv
MotorVehicle mv = new MotorVehicle();
7.2) for this mv object, assign 4 as number of wheels
7.3) what object oriented concept is demonstrated in the solution for the above question?
7.4) toString() is defined as regular method in both Vehicle and MotorVehicle, what is this phenomenon (of defining the method in both child and parent classes) called?
7.5)
Vehicle v = new MotorVehicle();
System.out.println(v.toString());
In the above statement, toString() defined in which class is called?
7.6) what object-oriented concept the following statement illustrates?
Vehicle v = new MotorVehicle();
Explanation / Answer
7.1) create a MotorVehicle object named mv
MotorVehicle mv = new MotorVehicle();
________________
7.2) for this mv object, assign 4 as number of wheels
mv. setNumOfWheels(4);
________________
7.3) what object oriented concept is demonstrated in the solution for the above question?
Encapsulation
Reason:
To access(for setting or getting the values) the member variables of a class we are using the member methods.
____________________
7.4) toString() is defined as regular method in both Vehicle and MotorVehicle, what is this phenomenon (of defining the method in both child and parent classes) called?
Ans)Polymorphism(Method overriding).
Method overriding:writing the methods with same name both in the child class and parent class.Here sub class methods overrides the parent class method.
______________________
7.5)
Vehicle v = new MotorVehicle();
System.out.println(v.toString());
In the above statement, toString() defined in which class is called?
Here are we are creating an object to the MotorVehicle class.so the variable v is pointing to the object of the MotorVehicle class.so that class method(toString) will be executed.
_____________________
7.6) what object-oriented concept the following statement illustrates?
Vehicle v = new MotorVehicle();
Ans) Inheritance
In inheritance we can refer to the sub class object by using the super class reference.
____________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.