Untitled

 avatar
unknown
java
2 years ago
2.5 kB
2
Indexable
package Labs.Assignment5;
import java.util.*;
public class QuestionFivePointNine
{
  
    public static void main(String[] args)
    {
        ///Prompt the user to enter a number
        System.out.println("Enter the number of students");
        Scanner scanner = new Scanner(System.in);
        int numStudents = scanner.nextInt();
        
        /*create for loop that takes in students name and score and places it into an arraylist */
        ArrayList<String> studentNames = new ArrayList<>();
        ArrayList<Double> studentGrades = new ArrayList<>();
        ArrayList<Double> studentGradesCopy = new ArrayList<>();
        for(int i = 0; i<numStudents;i++)
        {
            System.out.println("Enter the students name: ");
            studentNames.add(scanner.next());
            System.out.println("Enter the students grade: ");
            double grade = scanner.nextDouble();
            studentGrades.add(grade);
            studentGradesCopy.add(grade);
        }
        /*create a for loop that checks for the lowest grad in studentGradesCopy 
        then finds the index of it from the original studentGradesCopy
        then removes the lowest grade
        prints the appropirate statement using if loops
        */
        for(int i = 0;i<2;i++)
        {
            if(i==0)
            {
                double lowest = Collections.min(studentGradesCopy);
                int index = studentGrades.indexOf(lowest);
                String name = studentNames.get(index);
                System.out.println("The lowest scoring student is:" + name);
                studentGradesCopy.remove(lowest);

            }

            if(i==1)
            {
                double lowest = Collections.min(studentGradesCopy);
                int index = studentGrades.indexOf(lowest);
                String name = studentNames.get(index);
                System.out.println("The second lowest scoring student is:" + name);
                studentGradesCopy.remove(lowest);
            }
        
        }

        scanner.close();

        /*there are definitely shortcommings in this program 
         * espically if you give it something like this
         * John
         * 89
         * James
         * 89
         * Jacob
         * 87
         * Emily
         * 87
         * Aaron
         * 90
         * Given more time i would create a program that returns the students with the two
         * lowest distinct grades ie[John,James,Jacob,Emily]
         */
    }
}
Editor is loading...