Java Reference
In-Depth Information
thoroughly analyzed and carefully designed program is much easier to follow and modify.
Even the most experienced programmers spend a considerable amount of time analyzing
a problem and designing an algorithm.
Throughout this topic, you will learn not only the rules of writing programs in Java, but
also problem-solving techniques. Each chapter discusses several programming problems,
each of which is clearly marked as a Programming Example. The Programming Examples
teach techniques to analyze and solve the problems and also help you understand the
concepts discussed in the chapter. To gain the full benefit of this topic, we recommend
that you work through the Programming Examples at the end of each chapter.
EXAMPLE 1-1
In this example, we design an algorithm to find the perimeter and area of a rectangle.
To find the perimeter and area of a rectangle, you need to know the rectangle's length and
width. The perimeter and area of the rectangle are then given by the following formulas:
perimeter = 2 (length + width)
area = length width
The algorithm to find the perimeter and area of the rectangle is:
1. Get the length of the rectangle.
2. Get the width of the rectangle.
3. Find the perimeter using the following equation:
perimeter = 2 (length + width)
4. Find the area using the following equation:
area = length width
EXAMPLE 1-2
In this example, we design an algorithm that calculates the monthly paycheck of a
salesperson at a local department store.
Every salesperson has a base salary. The salesperson also receives a bonus at the end of
each month, based on the following criteria: If the salesperson has been with the store for
five years or less, the bonus is $10 for each year that he or she has worked there. If the
salesperson has been with the store for more than five years, the bonus is $20 for each
year that he or she has worked there. The salesperson can earn an additional bonus as
follows: If the total sales made by the salesperson for the month are greater than or equal
to $5,000 but less than $10,000, he or she receives a 3% commission on the sale. If the
total sales made by the salesperson for the month are at least $10,000, he or she receives a
6% commission on the sale.
 
Search WWH ::




Custom Search