Chapter 3 Program Statements Presentation slides for Java

  • Slides: 21
Download presentation
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design

Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved. Instructors using the textbook may use and modify these slides for pedagogical purposes.

Program Development Ø The creation of software involves four basic activities: • establishing the

Program Development Ø The creation of software involves four basic activities: • establishing the requirements • creating a design • implementing the code • testing the implementation Ø The development process is much more involved than this, but these are the four basic development activities 2

Requirements Ø Software requirements specify the tasks a program must accomplish (what to do,

Requirements Ø Software requirements specify the tasks a program must accomplish (what to do, not how to do it) Ø They often include a description of the user interface Ø An initial set of requirements often are provided, but usually must be critiqued, modified, and expanded Ø Often it is difficult to establish detailed, unambiguous, complete requirements Ø Careful attention to the requirements can save significant time and expense in the overall project 3

Design Ø A software design specifies how a program will accomplish its requirements Ø

Design Ø A software design specifies how a program will accomplish its requirements Ø A design includes one or more algorithms to accomplish its goal Ø An algorithm is a step-by-step process for solving a problem Ø An algorithm may be expressed in pseudocode, which is code-like, but does not necessarily follow any specific syntax Ø In object-oriented development, the design establishes the classes, objects, methods, and data that are required 4

Implementation Ø Implementation is the process of translating a design into source code Ø

Implementation Ø Implementation is the process of translating a design into source code Ø Most novice programmers think that writing code is the heart of software development, but actually it should be the least creative step Ø Almost all important decisions are made during requirements and design stages Ø Implementation should focus on coding details, including style guidelines and documentation 5

Testing Ø A program should be executed multiple times with various input in an

Testing Ø A program should be executed multiple times with various input in an attempt to find errors Ø Debugging is the process of discovering the causes of problems and fixing them Ø Programmers often think erroneously that there is "only one more bug" to fix Ø Tests should consider design details as well as overall requirements 6

The switch Statement Ø The expression of a switch statement must result in an

The switch Statement Ø The expression of a switch statement must result in an integral type, meaning an int or a char Ø It cannot be a boolean value, a floating point value (float or double), a byte, a short, or a long Ø The implicit boolean condition in a switch statement is equality - it tries to match the expression with a value Ø You cannot perform relational checks with a switch statement Ø See Grade. Report. java (page 147)

Comparing Characters Ø We can use the relational operators on character data Ø The

Comparing Characters Ø We can use the relational operators on character data Ø The results are based on the Unicode character set Ø The following condition is true because the character + comes before the character J in the Unicode character set: if ('+' < 'J') System. out. println ("+ is less than J"); Ø The uppercase alphabet (A-Z) followed by the lowercase alphabet (a-z) appear in alphabetical order in the Unicode character set

Lexicographic Ordering Ø Because comparing characters and strings is based on a character set,

Lexicographic Ordering Ø Because comparing characters and strings is based on a character set, it is called a lexicographic ordering Ø This is not strictly alphabetical when uppercase and lowercase characters are mixed Ø For example, the string "Great" comes before the string "fantastic" because all of the uppercase letters come before all of the lowercase letters in Unicode Ø Also, short strings come before longer strings with the same prefix (lexicographically) Ø Therefore "book" comes before "bookcase"

More Operators Ø To round out our knowledge of Java operators, let's examine a

More Operators Ø To round out our knowledge of Java operators, let's examine a few more Ø In particular, we will examine • the increment and decrement operators • the assignment operators • the conditional operator 10

Increment and Decrement Ø The increment and decrement operators are arithmetic and operate on

Increment and Decrement Ø The increment and decrement operators are arithmetic and operate on one operand Ø The increment operator (++) adds one to its operand Ø The decrement operator (--) subtracts one from its operand Ø The statement count++; is functionally equivalent to count = count + 1; 11

Increment and Decrement Ø The increment and decrement operators can be applied in prefix

Increment and Decrement Ø The increment and decrement operators can be applied in prefix form (before the operand) or postfix form (after the operand) Ø When used alone in a statement, the prefix and postfix forms are functionally equivalent. That is, count++; is equivalent to ++count; 12

Increment and Decrement Ø When used in a larger expression, the prefix and postfix

Increment and Decrement Ø When used in a larger expression, the prefix and postfix forms have different effects Ø In both cases the variable is incremented (decremented) Ø But the value used in the larger expression depends on the form used: Expression Operation Value Used in Expression count++ ++count---count add 1 subtract 1 old value new value 13

Increment and Decrement Ø If count currently contains 45, then the statement total =

Increment and Decrement Ø If count currently contains 45, then the statement total = count++; assigns 45 to total and 46 to count Ø If count currently contains 45, then the statement total = ++count; assigns the value 46 to both total and count 14

Assignment Operators Ø Often we perform an operation on a variable, and then store

Assignment Operators Ø Often we perform an operation on a variable, and then store the result back into that variable Ø Java provides assignment operators to simplify that process Ø For example, the statement num += count; is equivalent to num = num + count; 15

Assignment Operators Ø There are many assignment operators, including the following: Operator += -=

Assignment Operators Ø There are many assignment operators, including the following: Operator += -= *= /= %= Example x x x += -= *= /= %= y y y Equivalent To x x x = = = x x x + * / % y y y 16

Assignment Operators Ø The right hand side of an assignment operator can be a

Assignment Operators Ø The right hand side of an assignment operator can be a complex expression Ø The entire right-hand expression is evaluated first, then the result is combined with the original variable Ø Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num); 17

Assignment Operators Ø The behavior of some assignment operators depends on the types of

Assignment Operators Ø The behavior of some assignment operators depends on the types of the operands Ø If the operands to the += operator are strings, the assignment operator performs string concatenation Ø The behavior of an assignment operator (+=) is always consistent with the behavior of the "regular" operator (+)

The Conditional Operator Ø Java has a conditional operator that evaluates a boolean condition

The Conditional Operator Ø Java has a conditional operator that evaluates a boolean condition that determines which of two other expressions is evaluated Ø The result of the chosen expression is the result of the entire conditional operator Ø Its syntax is: condition ? expression 1 : expression 2 Ø If the condition is true, expression 1 is evaluated; if it is false, expression 2 is evaluated 19

The Conditional Operator Ø The conditional operator is similar to an if-else statement, except

The Conditional Operator Ø The conditional operator is similar to an if-else statement, except that it forms an expression that returns a value Ø For example: larger = ((num 1 > num 2) ? num 1 : num 2); Ø If num 1 is greater that num 2, then num 1 is assigned to larger; otherwise, num 2 is assigned to larger Ø The conditional operator is ternary because it requires three operands 20

The Conditional Operator Ø Another example: System. out. println ("Your change is " +

The Conditional Operator Ø Another example: System. out. println ("Your change is " + count + ((count == 1) ? "Dime" : "Dimes")); Ø If count equals 1, then "Dime" is printed Ø If count is anything other than 1, then "Dimes" is printed 21