Archive for July, 2007

Chapter 2 Introduction to Java Applications 89 First

Tuesday, July 31st, 2007

Chapter 2 Introduction to Java Applications 89 First floor Second floor Lights Person walking to elevator Elevator Elevator shaft Elevator door Floor buttons GUI button Elevator button Bell (Disabled) GUI button Fig. 2.222 Person moving towards elevator on the first floor. Fig. Fig. 2.2323 Person riding the elevator to the second floor. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

88 Introduction to Java Applications Chapter 2 resent (Web hosting companies)

Tuesday, July 31st, 2007

88 Introduction to Java Applications Chapter 2 resent the elevator door because mesh allows us to see inside the elevator). The floor door opens concurrently with the elevator door, so it appears as if both doors open at the same time. A person sees only one door, depending on that person s location. When the person is inside the elevator, the person sees the elevator door and can exit the elevator when this door opens; when the person is outside the elevator, the person sees the floor door and can enter the elevator when that door opens1. The elevator starts on the first floor with all the doors closed. To conserve energy, the elevator moves only when necessary. For simplicity, the elevator and floors each have a capacity of only one person.2 The user of our application should, at any time, be able to create a unique person in the simulation and situate that person on either the first or second floor (Fig. 2.22). When created, the person walks across the floor to the elevator. The person then presses a button on the floor next to the elevator shaft (referred to as a floor button ). When pressed, that floor button illuminates, then requests the elevator. When summoned, the elevator travels to the person s floor. If the elevator is already on that person floor, the elevator does not travel. Upon arrival, the elevator resets the button inside the elevator (called the elevator button ), sounds the bell inside the elevator, then opens the elevator door (which opens the floor door on that floor). The elevator then signals the elevator shaft of the arrival. The elevator shaft, upon receiving this message, resets the floor button and illuminates the light on that floor. Occasionally, a person requests the elevator when it is moving. If the request was generated at the floor from which the elevator just departed, the elevator must remember to revisit that floor after carrying the current passenger to the other floor. When the floor door opens, the person enters the elevator after the elevator passenger (if there is one) exits. If a person neither enters nor requests the elevator, the elevator closes its door and remains on that floor until the next person presses a floor button to summon the elevator. When a person enters the elevator, that person presses the elevator button, which also illuminates when pressed. The elevator closes its door (which also closes the floor door on that floor) and moves to the opposite floor. The elevator takes five seconds to travel between floors. When the elevator arrives at the destination floor, the elevator door opens (along with the floor door on that floor) and the person exits the elevator. The application user introduces a person onto the first or second floor by pressing the First Floor button or the Second Floor button, respectively. When the user presses the First Floor button, a person should be created (by the elevator simulation) and positioned on the first floor of the building. When the user presses the Second Floor button, a person should be created and positioned on the second floor. Over time, the user can create any number of people in the simulation, but the user cannot create a new person on an occupied floor. For example, Fig. 2.22 shows that the First Floor button is disabled to prevent the user from creating more than one person on the first floor. Figure 2.23 shows that this button is reenabled when the person rides the elevator. 1. Most people do not consider this when riding an elevator they really think of one elevator door, when in reality, there is a door in the elevator and a door on the floor, and these doors open and close in tandem. 2. After you have studied this case study, you may want to modify it to allow more than one person to ride the elevator at once and more than one person to wait on each floor at once. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 2 (Web hosting control panel) Introduction to Java Applications 87 We

Monday, July 30th, 2007

Chapter 2 Introduction to Java Applications 87 We have introduced many important features of Java in this chapter, including displaying data on the screen, inputting data from the keyboard, performing calculations and making decisions. We should note that these applications are meant to introduce the reader to basic programming concepts. As you will see in later chapters, more substantial Java applications contain just a few lines of code in method main that creates the objects that perform the work of the application. In Chapter 3, we demonstrate many similar techniques as we introduce Java applet programming. In Chapter 4, we build on the techniques of Chapter 2 and Chapter 3 as we introduce structured programming. You will become more familiar with indentation techniques. We will study how to specify and vary the order in which statements are executed; this order is called flow of control. 2.9 (Optional Case Study) Thinking About Objects: Examining the Problem Statement Now we begin our optional, object-oriented design and implementation case study. The Thinking About Objects sections at the ends of this and the next several chapters will ease you into object orientation by examining an elevator simulation case study. This case study will provide you with a substantial, carefully paced, complete design and implementation experience. In Chapters 3 through 13, Chapter 15 and Chapter 22, we will perform the various steps of an object-oriented design (OOD) process using the UML while relating to the object- oriented concepts discussed in the chapters. In Appendices G, H and I, we will implement the elevator simulator using the techniques of object-oriented programming (OOP) in Java. We present the complete case-study solution. This is not an exercise; rather, it is an end-to-end learning experience that concludes with a detailed walkthrough of the actual Java code. We have provided this case study so that you can become accustomed to the kinds of substantial problems encountered in industry. We hope you enjoy this learning experience. Problem Statement A company intends to build a two-floor office building and equip it with an elevator. The company wants you to develop an object-oriented software-simulator application in Java that models the operation of the elevator to determine whether it will meet the company s needs. The company wants the simulation to contain an elevator system. The application consists of three parts. The first and most substantial part is the simulator, which models the operation of the elevator system. The second part is the display of this model on screen so that the user may view it graphically. The final part is the graphical user interface, or GUI, that allows the user to control the simulation. Our design and implementation will follow the so-called Model-View-Controller architecture we will learn about in Section 13.17. The elevator system consists of an elevator shaft and an elevator car. In our simulation, we model people who ride the elevator car (referred to as the elevator ) to travel between the floors in the elevator shaft, as shown in Fig. 2.22, Fig. 2.23 and Fig. 2.24. The elevator contains a door (called the elevator door ) that opens upon the elevator s arrival at a floor and closes upon the elevator s departure from that floor. The elevator door is closed during the trips between floors to prevent the passenger from being injured by brushing against the wall of the elevator shaft. In addition, the elevator shaft connects to a door on each floor (referred to as the two floor doors ), so people cannot fall down the shaft when the elevator is not at a floor. Note that we do not display the floor doors in the figures, because they would obscure the inside of the elevator (we use a mesh door to rep Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

86 Introduction to Java Applications Chapter 2 Common

Monday, July 30th, 2007

86 Introduction to Java Applications Chapter 2 Common Programming Error 2.17 Placing a semicolon immediately after the right parenthesis of the condition in an ifstructure is normally a logic error. The semicolon will cause the body of the if structure to be empty, so the ifstructure itself will perform no action, regardless of whether its condition is true. Worse yet, the intended body statement of the ifstructure will now become a statement in sequence with the ifstructure and will always be executed. Notice the use of spacing in Fig. 2.20. Remember that white-space characters, such as tabs, newlines and spaces, are normally ignored by the compiler. So, statements may be split over several lines and may be spaced according to the programmer s preferences without affecting the meaning of a program. It is incorrect to split identifiers and string literals. Ideally, statements should be kept small, but it is not always possible to do so. Good Programming Practice 2.19 A lengthy statement may be spread over several lines. If a single statement must be split across lines, choose breaking points that make sense, such as after a comma in a comma- separated list, or after an operator in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines until the end of the statement. The chart in Fig. 2.21 shows the precedence of the operators introduced in this chapter. The operators are shown from top to bottom in decreasing order of precedence. Notice that all of these operators, with the exception of the assignment operator, =, associate from left to right. Addition is left associative, so an expression like x+y+zis evaluated as if it had been written as (x+y)+z. The assignment operator, =, associates from right to left, so an expression like x=y=0is evaluated as if it had been written as x=(y=0), which, as we will soon see, first assigns the value 0to variable yand then assigns the result of that assignment, 0, to x. Good Programming Practice 2.20 Refer to the operator precedence chart (see the complete chart in Appendix C) when writing expressions containing many operators. Confirm that the operations in the expression are performed in the order you expect. If you are uncertain about the order of evaluation in a complex expression, use parentheses to force the order, exactly as you would do in algebraic expressions. Be sure to observe that some operators, such as assignment, =, associate from right to left rather than from left to right. Operators Associativity Type () left to right parentheses * / % left to right multiplicative + -left to right additive < <= > >= left to right relational == != left to right equality = right to left assignment Fig. 2.2121 Precedence and associativity of the operators discussed so far. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Business web site - Chapter 2 Introduction to Java Applications 85 Good

Sunday, July 29th, 2007

Chapter 2 Introduction to Java Applications 85 Good Programming Practice 2.17 Indent the statement i7n the body of an ifstructure to make the body of the structure stand out and to enhance program readability. Good Programming Practice 2.18 Place only one statement per line in a program. This format enhances program readability In the preceding ifstructure, if the values of variables number1 and number2 are equal, line 35 assigns to result the value of result + number1 + “==” + number2. As discussed in Fig. 2.9, the + operator in this expression performs string concatenation. For this discussion, we assume that each of the variables number1 and number2 has the value 123. First, the expression converts number1 s value to a string and appends it to result (which currently contains the empty string) to produce the string “123″. Next, the expression appends “==”to “123″to produce the string “123==”. Finally, the expression appends number2 to “123==” to produce the string “123==123″. The Stringresult becomes longer as the program proceeds through the if structures and performs more concatenations. For example, given the value 123for both number1 and number2 in this discussion, the if conditions at lines 46 47 (<=) and 49 50 (>=) are also true. So, the program displays the result 123 == 123 123 <= 123 123 >= 123 in a message dialog. Common Programming Error 2.15 Replacing operator == in the condition of an ifstructure, such as if(x==1), with operator =, as in if(x=1), is a syntax error. Common Programming Error 2.16 Forgetting the left and right parentheses for the condition in an if structure is a syntax error. The parentheses are required. Notice that there is no semicolon (;) at the end of the first line of each if structure. Such a semicolon would result in a logic error at execution time. For example, if ( number1 == number2 ); // logic error result = result + number1 + ” == ” + number2; would actually be interpreted by Java as if ( number1 == number2 ) ; result = result + number1 + ” == ” + number2; where the semicolon on the line by itself called the empty statement is the statement to execute if the condition in the ifstructure is true. When the empty statement executes, no task is performed in the program. The program then continues with the assignment statement, which executes regardless of whether the condition is true or false. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

84 Introduction to Java Applications Chapter 2 or (Make my own web site)

Sunday, July 29th, 2007

84 Introduction to Java Applications Chapter 2 or as in String firstNumber, secondNumber, result; This is set of names known as a comma-separated list. Once again, notice the comment at the end of each declaration in lines 13 17, indicating the purpose of each variable in the program. Lines 20 21, firstNumber = JOptionPane.showInputDialog( “Enter first integer:” ); use JOptionPane.showInputDialog to allow the user to input the first integer value as a string and store it in firstNumber. Lines 24 25, secondNumber = JOptionPane.showInputDialog( “Enter second integer:” ); use JOptionPane.showInputDialog to allow the user to input the second integer value as a string and store it in secondNumber. Lines 28 29, number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); convert each string input by the user in the input dialogs to type int and assign the values to int variables number1 and number2. Line 32, result = “”; assigns to resultthe empty string a string containing no characters. Every variable declared in a method (such as main) must be initialized (given a value) before it can be used in an expression. Because we do not yet know what the final result string will be, we assign to result the empty string as a temporary initial value. Common Programming Error 2.14 Not initializing a variable defined in a method before that variable is used in the method s body is a syntax error. Lines 34 35, if ( number1 == number2 ) result = result + number1 + ” == ” + number2; define an ifstructure that compares the values of the variables number1 and number2 to determine if they are equal. The ifstructure always begins with keyword if, followed by a condition in parentheses. The if structure expects one statement in its body. The indentation shown here is not required, but it improves the readability of the program by emphasizing that the statement in line 35 is part of the if structure that begins on line 34. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 2 Introduction (Web site counters) to Java Applications 83 Fig.

Saturday, July 28th, 2007

Chapter 2 Introduction to Java Applications 83 Fig. 2.20Using equality and relational operators (part 3 of 3). Fig. 2.20 The definition of application class Comparisonbegins at line 8, public class Comparison { As discussed previously, method main (lines 11 59) begins the execution of every Java application. Lines 13 17, String firstNumber; // first string entered by user String secondNumber; // second string entered by user String result; // a string containing the output int number1; // first number to compare int number2; // second number to compare declare the variables used in method main. Note that there are three variables of type Stringand two variables of type int. Remember that variables of the same type may be declared in one declaration or in multiple declarations. If more than one name is declared in a declaration, the names are separated by commas (,), as in String firstNumber, secondNumber, result; Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

82 Introduction to Java Applications Chapter 2 27 (Web hosting ecommerce)

Saturday, July 28th, 2007

82 Introduction to Java Applications Chapter 2 27 // convert numbers from type String to type int 28 number1 = Integer.parseInt( firstNumber ); 29 number2 = Integer.parseInt( secondNumber ); 30 31 // initialize result to empty String 32 result = “”; 33 34 if ( number1 == number2 ) 35 result = number1 + ” == ” + number2; 36 37 if ( number1 != number2 ) 38 result = number1 + ” != ” + number2; 39 40 if ( number1 < number2 ) 41 result = result + "n" + number1 + " < " + number2; 42 43 if ( number1 > number2 ) 44 result = result + “n” + number1 + ” > ” + number2; 45 46 if ( number1 <= number2 ) 47 result = result + "n" + number1 + " <= " + number2; 48 49 if ( number1 >= number2 ) 50 result = result + “n” + number1 + ” >= ” + number2; 51 52 // Display results 53 JOptionPane.showMessageDialog( 54 null, result, “Comparison Results”, 55 JOptionPane.INFORMATION_MESSAGE ); 56 57 System.exit( 0 ); // terminate application 58 59 } // end method main 60 61 } // end class Comparison Fig. 2.20Using equality and relational operators (part 2 of 3). Fig. 2.20 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 2 Introduction to Java Applications 81 Common (Cool web site)

Saturday, July 28th, 2007

Chapter 2 Introduction to Java Applications 81 Common Programming Error 2.11 It is a syntax error if the operators ==, !=, >=and<= contain spaces between their symbols, as in = =, ! =, > =and < =, respectively. Common Programming Error 2.12 Reversing the operators !=, >=and <=, as in =!, =>and =<, is a syntax error. Common Programming Error 2.13 Confusing the equality operator, ==, with the assignment operator, =, can be a logic error or a syntax error. The equality operator should be read as is equal to, and the assignment operator should be read as gets or gets the value of. Some people prefer to read the equality operator as double equals or equals equals. The next example uses six ifstructures to compare two numbers input into text fields by the user. If the condition in any of these ifstatements is true, the assignment statement associated with that if structure executes. The user inputs two values through input dialogs. Next, the program converts the input values to integers and stores them in variables number1 and number2. Then, the program compares the numbers and displays the results of the comparisons in an information dialog. The program and sample outputs are shown in Fig. 2.20. 1 // Fig. 2.20: Comparison.java 2 // Compare integers using if structures, relational operators 3 // and equality operators. 4 5 // Java extension packages 6 import javax.swing.JOptionPane; 7 8 public class Comparison { 9 10 // main method begins execution of Java application 11 public static void main( String args[] ) 12 { 13 String firstNumber; // first string entered by user 14 String secondNumber; // second string entered by user 15 String result; // a string containing the output 16 int number1; // first number to compare 17 int number2; // second number to compare 18 19 // read first number from user as a string 20 firstNumber = 21 JOptionPane.showInputDialog( “Enter first integer:” ); 22 23 // read second number from user as a string 24 secondNumber = 25 JOptionPane.showInputDialog( “Enter second integer:” ); 26 Fig. 2.20Using equality and relational operators (part 1 of 3). Fig. 2.20 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

80 Introduction to Java Applications Chapter 2 (Kids web site) Step

Friday, July 27th, 2007

80 Introduction to Java Applications Chapter 2 Step 1. y = 2 * 5 * 5 + 3 * 5 + 7; 2 * 5 is 10 (Leftmost multiplication) Step 2. y = 10 * 5 + 3 * 5 + 7; 10 * 5 is 50 (Leftmost multiplication) Step 3. y = 50 + 3 * 5 + 7; 3 * 5 is 15 (Multiplication before addition) Step 4. y = 50 + 15 + 7; 50 + 15 is 65 (Leftmost addition) Step 5. y = 65 + 7; 65 + 7 is 72 (Last addition) Step 6. y = 72; (Last operation place 72into y) Fig. 2.1818 Order in which a second-degree polynomial is evaluated. Fig. Standard algebraic equality or relational operator Java equality or relational operator Example of Java condition Meaning of Java condition Equality operators = == x == y xis equal to y . != x != y xis not equal to y Relational operators > > x > xis greater than y < < x < xis less than y = >= x >= y xis greater than or equal to y = <= x <= y xis less than or equal to y Fig. 2.1919 Equality and relational operators. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01