Archive for September, 2007

Http web server - 156 Control Structures: Part 1 Chapter 4 The

Monday, September 17th, 2007

156 Control Structures: Part 1 Chapter 4 The conditional operator (?:) is related to the if/else structure. ?: is Java s only ternary operator it takes three operands. The operands together with ?: form a conditional expression. The first operand is a boolean expression, the second is the value for the conditional expression if the condition evaluates to true and the third is the value for the conditional expression if the condition evaluates to false. For example, the statement System.out.println( studentGrade>=60 ? “Passed”:”Failed” ); contains a conditional expression that evaluates to the string “Passed” if the condition studentGrade>=60 is true and to the string “Failed” if the condition is false. Thus, this statement with the conditional operator performs essentially the same function as the if/else statement given previously. The precedence of the conditional operator is low, so the entire conditional expression is normally placed in parentheses. We will see that conditional operators can be used in some situations where if/else statements cannot. Good Programming Practice 4.3 In general, conditional expressions are more difficult to read than if/else structures. Such expressions should be used with discretion when they help improve a program s readability. Nested if/else structures test for multiple cases by placing if/else structures inside if/else structures. For example, the following pseudocode statement prints A for exam grades greater than or equal to 90, B for grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all other grades: If student s grade is greater than or equal to 90 Print A else If student s grade is greater than or equal to 80 Print B else If student s grade is greater than or equal to 70 Print C else If student s grade is greater than or equal to 60 Print D else Print F This pseudocode may be written in Java as if ( studentGrade >= 90 ) System.out.println( “A” ); else if ( studentGrade >= 80 ) System.out.println( “B” ); else if ( studentGrade >= 70 ) System.out.println( “C” ); else if ( studentGrade >= 60 ) System.out.println( “D” ); else System.out.println( “F” ); Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 4 Control Structures: (Web hosting providers) Part 1 155 4.6

Sunday, September 16th, 2007

Chapter 4 Control Structures: Part 1 155 4.6 The if/elseSelection Structure The if selection structure performs an indicated action only when the given condition evaluates to true; otherwise, the action is skipped. The if/else selection structure allows the programmer to specify that a different action is to be performed when the condition is true rather than when the condition is false. For example, the pseudocode statement If student s grade is greater than or equal to 60 Print Passed else Print Failed prints Passed if the student s grade is greater than or equal to 60 and prints Failed if the student s grade is less than 60. In either case, after printing occurs, the next pseudocode statement in sequence is performed. Note that the body of the else is also indented. Good Programming Practice 4.2 Indent both body statements of an if/else structure. The indentation convention you choose should be carefully applied throughout your programs. It is difficult to read programs that do not use uniform spacing conventions. The preceding pseudocode If/else structure may be written in Java as if ( studentGrade >= 60 ) System.out.println( “Passed” ); else System.out.println( “Failed” ); The flowchart in Fig. 4.4 nicely illustrates the flow of control in an if/elsestructure. Once again, note that, besides small circles and arrows, the only symbols in the flowchart are rectangles (for actions) and a diamond (for a decision). We continue to emphasize this action/decision model of computing. Imagine again a deep bin containing as many empty double-selection structures as might be needed to build a Java algorithm. The programmer s job is to assemble the selection structures (by stacking and nesting) with other control structures required by the algorithm and to fill in the empty rectangles and empty diamonds with actions and decisions appropriate to the algorithm being implemented. grade >= 60 true print Failed false print Passed Fig. 4.4Flowcharting the double-selection if/elsestructure. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

154 Control Structures: Part 1 (Web server application) Chapter 4 Good

Sunday, September 16th, 2007

154 Control Structures: Part 1 Chapter 4 Good Programming Practice 4.1 Consistently applying reasonable indentation conventions throughout your programs improves program readability. We suggest a fixed-size tab of about inch or three spaces per indent. The preceding pseudocode if statement may be written in Java as if ( studentGrade >= 60 ) System.out.println( “Passed” ); Notice that the Java code corresponds closely to the pseudocode. This attribute is a property of pseudocode that makes it a useful program development tool. The statement in the body of the if structure outputs the character string “Passed” in the command window. The flowchart in Fig. 4.3 illustrates the single-selection if structure. This flowchart contains what is perhaps the most important flowcharting symbol the diamond symbol, also called the decision symbol, which indicates that a decision is to be made. The decision symbol contains an expression, such as a condition, that can be either trueor false. The decision symbol has two flowlines emerging from it. One indicates the direction to be taken when the expression in the symbol is true; the other indicates the direction to be taken when the expression is false. A decision can be made on any expression that evaluates to a value of Java s booleantype (i.e., any expression that evaluates to trueor false). Note that the if structure is a single-entry/single-exit structure. We will soon learn that the flowcharts for the remaining control structures also contain (besides small circle symbols and flowlines) only rectangle symbols, to indicate the actions to be performed, and diamond symbols, to indicate decisions to be made. This factor is indicative of the action/ decision model of programming we have been emphasizing throughout this chapter. We can envision seven bins, each containing only control structures of one of the seven types. These control structures are empty; nothing is written in the rectangles or in the diamonds. The programmer s task, then, is to assemble a program from as many of each type of control structure as the algorithm demands, combining the control structures in only two possible ways (stacking or nesting) and then filling in the actions and decisions in a manner appropriate for the algorithm. In this chapter we discuss the variety of ways in which actions and decisions may be written. grade >= 60 true false print Passed Fig. 4.3Flowcharting the single-selection ifstructure. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 4 Control Structures: Part (Yahoo free web hosting) 1 153 Java

Saturday, September 15th, 2007

Chapter 4 Control Structures: Part 1 153 Java Keywords static throw void super throws volatile switch transient while synchronized this true try Keywords that are reserved, but not used, by Java const goto Fig. 4.2Fig. Java keywords (part 2 of 2). Common Programming Error 4.1 Using a keyword as an identifier is a syntax error. Well, that is all there is. Java has only seven control structures: the sequence structure, three types of selection structures and three types of repetition structures. Each program is formed by combining as many of each type of control structure as is appropriate for the algorithm the program implements. As with the sequence structure in Fig. 4.1, we will see that each control structure is flowcharted with two small circle symbols, one at the entry point to the control structure and one at the exit point. Single-entry/single-exit control structures make it easy to build programs; the control structures are attached to one another by connecting the exit point of one control structure to the entry point of the next. This procedure is similar to the way in which a child stacks building blocks, so we call it control-structure stacking. We will learn that there is only one other way in which control structures may be connected: control-structure nesting. Thus, algorithms in Java programs are constructed from only seven different types of control structures, combined in only two ways. 4.5 The ifSelection Structure A selection structure is used to choose among alternative courses of action in a program. For example, suppose that the passing grade on an examination is 60 (out of 100). Then the pseudocode statement If student s grade is greater than or equal to 60 Print Passed determines if the condition student s grade is greater than or equal to 60 is true or false. If the condition is true, then Passed is printed, and the next pseudocode statement in order is performed. (Remember that pseudocode is not a real programming language.) If the condition is false, the Print statement is ignored, and the next pseudocode statement in order is performed. Note that the second line of this selection structure is indented. Such indentation is optional, but it is highly recommended, because it emphasizes the inherent structure of structured programs. The Java compiler ignores white-space characters, like blanks, tabs and newlines, used for indentation and vertical spacing. Programmers insert these white-space characters to enhance program clarity. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Web hosting compare - 152 Control Structures: Part 1 Chapter 4 Consider

Friday, September 14th, 2007

152 Control Structures: Part 1 Chapter 4 Consider the flowchart segment for the sequence structure in Fig. 4.1. We use the rectangle symbol, also called the action symbol, to indicate any type of action, including a calculation or an input/output operation. The flowlines in the figure indicate the order in which the actions are to be performed; first, grade is to be added to total, and then 1 is to be added to counter. Java allows us to have as many actions as we want in a sequence structure. As we will soon see, anywhere a single action may be placed, we may instead place several actions in sequence. When drawing a flowchart that represents a complete algorithm, an oval symbol containing the word Begin is the first symbol used in the flowchart; an oval symbol containing the word End indicates where the algorithm ends. When drawing only a portion of an algorithm, as in Fig. 4.1, the oval symbols are omitted in favor of small circle symbols, also called connector symbols. Perhaps the most important flowcharting symbol is the diamond symbol, also called the decision symbol, which indicates that a decision is to be made. We will discuss the diamond symbol in the next section. Java provides three types of selection structures; we discuss each in this chapter and in Chapter 5. The if selection structure either performs (selects) an action, if a condition is true, or skips the action, if the condition is false. The if/else selection structure performs an action if a condition is true and performs a different action if the condition is false. The switch selection structure (Chapter 5) performs one of many different actions, depending on the value of an expression. The if structure is called a single-selection structure, because it selects or ignores a single action (or, as we will soon see, a single group of actions). The if/else structure is called a double-selection structure, because it selects between two different actions (or groups of actions). The switchstructure is called a multiple-selection structure, because it selects among many different actions (or groups of actions). Java provides three types of repetition structures namely, while, do/while and for. (do/while and for are covered in Chapter 5.) Each of the words if, else, switch, while, do and for are Java keywords. These words are reserved by the language to implement various features, such as Java s control structures. Keywords cannot be used as identifiers, such as for variable names. A complete list of Java keywords is shown in Fig. 4.2. Java Keywords abstract boolean break byte case catch char class continue default do double else extends false final finally float for if implements import instanceof int interface long native new null package private protected public return short Fig. 4.2Java keywords (part 1 of 2). Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Http web server - Chapter 4 Control Structures: Part 1 151 During

Friday, September 14th, 2007

Chapter 4 Control Structures: Part 1 151 During the 1960s, it became clear that the indiscriminate use of transfers of control was the root of much difficulty experienced by software development groups. The finger of blame was pointed at the goto statement (used in several programming languages, including C and Basic), which allows the programmer to specify a transfer of control to one of a very wide range of possible destinations in a program. The notion of so-called structured programming became almost synonymous with goto elimination. Java does not have a gotostatement; however, gotois a reserved word and should not be used in a Java program. The research of Bohm and Jacopini1 had demonstrated that programs could be written without any gotostatements. The challenge of the era for programmers was to shift their styles to goto-less programming. It was not until the 1970s that programmers started taking structured programming seriously. The results have been impressive, as software development groups have reported reduced development times, more frequent on-time delivery of systems and more frequent within-budget completion of software projects. The key to these successes is that structured programs are clearer, easier to debug and modify and more likely to be bug free in the first place. Bohm and Jacopini s work demonstrated that all programs could be written in terms of only three control structures namely, the sequence structure, the selection structure and the repetition structure. The sequence structure is built into Java. Unless directed otherwise, the computer executes Java statements one after the other in the order in which they are written. The flowchart segment in Fig. 4.1 illustrates a typical sequence structure in which two calculations are performed in order. A flowchart is a graphical representation of an algorithm or a portion of an algorithm. Flowcharts are drawn using certain special-purpose symbols, such as rectangles, diamonds, ovals and small circles; these symbols are connected by arrows called flowlines, which indicate the order in which the actions of the algorithm execute. Like pseudocode, flowcharts are often useful for developing and representing algorithms, although pseudocode is strongly preferred by many programmers. Flowcharts show clearly how control structures operate; that is all we use them for in this text. The reader should carefully compare the pseudocode and flowchart representations of each control structure. add grade to total add 1 to counter total = total + grade; counter = counter + 1; Fig. 4.1Flowcharting Java s sequence structure. Fig. 1. Bohm, C., and G. Jacopini, Flow Diagrams, Turing Machines, and Languages with Only Two Formation Rules, Communications of the ACM, Vol. 9, No. 5, May 1966, pp. 336 371. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

150 Control (Zeus web server) Structures: Part 1 Chapter 4 Consider

Thursday, September 13th, 2007

150 Control Structures: Part 1 Chapter 4 Consider the rise-and-shine algorithm followed by one junior executive for getting out of bed and going to work: (1) Get out of bed, (2) take off pajamas, (3) take a shower, (4) get dressed, (5) eat breakfast, (6) carpool to work. This routine gets the executive to work well prepared to make critical decisions. Suppose, however, that the same steps are performed in a slightly different order: (1) Get out of bed, (2) take off pajamas, (3) get dressed, (4) take a shower, (5) eat breakfast, (6) carpool to work. In this case, our junior executive shows up for work soaking wet. Specifying the order in which statements are to be executed in a computer program is called program control. In this chapter and Chapter 5, we investigate the program control capabilities of Java. 4.3 Pseudocode Pseudocode is an artificial and informal language that helps programmers develop algorithms. The pseudocode we present here is particularly useful for developing algorithms that will be converted to structured portions of Java programs. Pseudocode is similar to everyday English; it is convenient and user friendly, although it is not an actual computer programming language. Pseudocode programs are not actually executed on computers. Rather, they help the programmer think out a program before attempting to write it in a programming language, such as Java. In this chapter, we give several examples of pseudocode programs. Software Engineering Observation 4.1 Pseudocode is often used to think out a program during the program design process. Then the pseudocode program is converted to Java. The style of pseudocode we present consists purely of characters, so programmers may conveniently type pseudocode programs using an editor program. The computer can produce a freshly printed copy of a pseudocode program on demand. A carefully prepared pseudocode program may be converted easily to a corresponding Java program. This conversion is done in many cases simply by replacing pseudocode statements with their Java equivalents. Pseudocode normally describes only executable statements the actions that are performed when the program is converted from pseudocode to Java and is run. Declarations are not executable statements. For example, the declaration int i; tells the compiler the type of variable i and instructs the compiler to reserve space in memory for the variable. This declaration does not cause any action such as input, output or a calculation to occur when the program is executed. Some programmers choose to list variables and mention the purpose of each at the beginning of a pseudocode program. 4.4 Control Structures Normally, statements in a program are executed one after the other in the order in which they are written. This process is called sequential execution. Various Java statements we will soon discuss enable the programmer to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 4 Control Structures: Part 1 149 Outline (Professional web hosting)

Wednesday, September 12th, 2007

Chapter 4 Control Structures: Part 1 149 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if/else Selection Structure 4.7 The while Repetition Structure 4.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) 4.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2 (Sentinel-Controlled Repetition) 4.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3 (Nested Control Structures) 4.11 Assignment Operators 4.12 Increment and Decrement Operators 4.13 Primitive Data Types 4.14 (Optional Case Study) Thinking About Objects: Identifying Class Attributes Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 4.1 Introduction Before writing a program to solve a problem, it is essential to have a thorough understanding of the problem and a carefully planned approach to solving the problem. When writing a program, it is equally essential to understand the types of building blocks that are available and to employ proven program construction principles. In this chapter and in Chapter 5, we discuss these issues in our presentation of the theory and principles of structured programming. The techniques you learn here are applicable to most high-level languages, including Java. When we study object-based programming in more depth in Chapter 8, we will see that control structures are helpful in building and manipulating objects. 4.2 Algorithms Any computing problem can be solved by executing a series of actions in a specific order. A procedure for solving a problem in terms of 1. the actions to be executed and 2. the order in which the actions are to be executed is called an algorithm. The following example demonstrates that correctly specifying the order in which the actions are to be executed is important. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

4 Control (Web hosting script) Structures: Part 1 Objectives To

Wednesday, September 12th, 2007

4 Control Structures: Part 1 Objectives To understand basic problem-solving techniques. To be able to develop algorithms through the process of top-down, stepwise refinement. To be able to use the ifand if/else selection structures to choose among alternative actions. To be able to use the while repetition structure to execute statements in a program repeatedly. To understand counter-controlled repetition and sentinel-controlled repetition. To be able to use the increment, decrement and assignment operators. Let s all move one place on. Lewis Carroll The wheel is come full circle. William Shakespeare, King Lear How many apples fell on Newton s head before he took the hint! Robert Frost, comment Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Chapter 3 Introduction to Java Applets 147 (Web hosting ecommerce) 3.12

Tuesday, September 11th, 2007

Chapter 3 Introduction to Java Applets 147 3.12 Write an applet that reads in two floating-point numbers and determines and prints if the first is a multiple of the second. (Hint: Use the modulus operator.) Use only the programming techniques you learned in this chapter and Chapter 2. Draw the results on the applet. 3.13 Write an applet that draws a checkerboard pattern as follows: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 3.14 Write an applet that draws a variety of rectangles of different sizes and locations. 3.15 Write an applet that allows the user to input the four arguments required by method draw- Rect, then draws a rectangle using the four input values. 3.16 The Graphics class contains a drawOval method that takes the same four arguments as method drawRect. However, the arguments for method drawOval specify the bounding box for the oval. The sides of the bounding box are the boundaries of the oval. Write a Java applet that draws an oval and a rectangle with the same four arguments. You will see that the oval touches the rectangle at the center of each side. 3.17 Modify the solution to Exercise 3.16 to output a variety of ovals of different shapes and sizes. 3.18 Write an applet that allows the user to input the four arguments required by method draw- Oval, then draws an oval using the four input values. 3.19 What does the following code print? g.drawString( “*”, 25, 25 ); g.drawString( “***”, 25, 55 ); g.drawString( “*****”, 25, 85 ); g.drawString( “****”, 25, 70 ); g.drawString( “**”, 25, 40 ); 3.20 Using only programming techniques from Chapter 2 and Chapter 3, write an applet that cal culates the squares and cubes of the numbers from 0 to 10 and draws the resulting values in table format as follows: number square cube 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 [Note: This program does not require any input from the user.] Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01