Home › Forums › Computers, Games and Technology › More Java Fun
This topic contains 11 replies, has 3 voices, and was last updated by
JollyMisanthrope 3 years, 12 months ago.
- AuthorPosts
This time I just have a small section of code that is confusing me. Everything else works fine. Basically I’m creating a Java class that deals with taking in quiz and test scores and outputting the individual grades for each, the final grade (as a percentage), and a letter grade A,B,C,D,F. If I comment out this code in particular and run the rest it does everything perfectly. But for some reason it gives me an error that says it isn’t returning a “char” value, which would be the letter grade. Does it need a final else statement at the end?
public char getLetterGrade() { double percentGrade = this.getPercentGrade(); if(percentGrade >= 0 && percentGrade <60) { return 'F'; } else { if(percentGrade >= 60.0 && percentGrade <70.0) { return 'D'; } else { if(percentGrade >= 70.0 && percentGrade <80.0) { return 'C'; } else { if(percentGrade >= 80.0 && percentGrade <90.0) { return 'B'; } else { if(percentGrade >= 90.0 && percentGrade <=100.0) { return 'A'; } } } } } }The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.I haven’t mess around with Java a lot but try default return of Z.
A MGTOW is a man who is not a woman's bitch!
That did the trick. Just needed to add one more else with just a return. Cheers!
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.You could also do it like this:
public char getLetterGrade() { double percentGrade = this.getPercentGrade(); if(percentGrade <60) { return 'F'; } else if (percentGrade >= 60 && percentGrade <70.0) { return 'D'; } else if(percentGrade >= 70.0 && percentGrade <80.0) { return 'C'; } else if(percentGrade >= 80.0 && percentGrade <90.0) { return 'B'; } else//This will return an A if the student has more than 90%. { return 'A'; } }OK one more question. Now I need to confirm if the grade information I entered matches another instance of grade with either similar or different numbers.
public class TestGrade { public static void main(String[] args) { Grade grade1 = new Grade(); grade1.setQuiz1(10); grade1.setQuiz2(10); grade1.setQuiz3(10); grade1.setmidTerm(100); grade1.setfinalGrade(100); System.out.println("The percent grade is: " + grade1.getPercentGrade() + "\n" + "The letter Grade is: " + grade1.getLetterGrade()); System.out.println(grade1); Grade grade2 = new Grade(10,10,10,100,100); System.out.println(grade1.equals(grade2)); // prints true } } ___________________________________ public boolean equals(Object obj) { if(!(obj instanceof Grade)) { return false; } Grade other = (Grade)obj; return this.getPercentGrade() == other.getPercentGrade(); }As of now I always get false as a printout.
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.I am not sure why your code always returns false.
public boolean equals(Object obj) { if(!(obj instanceof Grade)) { return false; } Grade other = (Grade)obj; return this.getPercentGrade() == other.getPercentGrade(); }seems to work fine for me.
If you try this:return this.getPercentGrade().equals(other.getPercentGrade());
what happens?My guess is that your getPercentGrade() method does not simply return and Integer like this?
public int getPercentGrade() { return finalGrade; }Here’s the method for getPercentGrade
public double getPercentGrade() { double quizWeight = (quiz1 + quiz2 + quiz3)*25.0/30.0; double midWeight = (midTerm) * (35.0/100.0); double finalWeight = (finalGrade) * (40.0/100.0); Math.round(quizWeight); Math.round(midWeight); Math.round(finalWeight); return quizWeight + midWeight + finalWeight; }The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.Says: Cannot invoke equals (double) on the primitive type double.
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.Says: Cannot invoke equals (double) on the primitive type double.
Ok that was to be expected.
If i try your code it works fine for me.
public static void main(String[] args) { // TODO Auto-generated method stub Grade grade1 = new Grade(); grade1.setQuiz1(10); grade1.setQuiz2(10); grade1.setQuiz3(10); grade1.setMidTerm(100); grade1.setFinalGrade(100); Grade grade2 = new Grade(10,10,10,100,100); System.out.println(grade1.getPercentGrade()); System.out.println(grade2.getPercentGrade()); System.out.println(grade1.equals(grade2)); }returns:
100.0
100.0
trueCould it be the case, that java does not use your custom “equals” method but the standard “equals” method? In that case it would return false.
I do not know why it would do that, though. As long as “equals” is defined inside the “Grade” class it should be using that one.For some reason the grade percentage isn’t rounding to two decimal places. Any advice? Is Math.round the wrong method to use?
public class Grade extends Object { private double quiz1; private double quiz2; private double quiz3; private double midTerm; private double finalGrade; public Grade () { this(0,0,0,0,0); } public Grade (double quiz1, double quiz2, double quiz3, double midTerm, double finalGrade) { } public void setQuiz1(double quiz1) { if(quiz1>10) { this.quiz1 = 10; } else { if(quiz1<0) { this.quiz1 =0; } else { this.quiz1 = quiz1; } } } public double getQuiz1() { return this.quiz1; } public void setQuiz2(double quiz2) { if(quiz2>10) { this.quiz2 = 10; } else { if(quiz2<0) { this.quiz2 =0; } else { this.quiz2 = quiz2; } } } public double getQuiz2() { return this.quiz2; } public void setQuiz3(double quiz3) { if(quiz3>10) { this.quiz3 = 10; } else { if(quiz3<0) { this.quiz3 =0; } else { this.quiz3 = quiz3; } } } public double getQuiz3() { return this.quiz3; } public void setmidTerm(double midTerm) { if(midTerm>100) { this.midTerm = 100; } else { if(midTerm<0) { this.midTerm =0; } else { this.midTerm = midTerm; } } } public double getmidTerm() { return this.midTerm; } public void setfinalGrade(double finalGrade) { if(finalGrade>100) { this.finalGrade = 100; } else { if(finalGrade<0) { this.finalGrade = 0; } else { this.finalGrade = finalGrade; } } } public double getfinalGrade() { return this.finalGrade; } public double getPercentGrade() { double quizWeight = (quiz1 + quiz2 + quiz3)*25.0/30.0; double midWeight = (midTerm) * (35.0/100.0); double finalWeight = (finalGrade) * (40.0/100.0); Math.round(quizWeight); Math.round(midWeight); Math.round(finalWeight); return quizWeight + midWeight + finalWeight; } public char getLetterGrade() { double percentGrade = this.getPercentGrade(); if(percentGrade >= 0 && percentGrade <60) { return 'F'; } else { if(percentGrade >= 60.0 && percentGrade <70.0) { return 'D'; } else { if(percentGrade >= 70.0 && percentGrade <80.0) { return 'C'; } else { if(percentGrade >= 80.0 && percentGrade <90.0) { return 'B'; } else { if(percentGrade >= 90.0 && percentGrade <=100.0) { return 'A'; } else{ return 'Z'; } } } } } } public boolean equals(Object obj) { if(!(obj instanceof Grade)) { return false; } Grade other = (Grade)obj; return this.getPercentGrade() == other.getPercentGrade(); } public String toString() { return "Quiz1: " + this.getQuiz1() +"\n" + "Quiz2: " + this.getQuiz2() +"\n" + "Quiz3: " + this.getQuiz3() +"\n" + "Mid Term: " + this.getmidTerm() + "\n" + "Final: " + this.getfinalGrade() + "\n"; } }public class TestGrade { public static void main(String[] args) { Grade grade1 = new Grade(); grade1.setQuiz1(8); grade1.setQuiz2(10); grade1.setQuiz3(5); grade1.setmidTerm(87); grade1.setfinalGrade(91); System.out.println("The percent grade is: " + grade1.getPercentGrade() + "\n" + "The letter Grade is: " + grade1.getLetterGrade()); System.out.println(grade1); Grade grade2 = new Grade(10,10,10,100,100); System.out.println(grade1.equals(grade2)); // prints true } }The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.Math.round(double a) will add 0.5 and then give that value to the Math.floor() Method which will round down.
So: 1.6 + 0.5 = 2.1 which will be rounded to two.
To get decimals you will have to muliply your original number and divide the result of Math.round(double a);
Like this:public static void main(String[] args) { double rand = Math.random()*1000; System.out.println(rand); System.out.println(Math.round(rand)); double round = Math.round(rand*100); round = round/100; System.out.println(round); }Output:
498.5190839276097
499
498.52However, round = Math.round(rand*100)/100; will not work. I am not quite sure why but the division has to be seperate.
Alternatively you can use the Math.rint(double a).
Using this you can do it like this:round = Math.rint(rand*100)/100;
rint() rounds differently though. 1.5 will be rounded up while 2.5 will be rounded down.For other numbers such as 2.7 or 1.2 both Methods will return the same result.
I appreciate it my good sir!
The Children of Doom... Doom's Children. They told my lord the way to the Mountain of Power. They told him to throw down his sword and return to the Earth... Ha! Time enough for the Earth in the grave.- AuthorPosts
You must be logged in to reply to this topic.

921526
921524
919244
916783
915526
915524
915354
915129
914037
909862
908811
908810
908500
908465
908464
908300
907963
907895
907477
902002
901301
901106
901105
901104
901024
901017
900393
900392
900391
900390
899038
898980
896844
896798
896797
895983
895850
895848
893740
893036
891671
891670
891336
891017
890865
889894
889741
889058
888157
887960
887768
886321
886306
885519
884948
883951
881340
881339
880491
878671
878351
877678
