Java Reference
In-Depth Information
Okay, that's enough coding; let's test a little.
5.
Add a main method.
6.
In the main method, add the following statements:
Employee emp;
emp = new Employee("","","","","");
System.out.println(emp.fedTaxCalc(15.50, 2));
Can you explain what these statements do?
The first statement creates a reference variable of type Employee called emp. The second statement creates
an Employee object (with blank values for all its properties) and assigns the object to the variable emp. The third
statement invokes the fedTaxCalc method using a payRate of 15.50 and 2 exemptions. The returned taxAmt is then
displayed in the console.
7.
Save the Employee source and run Employee as a Java application.
The result 100.0 should be displayed. If not, compare your source to the following to determine where
the mistake is.
private double taxAmt;
private double taxSal;
public double fedTaxCalc( double payRate, int exemptions) { taxSal = payRate * 40 - exemptions * 60;
if (taxSal <= 50) {
taxAmt = 0;
} else {
taxAmt = 100;
}
return taxAmt;
}
public static void main(String[] args) {
Employee emp;
emp = new Employee("","","","","");
System.out.println(emp.fedTaxCalc(15.50, 2));
}
Compound Conditions versus Nested Ifs
There are actually several ways to code the tax calculation. We will cover two methods: one using compound
conditions and the other using nested ifs.
The condition examples so far have been simple conditions: one comparison operator and two values. You can
actually build a condition with multiple comparisons by linking the comparisons with && (and) and || (or) operations.
For instance, if you were searching for employees with pay rates less than $10 and exemptions greater than 5, the
condition could be written as follows:
if ((payRate < 10) && (exemptions > 5))
Notice that the entire condition is enclosed in parentheses. The individual comparisons do not have to be in
parentheses but for clarity purposes, we have done so. You can use many &&'s and ||'s in any combination within a
single condition; however, the logic becomes very tricky, so be careful.
 
Search WWH ::




Custom Search