Page 101 - Introduction to Programming with Java: A Problem Solving Approach
P. 101

                Figure 3.3 BonusCalculator program
Apago PDF Enhancer
InthebonusMessageassignmentstatement,notetheparenthesesaround.02 * salary.Although the parentheses are not required by the compiler, we prefer to include them here because they improve the code’s readability. They improve readability by making it clear that the math operation (.02 × salary) is separate from the string concatenation operation. Use of discretionary parentheses to enhance clarity is an art. Sometimes it’s helpful, but don’t get carried away. If you use parentheses too often, your code can look cluttered.
In the salary assignment statement, note the 50000. You might be tempted to insert a comma in 50000 to make it read better; that is, you might be tempted to enter 50,000. If you do insert the comma, your program will not compile successfully. In Java programs, numbers are not allowed to have commas. Unfortunately, this makes it easy to accidentally enter the wrong number of zeros in a large number. Count those zeros!
Tracing
As part of a program’s presentation, we’ll sometimes ask you to trace the program. Tracing forces you to understand program details thoroughly. And understanding program details thoroughly is important for writing good programs. To set up a trace, provide a column heading for each variable and for output. Then execute each statement, starting with the first statement in main. For declaration statements, write a ? in the declared variable’s column, indicating that the variable exists, but it doesn’t have a value yet. For assignment statements, write the assigned value in the variable’s column. For a print statement, write the printed value in the output column.7
3.11 Assignment Statements 67
 /****************************************************************
*
BonusCalculator.java
Dean & Dean
This program calculates and prints a person's work bonus.
****************************************************************/
*
*
*
public class BonusCalculator
{
}
// end class BonusCalculator
public static void main(String[] args)
{
}
String bonusMessage; // specifies work bonus
salary = 50000;
bonusMessage = "Bonus 􏰀 $" + (.02 * salary); System.out.println(bonusMessage);
// end main
int salary;
// person's salary
 7 If you’d like a more detailed discussion of tracing, see Chapter 2, Section 2.11.





































































   99   100   101   102   103