Answer using basic programming beginner knowledge pls Otherw
Answer using basic programming beginner knowledge pls........... Otherwise let someone more explicit respond.
pls do post your program output........ thanks
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 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, the 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.
MY SOLUTION SO FAR
1 /* Write a class that uses and
2 The class should have methods thatreturn a specific student’s name,
3 the average test score, and a letter grade based on the average.
4
5 Demonstrate the class in a program that
6 and hisor her four test scores. It should then display each student’s average
7 test score and letter grade.
8 */
9
10 import java.util.Scanner;
11
12 public class GradeBook
13 {
14 // The Scores field is a variable
15 // that will reference an array
16 // of test scores.
17 private double[] scores;
18
19 /** Constructor
20 @param scoreArray An array of testscores.
21 */
22 public GradeBook(double[] scoreArray)
23 {
24 // Assign the array argument to the testScores field.
25 scores = scoreArray;
26 }
27
28 //create a scanner Object to read user input.
29 Scanner keyboard = new Scanner(System.in);
30
31 //allows the user to enter each student’s name
32 public String getStudentName()
33 {
34 String[] name = {\"\"};
35 String studentName = (\"\");
36
37 //loop through for five students names input.
38 for(int i = 0; i < name.length + 1; i++)
39 {
40 System.out.print(\"Enter student name #\" + (i+1) + \":\");;
41 studentName = keyboard.nextLine();
42 }
43
44 // Create the account.
45 array[i] = new GradeBook(studentName);
46 return studentName;
47 }
48
49 //user input four test scores for each student.
50 //display each student’s average test score.
51 public double getAverageTestScore(){
52 double totalTestScore = 0;
53 double score;
54
55 //ask user for four test scores using a for loop
56 for(int i = 0; i < scores.length; i++)
57 {
58 System.out.print(\"Enter Test Score #\" + (i+1) + \":\");
59 score = keyboard.nextDouble();
60
61
62 //Input Validation: Do not accept test scores less than zero or greater than 100.
63 if(score < 0 || score > 100){
64 System.out.println(\"ERROR: You must enter a test score between -1 and 101. \");
65
66 System.out.print(\"Enter Test Score #\" + (i+1) + \":\");
67 score = keyboard.nextDouble();
68 }
69 totalTestScore += score;
70 }
71 return (totalTestScore / scores.length);
72 }
73
74 //letter grade.
75 public char getTestGrade()
76 {
77 //An array of five characters to hold the five students’ letter grades.
78 char[] letterGrades = {\'A\',\'B\',\'C\',\'D\',\'F\'};
79
80 double averageTestScore;
81 averageTestScore = getAverageTestScore();
82
83 if(averageTestScore <= 100 || averageTestScore >= 90)
84 {
85 return letterGrades[0];
86 }
87
88 else if(averageTestScore < 90 || averageTestScore >= 80)
89 {
90 return letterGrades[1];
91 }
92
93 else if(averageTestScore < 80 || averageTestScore >= 70)
94 {
95 return letterGrades[2];
96 }
97
98 else if(averageTestScore < 70 || averageTestScore >= 60)
99 {
100 return letterGrades[3];
101 }
102 return letterGrades[4];
103 }
104 }
105
1 import java.util.*;
2 public class GradeBookDemo
3 {
4 public static void main(String[] args)
5 {
6 final int numScore = 4;
7 double[] testScores= new double[numScore];
8 double[] averageTestScoreStudent1 = new double[numScore];
9 // double[] testScoresStudent2 = new double[numScore];
10 // double[] testScoresStudent3 = new double[numScore];
11 // double[] testScoresStudent4 = new double[numScore];
12 // double[] testScoresStudent5 = new double[numScore];
13
14 String studentName = (\"\");
15 double score;
16 double TotalScore, studentTestGrade;
17
18 //A String array to hold the five students’ names
19 String[] names = new String[5];
20
21 //create a reference to the Grade book class passing testScores array as an argument to the constructor.
22 GradeBook grade = new GradeBook(testScores);
23 //create a new scanner object to hold user input.
24 Scanner keyboard = new Scanner(System.in);
25 //loop through for five students names input.
26 System.out.println(\"Enter each student names \");
27
28 //ask for each student\'s test scores.
29 for(int i = 0; i < names.length + 1; i++)
30 {
31 names[i] = grade.getStudentName();
32 System.out.println(\"Enter four test scores for \" + names[i]);
33 averageTestScoreStudent1[i] = grade.getAverageTestScore();
34 System.out.println(\"Average Test Score for \" + names[i] + \" is \" + averageTestScoreStudent1[i]);
35 studentTestGrade = grade.getTestGrade();
36 System.out.println(\"Test Grade for \" + names[i] + \" is \" + studentTestGrade);
37
38 }
Solution
Please read the Explaination and comments for understanding:
public class MyClass {
private String [] names; //
private char [] grades;
private int [][] scores;
public MyClass() { //To initialize the above instance variables for 5 students
names=new String[5];
grades=new char[5];
scores=new int[5][4];
}
public String[] getNames() {
return names;
}
public char[] getGrades() {
return grades;
}
public int[][] getScores() {
return scores;
}
public void displayDetails() {//To display details of 5 students
int avg,sum;
System.out.println(\"\ \ ************ Details Entered Are ************\");
for(int i=0;i<5;i++){
avg=0;
sum=0;
for(int j=0;j<4;j++){
sum+=scores[i][j];
}
//sum contains total of 4 scores
avg=sum/4; //Calculating average using sum
System.out.println(\"Student Name:\"+names[i]+\" Average Score:\"+avg+\" Grade Scored:\"+grades[i]);
}
}
public String getStudentByName(String name) { //As specified,a method to return details of a given Student
for (int i = 0; i < names.length; i++) {
if(names[i].equalsIgnoreCase(name)){ //If the Student name matches in the stored names
float avg=0.0f;
for (int j = 0; j < 4; j++) {
avg+=scores[i][j];
}
avg=avg/4; //Calculated the average
return \"Student Name:\"+names[i]+\" Average Score:\"+avg+\" Grade:\"+grades[i]; //It will return the details as the student was found
}
}
return \"Student not available\"; //If Student is not found in the Stored array
}
}
Testing CLASS:
package Nov1;
import java.util.Scanner;
public class ClassDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MyClass c1=new MyClass();
//For local purpose
int avg=0,sum=0;
//To get instance variables of MyClass object
String [] names;
char [] grades;
int [][] scores;
grades=c1.getGrades();
names=c1.getNames();
scores=c1.getScores();
//To get details from User for 5 Students:
for(int i=0;i<5;i++){
System.out.println(\"Enter details of Student \"+(i+1)+\":-\");
System.out.println(\"Enter name:\");
names[i]=sc.next();
avg=0;
sum=0;
int score=0;
for(int j=0;j<4;j++){
System.out.println(\"Enter Score:\"+(j+1));
score=sc.nextInt();
if(score <0 || score >100){ //Validating Score
System.out.println(\"Invalid Score try again...\");
j--; //Setting j back by 1 as this was wrong entry
continue;
}
scores[i][j]=score;
sum+=score;
}
//sum contains total of 4 scores
avg=sum/4; //Calculating average using sum
//To store the grade for respective student
if(avg>=90 && avg<=100){
grades[i]=\'A\';
}else if(avg>=80 && avg<=89){
grades[i]=\'B\';
}else if(avg>=70 && avg<=79){
grades[i]=\'C\';
}else if(avg>=60 && avg<=69){
grades[i]=\'D\';
}else if(avg>=0 && avg<=59){
grades[i]=\'F\';
}
}
//Now to display the details to user
c1.displayDetails();
}
}
Output:
Enter details of Student 1:-
Enter name:
A
Enter Score:1
50
Enter Score:2
60
Enter Score:3
50
Enter Score:4
60
Enter details of Student 2:-
Enter name:
B
Enter Score:1
48
Enter Score:2
90
Enter Score:3
60
Enter Score:4
70
Enter details of Student 3:-
Enter name:
C
Enter Score:1
45
Enter Score:2
75
Enter Score:3
89
Enter Score:4
14
Enter details of Student 4:-
Enter name:
D
Enter Score:1
448
Invalid Score try again...
Enter Score:1
48
Enter Score:2
56
Enter Score:3
23
Enter Score:4
95
Enter details of Student 5:-
Enter name:
E
Enter Score:1
98
Enter Score:2
55
Enter Score:3
66
Enter Score:4
87
************ Details Entered Are ************
Student Name:A Average Score:55 Grade Scored:F
Student Name:B Average Score:67 Grade Scored:D
Student Name:C Average Score:55 Grade Scored:F
Student Name:D Average Score:55 Grade Scored:F
Student Name:E Average Score:76 Grade Scored:C
EXPLAINATION:
Created a class named MyClass which contains 3 instance varibles which are arrays. Constructor of this class initialize these to size 5 (for 5 students). As mentioned there is a method to get details of a Student by name and a method to display all details of 5 students.
In demo class there are same 3 local variables which points to the instance variables of MyClass initialized by getters. Then a for loop is used for 5 students. In All three arrays names[],grades[] and scores[][], single index say i in all arrays represents the same student. For example names[1], grades[1], scores[1][] represents details of student 2. So the for loop is used 5 times to enter details and the variable \'i\' is used to set details in all arrays at the same indexes as all details are of a single student. For scores there is another loop used to get 4 scores from user (valid scores). Validation will work untill user enters a valid score. Then the average is calculated at the same time as grade is also needs to be given. Once average is calculated the grade is assigned to grades[i]. An addition method is created in MyClass to display details of all students.
Note: Change in the Arrays get from c1 object will reflect in c1 object as these are not copied only new refrences are given in main().
Hope this helps. For any query please comment.






