Thanks for the quick answer.
k. So what if you already have a constructor to receive student id and stuff?
I haven't been clear enough. Let me specify.
Problem Definition: Create a Grade class(you may choose a different name if you wish,as long as it is descriptive) that has the following properties(create accessor methods for these, but no mutators; this class is immutable and must be encapsulated(an abstract data type)
1. A student ID. Students IDs are digits but not numbers. Student IDS are exactly 4 digits long.
2. A student name
3. An array of quiz scores. Individual scores cannot be less than 0, can exceed 100 but not by "much" and can have decimal fractions. You will not be validation the scores. There is no limit to the number of quizzes a student could have taken.
4. You may add a toString() returning the text within the constraints of good design.
Create a constructor that will receive a student id, name and an array of quiz scores.
5. If any argument is invalid(student ID not exactly 4 digits or a value is empty or null) through an IllegalArgumentException with a message containing the parameter with the error and the nature of error ( For an invalid student Id, include the argument)
Create accessor methods to return the following information.
6. The average quiz score with the lowest quiz score excluded.
7. The grade for the average quiz score with the lowest quiz score excluded. A grade of A is 90% or greater, B is 80% up to 90%, C is 70% upto 80%, D is 60% upto 70%. F is anything else.
Here is my code to this. I haven't finished so any suggestions are appreciated.
Code:
public class Grade
{
private int studentID;
private String name;
private double[] scores;
private double average;
private char letterGrade;
/** This constructor gets the student's id, name and array of quiz scores
*/
public Grade(int studentID, String studentName, double[] quizScores)
throws IllegalArgumentException
{
if( studentID < 0 || studentID > 9999)
{
throw new IllegalArgumentException("Invalid Student Id" + studentID);
}
this.studentID = studentID;
name = studentName;
scores = new double[quizScores.length];
for (int index = 0; index < quizScores.length; index++)
{
if(quizScores[index] < 0.0 || quizScores[index] > 110.0)
{
throw new IllegalArgumentException("Invalid Quiz Score"
+ quizScores);
}
scores[index] = quizScores[index];
}
}
/**This method returns the student ID
@return The student ID
*/
public int getstudentID()
{
return studentID;
}
/**This method returns the student Name
@return The student Name
*/
public String getName()
{
return name;
}
/** This method returns the quiz scores
@return the quiz scores
*/
public double[] getQuizScores()
{
return scores;
}
/** This method computes, returns the average and drops the lowest score
@return The computed average without the lowest score
*/
public double getAverageQuizScore()
{
double total = 0;
double lowest = getLowest();
for(int index = 0; index < scores.length;index++)
{
total += scores[index];
}
total -= lowest;
average = total / (scores.length -1);
return average;
}
/** This method computes the average quiz letter grade
@return The letter grade for average quiz scores minus the lowest score
*/
public char getAverageQuizGrade()
{
if (average < 60)
{
letterGrade = 'F';
}
else if (average < 70)
{
letterGrade = 'D';
}
else if (average < 80)
{
letterGrade = 'C';
}
else if (average <90)
{
letterGrade = 'B';
}
else if (average > 90)
{
letterGrade = 'A';
}
return letterGrade;
}
/** This methods gets the lowest score
@return The lowest quiz Score
*/
private double getLowest()
{
double lowest = scores[0];
for(int index = 1; index < scores.length; index++)
{
if(scores[index] < lowest)
{
lowest = scores[index];
}
}
return lowest;
}
/** This method returns the string representation of object
@return String representation of object
*/
public String toString()
{
String str = "studentID = " + Integer.toString(getstudentID())
+ "; Average Quiz Score = " +
Double.toString(getAverageQuizScore());
return str;
}
}