Originally Posted by
GHero13
I have to modify this problem to find the savings between packages A and B, A and C, as well as B and C.
Java Code:
if(totalFeeA
> totalFeeB
){ savingsAB
= totalFeeA
- totalFeeB
; System.
out.
println("Total savings are: $" +savingsAB
);}if(totalFeeA
> totalFeeC
){ savingsAC
= totalFeeA
- totalFeeC
; System.
out.
println("Total savings are: $" +savingsAC
);} if(totalFeeB
> totalFeeC
){ savingsBC
= totalFeeB
- totalFeeC
; System.
out.
println("Total savings are: $" + savingsBC
);}else System.
out.
println("There are no savings available.");
Keep in mind that you can do the ABSOLUTE value of the difference between the two numbers to get a positive result. Thus you don't have to check which one is larger than the other and worry about flipping the negative..
Secondly, those if statements are each independent statements so your ELSE statement makes no sense as it is only attached to the LAST if statement. In other words, it does not do what you think it does.
Example:
Java Code:
if (expression1) //compiler checks here first.
{
execute1();
}
if (expression2) //then it checks here.
{
execute2();
}
if (expression3) //then it checks here as well.
{
execute3();
}
will execute ALL 3 functions (depending on whether or not the expressions are true of course) and it will CHECK EVERY if statement regardless of whether or not the previous if statement executed.
However:
Java Code:
if (blah) //compiler checks here first.
{
execute1();
}
else if (blah2) //it will only check here if blah was false!
{
execute2();
}
else //it will only go here if both blah and blah2 are false!
{
execute3();
}
Will execute ONE of the above statements. It will test if "blah" expression is true. If it is, only execute1 will be executed. If "blah" is false, it will then CHECK if "blah2" is true. If it is, then only execute2 will be executed. However, if both "blah" AND "blah2" is false, then it only execute3 will be executed.
Java Code:
savingsAB
= Math.
abs(totalFeeA
- totalFeeB
);savingsAC
= Math.
abs(totalFeeA
- totalFeeC
);savingsBC
= Math.
abs(totalFeeB
- totalFeeC
);System.
out.
println("Total savings are: $" +savingsAB
);System.
out.
println("Total savings are: $" +savingsAC
);System.
out.
println("Total savings are: $" + savingsBC
);//OR:savingsAB
= Math.
abs(totalFeeA
- totalFeeB
);savingsAC
= Math.
abs(totalFeeA
- totalFeeC
);savingsBC
= Math.
abs(totalFeeB
- totalFeeC
);if (savingsAB
> 0.0) { System.
out.
println("Total savings are: $" +savingsAB
);}else if (savingsAC
> 0.0) { System.
out.
println("Total savings are: $" +savingsAC
);}else if (savingsBC
> 0.0) { System.
out.
println("Total savings are: $" + savingsBC
);}else { System.
out.
println("No savings.");}