Skip to main content

Java program for student mark list using inheritance - java code examples with output

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

Let's try to create a simple example :

  ➤ Example : student.java;

  1. import java.io.*;

  2. class MList {

  3. static public StudentWrapper theStudents = new StudentWrapper();

  4. public void ViewRecords() {

  5.   System.out.println("_______________________________________________________________");

  6.   System.out.println("SNo Student Name Sub1 Sub2 Sub3 Sub4 Sub5 Total");

  7.   System.out.println("_______________________________________________________________");

  8.  

  9.   for(int i = 0; i < theStudents.m_nMaxStudents; i++){

  10.   System.out.format("%1$-5d", i+1);

  11.  

  12.   System.out.format("%1$-19s", theStudents.m_studList[i].name);

  13.   System.out.format("%1$-7d", theStudents.m_studList[i].marks[0]);

  14.   System.out.format("%1$-7d", theStudents.m_studList[i].marks[1]);

  15.   System.out.format("%1$-7d", theStudents.m_studList[i].marks[2]);

  16.   System.out.format("%1$-7d", theStudents.m_studList[i].marks[3]);

  17.   System.out.format("%1$-7d", theStudents.m_studList[i].marks[4]);

  18.   System.out.format("%1$-7d", theStudents.m_studList[i].total);

  19.   System.out.println();

  20. }

  21.   System.out.println("_______________________________________________________________");

  22. }

  23. public void InputRecords() {

  24. try{

  25.   InputStreamReader input = new InputStreamReader(System.in);

  26.   BufferedReader reader = new BufferedReader(input);

  27.  

  28.   System.out.print("Student Name : ");

  29.   String name;

  30.  

  31.   int[] marks = new int[5];

  32.   name = reader.readLine();

  33.  

  34. for(int i = 1; i <= 5; i++){

  35.   System.out.print("Sub "+i+" Mark : ");

  36.   marks[i-1] = Integer.parseInt(reader.readLine());

  37. }

  38.   theStudents.AddRecord(name, marks);

  39. }

  40. catch(Exception e){

  41.   e.printStackTrace();

  42. }

  43. }

  44. }

  45. class Student extends MList {

  46. public static void main(String args[]) {

  47.   MList obj_marks = new MList();

  48.   String stdNumber = "";

  49.   InputStreamReader input = new InputStreamReader(System.in);

  50.   BufferedReader reader = new BufferedReader(input);

  51.  

  52. try{

  53.   System.out.print("Enter the number of students: ");

  54.   stdNumber = reader.readLine();

  55.   int numStudents = Integer.parseInt(stdNumber);

  56.  

  57. for(int i = 1; i <= numStudents; i++){

  58.   System.out.println("\nEnter "+i+" Student Information \n");

  59.   obj_marks.InputRecords();

  60. }

  61.   obj_marks.ViewRecords();

  62. }

  63. catch(Exception e){

  64.   e.printStackTrace();

  65. }

  66. }

  67. }

  68. class StudentsInfo {

  69.   public String name;

  70.   public int[] marks = new int[5];

  71.   public int total;

  72. }

  73. class StudentWrapper {

  74. public StudentsInfo[] m_studList = new StudentsInfo[100];

  75. public int m_nMaxStudents;

  76.  

  77. public int AddRecord(String name, int[] marks) {

  78.   StudentsInfo stud = new StudentsInfo();

  79.   stud.name = name;

  80.   stud.marks = marks;

  81.   stud.total = 0;

  82. for(int i = 0; i < 5; i++){

  83.   stud.total += stud.marks[i];

  84. }

  85.   m_studList[m_nMaxStudents++] = stud;

  86.   return 1;

  87. }

  88. }

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

  • ---------------------------------------------------------------------

Screenshot :
Java program for student mark list using inheritance
Recommended Posts:

Comments

Popular Posts

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return...

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false  ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ '*' ] #Host name # Problem Fix: Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver --insecure --insecure: it means you can run serve...

how to create enum in java

What is enum in Java? An enum is a special "data type" that used to create our own datatype like classes and It represents a group of constants (a variable that does not change). The enum is short for "enumerations", which means "specifically listed" and the Enum data also known as Enumerated Data Type. # Defining Java enum Instead of C/C++, in java programming language enum types are more powerful. Here, we can define an enum both inside and enum outside the class but not inside a Method "Rules" to defing enum in Java: To create an enum in Java, use the enum keyword (instead of class or interface) , and separate the constants with a comma (,) . The enum can be defined within or outside the class because it is similar to a class. The semicolon (;) at the end of the enum constants are optional Because enum are constants, the names of an enum type's fields are in uppercase letters. ...