Jump to content

Some help for a beginner with using Java & OOP!

Hello programmers! I need help regarding if statements. The program I am creating is a simple calculator for my college assignment where, the user shall type in their name, their points, and this program will convert their points into a grade.  I understand if this is written confusingly, however if confused please reply, I don't have a lot of time to do this.

 

However, I am struggling with how to do the conversion part, where the program will convert the user's points into a grade. I was thinking of doing if statements (I have done one if statement in the screenshot below just to sort of get the jist of what i'm going for here.) The first if statement is if the user enters a value lower than 120, it will display a line of text. My next if statement will be greater than 120 and less than 180 and so on.

 

The part I am struggling with is where I should write the if statements (I have three classes, one main, one student where student variables are declared and another class called 'points' which is currently used for nothing.) At the moment, I am writing them in the main class because if I wrote them in the points class, I would have no idea how the main class would access it. Another part I need help with is how to write them out! Would I do another if statement? Or an else if statement? 

 

 

a85GISu.png

 

(Scenario: This program is a BTEC calculator, basically, the user will enter their name and then their UCAS points, which will then be converted into a BTEC grade. An example of this program used successfully can be found here: http://www.universityfinder.org.uk/ucas-points-calculator.html)

Student Life Build Yo: Windows 10 Pro 64-bit, Intel Core i5-3350p, ASRock H61M-VG4, Sapphire Nitro R9 380x

12GB DDR3-1333, Corsair CX600, SanDisk 120GB SSD, Asus Xonar DGX, Corsair 200R, Headphones: Takstar HI 2050.

~First PC Build!~

 

Link to comment
Share on other sites

Link to post
Share on other sites

The checking should go in the main (or if you want in a manager class but definitely not worth it for a program like this).

Also why do points have to be a class on their own?

Link to comment
Share on other sites

Link to post
Share on other sites

The checking should go in the main (or if you want in a manager class but definitely not worth it for a program like this).

Also why do points have to be a class on their own?

Wasn't too sure myself really, my tutor went through the classes that should be used, originally it was as follows: A main class, points class then a units class. I got confused with the 'units' so I got rid of that, and made a student class so student variables can be declared. I think the only reason the points class should be used is because the program needs at least three classes. Yeah I thought the checking should go in the main, good to see I did that right haha.

Student Life Build Yo: Windows 10 Pro 64-bit, Intel Core i5-3350p, ASRock H61M-VG4, Sapphire Nitro R9 380x

12GB DDR3-1333, Corsair CX600, SanDisk 120GB SSD, Asus Xonar DGX, Corsair 200R, Headphones: Takstar HI 2050.

~First PC Build!~

 

Link to comment
Share on other sites

Link to post
Share on other sites

Oh ok the picture didn't load, so what you are asking is if you need a separate if statement or an if else?

 

I'd go with the if else, after you checked if it's under 120 you don't need to check again if it's in between those two values if it's not needed, if it's under 120 it can't be between 120 and 180, do an else if.

 

Although for a project of this scale you won't notice any difference as far as performance goes so it's not a big deal.

Link to comment
Share on other sites

Link to post
Share on other sites

Oh ok the picture didn't load, so what you are asking is if you need a separate if statement or an if else?

 

I'd go with the if else, after you checked if it's under 120 you don't need to check again if it's in between those two values if it's not needed, if it's under 120 it can't be between 120 and 180, do an else if.

 

Although for a project of this scale you won't notice any difference as far as performance goes so it's not a big deal.

Sweet, thanks for your help! Just a couple things to clarify cause i'm not too brilliant on OOP: There will be about 10 if statements, would the rest of them be else if too? and did I write the code correctly in the screenshot (just the part where it says if (!ucaspoints < 120) because I had problems getting that to work, I would miss out a certain symbol etc.

Student Life Build Yo: Windows 10 Pro 64-bit, Intel Core i5-3350p, ASRock H61M-VG4, Sapphire Nitro R9 380x

12GB DDR3-1333, Corsair CX600, SanDisk 120GB SSD, Asus Xonar DGX, Corsair 200R, Headphones: Takstar HI 2050.

~First PC Build!~

 

Link to comment
Share on other sites

Link to post
Share on other sites

Sweet, thanks for your help! Just a couple things to clarify cause i'm not too brilliant on OOP: There will be about 10 if statements, would the rest of them be else if too? and did I write the code correctly in the screenshot (just the part where it says if (!ucaspoints < 120) because I had problems getting that to work, I would miss out a certain symbol etc.

Now I've never coded in Java but I can see that you are getting the line as a string, and you are comparing a string with a numeric value I doubt that's gonna work unless Java is smart enough to do an implicit conversion.

Link to comment
Share on other sites

Link to post
Share on other sites

Now I've never coded in Java but I can see that you are getting the line as a string, and you are comparing a string with a numeric value I doubt that's gonna work unless Java is smart enough to do an implicit conversion.

Yeah people in my class noticed that, it should originally be an integer, but having it as 'int' just comes up with syntax errors, I think that code is something I might have to ask my tutor about. I kept it as string for the moment because there were no errors with it (for starting off at least) :P

Student Life Build Yo: Windows 10 Pro 64-bit, Intel Core i5-3350p, ASRock H61M-VG4, Sapphire Nitro R9 380x

12GB DDR3-1333, Corsair CX600, SanDisk 120GB SSD, Asus Xonar DGX, Corsair 200R, Headphones: Takstar HI 2050.

~First PC Build!~

 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah people in my class noticed that, it should originally be an integer, but having it as 'int' just comes up with syntax errors, I think that code is something I might have to ask my tutor about. I kept it as string for the moment because there were no errors with it (for starting off at least) :P

You are missing a cast, it would be:

 

int points = Integer.parseInt(scanner.nextLine());

Link to comment
Share on other sites

Link to post
Share on other sites

You are missing a cast, it would be:

 

int points = Integer.parseInt(scanner.nextLine());

Sweet! I changed the code to that and it compiled with no syntax errors, what a pleasant sight. Think that would've been in a pretty big issue in the long run, pretty sure all I gotta do now is figure out how to write the other 'else if' statements, in terms of like what symbols to use etc. Then it would be ready to submit ^^

Student Life Build Yo: Windows 10 Pro 64-bit, Intel Core i5-3350p, ASRock H61M-VG4, Sapphire Nitro R9 380x

12GB DDR3-1333, Corsair CX600, SanDisk 120GB SSD, Asus Xonar DGX, Corsair 200R, Headphones: Takstar HI 2050.

~First PC Build!~

 

Link to comment
Share on other sites

Link to post
Share on other sites

Sweet! I changed the code to that and it compiled with no syntax errors, what a pleasant sight. Think that would've been in a pretty big issue in the long run, pretty sure all I gotta do now is figure out how to write the other 'else if' statements, in terms of like what symbols to use etc. Then it would be ready to submit ^^

Nice to know, if you need anything else just ask.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×