Please I want final source code files (i.e., .java files) for your Student class
ID: 3599651 • Letter: P
Question
Please I want final source code files (i.e., .java files) for your Student class and program I want to show me excitly what I write for this assignment.
Sorting Student Records
For this assignment, you are to write a Java application for sorting a large number of student records in a text file by student id.
Part 1: The Student Class
As with any typical Java application, the first step is to define the object classes. Fortunately, for this application, there is only one: the Student object class. Here is the data required for each Student:
In Part 2, you will be writing a program that sorts objects of this class, so you will also need to define your Student object class as implementing the Comparable interface, and its compareTo method. The functionality of your Student class' compareTo method is described in step 2 of Part 2 (below).
Part 2: The Application
Once your Student object class is defined, write a Java program that does the following:
1.Read the text file courses.txt into an array (or ArrayList) of Student objects.
i.There are no more than 1,000 lines in the courses.txt file.
ii.Each line of the file has data for the five data fields of a Student object, separated by tabs.
Suggestion: add a constructor or a method fromString to your Student class that, given a line of text, populates the Student object's data fields.
2. Sort the Student objects by:
i.Student ID, or
ii.Course Number and Section Number, or
iii.Semester and Year
Hint: use either Arrays.sort or Collections.sort
3.Write the Student objects into a second text file.
Suggestion: add a toString() method to your Student class that generates a single String with the Student object's data, and then, in your main method, just write that single String to the output text file.
Explanation / Answer
public class Student implements Comparable<Student> {
private int studentId;
private String courseNumber;
private int sectionNumber;
private String semister;
private int year;
public Student(String record) {
String[] split = record.split(" ");
this.studentId = Integer.parseInt(split[0]);
this.courseNumber = split[1];
this.sectionNumber = Integer.parseInt(split[2]);
this.semister = split[3];
this.year = Integer.parseInt(split[4]);
}
// Setter and Getters
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getCourseNumber() {
return courseNumber;
}
public void setCourseNumber(String courseNumber) {
this.courseNumber = courseNumber;
}
public int getSectionNumber() {
return sectionNumber;
}
public void setSectionNumber(int sectionNumber) {
this.sectionNumber = sectionNumber;
}
public String getSemister() {
return semister;
}
public void setSemister(String semister) {
this.semister = semister;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
// Overriding toString() that returns the attributes of Student object with tab seperated similar to a line in .txt file
@Override
public String toString() {
return studentId + " " + courseNumber + " " + sectionNumber + " " + semister + " " + year;
}
// sorting the student object based on student ID
@Override
public int compareTo(Student student) {
if (studentId == student.studentId) {
return 0;
}
return studentId > student.studentId ? 1 : -1;
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class StudentsInfo {
public static void main(String[] args) {
String line;
List<Student> studentsList = new ArrayList();
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
StringBuilder stringBuidler = new StringBuilder();
String OUTPUTFILENAME = "sortedStudents.txt"; // provide file path, in which you want the sorted students list
String INPUTFILENAME = "students.txt"; // provide your file path
try {
// Reading file data and storing each record to a student List
bufferedReader = new BufferedReader(new FileReader(INPUTFILENAME));
while ((line = bufferedReader.readLine()) != null) {
studentsList.add(new Student(line));
}
// sorting list as per studentID
Collections.sort(studentsList);
// Writing Sorted data to file
bufferedWriter = new BufferedWriter(new FileWriter(OUTPUTFILENAME));
Iterator<Student> iterator = studentsList.iterator();
while (iterator.hasNext()) {
stringBuidler.append(iterator.next().toString()).append(" ");
}
bufferedWriter.write(stringBuidler.toString());
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
}
}
}