Results 1 to 13 of 13
  1. #1
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default 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!
    Last edited by rhym1n; 03-04-2013 at 12:22 PM.

  2. #2
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default 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:

    Java 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.
    Last edited by rhym1n; 03-04-2013 at 12:57 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: Java Looping Question

    Java 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:
    Java Code:
    counter = counter + 5000
    But you'll also hit a problem with the check in the middle:
    Java 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
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default Re: Java Looping Question

    Awesome, thank you so much for that. I have one more question about the output code:
    Java 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.
    Last edited by rhym1n; 03-04-2013 at 01:34 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Java Looping Question

    Quote Originally Posted by Tolls View Post
    But you'll also hit a problem with the check in the middle:
    Java 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
    The only person who got everything done by Friday was Robinson Crusoe.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: Java Looping Question

    Quote Originally Posted by rhym1n View Post
    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:
    Java Code:
    if sales < somevalue then
       return 10% of sales
    else if sales < thenextvalue then
       return 20% of sales
    else
       return 25% of sales
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default 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):

    Java Code:
    for(double counter = annualSales; counter < annualSales * 1.5; 
                    counter +=5000)
            {
                System.out.println("Total Sales: " + counter + "\tTotal "
                        + "Compensation: " + someTypeOfCalculation;
            }
    Last edited by rhym1n; 03-04-2013 at 11:56 PM.

  10. #10
    rhym1n is offline Member
    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    0

    Default Re: Java Looping Question

    Does anyone know about the last part?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,474
    Rep Power
    21

    Default Re: Java Looping Question

    You had it before, with someTypeOfCalculation(counter).
    Have you done methods?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    silasndzewiyi is offline Member
    Join Date
    Jul 2014
    Posts
    2
    Rep Power
    0

    Default Re: Java Looping Question

    Quote Originally Posted by rhym1n View Post
    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):

    Java 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));
    }

  13. #13
    silasndzewiyi is offline Member
    Join Date
    Jul 2014
    Posts
    2
    Rep Power
    0

    Default 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));
    }

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2012, 11:01 PM
  2. Replies: 6
    Last Post: 01-14-2012, 04:21 AM
  3. Help! Using Looping in Java??
    By HappyGirl1184 in forum Eclipse
    Replies: 2
    Last Post: 09-14-2011, 01:40 PM
  4. Quick question about for looping and delay
    By DouboC@gmail.com in forum JDBC
    Replies: 0
    Last Post: 12-29-2010, 10:00 PM
  5. Looping question
    By lost_soul in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 04:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •