Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. public String setGrade() { // returns the letter grade of the student
  2.  
  3. if (percentage >= 90) letterGrade = "A";
  4. else if (percentage <= 90 && percentage >= 80) letterGrade = "B";
  5.  
  6. return letterGrade;
  7.  
  8. What is your name? John
  9. Enter the test scores
  10. 90
  11. 100
  12. 90
  13. Percentage: 93
  14. letterGrade: null
  15.  
  16. public class Student {
  17.  
  18. // Instance Variables
  19. // Each Student object has a name and three test scores
  20. private String name; // student name
  21. private int test1; // score for test 1
  22. private int test2; // score on test 2
  23. private int test3; // score on test 3
  24. private int percentage; // score the variable average to calculate grade
  25. private String letterGrade; // returns the letter grade of the student
  26.  
  27. // Constructor method
  28. public Student() {
  29. // Initialize a new student's name to the empty string and test
  30. // scores to zero
  31. name = "";
  32. test1 = 0;
  33. test2 = 0;
  34. test3 = 0;
  35.  
  36. }
  37.  
  38. // Other methods
  39.  
  40. public void setName (String nm) {
  41. // Set a student's name
  42. name = nm;
  43. }
  44. public String getName () {
  45. // Get a student's name
  46. return name;
  47. }
  48.  
  49. public void setScore (int i, int score) {
  50. // Set test i to score
  51. if (i == 1) test1 = score;
  52. else if (i == 2) test2 = score;
  53. else test3 = score;
  54. }
  55.  
  56. public int getScore (int i) {
  57. // Retrieve score i
  58. if (i == 1) return test1;
  59. else if (i == 2) return test2;
  60. else return test3;
  61. }
  62.  
  63. public int getAverage(){
  64. // Compute and return the average
  65. int average;
  66. average = (int) Math.round((test1 + test2 + test3) / 3.0);
  67. percentage = average;
  68. return average;
  69. }
  70.  
  71. public int getHighScore() {
  72. // Determine and return the highest score
  73. int highScore;
  74. highScore = test1;
  75. if (test2 > highScore) highScore = test2;
  76. if (test3 > highScore) highScore = test3;
  77. return highScore;
  78. }
  79.  
  80. /*
  81. New Methods by @SimeonTG
  82.  
  83. */
  84.  
  85. /**
  86. *
  87. *
  88. */
  89.  
  90.  
  91. public String setGrade() { // returns the letter grade of the student
  92.  
  93. if (percentage >= 90) letterGrade = "A";
  94. else if (percentage <= 90 && percentage >= 80) letterGrade = "B";
  95.  
  96. return letterGrade;
  97.  
  98.  
  99.  
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106. public String toString(){
  107. // Construct and return a string representation of the student
  108. String str;
  109. str = "Name: " + name + "n" + // "n" denotes a new line
  110. "Test 1: " + test1 + "n" +
  111. "Test 2: " + test2 + "n" +
  112. "Test 3: " + test3 + "n" +
  113. "Average: " + getAverage() + "n" +
  114. "Grade: " + letterGrade;
  115. System.out.println("Percentage: " + percentage + "n" + "letterGrade: " + letterGrade);
  116. return str;
  117. }
  118.  
  119. import java.util.Scanner;
  120. package student.application;
  121.  
  122. public class StudentApplication {
  123.  
  124. /**
  125. * @param args the command line arguments
  126. */
  127. public static void main(String[] args) {
  128. // TODO code application logic here
  129.  
  130. Scanner reader = new Scanner(System.in);
  131. Student learner = new Student();
  132. String name;
  133. int score;
  134.  
  135. // Asks user for information
  136. System.out.print("What is your name?");
  137. name = reader.nextLine();
  138. learner.setName(name); // sets the name of the student
  139.  
  140. System.out.println("Enter the test scores ");
  141. for (int i = 1; i <= 3; i++) {
  142. score = reader.nextInt();
  143. learner.setScore(i, score);
  144. }
  145.  
  146. // Prints out name, test scores, average
  147. learner.setGrade();
  148. learner.toString();
  149.  
  150.  
  151.  
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement