Java Code - Student and Marks Class
// Base class: Student
class Student {
int stuId;
String stuName;
String stuTele;
// Constructor for Student class
public Student(int stuId, String stuName, String stuTele) {
this.stuId = stuId;
this.stuName = stuName;
this.stuTele = stuTele;
// Display student information
public void displayStudentInfo() {
System.out.println("Student ID: " + stuId);
System.out.println("Student Name: " + stuName);
System.out.println("Student Telephone: " + stuTele);
// Derived class: Marks
class Marks extends Student {
double subject1;
double subject2;
double subject3;
// Constructor for Marks class
public Marks(int stuId, String stuName, String stuTele, double subject1, double subject2, double
subject3) {
super(stuId, stuName, stuTele); // Calling the constructor of the base class
this.subject1 = subject1;
this.subject2 = subject2;
this.subject3 = subject3;
// Method to calculate the total marks
public double calculateTotal() {
return subject1 + subject2 + subject3;
// Method to calculate the average marks
public double calculateAverage() {
return calculateTotal() / 3;
// Method to calculate the grade based on average marks
public String calculateGrade() {
double average = calculateAverage();
if (average >= 80) {
return "A";
} else if (average >= 60) {
return "B";
} else if (average >= 40) {
return "C";
} else {
return "F";
// Display student marks and grade
public void displayMarksAndGrade() {
System.out.println("Total Marks: " + calculateTotal());
System.out.println("Average Marks: " + calculateAverage());
System.out.println("Grade: " + calculateGrade());
// Main class to test the Student and Marks classes
public class Main {
public static void main(String[] args) {
// Create an object of Marks class
Marks student = new Marks(101, "John Doe", "123456789", 85, 75, 90);
// Display student info, marks, and grade
student.displayStudentInfo();
student.displayMarksAndGrade();