Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package adrianmom;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Scanner;
  12.  
  13. /**
  14. *
  15. * @author Archer
  16. */
  17. public class AdrianMom {
  18.  
  19. /**
  20. * @param args the command line arguments
  21. */
  22. public static void main(String[] args) {
  23. Map<String, Double> students = new HashMap<>();
  24. boolean running = true;
  25. Scanner scan = new Scanner(System.in);
  26. while(running){
  27. String name;
  28. double grade;
  29. System.out.print("Please enter student name: ");
  30. name = scan.nextLine();
  31. System.out.print("Please enter student grade: ");
  32. grade = scan.nextDouble();
  33. students.put(name, grade);
  34. System.out.print("Add more students? 1 for yes, any other number for no: ");
  35. int proceed;
  36. proceed = scan.nextInt();
  37. if(!(proceed == 1)){
  38. running = false;
  39. }
  40. scan.nextLine();
  41. }
  42.  
  43. /**
  44. * Calculates the average class test grade
  45. */
  46. double gradeTotal = 0;
  47. double highestGrade = Double.MIN_VALUE;
  48. for (Double grade : students.values()) {
  49. gradeTotal += grade;
  50. if(grade > highestGrade)
  51. highestGrade = grade;
  52. }
  53.  
  54. double classAverage = gradeTotal/students.size();
  55. ArrayList<String> studentsBelowAvg = new ArrayList<>();
  56. ArrayList<String> studentsHighestTest = new ArrayList<>();
  57.  
  58. /**
  59. * Runs through hash map entries, finding the students
  60. * with the highest test grade and below average test grades
  61. */
  62. for (Map.Entry entry : students.entrySet()) {
  63. String key = (String)entry.getKey();
  64. Double value = (Double)entry.getValue();
  65. if(value < classAverage)
  66. studentsBelowAvg.add(key);
  67. if(value == highestGrade)
  68. studentsHighestTest.add(key);
  69. }
  70.  
  71. /**
  72. * Prints out student data extracted from the arrayLists
  73. */
  74. System.out.println("Class Average: " + classAverage + "%");
  75. System.out.println("Students with below average test grades:");
  76. for(String studentName : studentsBelowAvg){
  77. System.out.println(studentName);
  78. }
  79. System.out.println("Next time study for the test!");
  80. System.out.println("");
  81. System.out.println("Students with the highest test grade:");
  82. for(String studentName : studentsHighestTest){
  83. System.out.println(studentName);
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement