We are going to redesign the grading program as an object-oriented design. We wi
ID: 3551068 • Letter: W
Question
We are going to redesign the grading program as an object-oriented design. We will start with a simple Student class. The student will have a name, an id (as a String) and a set of grades (as doubles).
The Student class should include:
* a constructor (which takes a name and id as strings),
* getters and setters for the name and id,
* addGrade method : void
*which takes a double,
*printGrades method : void
*which prints the name, a colon, and the grades separated by commas,
* getAdjustedAverage method : double
* computes the average, dropping the lowest score
*getLetterGrade method : char
* compute the letter grade based on the adjusted average and a straight scale
* toString() method : String
* prints the
Explanation / Answer
Your code.
It is not asked to write main method for it. If needed, let me know. Here is the desired functionlities. 100% working.
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
public class Student {
private String name;
private String id;
private HashSet<Double> grades;
//constructor
public Student(String name, String id) {
super();
this.name = name;
this.id = id;
}
//getters setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
//addGrade method which takes a double
public void addGrade(double grade){
grades.add(grade);
}
//*printGrades method : void
public void printGrades(){
String gradesPopulated = null;
Iterator <Double>iterator = grades.iterator();
while (iterator.hasNext()){
gradesPopulated+= iterator.next() + ", ";
}
gradesPopulated = gradesPopulated.substring(0, gradesPopulated.length()-1);
System.out.println(name+":"+gradesPopulated);
}
// getAdjustedAverage method : double
public double getAdjustedAverage(){
Double minValue = Collections.min(grades);
Iterator <Double>iterator = grades.iterator();
int counterOfSubjects = 0;
double sumOfScores = 0;
while (iterator.hasNext()){
if(iterator.next() != minValue){
sumOfScores += iterator.next();
counterOfSubjects++;
}
}
double average = sumOfScores/counterOfSubjects;
return average;
}
//getLetterGrade method : char
public char getLetterGrade(double average){
if(average >= 90 && average < 100)
return 'A';
else if (average >= 80)
return 'B';
else if (average >= 70)
return 'C';
else if (average >= 60)
return 'D';
else
return 'F';
}
@Override
public String toString() {
return "Student [name=" + name + ", id=" + id + ", grades=" + grades
+ "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.