Source Code..
//@author Java_Programs
abstract class Marks {
public abstract
float getPercentage();
}
class A extends Marks {
private float
marks1, marks2, marks3;
public A(float m1,
float m2, float m3) {
marks1 = m1;
marks2 = m2;
marks3 = m3;
}
public float
getPercentage() {
return
((marks1 + marks2 + marks3) / 300) * 100;
}
}
class B extends Marks {
private float
marks1, marks2, marks3, marks4;
public B(float m1,
float m2, float m3, float m4) {
marks1 = m1;
marks2 = m2;
marks3 = m3;
marks4 = m4;
}
public float
getPercentage() {
return
((marks1 + marks2 + marks3 + marks4) / 400) * 100;
}
}
public class Main {
public static void
main(String[] args) {
A studentA =
new A(80, 85, 90);
B studentB =
new B(75, 80, 85, 90);
System.out.println("Percentage of marks for student A: " +
studentA.getPercentage() + "%");
System.out.println("Percentage of marks for student B: " +
studentB.getPercentage() + "%");
}
}
we have created an abstract class Marks with an abstract
method getPercentage. We have also created two concrete classes A and B which
inherit the Marks class and implement the getPercentage method.
The A class has a constructor that takes the marks obtained
by student A in three subjects as its parameters. The getPercentage method of
the A class calculates the percentage of marks obtained by student A in these
three subjects and returns it.
Similarly, the B class has a constructor that takes the
marks obtained by student B in four subjects as its parameters. The
getPercentage method of the B class calculates the percentage of marks obtained
by student B in these four subjects and returns it.
Finally, in the Main class, we create objects studentA and
studentB of the A and B classes, respectively. We then call the getPercentage
method on both objects and print the percentage of marks obtained by each
student to the console.
When we run the program, we get the following output:
Percentage of marks for student A: 85.0%
Percentage of marks for student B: 82.5%
As we can see from the output, the getPercentage method of
the A class correctly calculates the percentage of marks obtained by student A
in three subjects, and the getPercentage method of the B class correctly
calculates the percentage of marks obtained by student B in four subjects.
No comments:
Post a Comment