# Java Looping Question

• 03-04-2013, 10:27 AM
rhym1n
Java Looping Question
Here is my assignment (Scroll down to bottom to see questions):

Modify the Week Two Java™ application using Java™ NetBeans™ IDE to meet these additional and changed business requirements:

• The company has recently changed its total annual compensation policy to improve sales.

• A salesperson will continue to earn a fixed monthly salary of \$4,000.00. The current sales target for every salesperson is \$120,000.00.

• The sales incentive will only start when 80% of the sales target is met. The current commission is 25% of total sales.

• If a salesperson exceeds the sales target, the commission will increase based on an acceleration factor. The acceleration factor is 1.25.

• The application should ask the user to enter annual sales, and it should display the total annual compensation.

• The application should also display a table of potential total annual compensation that the salesperson could have earned, in \$5000 increments above the salesperson’s annual sales, until it reaches 50% above the salesperson’s annual sales.

Sample Table: Assuming a total annual sales of \$100,000, the table would look like this:

Total Sales Total Compensation
100,000 <<Program calculated value>>
105,000 <<Program calculated value>>
110,000 <<Program calculated value>>
115,000 <<Program calculated value>>
120,000 <<Program calculated value>>
125,000 <<Program calculated value>>
130,000 <<Program calculated value>>
135,000 <<Program calculated value>>
140,000 <<Program calculated value>>
145,000 <<Program calculated value>>
150,000 <<Program calculated value>>

----------------------------------------------

(THIS PERTAINS TO THE BOLD PART ONLY)

I have figured out the code for everything (which took a long time I might add) up until the last part that is in bold. I know that I need a loop (while loop maybe?), but I can't figure out how to get started on this last piece to my program. I know how to make it increment in the amount of \$5,000, but that is about it in this certain situation (the examples I saw were much simpler).

1) Do I need a while loop?
2) How do I make it display the total annual compensation? (based on the annual sales - which I already have the input code that collects that part, as well as the formulas that calculate the total annual compensation - I'm just not sure how to use this in a loop as I am completely new to them)
3) How do I make the loop stop after 50% of the total annual compensation is listed?

All help is highly appreciated!
• 03-04-2013, 12:53 PM
rhym1n
Re: Java Looping Question
I think I can use a for statement according to my instructor, and I based this next line of code from an example of his, but I keep getting an error:

Code:

```for (double counter = annualSales; counter < counter * 1.5; counter +5000) { System.out.println("Stuff..."); }```
The error says not a statement, but I'm not sure what I am doing wrong. annualSales is a variable I already declared earlier.
• 03-04-2013, 01:07 PM
Tolls
Re: Java Looping Question
Code:

```for (double counter = annualSales; counter < counter * 1.5; counter +5000) { System.out.println("Stuff..."); }```
That should be an assignment at the end of the if:
Code:

`counter = counter + 5000`
But you'll also hit a problem with the check in the middle:
Code:

`counter < counter * 1.5`
counter is never going to be anything other than less than itself times 1.5.
I think you want to check it against annualSales * 1.5
• 03-04-2013, 01:24 PM
rhym1n
Re: Java Looping Question
Awesome, thank you so much for that. I have one more question about the output code:
Code:

```for(double counter = annualSales; counter < annualSales * 1.5;                 counter +=5000)         {             System.out.println("Total Sales: " + counter + "\tTotal "                     + "Compensation: " + someTypeOfCalculation(counter);         }```
Where I put "someTypeOfCalculation", I'm not exactly sure what to put there. You see, if the annual sales that one enters is < 96,000, they earn no commission...if the annual sales is between 96,000 and 120,000, they earn a 25% commission, and if the annual sales is above 120,000, they earn 31.25% commission.

I made an if-else-if statement before the loop code that performed these calculations separately, and now I really don't know what to do so it calculates and displays all of these correctly.

I can post the entire code if it would help.
• 03-04-2013, 01:46 PM
Tolls
Re: Java Looping Question
Well, the way you have it written there is actually quite good.
Now, just write a method with the same name (OK, you might want to use a different name than someTypeOfCalculation).
That method will return a value (String, double, up to you).
It will do exactly what you did in the previous if/else, but return a suitable value for each of these cases.
• 03-04-2013, 01:53 PM
rhym1n
Re: Java Looping Question
Hmm, the "someTypeOfCalculation(counter)" part really confuses me. All of my calculations were correct in the if-else-if statement, but I'm not sure how to write this because the commission rate has the potential to change due to the "50% above the salesperson's annual sales" part. For example: if the salesperson enters \$120,000, then he earns a 25% commission, but when it goes up \$5,000 (to \$125,000 total), he/she would earn a 31.25% commission rate.

Will I need to write multiple System.out.println() statements or can I fit this into one calculation? I wonder if I am overthinking this...sorry, it's only my 3rd week of learning Java and my brain goes haywire at times trying to figure it all out, lol.
• 03-04-2013, 02:37 PM
JosAH
Re: Java Looping Question
Quote:

Originally Posted by Tolls
But you'll also hit a problem with the check in the middle:
Code:

`counter < counter * 1.5`
counter is never going to be anything other than less than itself times 1.5.

Variable 'counter' could (theoretically) be negative ...

kind regards,

Jos
• 03-04-2013, 04:03 PM
Tolls
Re: Java Looping Question
Quote:

Originally Posted by rhym1n
Hmm, the "someTypeOfCalculation(counter)" part really confuses me. All of my calculations were correct in the if-else-if statement, but I'm not sure how to write this because the commission rate has the potential to change due to the "50% above the salesperson's annual sales" part. For example: if the salesperson enters \$120,000, then he earns a 25% commission, but when it goes up \$5,000 (to \$125,000 total), he/she would earn a 31.25% commission rate.

Will I need to write multiple System.out.println() statements or can I fit this into one calculation? I wonder if I am overthinking this...sorry, it's only my 3rd week of learning Java and my brain goes haywire at times trying to figure it all out, lol.

Your method would be something like:
Code:

```if sales < somevalue then   return 10% of sales else if sales < thenextvalue then   return 20% of sales else   return 25% of sales```
• 03-04-2013, 11:46 PM
rhym1n
Re: Java Looping Question
You can put if-else-if statements within a loop?

This is what I am having trouble with, the someTypeOfCalculation part (which isn't really a variable):

Code:

```for(double counter = annualSales; counter < annualSales * 1.5;                 counter +=5000)         {             System.out.println("Total Sales: " + counter + "\tTotal "                     + "Compensation: " + someTypeOfCalculation;         }```
• 03-05-2013, 04:45 AM
rhym1n
Re: Java Looping Question
Does anyone know about the last part?
• 03-05-2013, 11:03 AM
Tolls
Re: Java Looping Question
You had it before, with someTypeOfCalculation(counter).
Have you done methods?
• 07-15-2014, 07:34 AM
silasndzewiyi
Re: Java Looping Question
Quote:

Originally Posted by rhym1n
You can put if-else-if statements within a loop?

This is what I am having trouble with, the someTypeOfCalculation part (which isn't really a variable):

Code:

```for(double counter = annualSales; counter < annualSales * 1.5;                 counter +=5000)         {             System.out.println("Total Sales: " + counter + "\tTotal "                     + "Compensation: " + someTypeOfCalculation;         }```

I don't know how you came about with the someTypeOfCalculation but
This is what I did with mine and it worked.

//Table loop
for(double counter = sales; counter <= sales * 1.5;
counter +=5000)
{

baseIncome = 51000;
if(counter < 96000)
rate = 0.00; //No commission for sales less than 80% of the targeted 120000.
else if(counter >= 96000 && counter <= 120000)
rate = 0.13; //Commission rate for sales above 80% is 13%
else rate = 0.13 * 1.25; //Sales above targeted numbers will have an increment factor of 1.25 in the commission rate

//Calculate commissionIncome
commissionIncome = counter * rate;

//Calculate totalIncome
totalIncome = commissionIncome + baseIncome;
System.out.println(USDFormat.format(counter)+ " \t " + USDFormat.format(totalIncome));
}
• 07-15-2014, 07:36 AM
silasndzewiyi
Re: Java Looping Question
I don't know how you came about with the someTypeOfCalculation but
This is what I did with mine and it worked.

//Table loop
for(double counter = sales; counter <= sales * 1.5;
counter +=5000)
{

baseIncome = 51000;
if(counter < 96000)
rate = 0.00; //No commission for sales less than 80% of the targeted 120000.
else if(counter >= 96000 && counter <= 120000)
rate = 0.13; //Commission rate for sales above 80% is 13%
else rate = 0.13 * 1.25; //Sales above targeted numbers will have an increment factor of 1.25 in the commission rate

//Calculate commissionIncome
commissionIncome = counter * rate;

//Calculate totalIncome
totalIncome = commissionIncome + baseIncome;
System.out.println(USDFormat.format(counter)+ " \t " + USDFormat.format(totalIncome));
}