Reputation: 11
I am on my week four of learning Java online and have an assignment to create a grading scale using enumerations.
I have created the grading scale enum but am completely lost on how to allow a user to input a numeric value to then output the letter value. Each attempt, it either tells me I can't use return function with what I've chosen or system.out.println doesn't want to accept my values either. I've decided to completely scrap my class file and restart, but have no idea what I've done wrong. Here is my enum code:
public enum Grade {
A(95-100, "A"),
AMINUS(92-94, "A-"),
BPLUS(89-91, "B+"),
B(86-88, "B"),
BMINUS(83-85, "B-"),
CPLUS(80-82, "C+"),
C(77-79, "C"),
CMINUS(74-76, "C-"),
DPLUS(71-73, "D+"),
D(68-70, "D"),
DMINUS(65-67, "D-"),
F(1-64, "F"),
FA(0, "FAILURE TO APPEAR");
private final String gradeText;
private Grade(int gradeValue, String gradeText) {
this.gradeText = gradeText;
}
public String printGrade() {
return gradeText;
}
}
Upvotes: 1
Views: 2206
Reputation: 18255
public enum Grade {
A("A", 95, 100),
AMINUS("A-", 92, 94),
BPLUS("B+", 89, 91),
B("B", 86, 88),
BMINUS("B-", 83, 85),
CPLUS("C+", 80, 82),
C("C", 77, 79),
CMINUS("C-", 74, 76),
DPLUS("D+", 71, 73),
D("D", 68, 70),
DMINUS("D-", 65, 67),
F("F", 1, 64),
FA("FAILURE TO APPEAR", 0, 0);
private final String text;
private final int min;
private final int max;
Grade(String text, int min, int maxScore) {
this.text = text;
this.min = min;
this.max = maxScore;
}
public String getText() {
return text;
}
public static Grade parseScore(int score) {
for (Grade grade : values())
if (score >= grade.min && score <= grade.max)
return grade;
throw new EnumConstantNotPresentException(Grade.class, String.valueOf(score));
}
}
And now you can convert score into Gradle
:
Grade grade = Grade.parseScore(85); // BMINUS
Upvotes: 0
Reputation: 37
The constructor won't work the way you're using it. If you want to add values to an enum type, as you've done here, you need to set those values in the constructor.
An enum type only needs a constructor for that reason, and the constructor can never be used by another java class.
Since there is no easy range class in Java, I suggest you define your minimum and maximum values as seperate parameters:
public enum Grade {
A(95, 100, "A")
// and so on
String gradeText;
int gradeMinPoints;
int gradeMaxPoints;
// ...
}
In your constructor, you need to assign those values:
private Grade (int minimum, int maximum, String text) {
this.gradeMinPoints = minimum;
this.gradeMaxPoints = maximum;
this.gradeText = text;
}
You can access the variables through additional functions, and declare static functions that return the 'grade' enum type depending on the parameters you provide. The other answers to this post provide much better functions than I could have written, so I'll refer you to those.
Upvotes: 0
Reputation: 591
I guess you are trying to achieve something like this:
public enum Grade {
A(95, 100, "A"),
AMINUS(92, 94, "A-"),
BPLUS(89, 91, "B+"),
B(86, 88, "B"),
BMINUS(83, 85, "B-"),
CPLUS(80, 82, "C+"),
C(77, 79, "C"),
CMINUS(74, 76, "C-"),
DPLUS(71, 73, "D+"),
D(68, 70, "D"),
DMINUS(65, 67, "D-"),
F(1, 64, "F"),
FA(0, 0, "FAILURE TO APPEAR");
private final String gradeText;
private final int lower;
private final int higher;
private Grade(int lower, int higher, String gradeText) {
this.gradeText = gradeText;
this.lower = lower;
this.higher = higher;
}
public String getGradeText() {
return this.gradeText;
}
public static Grade getGrade(int points) {
for (Grade g : Grade.values()) {
if (g.lower <= points && points <= g.higher) {
return g;
}
}
return null;
}
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
System.out.println(Grade.getGrade(i));
}
}
}
I've replaced your "range" definition with two separate parameters, stored them into the enum fields and added a getGrade(int points)
method, that returns the Grade
enum item corresponding with given number of points. Should you enter any number not within the range from 0
to 100
, the method returns null. The main
method is there only to complete this simple proof of concept and an example on how to use the getGrade(int)
method.
Upvotes: 0
Reputation: 1291
You need to define the boundaries as separate fields for the compiler to understand. Probably you are looking for an Enum like this:
public enum Grade {
A("A", 95, 100),
AMINUS("A-", 92, 94);
// DEFINE ALL YOUR GRADES HERE
String grade;
int gradeMin;
int gradeMax;
Grade(String grade, int gradeMin, int gradeMax) {
this.grade = grade;
this.gradeMin = gradeMin;
this.gradeMax = gradeMax;
}
/**
* @return the grade
*/
public String getGrade() {
return grade;
}
/**
* @return the gradeMin
*/
public int getGradeMin() {
return gradeMin;
}
/**
* @return the gradeMax
*/
public int getGradeMax() {
return gradeMax;
}
public static Grade getGrade(int marks) {
for (Grade g : values()) {
if (marks >= g.getGradeMin() && marks <= g.getGradeMax()) {
return g;
}
}
return null;
}
}
Then you can use this in your class to get the Grade as
public class GradeRetriever {
public static void main(String... args) {
System.out.println(Grade.getGrade(92).getGrade());
}
}
Upvotes: 0
Reputation: 727
I think this approach could help you.
Instead of trying to store the range in the enum, store the maximum value that could still be considered that grade, for each grade. Then you can retrieve actual letter grades with the help of a static method:
public enum Grade {
A(100, "A"), AMINUS(94, "A-"), BPLUS(91, "B+"), B(88, "B"), BMINUS(85, "B-"),
CPLUS(82, "C+"), C(79, "C"), CMINUS(76, "C-"), DPLUS(73, "D+"), D(70, "D"),
DMINUS(67, "D-"), F(64, "F"), FA(0, "FAILURE TO APPEAR");
private final int gradeValue;
private final String gradeText;
private Grade(int gradeValue, String gradeText) {
this.gradeValue = gradeValue;
this.gradeText = gradeText;
}
public static Grade getGrade(int value) {
Grade grade = FA;
for(Grade g : values()) {
if(value <= g.gradeValue) {
grade = g;
}
}
return grade;
}
public String printGrade() {
return gradeText;
}
}
Testing it out:
System.out.println(Grade.getGrade(99).printGrade());
System.out.println(Grade.getGrade(80).printGrade());
System.out.println(Grade.getGrade(88).printGrade());
System.out.println(Grade.getGrade(75).printGrade());
gives us this:
A
C+
B
C-
Upvotes: 1