Java Assignment
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author mrahman */ public class StudentClient { /** * This method reads student's data from the "std.txt" file and * stores them in an array list. * The std.txt file format: * First line contains first and last name of the student * Second line contains the number of courses (n) * Following 'n' lines, each contains credit (an integer) and grade(a double) * values. * There is no limit on number of students in the file. Example: * * Musfiq Rahman * 4 * 3 3.0 * 4 4.0 * 3 4.0 * 4 4.0 * * Adam Edge * 5 * 3 4.0 * 3 2.5 * 1 4.0 * 4 3.5 * 3 3.0 * * @param stds array list to store student's data * @throws FileNotFoundException */ public static void readStudentsData(ArrayList<Student> stds) throws FileNotFoundException{ File f = new File("std.txt"); Scanner sk = new Scanner(f); // process each studnets data while(sk.hasNextLine()){ // first line should be the studnet's name String l = sk.nextLine(); if(l.length() <= 0) continue; // Splits first and last name String [] r = l.split(" "); String fname = r[0]; String lname = r[1]; // Creat a studnet object with the first and last name Student s = new Student(fname, lname); // Now, read how many courses data are there for this student int count = sk.nextInt(); // For each course, read the credit and the grade values // for the student for(int j=0; j<count; j++){ int credits = sk.nextInt(); double gr = sk.nextDouble(); s.addCourse(credits, gr); } // Add the student's data to the Array list. stds.add(s); } } public static void main(String[] args) { try { // ArrayList for storing students data ArrayList<Student> stds = new ArrayList<Student>(); // Read and store students data from the std.txt file readStudentsData(stds); // Sort studets data based on their gpa Collections.sort(stds); // Print all students data in increasing order of gpa value. for(Student s:stds) System.out.println(s); } catch (FileNotFoundException ex) { System.out.println("Data file (std.txt) not found."); } } }