Students CH8 Polymorphism PDF
Students CH8 Polymorphism PDF
▪ Static/Dynamic binding
▪ Polymorphism presentation
static binding.
binding.
binding.
▪ Polymorphism presentation
Polymorphism:
objectDemo.getClass()
▪ When the compiler encounters a method call made through a variable, the
compiler determines if the method can be called by checking the variable’s
class type.
▪ If that class contains the proper method declaration (or inherits one),
the call is compiled.
▪ At execution time, the type of the object to which the variable refers
determines the actual method to use: dynamic binding.
▪ Polymorphism presentation
▪ Polymorphism presentation
Tasks
1 Write the headers of all the classes and interface of the diagram
2 Write the Interface code
Write StudentActivity class, it will not implement the interface method.
3
The studentLevel must be between 1 and 9
4 Write Employee classes, it will not implement the interface method.
Write the second level classes. The class socialActivity will not implement the
5
interface method. The constrains will be provided in the corresponding slides.
deduceGrade method will be implemented in class GradedActivity and overridden in
PassFailActivity. It has been written in a chapter 3, so students can rewrite the same.
NOTE: Write all the toString methods in a way that all the needed data will be
printed in the main.
6 Write the main class methods. Details will be given in the corresponding slide.
7 Finish coding the whole project for next class
Dr. MFH, 2023 16
Tasks
1 Write the headers of all the classes and interface of the diagram
…………………………………→
…………………………………
}
Dr. MFH, 2023 21
Tasks
4 Write Employee classes, it will not implement the interface method.
}
Dr. MFH, 2023 22
Tasks
Write the second level classes. The class socialActivity will not implement the
5
interface method. class GradedActivity. Override the toString method.
…………………………………→ 23
Dr. MFH, 2023
Tasks
5 Write the second level classes. The class socialActivity will not implement the interface method. class GradedActivity
…………………………………
@Override
public double getPaymentAmount(){
if (getScore()>=90){return getBasicPayment()+getBasicPayment()*0.2;}
else if(getScore()>=80){return getBasicPayment()+getBasicPayment()*0.15;}
else if(getScore()>=70){return getBasicPayment()+getBasicPayment()*0.1;}
else if(getScore()>=60){return getBasicPayment()+getBasicPayment()*0.05;}
else { return getBasicPayment()*0.7; }
}
…………………………………→
…………………………………
public char deduceGrade()
{ // Same code as in Chapter 3 }
@Override
public String toString(){
…………………………………→
Dr. MFH, 2023 27
Tasks
Write the second level classes. The class socialActivity will not implement the
5 interface method. class SalariedEmployee
getPaymentAmount() in SalariedEmployee will return the weeklySalary
…………………………………
@Override
public String toString() {
}
} Dr. MFH, 2023 28
Method/Class
Finish coding the whole project for next class
8
NOTES to consider in your work:
NOTE: getPaymentAmount()in Training:
If nbhours > minhours, the Paymentamount will be the product of number of hours
with the hourpayment.
And if not online, we add 20% to the payment amount
NOTE: DeduceGrade in Assignement will return T if submission is true, F otherwise.
NOTE: In Exam constructor: the score value will be calculated and then it will be
used to set the score field.
• Declare a local variable for the score calculation.
• Calculate points for each question and numeric score for this exam. We suppose that
the total is over 100.
• pointsByQuestion = 100.0 / nbquestions;
• Score calculation formula = 100.0 - (nbMissedQuestions * pointsByQuestion);
• Set the score with the calculation result.
6 Write the main class methods. Details will be given in the corresponding slide.
1. The main class will contain the following methods: main, menu, FillList,
DisplayList.
2. The method menu(), will display a menu for the user.
3. The method FillList will take the user choices and create and add the objects in
the list.
4. The method DisplayList:
menu();
FillList(MyList);
DisplayList(MyList);
While looping inside the arraylist, we will verify if the current element is an
instance of class Exam.
If yes, we will create a variable and process a downcasting (Slide 36) on it.
Usage
Downcasting is used when we want to
perform some specific operation in a
generalised method depending upon the
subtype of object.
Text Book:
Chapter 10