Guest User

Untitled

a guest
Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. /*
  4. ==========================================
  5.  
  6. ONYEN: jqpublic
  7. NAME: John Q. Public
  8.  
  9. ASSIGNMENT: A7
  10.  
  11. CLASS: COMP 110-004 fall 2010
  12. INSTRUCTOR: Stotts
  13.  
  14. ==========================================
  15. */
  16.  
  17.  
  18. class HW7 {
  19.  
  20.  
  21. public static void main (String[] args) throws IOException {
  22. int i;
  23. String line;
  24. // get two run line arguments (2 integers)
  25. int NSTUDENTS = Integer.parseInt(args[0]);
  26. int NGRADES = Integer.parseInt(args[1]);
  27.  
  28. if (NSTUDENTS <= 0){
  29. System.out.println("Number of students must be greater than 0");
  30. return;
  31. }
  32. if (NGRADES <= 0){
  33. System.out.println("Number of grades must be greater than 0");
  34. return;
  35. }
  36. // create an array of Student objects
  37. Student[] people = new Student[NSTUDENTS];
  38. Student stu;
  39.  
  40. Prompter prompter;
  41.  
  42. // create the Student objects and fill them
  43. // use a for loop
  44. for ( i = 0; i < NSTUDENTS; i++ ) {
  45. // access each slot in the array
  46. int year;
  47. // create a new student object
  48. stu = new Student(NGRADES);
  49. prompter = new Prompter();
  50. // fill the student object with info
  51. // typed at the keyboard
  52. line = prompter.prompt("Input first name: ");
  53. stu.setFname(line); // of course, you have to get the line
  54. // from the user, convert to num if needed
  55.  
  56. line = prompter.prompt("Input last name: ");
  57. stu.setLname(line);
  58.  
  59. line = prompter.prompt("Input major: ");
  60. stu.setMajor(line);
  61.  
  62. line = prompter.prompt("Input year: ");
  63. year = Integer.parseInt(line);
  64. if ( year <= 0 || year > 5){
  65. System.out.println("Year must be 1 through 5");
  66. return;
  67. }
  68. stu.setYear(year);
  69.  
  70. // store the filled student object in the current array slot
  71. people[i] = stu;
  72. }
  73.  
  74.  
  75. // go into a forever loop
  76. while(true){
  77. // show the menu
  78. System.out.println("Select an action to perform: "
  79. + "\n 0 - quit"
  80. + "\n 1 - show class roster (showing grade average for each student)"
  81. + "\n 2 - add an exam grade for all students"
  82. + "\n 3 - show student information"
  83. + "\n 4 - show class average"
  84. + "\n 5 - show overall average for a specific major"
  85. + "\n 6 - show overall average for a specific year"
  86. + "\n 7 - show class \"distribution\"... how many A's, B's, C's, D's, F's");
  87.  
  88. // get user selection as keyboard input
  89. prompter = new Prompter();
  90. int userSelection = Integer.parseInt(prompter.prompt(""));
  91. int count;
  92.  
  93. // switch statement on user selection
  94. switch (userSelection) {
  95. case 0:
  96. System.out.println("quitting...");
  97. System.exit(0); // ends the program
  98. break;
  99. case 1:
  100. System.out.println("show class roster");
  101. doShowRoster(people);
  102. break;
  103. case 2:
  104. System.out.println("add grade to each student");
  105. doAddGrades(people);
  106. break;
  107. case 3:
  108. String name = prompter.prompt("Enter student name \"first last\": ");
  109. for (i = 0; i < NSTUDENTS; i++){
  110. if (name.equalsIgnoreCase(people[i].getName())){
  111. System.out.println(people[i].getName()
  112. + ", " + people[i].getMajor()
  113. + ", " + people[i].getYear()
  114. + "\n" + people[i].getGrades()
  115. + "\n" + people[i].getAverage());
  116. }
  117. }
  118. break;
  119. case 4:
  120. double average = 0;
  121. for (i = 0; i < NSTUDENTS; i++){
  122. average += people[i].getAverage();
  123. }
  124. average = average / NSTUDENTS;
  125. System.out.println("Class average: " + average);
  126. break;
  127. case 5:
  128. String major = prompter.prompt("Enter which major: ");
  129. average = 0;
  130. count = 0;
  131. for (i = 0; i < NSTUDENTS; i++){
  132. if (major.equalsIgnoreCase(people[i].getMajor())){
  133. average += people[i].getAverage();
  134. count += 1;
  135. }
  136. }
  137. average = average / count;
  138. System.out.println("Average for " + major + ": " + average);
  139. break;
  140. case 6:
  141. int year = Integer.parseInt(prompter.prompt("Enter which year: "));
  142. if ( year <= 0 || year > 5){
  143. System.out.println("Year must be 1 through 5");
  144. break;
  145. }
  146. average = 0;
  147. count = 0;
  148. for (i = 0; i < NSTUDENTS; i++){
  149. if (year == people[i].getYear()){
  150. average += people[i].getAverage();
  151. count += 1;
  152. }
  153. }
  154. average = average / count;
  155. System.out.println("Average for " + year + ": " + average);
  156. break;
  157.  
  158. case 7:
  159. int nA = 0;
  160. int nB = 0;
  161. int nC = 0;
  162. int nD = 0;
  163. int nF = 0;
  164.  
  165. double grade;
  166. for (i = 0; i < NSTUDENTS; i++){
  167. grade = people[i].getAverage();
  168. if (grade >= 90){
  169. nA += 1;
  170. }
  171. else if (grade >= 80){
  172. nB += 1;
  173. }
  174. else if (grade >= 70){
  175. nC += 1;
  176. }
  177. else if (grade >= 60){
  178. nD += 1;
  179. }
  180. else {
  181. nF += 1;
  182. }
  183. }
  184. System.out.println("A's: " + nA + " B's: " + nB + " C's: " + nC + " D's: " + nD + " F's: " + nF);
  185. break;
  186.  
  187.  
  188. default:
  189. System.out.println("bad selection");
  190. break;
  191. }
  192. }
  193. // end of the forever loop body
  194.  
  195. }
  196.  
  197. public static void doShowRoster(Student[] all) { // stub
  198. // you may need more parameters
  199. int i;
  200. for (i = 0; i < all.length; i++ ){
  201. System.out.println(all[i].getName() + " " + all[i].getAverage());
  202. }
  203. }
  204.  
  205. public static void doAddGrades (Student[] all) throws NumberFormatException, IOException { // stub
  206. // you may need more parameters
  207. Prompter prompter = new Prompter();
  208.  
  209. int i;
  210. for (i = 0; i < all.length; i++){
  211. double grade = Double.parseDouble(prompter.prompt("Enter grade for " + all[i].getName() +": "));
  212. if (grade > 0.00 || grade < 100.00){
  213. all[i].addGrade(grade);
  214. }
  215. else {
  216. System.out.println("Must be between 0.00 and 100.00");
  217. i -= 1;
  218. }
  219. }
  220.  
  221. }
  222.  
  223.  
  224. }
Add Comment
Please, Sign In to add comment