another noob

• 08-31-2012, 04:17 PM
Blauv
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?

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```
• 08-31-2012, 05:37 PM
DarrylBurke
Re: another noob
Forum Rules -- see the third paragraph.

db
• 08-31-2012, 05:47 PM
Blauv
Re: another noob
sorry for that.
• 08-31-2012, 06:16 PM
Norm
Re: another noob
Quote:

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.
• 08-31-2012, 06:21 PM
Blauv
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.
• 08-31-2012, 06:35 PM
Norm
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
• 08-31-2012, 06:42 PM
Blauv
Re: another noob
the code uses the 0.025 from 960000 to 1200000, from 1200000 it needs to use the 0.0375
• 08-31-2012, 06:51 PM
Norm
Re: another noob
Look at the if tests. Hint: If x > 20 it's also > 5
Code:

```If(x > 5) {   do this }else if (x > 20) {   do that }```
When will 'do that' be executed? What is executed when x = 30?
• 08-31-2012, 07:02 PM
Blauv
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.
• 08-31-2012, 07:34 PM
Norm
Re: another noob
Did you see my last post about if tests? Compare the code in that post with this code:
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
• 08-31-2012, 07:46 PM
Blauv
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?
• 08-31-2012, 07:59 PM
Norm
Re: another noob
Quote:

yea i get that
I'm not sure you do.
Your code is exactly like this:
What is executed when x = 30?
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.
• 08-31-2012, 10:05 PM
Blauv
Re: another noob
i guess I don't get it then, because from what i can see it does what you say

{
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
}
• 08-31-2012, 10:15 PM
Norm
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.
• 08-31-2012, 10:57 PM
Blauv
Re: another noob
that makes sense and also fixed the problem.
Thank you!

and im sure ill be talking to you again soon.
• 08-31-2012, 11:04 PM
Norm
Re: another noob
Glad to help. See you next time.