Program to print student mark list using inheritance
In this section, You will learn how to calculate and print student marks lists of n number of student in java, with the help of single level inheritance.
![single level inheritance](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi2oqiaaw7E9pQ06t3D1sa4odkQwTHdF1pBxlA5b1IaR1fVYnWCmRr68iVfJvnHijpYi-QQ7qeAIm3ro42B7FxEhEwb5K2tXhuFhGuV0a_1DKEdPJTzGiZKgKg1-2aET10I3hyUcuASNUQ/s0-rw/Single-level+Inheritance.png)
Let's try to create a simple example :
➤ Example : student.java;
import java.io.*;
class MList {
static public StudentWrapper theStudents = new StudentWrapper();
public void ViewRecords() {
System.out.println("_______________________________________________________________");
System.out.println("SNo Student Name Sub1 Sub2 Sub3 Sub4 Sub5 Total");
System.out.println("_______________________________________________________________");
for(int i = 0; i < theStudents.m_nMaxStudents; i++){
System.out.format("%1$-5d", i+1);
System.out.format("%1$-19s", theStudents.m_studList[i].name);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[0]);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[1]);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[2]);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[3]);
System.out.format("%1$-7d", theStudents.m_studList[i].marks[4]);
System.out.format("%1$-7d", theStudents.m_studList[i].total);
System.out.println();
}
System.out.println("_______________________________________________________________");
}
public void InputRecords() {
try{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
System.out.print("Student Name : ");
String name;
int[] marks = new int[5];
name = reader.readLine();
for(int i = 1; i <= 5; i++){
System.out.print("Sub "+i+" Mark : ");
marks[i-1] = Integer.parseInt(reader.readLine());
}
theStudents.AddRecord(name, marks);
}
catch(Exception e){
e.printStackTrace();
}
}
}
class Student extends MList {
public static void main(String args[]) {
MList obj_marks = new MList();
String stdNumber = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
try{
System.out.print("Enter the number of students: ");
stdNumber = reader.readLine();
int numStudents = Integer.parseInt(stdNumber);
for(int i = 1; i <= numStudents; i++){
System.out.println("\nEnter "+i+" Student Information \n");
obj_marks.InputRecords();
}
obj_marks.ViewRecords();
}
catch(Exception e){
e.printStackTrace();
}
}
}
class StudentsInfo {
public String name;
public int[] marks = new int[5];
public int total;
}
class StudentWrapper {
public StudentsInfo[] m_studList = new StudentsInfo[100];
public int m_nMaxStudents;
public int AddRecord(String name, int[] marks) {
StudentsInfo stud = new StudentsInfo();
stud.name = name;
stud.marks = marks;
stud.total = 0;
for(int i = 0; i < 5; i++){
stud.total += stud.marks[i];
}
m_studList[m_nMaxStudents++] = stud;
return 1;
}
}
Output :
Enter the number of students:2
Enter 1 Student Information
Student Name: Ankaj
Sub 1 Mark : 8
Sub 1 Mark : 9
Sub 1 Mark : 6
Sub 1 Mark : 7
Sub 1 Mark : 9
Enter 1 Student Information
Student Name: Anmol
Sub 1 Mark : 9
Sub 1 Mark : 6
Sub 1 Mark : 7
Sub 1 Mark : 8
Sub 1 Mark : 9
---------------------------------------------------------------------
SNO Student Name Sub1 Sub2 Sub3 Sub4 Sub5 Total
---------------------------------------------------------------------
1 Ankaj 8 9 6 7 9 39
2 Anmol 9 6 7 8 9 39
---------------------------------------------------------------------
Comments