Question

Java: A teacher has five students who have taken four tests. The teacher uses the following...

Java:

A teacher has five students who have taken four tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her four test scores:

Test Score

Letter Grade

90–100

A

80–89

B

70–79

C

60–69

D

0–59

F

Write a class that uses a String array (or an ArrayList object) to hold the five students’ names, an array of five characters to hold the five students’ letter grades, and five arrays of four doubles each to hold each student’s set of test scores. The class should have methods that return a specific student’s name, average test score, and a letter grade based on the average.

Demonstrate the class in a program that allows the user to enter each student’s name and his or her four test scores. It should then display each student’s average test score and letter grade.

Input Validation: Do not accept test scores less than zero or greater than 100.

Homework Answers

Answer #1

The Java Program for taking input for and calculating the average score and letter grades for five students is given below. Comments have been added for ease of understanding. If you have any other queries/doubts, please leave a comment and I will get back to you.

Student_Grades.java

import java.util.Scanner;

public class Student_Grades {

    //declare private variables for student name, letter grades

    //and a 2D 5x4 Matrix for storing the scores of 5 students in 4 subjects

    private String studentNames[];

    private char letterGrades[];

    private double scores[][];

    //Initialize the private variables in default constructor

    public Student_Grades() {

        studentNames = new String[5];

        letterGrades = new char[5];

        scores = new double[5][4];

    }

    //Take user input for each student's name and test scores, after which calculate and show their

    //average score and letter grade

    public void getInput() {

        Scanner in = new Scanner(System.in);

        for(int i = 0; i < 5; i++) {

            System.out.println();

            System.out.println("**Enter data for student "+(i+1)+"***");

            System.out.println("******************************");

            System.out.print("Name : ");

            studentNames[i] = in.next();

            System.out.println("Enter Test scores of four subjects one by one");

            for(int j = 0; j < 4; j++) {

                //Do input validation, use a do while loop to check if the scores entered are in the

                //range 0-100. If not make the user enter input until a vali score is entered

                do {

                    System.out.print("Test "+(j+1)+": ");

                    scores[i][j] = in.nextDouble();

                    if(scores[i][j] < 0 || scores[i][j] > 100)

                        System.out.println("Invalid score. Has to be between 0 and 100. Try again!");

                } while(scores[i][j] < 0 || scores[i][j] > 100);

            }

            //calculate average score and letter grade

            double avgScore = averageScore(scores[i]);

            letterGrades[i] = getLetterGrade(avgScore);

            System.out.println("Average Score : "+avgScore);

            System.out.println("Letter Grade : "+letterGrades[i]);

            System.out.println();

        }

        in.close();

    }

    //takes an double array as its argument and returns the mean score

    private double averageScore(double scores[]) {

        double total = 0;

        for(int i = 0; i < scores.length; i++)

            total += scores[i];

        return total/scores.length;

    }

    //returns the letter grade given the average score

    private char getLetterGrade(double avgScore) {

        if(avgScore >= 90 && avgScore <= 100)

            return 'A';

        else if(avgScore >= 80 && avgScore <= 89)

            return 'B';

        else if(avgScore >= 70 && avgScore <= 79)

            return 'C';

        else if(avgScore >= 60 && avgScore <= 69)

            return 'D';

        else

            return 'F';

    }

    //returns the name of the student at index ind if ind is valid (between 0 and 4)

    //if ind is invalid it returns an error message

    public String getStudentName(int ind) {

        if(ind >= 0 && ind < 5) {

            return studentNames[ind];

        }

        else

            return "ERROR: Index Out of range";

    }

    //returns the average score of the student at index ind if ind is valid (between 0 and 4)

    //if ind is invalid it returns -1.0

    public double getStudentAvgScore(int ind) {

        if(ind >= 0 && ind < 5) {

            return averageScore(scores[ind]);

        }

        else {

            System.out.println("ERROR: Index Out of range");

            return -1.0;

        }

    }

    //returns the letter grade of the student at index ind if ind is valid (between 0 and 4)

    //if ind is invalid it returns 'X'

    public char getStudentLetterGrade(int ind) {

        if(ind >= 0 && ind < 5) {

            return getLetterGrade(averageScore(scores[ind]));

        }

        else {

            System.out.println("ERROR: Index Out of range");

            return 'X';

        }

    }

    public static void main(String args[]) {

        System.out.println();

        System.out.println("Creating a Student_Grades Object");

        Student_Grades sg = new Student_Grades();

        System.out.println("Now taking user input...");

        sg.getInput();

        System.out.println("Testing the public methods");

        System.out.println("Get the details of student 1");

        System.out.println("Name: "+sg.getStudentName(0));

        System.out.println("Average Score: "+sg.getStudentAvgScore(0));

        System.out.println("Letter Grade: "+sg.getStudentLetterGrade(0));

        System.out.println("Get the details of student 4");

        System.out.println("Name: "+sg.getStudentName(3));

        System.out.println("Average Score: "+sg.getStudentAvgScore(3));

        System.out.println("Letter Grade: "+sg.getStudentLetterGrade(3));

        System.out.println("Get the details of student 6");

        System.out.println("Name: "+sg.getStudentName(5));

        System.out.println("Average Score: "+sg.getStudentAvgScore(5));

        System.out.println("Letter Grade: "+sg.getStudentLetterGrade(5));

    }

}

Sample Run:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ needs to output all of the name of the students not just first Write a...
C++ needs to output all of the name of the students not just first Write a program which uses an array of string objects to hold the five students' full names (including spaces), an array of five characters to hold each student's letter grade, a multi-dimensional array of integers to hold each of the five student's four test scores, and an array of five doubles to hold each student's average test score. The program will use a function to allow...
Show work a) A teacher informs her computational physics class (of 500+ students) that a test...
Show work a) A teacher informs her computational physics class (of 500+ students) that a test was very difficult, but the grades would be curved. Scores on the test were normally distributed with a mean of 25 and a standard deviation of 7.3. The maximum possible score on the test was 100 points. Because of partial credit, scores were recorded with 1 decimal point accuracy. (Thus, a student could earn a 25.4, but not a 24.42.) The grades are curved...
Students in a chemistry class convince their teacher to use the following "group grading" scenario. Students...
Students in a chemistry class convince their teacher to use the following "group grading" scenario. Students will all take the exam on their own; however, the grade they receive will be the mean of their test score with 4 other randomly selected classmates. Assume that the test scores for this particular exam are normally distributed with a mean of 74 and a standard deviation of 12 points. You need an 80 or better on this exam. What is the probability...
PLEASE USE IDLE PYTHON: Question (Arrays/Lists) A teacher uses 2   arrays (or Lists) to keep track...
PLEASE USE IDLE PYTHON: Question (Arrays/Lists) A teacher uses 2   arrays (or Lists) to keep track of his students. One is used to store student names and the other stores his grade (0-100). Write a program to create these two arrays (or lists) that are originally empty, and do the following using a menu: 1. Add a new student and his/her grade. 2. Print the name and grade of all current students, one student per line. 3. Display the number...
A teacher informs his psyhcology class (of 500+ students) that a test was very difficult, but...
A teacher informs his psyhcology class (of 500+ students) that a test was very difficult, but the grades would be curved. Scores on the test were normally distributed with a mean of 28 and a standard deviation of 9.2. The maximum possible score on the test was 100 points. Because of partial credit, scores were recorded with 1 decimal point accuracy. (Thus, a student could earn a 28.3, but not a 27.33.) The grades are curved according to the following...
A teacher has taken attendance of her class of five students for the entire semester and...
A teacher has taken attendance of her class of five students for the entire semester and found the number of days with each level of attendance. Her records show the following:                                      # of Students attending class: 1      2      3     4      5 Days in the semester with this attendance level (out of 30 days):            4      2     10    4     10 a) What’s the probability of there being 3 or more students on any random day? b) What is the expected number...
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students....
JAVA Question a) Read a file named testScoreIn.txt representing test scores for a class of students. Each record contains an id number and three test scores (Note: the test scores are integers). For example, a student record might look like: BK1234 87 72 91 b) Without using an array, read each student’s information, one record at a time and compute the student’s average (as a double). c) Write to an output file, testScoreOut.txt, as a neatly formatted table with column...
You are to write a program that will process students and their grades. For each student...
You are to write a program that will process students and their grades. For each student the program will read in a student’s name. It should also read in 10 test scores for the student and calculate his or her average. You must read the scores using a loop. The program should output the student’s name, average and letter grade. The average should be displayed accurate to 1 decimal digit. Letter grades are assigned based on the scale: 90-100    A...
Professor Moriarty has never taken a formal statistics course; however, he has heard about the bell-shaped...
Professor Moriarty has never taken a formal statistics course; however, he has heard about the bell-shaped curve and has some knowledge of the Empirical Rule for normal distributions. Professor Moriarty teaches as Honors Quantum Physics class in which he grades on the bell curve. He assigns letter grades to his students' tests by assuming a normal distribution and utilizing the Empirical Rule. The Professor reasons that if IQ and SAT scores follow a normal distribution, then his students' scores must...
7.1 Regional College increased their admission standards so that they only admit students who have scored...
7.1 Regional College increased their admission standards so that they only admit students who have scored in the 80th percentile or higher (top 20%) of all scores on the SAT. What SAT cutoff will the school use this year? (Recall that for the SAT μ = 500, σ = 100, and the scores are normally distributed) 7.2 Even with the increase in standards, the Registrar at Regional College notices that people who don’t meet the minimum continue to apply for...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT