• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Beginner with methods

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Up front - this is an assignment, but I am seriously struggling with it and our text book doesn't offer much in order to get me through working this one out. I can pass simple integer values back and forth with methods without too much trouble, but in this one we're supposed to pass a String and I need help (afraid that my instructor will have trouble explaining it to me in a way that I can understand).



I hope what I'm trying to do here is self-explanatory. I need to pass a letter grade back from 'determineGrade' to the main and then into the 'showResult' method. We haven't covered Strings in depth enough for me to get what I'm missing here. The program works when it's just displaying the average and I take out all references to the letter grade, or even like this:



But I'm sure this example is not what she's looking for if she's asking for Strings.

(Edit: whoops, forgot the main method closing bracket there.)
 
Ranch Hand
Posts: 385
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Welcome to the Ranch
2) Saying your teacher will not be able to explain something in an easy way isn't a good route to go down - I am sure your teacher is perfectly capable of explaining things well to her students.
3) The problem is that in your showResult method, you are outputting the String variable grade, when in fact you should be outputting finalGrade.

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Does your program compile and run correctly? Is the output what you expect?

Given your "returning string" task, your determineGrade method looks fine... of course those if/else can be improved. But it does what is required.

Your showResult method then prints out using those 2 passed-in parameters.

 
Anita Palermo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:1) Welcome to the Ranch
2) Saying your teacher will not be able to explain something in an easy way isn't a good route to go down - I am sure your teacher is perfectly capable of explaining things well to her students.
3) The problem is that in your showResult method, you are outputting the String variable grade, when in fact you should be outputting finalGrade.



OK, I fixed that, but I get this error now: Main.java:55: error: class, interface, or enum expected
}
^
1 error

exit status 1

As to point 2 - I know, but there are reasons not relevant here. ;)
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anita Palermo wrote:

OK, I fixed that, but I get this error now: Main.java:55: error: class, interface, or enum expected
}
^
1 error

exit status 1

As to point 2 - I know, but there are reasons not relevant here. ;)



You have an extra bracket on that line - get rid of it.

As for point 2, I know what the reason is, it's very common amongst students to try and shift the blame on their teacher when they do not understand things. I was simply giving you some advice that doing that isn't a good strategy.
 
Anita Palermo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the second set of eyes!
 
Anita Palermo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:

Anita Palermo wrote:

As for point 2, I know what the reason is, it's very common amongst students to try and shift the blame on their teacher when they do not understand things. I was simply giving you some advice that doing that isn't a good strategy.



Nah, it's not her fault at all, really and I didn't mean it to sound like I was blaming her. Sometimes I just have to walk away and figure it out and come back with something to troubleshoot.

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anita Palermo wrote:

Ahmed Bin S wrote:

As for point 2, I know what the reason is, it's very common amongst students to try and shift the blame on their teacher when they do not understand things. I was simply giving you some advice that doing that isn't a good strategy.



Nah, it's not her fault at all, really and I didn't mean it to sound like I was blaming her. Sometimes I just have to walk away and figure it out and come back with something to troubleshoot.



Fair enough.

BTW, if I was a student at your school, and I got 90, 90, 89, I would be pretty annoyed, because I would think I deserve to get an A grade, but your program would award me a B grade.
 
Anita Palermo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:
BTW, if I was a student at your school, and I got 90, 90, 89, I would be pretty annoyed, because I would think I deserve to get an A grade, but your program would award me a B grade.



Ha! It does do that, doesn't it? At least it's just practice.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ahmed Bin S wrote:BTW, if I was a student at your school, and I got 90, 90, 89, I would be pretty annoyed, because I would think I deserve to get an A grade, but your program would award me a B grade.


then you need to study math harder. The criterion for getting an A is average>=90 and clearly the average of (90,90,89) is not >= 90.

There is no need to specify a range for each grade. Just use:

Note that even if the average were to be computed using doubles instead of ints, your grade would still be B.



 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Kleinschmidt wrote:

Ahmed Bin S wrote:BTW, if I was a student at your school, and I got 90, 90, 89, I would be pretty annoyed, because I would think I deserve to get an A grade, but your program would award me a B grade.


then you need to study math harder. The criterion for getting an A is average>=90 and clearly the average of (90,90,89) is not >= 90.

There is no need to specify a range for each grade. Just use:

Note that even if the average were to be computed using doubles instead of ints, your grade would still be B.





No, I do not need to study Maths harder - you need to study how exams are graded.

Say I do 8 modules, and I score 90, 90, 90, 90, 90, 90, 90, 89.
I scored A in 7 modules and scored B in 1 module.
So what should the university award me, A or B?
They could use the mean, which is 89.875, and award me a B.
But that would be unfair, I got 7 As and only 1 B, and the B I got was only 1 percent off - that isn't fair that I should then get a B.

In fact, if I took a 1000 exams, and I got 90 999 times, and got 89 1 time, my mean would be 89.999 and I would get a B.
How can that be right, I got 999 As and 1 B, and you are awarding me a B?

So, there is this thing called "rounding" that examiners use when awarding grades - it helps get rid of this ridiculous situation described above.
 
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No need to get annoyed with each other.

If an examiner thinks the results deserve a B not an A they will make sure all the 89s are rounded … to 86!
 
Campbell Ritchie
Marshal
Posts: 79977
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AP: Welcome to the Ranch

When using else‑if, you should simplify your tests. Line 28 in your second code block does not do what you think it does. I would take the opposite direction from FK's, because it allows me to avoid ≤ and ≥. The > and < operators are probably easier to understand.If I were writing that code, I would probably use an enumerated type for the grades. Alternatively, you can change those constants to Strings quite easily. Or define String constants. You know you can use enum constants or Strings after the case keyword in the switch statement?

Don't create a new Scanner object reading from System.in every time you run that method. Create one as a final (probably static) field and never try to close it. Scanners reading from any other resource must be closed, however.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No need to get annoyed with each other.



I wasn't getting annoyed. Was just showing how absurd it is to not apply rounding.

Campbell Ritchie wrote:If an examiner thinks the results deserve a B not an A they will make sure all the 89s are rounded … to 86!



Indeed!

Examiners in real life employ quite a lot of flexibility, say for example you got 7 As, and then in one exam you got an E, and that brings your average down to a B, they might still award you an A - they might think you just had an off day, and therefore weigh the one exam you performed badly on lower. After all, mean isn't the only way of calculating an average, you also have mode and median etc, and using those you clearly got an A.

 
Marshal
Posts: 8967
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anita, in your second code version you got

Anita Palermo wrote:

I hope you see what could possibly go wrong here.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic