Reputation: 380
I have this assignment that I need use a static method to get student IDs then within that static method evaluate them if the students have passed or failed their classes. It is a bit challenging for me because the static method should only take one argument.
Can this be accomplished by not changing private variables to private static variables?
Any direction is much appreciated.
import java.util.ArrayList;
public class Grade {
public static void main(String[] args) {
ArrayList<GradeDetail> gradeList = new ArrayList<GradeDetail>() ;
System.out.println("Student ID: ");
for(String s : args) {
boolean match = false;
for(GradeDetail grade : GradeDetail.values()) {
if(s.equals(grade.getCode())) {
gradeList.add(grade);
match = true;
}
}
if(!match) {
System.out.printf("unknown student ID entered!");
}
}
System.out.println("Students who passed: ");
//Some function here
System.out.println("Students who failed: ")
//Some function here
return;
}
}
enum GradeDetail {
JOHN (101, 90)
, ROB (102, 50)
, JAMES (103, 55)
;
private final int studentID;
private final int studentGrade;
GradeDetail(int id, int sGrade) {
studentID = id;
studentGrade = sGrade;
}
public int getID() {return studentID;}
public int getGrade() {return studentGrade;}
}
I honestly don't know how to go about this..
Upvotes: 1
Views: 4241
Reputation: 8009
Enums are for Static data such as the value of the grade A, B, C. You would never store transient values like John's grade in an Enum. Why don't you try using the enum for the static values.
enum GradeRange {
A (100, 90),
B (89, 80),
C (79, 70);
private final int high;
private final int low;
GradeRange(int high, int low) {
high = high;
low = low;
}
public GradeRange getGrade(int percent) {
for (GradeRange gradeRange : GradeRange.values() {
if (percent <= high && percent >= low)
return gradeRange;
}
}
}
PS - I did not test this code
Upvotes: 4
Reputation: 20163
It would be appropriate to store your data in a class like
class GradeDetail {
public final String sName ;
public final int iId ;
public final int iGrade;
public GradeDetail(String s_name, int i_id, int i_grade) {
sName = s_name;
iId = i_id;
iGrade = i_grade;
}
}
You would then create instances with
GradeDetail gdJohn = new GradeDetail("John", 101, 90)
and access it, for example, with
gdJohn.iGrade;
This should hopefully get you started on the right path.
Upvotes: 2