Answer to Question #9849 in Java | JSP | JSF for sortingalgorithms

Question #9849
Create a program containing and showing random information about name, ID
and status (year, current and cumulative performance) of students from a file
students.txt. This program should process a list of 20 students, filtering those of
them having a name beginning with the same letter (prompt the user to choose
one), who are third year students and have current as well as cumulative
performance above the average of this filtered list. Sort the filtered users
according to their user ID (you can use bubble sort etc). Save the
results in file studentsFilter.txt.
1
Expert's answer
2012-05-22T10:18:01-0400

public class Students implements Comparable{

private String name;
private int ID;
private String status;
private int year;

public Students(String name, int id, String status, int year){
&
& this.name = name;
& this.ID = id;
& this.status = status;
& this.year = year;
}

public String getName() {
& return name;
}

public int getID() {
& return ID;
}

public String getStatus() {
& return status;
}

public int getYear() {
& return year;
}

public int compareTo(Object obj){
& Students otherStud = (Students) obj;
& if(this.ID < otherStud.getID())
& return -1;
& else if (this.ID > otherStud.getID())
& return 1;
& return 0;
}


}



import java.io.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class StudentsRun {


public static void main(String[] args) {
& // TODO Auto-generated method stub
&
& List<Students> listStudents = new ArrayList<Students>();
& try {
& BufferedReader br = new BufferedReader(new FileReader(new File("./students.txt")));
& BufferedWriter bw = new BufferedWriter(new FileWriter(new File("./studentsFilter.txt")));
&
& String str = "";
& Students currStud;
&
& // Example of row in file students.txt - name ID status year
&
& while((str = br.readLine()) != null){
& String[] arr = str.split(" ");
& currStud = new Students(arr[0], Integer.parseInt(arr[1]), arr[2], Integer.parseInt(arr[3]));
& if(currStud.getYear() == 3)
& listStudents.add(currStud);
& }
&
& br.close();
&
& Collections.sort(listStudents);
& Iterator it = listStudents.iterator();
& while(it.hasNext()){
& Students stud = (Students)it.next();
& bw.write(stud.getName() + " " + stud.getID() + " " + stud.getStatus() + " " + stud.getYear());
& bw.newLine();
& }
&
& bw.close();
&
& } catch (FileNotFoundException e) {
& // TODO Auto-generated catch block
& e.printStackTrace();
& } catch (IOException e) {
& // TODO Auto-generated catch block
& e.printStackTrace();
& }

}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS