Results 1 to 16 of 16

Thread: another noob

  1. #1
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Help with if-else-if staement

    Hello all and thank you for your time for starters.
    My question and problem is i need the applet to re-calculate the compensation in the second else if statement by 0.0375 not 0.025.
    what and where am i wrong?

    please ignore the comments they are not accurate.


    Java Code:
    // import java libraries
    import java.text.NumberFormat;
    import java.util.Scanner;
            
    public class Math 
    {
        // set and declare specific variables
        double baseSalary = 60000;     // set baseSalary
        double annualSales;            // set annualSales    
        double commission, below80 = 0.0, totalCompensation;             // set commision to 0 beloew 80% objective
        double startCompensation = 960000;
        double commission80, above80 = 0.025, totalCompensation80;        // set percentage after 80% is obtained
        double additionalCompensation = 1200000;
        double commission100, above100 = 0.0375, totalCompensation100;      // set percent above 100% of Sales Goal
        NumberFormat currency = NumberFormat.getCurrencyInstance(); // Get currency format from import
        Scanner userInput = new Scanner( System.in );               // Create Scanner to obtain userInput from User
        
        // Create userInput, calculator, and final display
        public void Calculator() 
        {
                            // Get userInput and perform math calculations
                System.out.print("Enter Salesman Annual Sales: $"); // Display message for userInput
                annualSales = userInput.nextDouble();               // Read Salary from userInput
                System.out.println();                               // creates blank line
                
                commission = annualSales * below80 ;             // Total of annualSales below 80%
                totalCompensation = commission + baseSalary ;                   // Total compensation
                
                // Display Compensation below 80% of sales goal
                if (annualSales < startCompensation)  // Compare annualSales against 960000
                        {
                    System.out.println("      No extra compensation is offered untill\n         the 80% or 960000 goal is reached.");                         // Display reason for no incentive
                    System.out.println("Therefore the Salesman's Total Annual Compensation of\n   " + currency.format(totalCompensation) + " is without any additional incentives.");  // Display total compensation
                        }
                // Display Compensation above 80% of sales goal
                else if (annualSales >= startCompensation)
                {
                    commission80 = annualSales * above80 ;             // Total of annualSales above 80%
                    totalCompensation80 = commission80 + baseSalary ;                   // Total commission
                    
                    // Display Commision
                    System.out.println("       Annual Sales Amount of = " + currency.format(annualSales) );                                 // Display sales amount
                    System.out.println("            Multiplied by = " + above80 + "%");                                                       // Display percent
                    System.out.println("       Gives a Total Commission of = " + currency.format(commission80) );                             // Display commision
                    System.out.println("Total Commission Added to Base Salary of = " + currency.format(baseSalary) );                // Display base salary
                    System.out.println("\n           Gives the Salesman's\n Total Annual Compensation of = " + currency.format(totalCompensation80) );  // Display total compensation
                }
                // Display Compensation above 100% of sales goal
                else if (annualSales >= additionalCompensation)
                {
                    commission100 = annualSales * above100 ;             // Total of annualSales above 80%
                    totalCompensation100 = commission100 + baseSalary ;                   // Total commission
                    // Display Commision
                    System.out.println("       Annual Sales Amount of = " + currency.format(annualSales) );                                 // Display sales amount
                    System.out.println("            Multiplied by = " + above100 + "%");                                                       // Display percent
                    System.out.println("       Gives a Total Commission of = " + currency.format(commission100) );                             // Display commision
                    System.out.println("Total Commission Added to Base Salary of = " + currency.format(baseSalary) );                // Display base salary
                    System.out.println("\n           Gives the Salesman's\n Total Annual Compensation of = " + currency.format(totalCompensation100) );  // Display total compensation
                } // End if-else-if Statement
        } // End Main method
    } // End class Multiply
    Last edited by Blauv; 08-31-2012 at 05:44 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: another noob

    Forum Rules -- see the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: another noob

    sorry for that.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: another noob

    i need the applet to re-calculate the compensation
    Can you explain the problem? What does the code do now that you want to change?
    Can you post the current output and add some comments to it to show what you want to change it to.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: another noob

    here is the output at present it calculates number above 1,200,000 by 0.025 and should be using 0.0375 instead..

    Welcome to the Salesman Annual Compensation Calculator.
    Enter Salesman Annual Sales: $1200000

    Annual Sales Amount of = $1,200,000.00
    Multiplied by = 0.025%
    Gives a Total Commission of = $30,000.00
    Total Commission Added to Base Salary of = $60,000.00

    Gives the Salesman's
    Total Annual Compensation of = $90,000.00

    Thank you for using the
    Salesman Annual Comensation Calculator.
    Feel free to use the Calculator anytime!

    This should be accomplished in the second else if statement.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: another noob

    Are you saying this line:
    Multiplied by = 0.025%
    should be this:
    Multiplied by = 0.0375%

    You need to look at the logic of the code where the if tests are and change the logic so it uses the value you want when you want it to.
    Can you explain in words what the code is supposed to do? When should it use the value of 0.0375 and when 0.025

    Look at the if tests. Hint: If x > 20 it's also > 5
    Last edited by Norm; 08-31-2012 at 06:41 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: another noob

    the code uses the 0.025 from 960000 to 1200000, from 1200000 it needs to use the 0.0375

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: another noob

    Look at the if tests. Hint: If x > 20 it's also > 5
    Java Code:
    If(x > 5) {
      do this
    }else if (x > 20) {
      do that
    }
    When will 'do that' be executed? What is executed when x = 30?
    Last edited by Norm; 08-31-2012 at 07:36 PM. Reason: changed wording
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: another noob

    ok, in the first if it test for salary lower then 960000 and multiplies by 0
    this works correctly
    in the following else if it tests for salary above 960000 and multiplies by 0.025
    this works correctly
    in the following else if it test for salary above 1200000 and multiplies by 0.0375
    this is not working correctly

    instead of multiplying by the expected 0.0375 it continues to multiply by 0.025.

    that is what i cant figure out. why it wont switch to the 0.0375 for multiplication.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: another noob

    Did you see my last post about if tests? Compare the code in that post with this code:
    Java Code:
    If(x > 5) {
      do this
    }
    if (x > 20) {
      do that
    }
    In this code, both do this AND do that will execute when x > 5
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: another noob

    yea i get that, and everything looks like it should work correctly to me.

    can u look at above code in the last else if statement to see if u see wjy it wont use the 0.0375 for multiplication?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: another noob

    yea i get that
    I'm not sure you do.
    Your code is exactly like this:
    What is executed when x = 30?
    Java Code:
    If(x > 5) {
      do this
    }else if (x > 20) {
      do that
    }
    When will 'do that' be executed? Hint: Never

    Remember with if /else if / else if /else statements,
    the first if that is true will cause the rest of the if statements to be skipped.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: another noob

    i guess I don't get it then, because from what i can see it does what you say

    }else if (annualSales > additionalCompensation)
    {
    do that
    commission100 = annualSales * above100 ; // Total of annualSales above 80%
    totalCompensation100 = commission100 + baseSalary ; // Total commission
    // Display Commision
    System.out.println(" Annual Sales Amount of = " + currency.format(annualSales) ); // Display sales amount
    System.out.println(" Multiplied by = " + above100 + "%"); // Display percent
    System.out.println(" Gives a Total Commission of = " + currency.format(commission100) ); // Display commision
    System.out.println("Total Commission Added to Base Salary of = " + currency.format(baseSalary) ); // Display base salary
    System.out.println("\n Gives the Salesman's\n Total Annual Compensation of = " + currency.format(totalCompensation100) ); // Display total compensation
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: another noob

    Look at the code I posted. If x = 30 then the if(x>5) will be true and 'do this' will be executed. 'do that' will never be executed because this test: (x > 5) will be true first.
    To test for a range of the values of x, say > 5 and < 20 you need to add another condition to the if statement to test x against its upper value:
    if(x > 5 && x < 20)

    Write a 10 line program with those if statements and some printlns that print either do this or do that and see what happens.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Blauv is offline Member
    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    Default Re: another noob

    that makes sense and also fixed the problem.
    Thank you!

    and im sure ill be talking to you again soon.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: another noob

    Glad to help. See you next time.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. I am a super noob with a super noob question.
    By LittleZoppo in forum Java Applets
    Replies: 3
    Last Post: 04-27-2012, 03:50 AM
  2. Noob Here
    By Salamander in forum Introductions
    Replies: 1
    Last Post: 08-03-2010, 08:19 AM
  3. Very noob, but need help!!!
    By Guilbertda in forum New To Java
    Replies: 2
    Last Post: 02-01-2010, 10:32 PM
  4. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 AM
  5. Noob
    By nokomis in forum Introductions
    Replies: 2
    Last Post: 03-06-2009, 05:10 PM

Posting Permissions

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