Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import java.io.*;
  4.  
  5. import java.util.Arrays;
  6.  
  7. public class IPJASS2Q2 {
  8. //Initializing arrays
  9.  
  10. static String names[] = new String[20]; // a one-dimensional array to store the student names
  11.  
  12. static int Scores[][] = new int[20][5]; // a (parallel) two-dimensional array to store the test score
  13.  
  14. static char grades[] = new char[20]; // a parallel one-dimensional array to store grades
  15.  
  16. public static int read_data(String filename) throws FileNotFoundException {
  17.  
  18. String inputLine;
  19.  
  20. int arraylength=0;
  21.  
  22.  
  23.  
  24. Scanner scanner = new Scanner(new File(filename));
  25.  
  26. //data storing
  27.  
  28. while (scanner.hasNextLine()) {
  29.  
  30. inputLine = scanner.nextLine();
  31.  
  32. String[] temp = inputLine.split(" ");
  33.  
  34. for(int i=0;i<temp.length;i++) {
  35.  
  36. if(i==0)
  37.  
  38. names[arraylength] = temp[i];
  39.  
  40. else
  41.  
  42. Scores[arraylength][i-1] = Integer.parseInt(temp[i]);
  43.  
  44. }
  45.  
  46. arraylength++;
  47.  
  48. }
  49.  
  50. //computing grades
  51.  
  52.  
  53.  
  54. return arraylength;
  55.  
  56. }
  57.  
  58. public static void calculate(int arraylength) {
  59.  
  60. for(int i=0;i<arraylength;i++) {
  61.  
  62. //getting sum of 5 scores
  63.  
  64. int s=0;
  65.  
  66. for(int j=0;j<5;j++) {
  67.  
  68. s+=Scores[i][j];
  69.  
  70. }
  71.  
  72. //calculating avg
  73.  
  74. double avg = s/5;
  75.  
  76. //calculating grade
  77.  
  78. if(avg >=85)
  79.  
  80. grades[i] = 'A';
  81.  
  82. else if(avg >= 75)
  83.  
  84. grades[i] = 'B';
  85.  
  86. else if(avg >= 65)
  87.  
  88. grades[i] = 'C';
  89.  
  90. else if(avg >= 50)
  91.  
  92. grades[i] = 'D';
  93.  
  94. else
  95.  
  96. grades[i] = 'F';
  97.  
  98. }
  99.  
  100. }
  101.  
  102. public static void print_data(int arraylength) {
  103.  
  104. for(int i=0;i<arraylength;i++) {
  105.  
  106. System.out.printf("\n %s got %c Grade",names[i],grades[i]);
  107. System.out.println(names+" Average Score Is : " +);
  108.  
  109. }
  110.  
  111.  
  112. }
  113.  
  114. public static void main(String[] args) throws FileNotFoundException {
  115.  
  116. //reading data from file
  117.  
  118. int arraylength = read_data("C:\\Users\\BW\\IdeaProjects\\IPJAssignment\\src\\data");
  119.  
  120. //calculating avg score and grades
  121.  
  122. calculate(arraylength);
  123.  
  124. //to print the data
  125.  
  126. print_data(arraylength);
  127.  
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement