Java Challenge: Student Performance Analyzer
Challenge Description
You are hired to develop a Java program called Student Performance Analyzer. The program must read a list
of student records from a file, analyze their grades, and provide various statistics and operations.
Input File Format (students.txt):
Each line contains:
StudentID,FullName,MathScore,EnglishScore,ScienceScore
Example:
S001,John Doe,85,78,90
Requirements:
1. Read data from students.txt.
2. Create a Student class with appropriate fields/methods.
3. Store students in a suitable data structure.
4. Compute:
a. Each student's average score
b. Class average for each subject
c. Top-performing student by average
d. List of students who failed (avg < 60)
5. Search student by ID and display details.
6. Display data in tabular format.
Constraints:
- Handle malformed lines gracefully.
- Use standard Java SE only.
Solution Code - Student.java
public class Student {
Java Challenge: Student Performance Analyzer
private String studentId;
private String fullName;
private int mathScore, englishScore, scienceScore;
public Student(String studentId, String fullName, int math, int english, int science) {
this.studentId = studentId;
this.fullName = fullName;
this.mathScore = math;
this.englishScore = english;
this.scienceScore = science;
}
public double getAverage() {
return (mathScore + englishScore + scienceScore) / 3.0;
}
public String getStatus() {
return getAverage() < 60 ? "Fail" : "Pass";
}
public String getStudentId() {
return studentId;
}
public String getFullName() {
return fullName;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getScienceScore() {
return scienceScore;
}
}
Solution Code - Main.java
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
Java Challenge: Student Performance Analyzer
Scanner scanner = new Scanner(System.in);
try (BufferedReader br = new BufferedReader(new FileReader("students.txt"))) {
String line;
while ((line = br.readLine()) != null) {
try {
String[] parts = line.split(",");
if (parts.length != 5) continue;
String id = parts[0];
String name = parts[1];
int math = Integer.parseInt(parts[2]);
int english = Integer.parseInt(parts[3]);
int science = Integer.parseInt(parts[4]);
students.add(new Student(id, name, math, english, science));
} catch (Exception e) {
continue;
}
}
} catch (IOException e) {
System.out.println("Error reading file.");
return;
}
printReport(students);
searchStudent(scanner, students);
}
static void printReport(List<Student> students) {
System.out.println("Student Report:");
System.out.println("-------------------------------------------------------------");
System.out.println("ID Name Math English Science Avg Status");
System.out.println("-------------------------------------------------------------");
double totalMath = 0, totalEnglish = 0, totalScience = 0;
Student topStudent = null;
for (Student s : students) {
double avg = s.getAverage();
System.out.printf("%-6s %-14s %-5d %-8d %-8d %-7.2f %-5s\n",
s.getStudentId(), s.getFullName(), s.getMathScore(),
s.getEnglishScore(), s.getScienceScore(),
avg, s.getStatus());
totalMath += s.getMathScore();
totalEnglish += s.getEnglishScore();
totalScience += s.getScienceScore();
if (topStudent == null || avg > topStudent.getAverage()) {
topStudent = s;
}
}
int n = students.size();
Java Challenge: Student Performance Analyzer
System.out.printf("\nClass Averages:\nMath: %.2f, English: %.2f, Science: %.2f\n",
totalMath / n, totalEnglish / n, totalScience / n);
if (topStudent != null) {
System.out.printf("\nTop Student: %s (%s) with average %.2f\n",
topStudent.getFullName(), topStudent.getStudentId(), topStudent.getAverage());
}
System.out.println("\nFailed Students:");
for (Student s : students) {
if (s.getAverage() < 60) {
System.out.println(s.getFullName() + " - " + s.getAverage());
}
}
}
static void searchStudent(Scanner scanner, List<Student> students) {
System.out.print("\nSearch by StudentID: ");
String searchId = scanner.nextLine().trim();
for (Student s : students) {
if (s.getStudentId().equalsIgnoreCase(searchId)) {
System.out.printf("Result: %s - Avg: %.2f - %s\n",
s.getFullName(), s.getAverage(), s.getStatus());
return;
}
}
System.out.println("Student not found.");
}
}