• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

displaying gpa and lower grade first?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello i'm still a beginner in java and i have an assignment which i tried to solve myself but there's one part that says "This information should then be displayed with the lowest grade first" which i dont know how to do
here's the full question and i will post what i tired doing too
( As a part-time student, you took two courses last term. Write a Java program that calculates and displays your grade point average (GPA) for the term. Your program should prompt the user to enter the grade and credit hours for each course. This information should then be displayed with the lowest grade first, and the GPA for the term should be calculated and displayed. A warning message should be printed if the GPA is less 2.0 and a congratulatory message if the GPA is 3.5 or above.)

here's my answer:



please help me out and tell me where i have mistakes because it's due soon
much appreciated everyone!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first problem I can see is that you only have one grade variable. You have two variables to store creditHours though, creditHours1 and creditHours2, which is fine. With grade, however, you lose the first value entered when you assign the second value entered to it. You have essentially lost the first value and replaced it with the second value.
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i just noticed that !!
here i fixed it



now is it all correct ?
of course I'm still missing out the ( display lower grade first )

please reply thanks in advance
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sara james wrote:
of course I'm still missing out the ( display lower grade first )


Well, you already know about the if-statement and how to use the relational operators like < > and the equality operator, ==. Char values can be treated as numbers (char is technically an integer type) so you can compare them to each other and see if one is greater or less than the other.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And BTW, Welcome to the Ranch!
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for welcoming me

i just wanna ask if i put the statement like:
if(grade1>'A')
i know B,C,D,etc are bigger...
so what should i print out after the if-statement? this part is confusing me
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you say it out in plain English, it's pretty easy, right?

Now translate that into code using what you know about how to display messages and the order that statements are executed in the program.
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried from what i understood but looks like java doesn't get me haha


it shows BA in the output which i guess means my printing is wrong :/
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with BA? That is printing the lower grade first.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What is wrong with BA? That is printing the lower grade first.


Ah, there's the catch!

In the context of grades, an A is higher/greater than a B, and a B is higher/greater than a C, and so on.

However, Java doesn't know about this. All Java knows is that 'A' is less than 'B' (normal alphabetical/dictionary order). So you will have to turn your logic on its head to fit what Java thinks.

One thing you can do is to hide that confusion in its own method, say a method called isHigher:

Where someExpression is a boolean expression that flips Java's integer logic around so you get the correct letter grade comparison.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One last hint: you can use the negation operator (!) to return the opposite of a boolean value.

That is, if boolExpr is true, !boolExpr will be false and vice versa.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sara james wrote:I tried from what i understood but looks like java doesn't get me haha

it shows BA in the output which i guess means my printing is wrong :/


Actually, this is already the correct logic for letter grades. I don't know if you are just confused or what but if grade1 is 'A' and grade2 is 'B' then, you should expect to get an output of "BA". It's a little confusing to read though.
It would be clearer if you could write something like this (and note how it is formatted):

That would be less confusing.
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm sorry but what is (isHigher)? we still didn't take this in my course ..
and this is supposed to be an assignment to submit so i don't think this should be included because we didn't take it .. rather than this isn't there another way?
thank you so much
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we also didn't take this
"boolExpr is true .."
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this output correct?

enter grade for course1 like A,B,C,D,F: B
enter credit hours for course1: 3
enter grade for course2 like A,B,C,D,F: A
enter credit hours for course2: 3
your gpa is3.5
Congratulations
A
B
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know whether that is correct. It appears to be printing A before B so that doesn't look right to me, but you will have to decide yourself.
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i corrected it

enter grade for course1 like A,B,C,D,F: B
enter credit hours for course1: 3
enter grade for course2 like A,B,C,D,F: A
enter credit hours for course2: 3
your gpa is3.5
Congratulations
B
A


my question is does the assignment wants it to be displayed like that?
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok here's the final one i decided on




and this the output

enter grade for course1 like A,B,C,D,F: B
enter credit hours for course1: 3
enter grade for course2 like A,B,C,D,F: A
enter credit hours for course2: 3
your gpa is3.5
B
A
Congratulations



do you say it's 100% correct and i can submit it?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sara james wrote: . . . do you say it's 100% correct

No.

and i can submit it?

If that is what the assignment wants, yes. But you can improve that code no end.
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you tell me what is wrong?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sara james wrote:

Where curly brackets gone?
It makes your code less readable.

If we wouldn't know the truth
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing "wrong" per se but there are things that you can make better.

1. Formatting
2. Eliminating duplicate code

You can go ahead and submit your work and be proud of what you have done
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends how far you want to go and how much you want to change.

Style: needs more spaces. You should only ever have a { followed by something in an array indicstorinitialiser. Look at your lines 1 2 3 and 4, for example. Look at our formatting suggestions.
More in the formatting suggestions: put {} after every occurrence of if, while(...), for(...), do and else.

Structure: You are repeating yourself in lines 15-22. That should be a separate method. A method can only return one thing, so maybe you should create a Grade class which encapsulates the grade and the hours.
Calculating the score from the grade letter should be a separate method. Actually that is better done inside the Grade class.
I don't like the repeated ifs. Try a switch. Or you can even do arithmetic with the chars. That is also why you can use the < operator on the chars in line 61. And you have another lot of repetition there. And if A is higher than B your use of < will display the higher grade first.
I don't like your validation at the end of the method. Validation should be done as early as possible.

Depending how much time you have available, you should consider which of those suggestions to implement. Do not attempt them all at once. Go slowly. Record what you are doing as you go, so you can revert to earlier versions if necessary.
 
sara james
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much everyone this was really helpful!
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what your code looks like when formatted using the Eclipse IDE default formatter.

I added a few blank spaces to give more visual separation between sections of your code.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Worth to mention, that code was assumed (by eclipse) differently from original code (lines 31 - 33 and lines 43 - 45)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Worth to mention, that code was assumed (by eclipse) differently from original code (lines 31 - 33 and lines 43 - 45)


Not exactly sure what you mean by that but Eclipse simply ignored any grouping or structure implied by the original indentation, which was misleading, and formatted the code to a standard, the way it is actually executed.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just noticed that. No actual impact in this case, but could be in different.
So, actually it is important for OP do not omit braces in any case.
Sorry for not being clear enough.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sara james wrote:we also didn't take this
"boolExpr is true .."


That's fine. "boolExpr" is just a shorthand for "boolean expression," which is an expression that evaluates to true or false. See boolean expression.

As I was saying, !boolExpr just gives you back the opposite value of boolExpr. Here's an example:

When you flip the value from true to false and vice versa like this, we often say that you are "toggling" the variable, just as you would toggle a switch on and off. Also, see how I tried to choose the variable names so that the code reads almost like a normal English sentence: If it's on, then show the message "ON" otherwise show the message "OFF".

Note also that I didn't write this:

That would be redundant and is of the form that was mentioned before: boolExpr == true or boolExpr == false. Comparing a boolean expression to true or false is not good style.
 
I'm still in control here. LOOK at this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic