PDA

View Full Version : Java Help!




pranavss11
Sep 13, 2008, 07:11 PM
Hello,
I am learning programming in college and need some help.

I have to create a Grade class which has a method that gets the Student's ID numbers. The question I have is that, it says the Student IDs are not numbers but digits and the IDs can only be 4 digits long.

How do I go about doing this?

Help is appreciated.

Thanks-

EDIT: Please read last post-



lee1210
Sep 13, 2008, 07:53 PM
This is pretty unclear. a series of digits is a number. They may be indicating that you should store the ID as a string and not a numerical primitive? You should get a better explanation.

-Lee

italiano40
Sep 13, 2008, 11:08 PM
based on what you said would be like this

public Grade{
public void getid(String id){
if(id.length<=4){
system.out.println("Good id");
}
else{
system.out.println("Bad id");
}
}
}

pranavss11
Sep 14, 2008, 02:24 AM
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.
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;
}
}

lee1210
Sep 14, 2008, 10:50 AM
A few things:
ID is still bothering me, as I don't feel the project is quite clear enough how this is to be stored. There are two options in my opinion:
1) Store as an integer as you are doing, and check the bounds. This seems fine. However, any number less than 1000 will need to be 0 padded for output. What to do? If you return an int, you have no control over the output. In your toString method you should at least ensure that this outputs with padding to 4 places. There are a number of ways to do this, if you need help with it post again.
2) Store as a String. You could still accept an int, verify the size, then store it in its 0-padding string form. This way when you return it as a String it is already formatted and ready to go. There's no reason that you need to do math with IDs, so there's no reason you should need them as a numerical data type.

I would say I would prefer 2, because it allows for proper display outside of the class without special work.

Other than that, this looks very good. There is a logic error getting the letter grade. What is the letter grade when the average is exactly 90?

-Lee

P.S. You may want to ask your professor about posting code here. Posting questions is fine, but I wouldn't post more than a few lines of code that you are having trouble with. Since you are pretty much done with the project, if one of your less-than-honest classmates stumbled upon this they could just steal your work. In this case, you may be implicated in cheating even though you didn't intend to share your work.

kcjones
Sep 15, 2008, 02:03 AM
Another couple of things about grading is that (apart from 90 having no grade):

3. Individual scores cannot be less than 0, can exceed 100 but not by "much" and can have decimal fractions.

The way you are calculating the scores doesn't give a proper percentage because scores can be above 100.

Also at the beginning of your

public char getAverageQuizGrade()

You should call

public double getAverageQuizScore()

Because someone using the class will not know that they have to call one before the other.

Even better you should make a void private function called calculateAverageQuizScore() that gets called at the end of your constructor and getAverageQuizScore should just return self.average

I would store the student IDs as Strings because "Students IDs are digits but not numbers" so you cant use ints

SydneyDev
Sep 15, 2008, 06:49 AM
You probably don't need member variables for average and letterGrade - you are calculating them on the fly anyway.