Skip to main content

Java program for student mark list using class and object - java code examples with output

Program to print student mark list using class and object

In this section, You will learn how to calculate and print student total marks lists of n nuumber of student in java, with the help of InputStreamReader and BufferedReader class.

java program to calculate and display the total and the n student marks
Java program for student mark list using class and object

Let's try to create a simple example :

  ➤ Example : student.java;

  1. import java.io.*;

  2. class MList {

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

  4. public static 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 static 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. public static void main(String args[]) {

  45.   String stdNumber = "";

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

  47.   BufferedReader reader = new BufferedReader(input);

  48.  

  49. try{

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

  51.   stdNumber = reader.readLine();

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

  53.  

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

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

  56.   InputRecords();

  57. }

  58.   ViewRecords();

  59. }

  60. catch(Exception e){

  61.   e.printStackTrace();

  62. }

  63. }

  64. }

  65. class Student {

  66.   public String name;

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

  68.   public int total;

  69. }

  70. class StudentsInfo {

  71. public Student[] m_studList = new Student[100];

  72. public int m_nMaxStudents;

  73.  

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

  75.   Student stud = new Student();

  76.   stud.name = name;

  77.   stud.marks = marks;

  78.   stud.total = 0;

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

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

  81. }

  82.   m_studList[m_nMaxStudents++] = stud;

  83.   return 1;

  84. }

  85. }

import java.io.*;

class MList {
    static public StudentsInfo theStudents = new StudentsInfo();

    public static 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 static 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();
        }
    }

    public static void main(String args[]) {
        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");
                InputRecords();
            }

            ViewRecords();

        }
        catch(Exception e){
            e.printStackTrace();

        }

    }

}

class Student {
  public String name;
  public int[] marks = new int[5];
  public int total;

}

class StudentsInfo {
    public Student[] m_studList = new Student[100];
    public int m_nMaxStudents;

    public int AddRecord(String name, int[] marks) {
        Student stud = new Student();
        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 : 90

  • Sub 1 Mark : 80

  • Sub 1 Mark : 85

  • Sub 1 Mark : 82

  • Sub 1 Mark : 70

  •  

  • Enter 1 Student Information

  •  

  • Student Name: Anmol

  • Sub 1 Mark : 88

  • Sub 1 Mark : 75

  • Sub 1 Mark : 80

  • Sub 1 Mark : 92

  • Sub 1 Mark : 70

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

  • SNO Student Name Sub1 Sub2 Sub3 Sub4 Sub5 Total

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

  • 1 Ankaj 90 80 85 82 70 407

  • 2 Anmol 88 75 80 92 70 405

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

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...

Django not able to find static files[Solved]

# Django static files not working In this article, you will learn how to fix the problem of not loading static files. This is the easiest and safest solution. In the new version of Django 3.1 and above the settings.py file uses PATH instead of OS to deal with the directory structure. So all the code, we wrote for our static dirs and things like that will no longer work unless we import os at the top of our settings.py file.. Note : This article related to DEBUG = True , if you have problems with DEBUG = False then you can follow this article; Static files not working when DEGUB= False . Have fun! Let's follow a few steps and, solve the problem of not loading Django static files: Step 1 : Check that "BASE_DIR" is defined in your project settings.py , if not then define it  ➤ Code: settings.py import os BASE_DIR = ...