Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. public class BCS345hw4 {
  2.  
  3.  
  4. public static void main(String args[]) throws IOException {
  5.  
  6.  
  7.  
  8. Student[]students;
  9. students = new Student[25];
  10.  
  11. final int SIZE = 5;
  12. int[]scores = new int[SIZE];
  13. int i = 0;
  14.  
  15. File myFile = new File("hw4data.txt");
  16. Scanner scanner = new Scanner(myFile);
  17.  
  18.  
  19. System.out.printf("%2s%15s%15s%24s%28s%10s\n", "ID","First Name","Last Name","Scores", "Average","Grade" );
  20. while(scanner.hasNext() && i < students.length)
  21. {
  22.  
  23. Student student = new Student();
  24. student.setID(scanner.nextInt());
  25. student.setFName(scanner.next());
  26. student.setLName(scanner.next());
  27.  
  28.  
  29.  
  30. for(int j = 0; j < scores.length; j++) {
  31.  
  32. scores[j] = scanner.nextInt();
  33. student.setScores(scores);
  34.  
  35. }
  36.  
  37.  
  38.  
  39.  
  40. System.out.println(student);
  41.  
  42. i++;
  43. }
  44.  
  45.  
  46.  
  47. scanner.close();
  48.  
  49.  
  50.  
  51. }
  52.  
  53.  
  54. import java.util.Arrays;
  55.  
  56. /*
  57. * Talha Riaz
  58. * Professor Li BCS 345
  59. * Assignment 4 @duedate 10/30/17
  60. */
  61.  
  62. public class Student{
  63.  
  64. public int ID;
  65. private String firstName;
  66. private String lastName;
  67. private int scores[];
  68. float average;
  69. char grade;
  70.  
  71. /*
  72. * Default constructor with no arguments
  73. * sets all variables to default values
  74. */
  75. public Student()
  76. {
  77.  
  78. ID = 0;
  79. firstName = "No_Name";
  80. lastName = "No_Name";
  81. scores = new int[] {0,0,0,0,0};
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88. /**
  89. * overloaded constructor for class Student
  90. * uses variables as parameters to set values of all variables
  91. * @param ID
  92. * @param firstName
  93. * @param lastName
  94. */
  95.  
  96. public Student(int I, String fName,String lName,int[]array)
  97. {
  98.  
  99. ID = I;
  100. firstName = fName;
  101. lastName = lName;
  102. scores = array;
  103.  
  104.  
  105.  
  106.  
  107.  
  108. }
  109.  
  110.  
  111. /**
  112. * Sets value of ID = I and stores it in I
  113. * @param I
  114. * @return I
  115. */
  116. public void setID(int I) {
  117.  
  118. ID = I;
  119.  
  120. }
  121.  
  122. /**
  123. * sets value of firstName and stores it in F
  124. * @param F
  125. * @return
  126. */
  127. public void setFName(String F) {
  128.  
  129. firstName = F;
  130. }
  131.  
  132.  
  133.  
  134. /*
  135. * sets value of lastName stores it in L
  136. * @param L
  137. * @return L
  138. */
  139. public void setLName(String L) {
  140.  
  141. lastName = L;
  142.  
  143. }
  144.  
  145.  
  146.  
  147. /**
  148. * Sets the scores, stores them in a variable score
  149. * @param x
  150. * @return
  151. */
  152.  
  153.  
  154. public void setScores(int[]x) {
  155.  
  156. for(int i = 0; i < x.length; i++)
  157. {
  158.  
  159. scores[i] = x[i];
  160.  
  161. }
  162.  
  163.  
  164. }
  165.  
  166.  
  167.  
  168. /**
  169. *
  170. * @return value of ID
  171. */
  172. public int getID() {
  173.  
  174. return ID;
  175.  
  176. }
  177.  
  178.  
  179. /**
  180. *
  181. * @return value of firstName
  182. */
  183. public String getFName() {
  184.  
  185. return firstName;
  186.  
  187. }
  188.  
  189.  
  190. /**
  191. *
  192. * @return value of lastName
  193. */
  194. public String getLName() {
  195.  
  196. return lastName;
  197.  
  198. }
  199.  
  200. public int[] getScores()
  201. {
  202.  
  203. return scores;
  204.  
  205. }
  206.  
  207. /**
  208. * calculates average of test scores
  209. * returns the average with the lowest test score dropped
  210. * @return average
  211. */
  212. public float getAverage() {
  213.  
  214. float average = 0;
  215. float total = 0;
  216. int lowest = scores[0];
  217.  
  218.  
  219. for(int i = 0; i < scores.length; i++)
  220. {
  221. if(scores[i] < lowest) {
  222. lowest = scores[i];
  223. }
  224.  
  225.  
  226. }
  227.  
  228. for(int i = 0; i < scores.length; i++)
  229. {
  230.  
  231. if(scores[i] > lowest) {
  232.  
  233. total += scores[i];
  234. }
  235.  
  236.  
  237. }
  238.  
  239. average = total/(scores.length - 1);
  240.  
  241. return average;
  242. }
  243.  
  244.  
  245. /**
  246. * Calculates the grade based on the average
  247. * @return grade
  248. */
  249.  
  250.  
  251. public char getGrade()
  252. {
  253.  
  254. average = getAverage();
  255.  
  256. if(average >= 90 && average <= 100) {
  257.  
  258. grade = 'A';
  259.  
  260.  
  261.  
  262. }
  263. else if(average >= 80 && average < 90) {
  264.  
  265.  
  266. grade = 'B';
  267.  
  268.  
  269. }
  270.  
  271.  
  272. else if(average >= 70 && average < 80) {
  273.  
  274. grade = 'C';
  275.  
  276. }
  277.  
  278.  
  279. else if(average >= 60 && average < 70) {
  280.  
  281. grade = 'D';
  282.  
  283.  
  284.  
  285. }
  286. else {
  287. grade = 'F';
  288.  
  289. }
  290.  
  291.  
  292. return grade;
  293.  
  294. }
  295.  
  296.  
  297.  
  298.  
  299.  
  300. /**
  301. * prints out information about the student object
  302. */
  303.  
  304. public String toString() {
  305.  
  306. String str = String.format("%5d%10s%15s",ID,firstName,lastName);
  307.  
  308. for(int i = 0; i < scores.length; i++)
  309. {
  310.  
  311. str += String.format("%8d", scores[i]);
  312.  
  313.  
  314. }
  315.  
  316. str += String.format("%13.2f%9c\n", getAverage(), getGrade());
  317.  
  318.  
  319.  
  320. return str;
  321. }
  322.  
  323.  
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement