Java Program to calculate Student Average of a Class using User defined Storage Classes

Program

import java.util.*;
class Student
{
	private String name;
	private String usn;
	private Integer marks;
	Student(String name, String usn, Integer marks)
	{
		this.name = name;
		this.usn = usn;
		this.marks = marks;
	}
	public Integer getMarks()
	{
		return marks;
	}
	public String toString()
	{
		return (name + "\t" + usn + "\t" + marks);
	}
}
public class ClassAverage
{
	public static void main(String[] args)
	{
		LinkedList<Student> stuDetails = new LinkedList<Student>();
		String name, usn;
		char sec;
		Integer sem, m1, m2, m3, total;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter the number of students:\t");
		int n = sc.nextInt();
		try
		{
			for(int i = 0; i < n; i++)
			{
				System.out.println("*******************************");
				System.out.println("Enter details of Student " + (i + 1));
				System.out.println("*******************************");
				System.out.println("Enter the name:\t");
				name = sc.next();
				System.out.println("Enter the USN:\t");
				usn = sc.next();
				System.out.println("Enter marks 1:\t");
				m1 = sc.nextInt();
				System.out.println("Enter marks 2:\t");
				m2 = sc.nextInt();
				System.out.println("Enter marks 3:\t");
				m3 = sc.nextInt();
				total = m1 + m2 + m3;
				stuDetails.add(new Student(name, usn, total));
			}
			Iterator itr = stuDetails.iterator();
			float avgMarks = 0;
			System.out.println("*******************************");
			System.out.println("Name\tUSN\tTotal Marks");
			System.out.println("*******************************");
			while(itr.hasNext())
			{
				Object elem = itr.next();
				System.out.println(elem);
			}
			System.out.println("*******************************");
			for(Student s: stuDetails)
			{
				avgMarks += (float)s.getMarks();
			}
			System.out.println("\nClass Average: "+ (avgMarks/stuDetails.size()));
		}
		catch (Exception err)
		{
			System.out.println(err.getMessage());
		}
	}
}

This Java program calculates the class average marks of students using a user-defined storage class (Student) and a LinkedList to store the student details.

  1. Student Class:

    • Represents a student's details with the following attributes:
      • name: Name of the student.
      • usn: Unique student number (USN).
      • marks: Total marks obtained by the student.
    • Encapsulates student data and provides methods for accessing and displaying the data.
    • Includes:
      • A constructor to initialize the attributes.
      • A getMarks() method to return the student's total marks.
      • A toString() method to format the output of a student's details.
  2. ClassAverage Class:

    • The main method manages the student data, calculates the class average, and handles user interaction.
    • Computes the average marks for the class dynamically based on user input.
  3. Data Collection:

    • Prompts the user to enter the number of students.
    • For each student, asks for their name, USN, and three marks (marks1, marks2, marks3).
    • Calculates the total marks for the student and stores a Student object in a LinkedList.
  4. Display of Details:

    • Uses an Iterator to traverse the LinkedList and print the name, USN, and total marks of each student.
  5. Class Average Calculation:

    • Iterates through the LinkedList and sums the total marks for all students.
    • Computes the average by dividing the total marks by the number of students and prints it.
  6. Exception Handling:

    • Surrounds the main functionality with a try-catch block to handle potential exceptions (e.g., invalid input).

Output

Enter the number of students:	3
*******************************
Enter details of Student 1
*******************************
Enter the name:	
John
Enter the USN:	
123
Enter marks 1:	
30
Enter marks 2:	
35
Enter marks 3:	
32
*******************************
Enter details of Student 2
*******************************
Enter the name:	
Mark
Enter the USN:	
124
Enter marks 1:	
46
Enter marks 2:	
47
Enter marks 3:	
40
*******************************
Enter details of Student 3
*******************************
Enter the name:	
Peter
Enter the USN:	
125
Enter marks 1:	
49
Enter marks 2:	
32
Enter marks 3:	
46
*******************************
Name	USN	Total Marks
*******************************
John	123	97
Mark	124	133
Peter	125	127
*******************************
Class Average: 119.0