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