hey guys,, can anybody help me with this? its for java neatbeans.. thanks A prof
ID: 3792815 • Letter: H
Question
hey guys,, can anybody help me with this? its for java neatbeans.. thanks
A professor has posted a list of student numbers and marks. Write a program to read the marks and compute the rank of a student in class; the rank is computed using the usual manner as follows: if the student has the top mark (or is tied for the top mark) his rank is 1; if he has the second best mark (or is tied) his rank is 2, and so on. Input (from a file called rank. in) The input consists of several test cases. Each case begins with Ahmed's student number, an integer between 1000000 and 9999999. Following the student number are a number of lines, each containing a student number between 1000000 and 9999999 and a mark between 0 and 100. A line with a student number and mark of 0 terminates each test case. There are no more than 1000 students in the class, and each has a unique student number. output (to a file called rank. out) For each test case, output a line giving Ahmed's rank in the class.Explanation / Answer
Student class holds each student's student number and his marks
Main.java - Reads the input files, stores students and outputs seeked student's rank in an output file
Student.java
/**
* Student class to store student number and marks
* @author Anonymous
*
*/
public class Student implements Comparable<Student>{
private int id;
private int marks;
public Student(int id, int marks){
this.id = id;
this.marks = marks;
}
public int getId() {
return id;
}
public int getMarks() {
return marks;
}
/**
* Overridden compareTo method of Comparable
*/
@Override
public int compareTo(Student obj) {
if(this.getMarks()<=obj.getMarks()){
return 1;
}
return -1;
}
}
Main.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String args[]) throws FileNotFoundException{
//List of students
List<Student> studentList = null;
//Input file
File file = new File("<<Input file goes here>>");
FileInputStream fis = new FileInputStream(file);
//Scanner object on File Input Stream
Scanner sc = new Scanner(fis);
//Output file
File fOut = new File("<<Output File goes here>>");
PrintWriter pw = new PrintWriter(new FileOutputStream(fOut));
int studentID;
while(sc.hasNext()){
//Reading student number of Student whose rank is to be found
studentID = Integer.parseInt(sc.nextLine());
int id,marks;
//Creating a linked list
studentList = new LinkedList<Student>();
while(sc.hasNext()){
//Reading next line
String str = sc.nextLine();
//Splitting line on whitespace
String[] strTokens = str.split(" ");
//First token
id = Integer.parseInt(strTokens[0]);
//Second token
marks = Integer.parseInt(strTokens[1]);
//If student number == 0 and marks == 0 then break from loop
if(id == 0 && marks == 0) break;
//Create a student object
Student student = new Student(id,marks);
//Add to the student list
studentList.add(student);
}
//Sort the collections in descending order based on marks
Collections.sort(studentList);
for(int i=0;i<studentList.size();i++){
if(studentList.get(i).getId() == studentID){
//Output rank of Student whose rank had to be found
pw.write(String.valueOf(i+1));
pw.println();
break;
}
}
}
//Close file
pw.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.