Answer to Question #124095 in Java | JSP | JSF for shabab hussain

Question #124095
Accept names and marks of 10 students from user
. 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”
. 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:35-0400
package com.company;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static final int STUDENTS = 10;

    public static class Student implements Comparable<Student>
    {
        private String name;
        private int mark;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getMark() {
            return mark;
        }

        public void setMark(int mark) {
            this.mark = mark;
        }

        @Override
        public int compareTo(Student other) {
           return Integer.valueOf(this.getMark()).compareTo(other.getMark());
        }
    }

    public static String enterName()
    {
        String name;
        do {
            System.out.print("Enter name: ");
            name = new Scanner(System.in).nextLine();
            if (name.matches("-?\\d+(\\.\\d+)?"))
                System.out.println("Name cannot be a number!!!!");

        }while(name.matches("-?\\d+(\\.\\d+)?"));
        return name;
    }

    public static int enterMark()
    {
        String mark;
        do {
            System.out.print("Enter mark: ");
            mark = new Scanner(System.in).nextLine();
            if (!mark.matches("-?\\d+(\\.\\d+)?"))
                System.out.println("Mark must be a number!!!!");
            else if(Integer.parseInt(mark) <= 0)
                System.out.println("Mark must be greater than 0");

        }while(!mark.matches("-?\\d+(\\.\\d+)?") || Integer.parseInt(mark) <=0);
        return Integer.parseInt(mark);
    }

    public static ArrayList<Student> inputStudentsData()
    {
        ArrayList<Student> students = new ArrayList<>();
        for(int i = 0; i < STUDENTS; i++)
        {
            Student student = new Student();
            String name = enterName();
            int mark = enterMark();
            student.setName(name);
            student.setMark(mark);
            students.add(student);
        }
        return students;
    }

    public static ArrayList<Student> sortStudentsByMark(ArrayList<Student> students)
    {
        Collections.sort(students, Collections.reverseOrder());
        return students;
    }

    public static void main(String[] args) {
   // write your code here
        ArrayList<Student> students = inputStudentsData();
        students = sortStudentsByMark(students);

        System.out.println("\nSorted by marks");
        for(Student student:students)
            System.out.println(String.format("%8s%8d", student.getName(), student.getMark()));
    }
}

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