We are going to redesign the grading program as an object-oriented design. We wi
ID: 3551069 • 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
Note: Now you can copu this code.
/* File: Student.java */
/* Student class */
import java.util.ArrayList;
public class Student
{
/* The student will have a name, an id (as a String) and a set of grades (as doubles). */
private String name;
private String id;
private ArrayList<Double> setOfGrades;
/* a constructor (which takes a name and id as strings) */
public Student(String sName, String sID)
{
name = sName;
id = sID;
setOfGrades = new ArrayList<Double>();
}
/* getters and setters for the name and id */
public String getName()
{
return name;
}
public String getID()
{
return id;
}
public void setName(String sName)
{
name = sName;
}
public void setID(String sID)
{
id = sID;
}
/* addGrade method : void
which takes a double, */
public void addGrade(double grade)
{
setOfGrades.add(grade);
}
/* printGrades method : void
which prints the name, a colon, and the grades separated by commas */
public void printGrades()
{
System.out.print(name + ": ");
for(int i = 0; i < setOfGrades.size(); i++)
{
if(i != setOfGrades.size() - 1)
System.out.print(setOfGrades.get(i) + ", ");
else
System.out.println(setOfGrades.get(i));
}
}
/* getAdjustedAverage method : double
computes the average, dropping the lowest score */
public double getAdjustedAverage()
{
// find the lowest score
double lowest = setOfGrades.get(0);
int lowestIndex = 0;
for(int i = 1; i < setOfGrades.size(); i++)
{
if(setOfGrades.get(i) < lowest)
{
lowest = setOfGrades.get(i);
lowestIndex = i;
}
}
/* remove the lowest score */
setOfGrades.remove(lowestIndex);
/* now calculate the sum for remaining */
double sum = 0;
for(int i = 0; i < setOfGrades.size(); i++)
sum += setOfGrades.get(i);
/* now calculate the adjusted average for remaining */
double adjustedAverage = 0;
if(setOfGrades.size() != 0)
adjustedAverage = sum / setOfGrades.size();
return adjustedAverage;
}
/* getLetterGrade method : char
compute the letter grade based on the adjusted average and a straight scale */
public char getLetterGrade()
{
double avgGrade = getAdjustedAverage();
char charGrade;
if(avgGrade >= 90)
charGrade = 'A';
else if(avgGrade >= 80)
charGrade = 'B';
else if(avgGrade >= 70)
charGrade = 'C';
else if(avgGrade >= 60)
charGrade = 'D';
else // if avgGrade < 60
charGrade = 'F';
return charGrade;
}
/* toString() method : String
Prints the
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.