|
| 1 | +package Practice1; |
| 2 | + |
| 3 | +/* ******************************************************************** |
| 4 | +Step 1: Prompt the user to enter numberOfStudents (int). |
| 5 | +
|
| 6 | +Step 2: Create an array for scores using new double[numberOfStudents]. |
| 7 | +
|
| 8 | +Step 3: Declare and initialize variable best to keep the best score. |
| 9 | +Set the initial value to 0. |
| 10 | +
|
| 11 | +Step 4: Prompt the user to enter the scores in a loop that executes |
| 12 | +numberOfStudents times. |
| 13 | +For each score entered, store it in scores[i]. |
| 14 | +Compare it with best. If it is greater than best, assign it to best. |
| 15 | +
|
| 16 | +Step 5: Write a for loop for i from 0 to numberOfStudents - 1, |
| 17 | +compare scores[i] with grade to assign the grade for the student. |
| 18 | + ************************************************************************/ |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.*; |
| 23 | + |
| 24 | +public class ExcerciseSevenOne { |
| 25 | + public static void main(String[] args) { |
| 26 | + |
| 27 | + //StudentGradingSystemOne(); |
| 28 | + StudentGradingSystemTwo(); |
| 29 | + } |
| 30 | + |
| 31 | + public static void StudentGradingSystemOne() |
| 32 | + { |
| 33 | + Scanner input = new Scanner(System.in); |
| 34 | + |
| 35 | + System.out.print("Enter the number of students: "); |
| 36 | + int numberOfStudents = input.nextInt(); |
| 37 | + |
| 38 | + double[] scores = new double[numberOfStudents]; |
| 39 | + |
| 40 | + double best = 0; |
| 41 | + |
| 42 | + for(int i= 0; i < numberOfStudents; i++){ |
| 43 | + System.out.print("Enter score for student " + (i+1) + ": "); |
| 44 | + scores[i] = input.nextDouble(); |
| 45 | + if(scores[i] > best) { |
| 46 | + best = scores[i]; |
| 47 | + } |
| 48 | + } |
| 49 | + for (int i = 0; i < numberOfStudents; i++){ |
| 50 | + System.out.print("Student " + (i + 1) + " score: " + scores[i] + " - "); |
| 51 | + |
| 52 | + if(scores[i] >= best -10){ |
| 53 | + System.out.println("A"); |
| 54 | + } else if (scores[i] >= best -20) { |
| 55 | + System.out.println("B"); |
| 56 | + } else if(scores[i] >= -30){ |
| 57 | + System.out.println("C"); |
| 58 | + } else { |
| 59 | + System.out.println("D"); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static void StudentGradingSystemTwo() { |
| 65 | + Scanner input = new Scanner(System.in); |
| 66 | + |
| 67 | + System.out.print("Enter the number of students: "); |
| 68 | + int numberOfStudents = input.nextInt(); |
| 69 | + |
| 70 | + List<Double> scores = new ArrayList<>(); |
| 71 | + |
| 72 | + System.out.println("Enter the scores: "); |
| 73 | + for(int i = 0; i < numberOfStudents; i++){ |
| 74 | + System.out.print("Score for student " + (i + 1) + ": "); |
| 75 | + scores.add(input.nextDouble()); |
| 76 | + } |
| 77 | + |
| 78 | + double best = scores.stream(). |
| 79 | + mapToDouble(Double::doubleValue). |
| 80 | + max().orElse(0); |
| 81 | + |
| 82 | + scores.stream().forEach(score -> { |
| 83 | + System.out.print("Score: " + score + " - "); |
| 84 | + |
| 85 | + if(score >= best - 10) { |
| 86 | + System.out.println("A"); |
| 87 | + } else if (score >= best -20) { |
| 88 | + System.out.println("B"); |
| 89 | + } else if (score >= - 30){ |
| 90 | + System.out.println("C"); |
| 91 | + } else { |
| 92 | + System.out.println("D"); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments