Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FinalExamMark {
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6.  
  7. while (true) {
  8. //get average the user wants
  9. System.out.print("Enter the desired average: ");
  10. int d = input.nextInt();
  11.  
  12. //if input average is negative, exit the program
  13. if (d < 0) {
  14. System.out.println("Goodbye.");
  15. System.exit(1);
  16. }
  17.  
  18. //get number of tests including the final
  19. System.out.print("Enter the number of tests (including the final): ");
  20. int k = input.nextInt();
  21. //use nextLine to avoid skipping weights entry
  22. input.nextLine();
  23.  
  24. //get the test weights
  25. System.out.print("Enter the test weights: ");
  26. String weightsString = input.nextLine();
  27.  
  28. int[] weights = stringToArray(weightsString, k);
  29.  
  30. //get the test grades before the exam (k - 1)
  31. System.out.print("Enter the previous test grades (out of 100): ");
  32. String gradesString = input.nextLine();
  33.  
  34. int[] gradesInt = stringToArray(gradesString, (k - 1));
  35. double[] grades = new double[gradesInt.length];
  36.  
  37. //convert grades to a double array
  38. for (int i = 0; i < (k - 1); i++) {
  39. grades[i] = gradesInt[i];
  40. }
  41.  
  42. //debug lines
  43. //System.out.println(Arrays.toString(weights));
  44. //System.out.println(Arrays.toString(grades));
  45.  
  46. //divide the marks for each test by 100
  47. for (int i = 0; i < grades.length; i++) {
  48. grades[i] = grades[i] / 100;
  49. }
  50.  
  51. //add the marks for each test up
  52. double totalMarks = 0;
  53. for (int i = 0; i < grades.length; i++) {
  54. totalMarks += grades[i];
  55. }
  56.  
  57. //divide totalMarks by the number of tests
  58. totalMarks = totalMarks / (k - 1);
  59. //multiply that value by 100 to get the mark thus far
  60. totalMarks = totalMarks * 100;
  61.  
  62. double finalWeight = weights[weights.length - 1] / 100;
  63.  
  64. double finalMark = (d - ((1 - finalWeight) * totalMarks)) / finalWeight;
  65.  
  66. System.out.println(finalMark);
  67. }
  68. }
  69.  
  70. public static int[] stringToArray(String string, int arrayLength) {
  71. //split all the numbers up into an array
  72. String[] stringArray = string.split("\\s+");
  73. int[] array = new int[arrayLength];
  74.  
  75. //convert the string array to an int array
  76. for (int i = 0; i < arrayLength; i++) {
  77. array[i] = Integer.parseInt(stringArray[i]);
  78. }
  79.  
  80. return array;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement