September 29, 2020
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 number of student in java, with the help of InputStreamReader and BufferedReader class.
Let's try to create a simple example :
→ Example : student.java;
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:
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;
}
}
Java program