• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with code  RSS feed

 
Daniel Sheppard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code so far...



how do I get the table to generate a sales amount needed to achieve the salary input by the user. Im confused on how to get the table/loop generated prior to interact with the user input.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a new method "computeSalesAmount( double commission )". You'll have to go through your computeCommission() method and invert the logic to put in the new method.
 
Simon Roberts
Author
Ranch Hand
Posts: 182
9
Java Linux Netbeans IDE Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a conceptual level, you have two choices, I believe.

Option one (preferred) : do the algebra to turn the formula that creates a revenue given a sales amount into a formula that gives a sales amount given a target revenue. That's just high school math, and the "right" way to attack it, I believe.

Option two (well, we don't sweat when the CPU does work, so why would I care ) : ask the user for a target revenue, then each time round the loop that calculates commission, compare the computed revenue with the target, and, if the revenue exceeds that target a) print a message to that effect and b) break out of the loop (because you're done, presumably)

HTH
Simon
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

Beside what guys mentioned, I'd fix other issues too. Your program is a bit chaotic at the moment.

First of all, in my opinion "for" loop in this case is not a good choise to go with. I'd go with "while" as it would fulfill your goal better and would be more clear. But before that, please help us to understand, what LOOP_INITIAL, LOOP_MAX and LOOP_STEP represents? Main problem is that variable names are poorly chosen, because simply they don't say much. Of course looking at the method computeCommision I could do my guess, but we don't want to do that in computer science, it should be clear instantly by looking to a variable names.

What else I'd do if I were you, I'd try to slice your code into smaller pieces and try to make more methods where each of them would do by 1 task. At the moment you have 2 methods, one suppose to compute commissions, and the other suppose to start your program, but the latter does more than that.

Commission.computeCommission(i) you don't need to write class name prior the method. You have 2 methods, and both are static, so you can access it without class name. Have you tested computeComission method? Does it produces expected results?
 
Daniel Sheppard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i construct new method computeSalesAmount would I do an --- if statement to desiredcommission instead of Salesamount or just commission...
 
Daniel Sheppard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
our assignment required us to display the table which the main loop integers and and static double compute commission work together to compile... then after displaying the table in the user output we are required to ask the said user to ask for there desired salary and the program is required to display the sales amount required to achieve such salary. Im having trouble getting the program to pull data from the loop table that displays first.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said earlier. Your program is chaotic, you need to fix that, so you could see where your logic flaws. What I'd do.

1. Name variables so by looking at them they would tell you what they are meant to store.

2. Create a small method, even wrap 1 line in a method and name it "printTableHeader":

3. Create another method, "printTableContent" (change it to while loop as for loop not readable here):

4. Create another method, "getDesiredSalary":

5. One more method, "printStatistics" (this line is not correct, you cannot retrieve commission in this way, you need to call method to calculate it):

6. The you likely need another two methods (one you have, you need another to "computeNeededSalesAmount").

When you get all those, you need another method, where your all program logic flow will go, check this FAQ (<- link to click on), so you'll see how to not write lots of code in main method.
 
Daniel Sheppard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:As I said earlier. Your program is chaotic, you need to fix that, so you could see where your logic flaws. What I'd do.

1. Name variables so by looking at them they would tell you what they are meant to store.

2. Create a small method, even wrap 1 line in a method and name it "printTableHeader":

3. Create another method, "printTableContent" (change it to while loop as for loop not readable here):

4. Create another method, "getDesiredSalary":

5. One more method, "printStatistics" (this line is not correct, you cannot retrieve commission in this way, you need to call method to calculate it):

6. The you likely need another two methods (one you have, you need another to "computeNeededSalesAmount").

When you get all those, you need another method, where your all program logic flow will go, check this FAQ (<- link to click on), so you'll see how to not write lots of code in main method.


Ok i started from stratch and try something else... I believe I got it down but was wondering if you could review it for me to cut it down... the program is required to show the compute commission table in output prior to ask for target commission which i did...but we are required to call the computeCommission method inside the loop... which im confused on how to do...
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Sheppard wrote:Ok i started from stratch and try something else...
No, I don't see that, it looks similar, without any valid changes. It feels like you're guessing.
Daniel Sheppard wrote:I believe I got it down
It is not enough, need to test it and make sure. I have asked you question yesterday, is your computeCommision method gives you desired results, didn't get any answer on this.
Daniel Sheppard wrote:we are required to call the computeCommission method inside the loop
This is where you have this call now.

We cannot help you if you're not accepting any help. You have been given quite few advises from multiple people, but it seems decided do not follow them. Not sure how we could help you further, lets see, might someone else will find the way.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take heed of Liutauras' advice to reduce the chaos in the code. It's very difficult to see what's going on, because you're not breaking the code up in simple methods with understandable names.



As for the loop, you can just call your computeCommission() method from inside the loop body. A few things to note: Don't use the do-while loop, use the while loop instead. As a matter of fact, in this exercise you don't need a loop at all, because computeRequiredSales() can simply reverse the calculation done in computeCommission().
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I realize this is homework, but if in future endeavors you're writing code that handles money, NEVER use floating points. Instead, use BigDecimal.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, as you see Stephan found the way to help you and defined the methods you are needed to implement in order to achieve your goals. As you'll notice, some of the methods implementations you have already, just need to make sure they do what you expecting them to do.
Now please take a look at the method "main", it contains mainly method calls, so, by looking at them you can understand what your program is meant to do, and define the sequence of your program. Try to keep program structures at the moment like this, and will be the way easier to solve your exercises.

One more thing. Try to implement by 1 method at a time and test it. First "printComissionTable", once you done that, try to compile your program and run it (comment other method calls) to see if it gives you a desired result. Once you are happy with output, move towards other method "getTargetCompensation". It suppose to get user input. Once you done, do test again, and see if it takes user input (you can print "target" variable out to see if it prints what was entered). And keep going like this by small steps and you'll get there.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:you can print "target" variable out to see if it prints what was entered

Unrelated to the original discussion, but this is also a great opportunity to add tracing to the application. Instead of printing, you can write to a logger, and then leave the code in production.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!