Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String setGrade() { // returns the letter grade of the student
- if (percentage >= 90) letterGrade = "A";
- else if (percentage <= 90 && percentage >= 80) letterGrade = "B";
- return letterGrade;
- What is your name? John
- Enter the test scores
- 90
- 100
- 90
- Percentage: 93
- letterGrade: null
- public class Student {
- // Instance Variables
- // Each Student object has a name and three test scores
- private String name; // student name
- private int test1; // score for test 1
- private int test2; // score on test 2
- private int test3; // score on test 3
- private int percentage; // score the variable average to calculate grade
- private String letterGrade; // returns the letter grade of the student
- // Constructor method
- public Student() {
- // Initialize a new student's name to the empty string and test
- // scores to zero
- name = "";
- test1 = 0;
- test2 = 0;
- test3 = 0;
- }
- // Other methods
- public void setName (String nm) {
- // Set a student's name
- name = nm;
- }
- public String getName () {
- // Get a student's name
- return name;
- }
- public void setScore (int i, int score) {
- // Set test i to score
- if (i == 1) test1 = score;
- else if (i == 2) test2 = score;
- else test3 = score;
- }
- public int getScore (int i) {
- // Retrieve score i
- if (i == 1) return test1;
- else if (i == 2) return test2;
- else return test3;
- }
- public int getAverage(){
- // Compute and return the average
- int average;
- average = (int) Math.round((test1 + test2 + test3) / 3.0);
- percentage = average;
- return average;
- }
- public int getHighScore() {
- // Determine and return the highest score
- int highScore;
- highScore = test1;
- if (test2 > highScore) highScore = test2;
- if (test3 > highScore) highScore = test3;
- return highScore;
- }
- /*
- New Methods by @SimeonTG
- */
- /**
- *
- *
- */
- public String setGrade() { // returns the letter grade of the student
- if (percentage >= 90) letterGrade = "A";
- else if (percentage <= 90 && percentage >= 80) letterGrade = "B";
- return letterGrade;
- }
- public String toString(){
- // Construct and return a string representation of the student
- String str;
- str = "Name: " + name + "n" + // "n" denotes a new line
- "Test 1: " + test1 + "n" +
- "Test 2: " + test2 + "n" +
- "Test 3: " + test3 + "n" +
- "Average: " + getAverage() + "n" +
- "Grade: " + letterGrade;
- System.out.println("Percentage: " + percentage + "n" + "letterGrade: " + letterGrade);
- return str;
- }
- import java.util.Scanner;
- package student.application;
- public class StudentApplication {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Scanner reader = new Scanner(System.in);
- Student learner = new Student();
- String name;
- int score;
- // Asks user for information
- System.out.print("What is your name?");
- name = reader.nextLine();
- learner.setName(name); // sets the name of the student
- System.out.println("Enter the test scores ");
- for (int i = 1; i <= 3; i++) {
- score = reader.nextInt();
- learner.setScore(i, score);
- }
- // Prints out name, test scores, average
- learner.setGrade();
- learner.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement