Page 213 - PowerPoint Presentation
P. 213
CAVITE STATE UNIVERSITY
T3 CAMPUS
Department of Information Technology COSC 65 – Programming Languages
Hierarchical Inheritance Example
class Animal {
void eat() {
System.out.println(“The animal is eating”);
}
}
class Dog extends Animal {
void bark() {
System.out.println(“The Dog is barking”);
}
}
class Cat extends Animal {
void meow() {
System.out.println(“The Cat is Meowing”);
}
}
public class TestInheritance4 {
public static void main(String[]args){
Cat c = new Cat();
c.meow();
c.eat();
// c.bark(); will give an error.
}
}
QUIZ NO. 8 | Hardcoding: Write a program on a piece of paper that applies ‘Multi-level
Inheritance’.
Intructions:
1. Write a program that has 3 classes besides the main class namely Student, Marks and
Results.
2. On Student class, add a method that allows the user to ENTER a Student ID and
Student Name. use method name insertStudentInfo()
3. On Student class also, add another method that displays the Information entered by
the user on method insertStudentInfo(). Use method name displayInfo()
4. On Marks Class, add a method that allows the user to enter a grade for Math, English
and Filipino subject. use method name insertMarks()
5. On Marks class also, add another method that displays all the grades entered by the
user on 3 subjects. Use method name displayMarks()
6. On the Result Class, create a method that computes and displays the average of the
3 grades entered by the user on the insertMarks() method. Use method name
showAverage()
7. On the main class, create 1 object and name it as Student1, then use all the methods
to perform inserting information to the Student1, displaying the information of the
Student1, inserting grades to the Student1, displaying the grades of the Student1, and
showing the average of the 3 grades of the Student1.
NOTE: The program has only 5 methods namely: insertStudentInfo(), displayInfo(),
insertMarks(), displayMarks() and showAverage().
Page | 72