I am to modify a question to that it drops the lowest score when determining the
ID: 3818819 • Letter: I
Question
I am to modify a question to that it drops the lowest score when determining the test scoer average and letter grade. I have my program running however it does not drop the lowest score and the grade is messes up.
my program
public class MyClass {
private String[] names; // names of the studi (sorry a fall back on a former life)
private char[] grades; // self explanatory
private int[][] scores; // one for the gipper
// To my class and to initialize the new vaiables
public MyClass() {
names = new String[5];
grades = new char[5];
scores = new int[5][4];
}
// to return names
public String[] getNames() {
return names;
}
// to return the grade... but not stats
public char[] getGrades() {
return grades;
}
// to return the scores
public int[][] getScores() {
return scores;
}
// To display details of 5 students
public void displayDetails() {
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
// Calculating average using sum
avg = sum / 4;
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 } }
}
public void setNames(String names, int i) {
this.names[i] = names;
}
public void setGrades(char grade, int i) {
this.grades[i]= grade;
}
public void setScores(int scores, int i, int j) {
this.scores[i][j] = scores;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import java.util.Scanner; // for scanner import still not worthy
import java.util.Collections; // wahoo a new util i found that did my trick
public class ClassDemo {
// i had have not idea what i should call this
public static void main(String[] args) {
// for the scanner class
Scanner sc = new Scanner(System.in);
MyClass c1 = new MyClass(); // For local purpose
double avg, sum; // 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();
c1.setNames(names[i], i);//setting the name;
avg = 0;
sum = 0;
int low = 100;
int score = 0;
for (int j = 0; j < 4; j++) {
System.out.println("Enter Score:" + (j + 1));
score = sc.nextInt();
// Validating Score reallly had an issue with this not sure why
if (score < 0 || score > 100) {
System.out.println("Invalid Score try again...");
j--; // Setting j back by 1 as this was wrong entry - found
// out this was a big issue... dumb!
continue;
} else {
c1.setScores(score, i, j);//sets scores
if(low>score){
low=score;
System.out.println(low);
}
}
}
scores=c1.getScores();
avg=calculateAvg(scores[i], low);
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';
}
c1.setGrades(grades[i], i);
}
c1.displayDetails();
}
public static double calculateAvg(int inputs[], int lowNum) {
double sum = 0;
double avg = 0;
int i = 0;
// Calcuates the average of the array list with the lowest numbers
// dropped
for (i = 0; i < inputs.length; i++) {
if (inputs[i] > lowNum) {
sum = sum + inputs[i];
}
}
avg = (sum / inputs.length);
//System.out.println(avg);
return avg;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
output
Enter details of Student 1:-
Enter name:
Ann
Enter Score:1
98
98
Enter Score:2
98
Enter Score:3
15
15
Enter Score:4
78
Enter details of Student 2:-
Enter name:
tes
Enter Score:1
56
56
Enter Score:2
17
17
Enter Score:3
56
Enter Score:4
15
15
Enter details of Student 3:-
Enter name:
tet
Enter Score:1
15
15
Enter Score:2
16
Enter Score:3
16
Enter Score:4
16
Enter details of Student 4:-
Enter name:
ter
Enter Score:1
18
18
Enter Score:2
15
15
Enter Score:3
18
Enter Score:4
15
Enter details of Student 5:-
Enter name:
teg
Enter Score:1
10
10
Enter Score:2
4
4
Enter Score:3
4
Enter Score:4
4
************ Details Entered Are ************
Student Name:Ann Average Score:72 Grade Scored:D
Student Name:tes Average Score:36 Grade Scored:F
Student Name:tet Average Score:15 Grade Scored:F
Student Name:ter Average Score:16 Grade Scored:F
Student Name:teg Average Score:5 Grade Scored:F
----jGRASP: operation complete.
Explanation / Answer
Note:if you have any doubt comment
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.