• Post Reply Bookmark Topic Watch Topic
  • New Topic

process a given minimum value and a value if < than given minimum  RSS feed

 
Anthony Valdez
Greenhorn
Posts: 4
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a Java newbie and have been working on this program for about a month. Java has been very challenging, but I am enjoying learning. My issue starts with line 49 or 50 I think. I would like my program to be able to recognize that the minimum # of CUs could be < than 12 if the student only has 6 CUs left until graduation, but I also need it to recognize if I enter a letter or negative number which I somehow managed to do at the top of the code. I tried to pull this off with sum == sum which isn't giving me the desired output. I tried to reuse code from my lines 15-38 cause I know that part of the program responds correctly when I try to enter a letter or negative number, but I obviously was not implementing the loop correctly when I tried to put it on line 57 because the program runs but doesn't produce anything when it gets to that point. In summary, I would appreciate some assistance getting my code to realize that a student may not have the minimum CUs left (12) and may need < than that to graduate, but also not accept negative number or letters as input. Any feedback on the code formatting would be appreciated as well.

 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anthony Valdez,

You wrote quite a lot lines of code. Some code fragments are good, some - not. What I'd suggest you is, to write down the steps in english, where each step suppose to describe a single small task. After you clearly identify these steps/tasks - create a method for each task in order to accomplish that. Then, would be pretty easy to identify, which parts doesn't give you a wanted result.
Your issues actually starts a bit earlier than from the line 49, so, to know how to fix them, I'd encourage you clearly identify the steps your program are intended to do. If you want, try to write down over here, so it would help everyone to understand your program idea.
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!



What do you think this means?
 
Anthony Valdez
Greenhorn
Posts: 4
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for the feedback. I took everyone's advice and reevaluated what I had done. sum == sum will ALWAYS be true so that had to go and as I wrote the project down step by step, I could see where I went wrong. Here is what I came up with. Now I am closer to getting the output I am looking for. My only issue now is that if I student enters 0 CUs as the first input (because I am using 0 to tell the program I am done with those inputs and to sum the numbers entered) it is still showing that they have 1 term to complete, which is not correct. I believe that I can fix this by implementing something similar as I did here: Any feedback would be appreciated. I just wanted everyone to know where I was at since you guys took time out to offer me feedback. Thanks again!



This project is a graduation planner that a student would use to see how long it would take to graduate and how much money it would cost. Below is the complete code:

 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of what condition will be true only if the user doesn't enter any CUs and use that as a way to avoid the final processing.

Other observations:

In general, it's better to write


than


The idea is you want to use an interface as your type if possible. This gives your program more flexibility.

You have


twice, and is it really checking for positive integers?

The variable name CUs should start with a lowercase letter; otherwise it looks like a class.


This is the same as break.
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anthony again,

I am looking at the code, and it looks very complicated, it seems for a quite simple problem.
As I mentioned above, I encourage you to simplify all that logic by dividing your program into small pieces of code and making methods from them.

1. By making an assumption this code is correct, it is being repeated in your program at least twice. So, it means it requires to become a single method, which accomplish the single task.

2. Iterate loop simply while "true" is not a good practice, as it doesn't give you a clue, in which circumstances it would stop. Look what you have inside body of "while" loop. Might would work to add a condition in a while loop, like "while (input != exitCode)" where exitCode is equal 0? Moreover, in this case probably would work better do-while loop. Have you read about it? Check here (<- link)

3. This should become a method also, with a meaning name, so you could look at it, and have an understanding, what is meant to do. In addition, variable "CUs" doesn't say much, I'd pick something with more meaning.

4. This code could become a method also. Maybe "defineCreditsPerTerm" or something, you know better what is meant to do.

5. Even this code could become a method, like "printStudiesStatistics" or something similar.

Hopefully you got the idea. Anyone who ever would look to your code, looking only at the method declaration, could have a clue, what program is meant to do. In this way would be the way easier to notice, where the program flow goes wrong, which method produces wrong results and many more advantages.

And this program finally should become something similar to:
It is just an example. Best luck.
 
Anthony Valdez
Greenhorn
Posts: 4
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate all the feedback from everyone here. I appreciate the explanations and will implement. Unfortunately, I always overcomplicate everything. Something I definitely need to work on.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!