Answer to Question #124012 in Java | JSP | JSF for Niazi

Question #124012
Java program to
a. Accept names and marks of 10 students from user
b. Ensure that valid data is entered. If user enters an invalid data then appropriate message should be displayed, e.g. “Name cannot be a number” or “Marks must be an integer value”
c. Display this data in descending order according to the marks such that name of the student having maximum marks should be displayed at the top and the name of the student having minimum marks should be displayed at the bottom.
1
Expert's answer
2020-06-29T08:19:58-0400
class Student implements Comparable<Student> {
    
    String name;
    int score;

    public Student(){}
    
    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getScore() {
        return score;
    }
    
    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public int compareTo(Student o) {
        return this.getScore() - o.getScore();
    }
}


import java.util.Scanner;

public class InputHandler {

    Scanner scanner = new Scanner(System.in);
    Scanner scannerString = new Scanner(System.in);

    public int getTotalStudents() throws Exception {
        System.out.println("Input the total number of available students then press Enter");
        return Integer.parseInt(scanner.nextLine().trim());
    }

    public Student addRecord() throws Exception {
        String name;
        int score;
        System.out.println("Please input student name & press Enter and then Enter score");
        name = scannerString.next();
        score = scanner.nextInt();
        Student student = new Student(name, score);
        return student;
    }

}

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        
        System.out.println("Welcome to Student Score Maanagement System");
        List<Student> students = getInput();
        students.sort((student1, student2) -> student2.getScore() - student1.getScore());
        System.out.println("Student Records in decending order of score:");
        students.forEach(student -> System.out.println(student.getName() + ":" + student.getScore()));
        System.exit(0);
        
    }
    
    private static List<Student> getInput() {
        InputHandler handler = new InputHandler();
        int studentCount = 0;
        List<Student> students = new ArrayList<>();
        boolean countFlag = true;
        while (countFlag) {
            try {
                studentCount = handler.getTotalStudents();
                countFlag = false;
            } catch (Exception e) {
                System.out.println("Invalid ipnut entered. Please re-enter valid value.");
            }
        }

        for (int i = 0; i < studentCount; i++) {
            boolean studentFlag = true;
            while (studentFlag) {
                try {
                    students.add(handler.addRecord());
                    studentFlag = false;
                } catch (Exception e) {
                    System.out.println("Invalid ipnut entered. Please re-enter valid value.");
                }
            }
        }
        return students;
    }
    
} 

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