1 / 27

Program Structure

***** SWTJC STEM *****. Program Structure. Structure refers to how the internal parts of programs are put together. Software is “structured” by design rules that have been adopted and used. Design rules make structured programs . . . Easy to write and read

celina
Télécharger la présentation

Program Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ***** SWTJC STEM ***** Program Structure • Structure refers to how the internal parts of programs are put together. • Software is “structured” by design rules that have been adopted and used. • Design rules make structured programs . . . • Easy to write and read • Easy to debug (find  mistakes and errors) • Easy to understand • Easy to modify • Easy to reuse Chapter 5 cg 49

  2. ***** SWTJC STEM ***** Program Structure cont. • Program Structure (cont.) • No one wants an expensive, flashy sports car that stalls in traffic because of poor design. • No one wants a program with a trendy, colorful Windows interface that suddenly crashes with the infamous “blue screen of death”. Chapter 5 cg 49

  3. ***** SWTJC STEM ***** Flow of control is the order in which program statements execute within a method. Flow of Control Three Control Structures • Sequence • Allows you to create a step-by-step process. • Selection (Decision Making) • Allows you to choose which action to take . • Repetition (Loops) • Allows you to execute an action many times. These three structures constitute all that is needed to program any algorithm! Chapter 5 cg 50

  4. Entry Point Step 1 Step 2 Step 3 Step 4 Exit Point ***** SWTJC STEM ***** Flow of ControlSequence Structure Unless otherwise directed, programs proceed in a step by step manner. Note there is one entry point and one exit point. Chapter 5 cg 51

  5. Entry Point Step 1 Step 2 Step 3 Step 4 Exit Point ***** SWTJC STEM ***** Sequence StructureWith a Method Execution Entry Point Step 1 Step 2 Method Step 3 Step 4 Exit Point At step 2 the method is invoked. After completion, execution returns to the next step (step 3 in this case). Chapter 5 cg 51

  6. ***** SWTJC STEM ***** Flow of ControlSelection Construct Choices • One-way selection - Use simple if. • Two-way selection - Use if - else. • Multi-way selection - Use either: • Differing conditions - Useif - else if - else if - .... - else. • Same variable or expression with multiple choices - Useswitch. Chapter 5 cg 52

  7. ***** SWTJC STEM ***** Flow of ControlWith a Selection Structure • Selection structure is handled using aselection statement. • In Java, there are four selection statement constructs: • ifcondition. . . • if condition . . . else • if condition . . . else if . . . else if . . .else • switch . . . case 1condition . . . case 2 condition . . . default • In each case, the condition is based on a Boolean expression that evaluates to either true or false. • Boolean expressions contain Boolean variables, literals (true and false), Boolean relational operators, and logical operators. Chapter 5 cg 52-53

  8. Relational Operator Use Returns true if > op1 > op2 op1 is greater than op2 >= op1 >= op2 op1 is greater than or equal to op2 < op1 < op2 op1 is less than op2 <= op1 <= op2 op1 is less than or equal to op2 == op1 == op2 op1 and op2 are equal != op1 != op2 op1 and op2 are not equal ***** SWTJC STEM ***** • Relational expressions compare numeric, char, and string expressions taking the form • expression1 relational operator expression2 • Example:(x + 2) > y2 Boolean Relational Operators Chapter 5 cg 54

  9. ***** SWTJC STEM ***** Boolean Relational Examples • Note that the relational operator for equality is double equal sign==as distinguished from the assignment operator that is a single equal sign=. • Boolean variables hold Boolean values; i.e.,trueorfalse. • Examples: • boolean lightsOn = true;Declares a Boolean variable lightsOn and assigns it the valuetrue. • int i = 10, j = 5;boolean bValue = (i > j);Boolean expression (i > j)evaluates totrue(iis greater thanj)thena true value is assigned to bValue. Chapter 5 cg 54

  10. ***** SWTJC STEM ***** • Examples (cont.): • int i = 10, j = 12; boolean bValue = (j - 3 > i);Boolean expression (j - 3 > i)evaluates tofalse(9is notgreater than10),thena false value is assigned to bValue.* • int waterTempMax = 230, waterTemp = 240;boolean tempAlarm = (waterTemp >= waterTempMax);Boolean expression (waterTemp >= waterTempMax)evaluates totrue (waterTemp is greater than or equal to waterTempMax),then atrueis assigned totempAlarm. Boolean Relational Examples *Note: Relational operators are lower precedence than numeric or character/string operators and are done last. Chapter 5 cg 54

  11. ***** SWTJC STEM ***** One-Way Selection Entry Point Inone-way selection, the simpleifstatement appears as if ( relational expression ) executable statement; “Statement” is executed only if “Decision” istrue. Otherwise,execution skips “Statement” and passes to the “Exit Point”. false Decision true Statement Exit Point Chapter 5 cg 55

  12. ***** SWTJC STEM ***** One-Way Selection Example Entry Point false Age<21? Consider this example: “If a person is a minor (under 21 years of age), print the message ‘Youth is a wonderful thing. Enjoy it!’. In any case, print the message, ‘Age is a state of mind.’.” true “Youth is . . .” “Age is . . .” Exit Point Chapter 5 cg 55

  13. ***** SWTJC STEM ***** The Java code from AgeOneWay.java is . . . final int MINOR = 21; int age = 18; if ( age < MINOR) System.out.println(“Youth is a wonderful thing. Enjoy it!”); System.out.println("Age is a state of mind."); . . . One-Way Selection Code Iftrue • Note that the executable part of the ifstatement is indented to indicate it is governed by the ifstatement. • The indentation is strictly for human readability! Only the first executable line after the if is governed by the if. See the red bracket above. Chapter 5 cg 55

  14. Decision ***** SWTJC STEM ***** Two-Way Selection In two-way selection, the if-elsestatement permits execution of different statements for true andfalse. It appears as if ( relational expression ) executable statement - true; else executable statement-false; “Step 2T” is executed only if “Decision” istrue, while “Step 2F” is executed only if “Decision” is false. Entry Point Step 1 false true Step 2T Step 2F Exit Point Chapter 5 cg 56

  15. ***** SWTJC STEM ***** Entry Point Two-Way Selection Example Consider this example: “If a person is a minor (under 21 years of age), print the message ‘Youth is a wonderful thing. Enjoy it!’. If a person is not a minor, print the message, “Too soon the bird of youth flies the coop.” In any case, print the message, ‘Age is a state of mind.’.” true Age<21? false “Youth is . . .” “Too soon . . .” “Age is . . .” Exit Point Chapter 5 cg 56

  16. ***** SWTJC STEM ***** From AgeTwoWay.java, . . . final int MINOR = 21; int age = 18; if ( age < MINOR) System.out.println(“Youth is a wonderful thing. Enjoy it!”); else System.out.println(“Too soon the bird of youth flies the coop!”); System.out.println("Age is a state of mind."); . . . Two-Way SelectionCode If true If false Note that the executable part of the ifandelsestatements are single lines and indented for readability to indicate they are governed by the ifstatement. Chapter 5 cg 56

  17. Decision ***** SWTJC STEM ***** When more than one statement appears in an if-else structure use braces { } to define a block. It appears as if ( relational expression ) { executable statement 2t; executable statement 3t; . . . } else { executable statement 2f; executable statement 3f; . . . } Entry Point Multi-statement Selection Step 1 true false ex.st 2f ex.st 2t ex.st 3t ex.st 3f Exit Point Chapter 5 cg 56

  18. ***** SWTJC STEM ***** Multi-Statement Example Entry Point Consider this problem: “A segment of code is used to increment a variable named num. However, when num reaches 100, instead of incrementing it is reset to zero. An appropriate message is output for each situation.” true num<100? false num = 0 num = num +1 “num is reset” “num is . . .” Exit Point Chapter 5 cg 56

  19. ***** SWTJC STEM ***** Ablockis a sequence of statements delimited by a pair of braces{ }. From Incr.java, Multi-Statement Code . . . int num = 50; if (num < 100) { ++num; System.out.println(“num is incremented to “ + num); } else { num = 0; System.out.println(“num is reset to “ + num); } . . . If true true block false block If false Chapter 5 cg 56

  20. ***** SWTJC STEM ***** Chapter 5 Part a Nested if’s • A block can contain additional if-else statements, with additional indented blocks. Such constructs are called nested if’s. • Next slide please. Chapter 5 cg 59

  21. ***** SWTJC STEM ***** • Consider the problem: • “If the temperature is 80 degrees or greater and the humidity is greater than 60%, most people find it uncomfortably warm. If the temperature is greater than 80 degrees and the humidity is 60% or less, most people find it warm, but not uncomfortable. Temperatures below 80, regardless of humidity, are comfortable. Create a program segment that represents this situation.” Nested if’s Entry Point true T<80? false Comfortable true H≤60? false Warm but Uncomfortable Warm and uncomfortable Nested if Exit Point Chapter 5 cg 59

  22. Nested if ***** SWTJC STEM ***** • From AgeOneWay.java, • . . . • if (temperature < 80) { • return “T = “ + Int.toString( temperature) + • “, H= “ + Int.toString(humidity) + • “: It's comfortable."; • } • else { • if (humidity <= 60) • { • return “T = “ + Int.toString( temperature) + • “, H= “ + Int.toString(humidity) + “: It's warm, but comfortable."; • } • else { • return “T = “ + Int.toString( temperature) + • “, H= “ + Int.toString(humidity) + • “: It's warm and uncomfortable!"; • } • } Nested if’s Chapter 5 cg 59

  23. ***** SWTJC STEM ***** Comfort Object (UML) Comfort ---------------------------------------------------- temperature: int humidity: int ---------------------------------------------------- setTemperature (int value): void getTemperature( ): int setHumidity (double int): void getHumidity( ): int isComfortable: boolean toString( ): String Class Attributes Methods Unified Modeling Language (UML) Diagram. Chapter 5 cg 59

  24. ***** SWTJC STEM ***** • public class Comfort { • private int temperature, humidity; • public Comfort(int temperature, int humidity) { • this.temperature = temperature; • this.humidity = humidity; • } Declarations and Constructor Chapter 5 cg 59

  25. ***** SWTJC STEM ***** • public void setTemperature(int temperature){ • this.temperature = temperature; • } • public void setHumidity(int humidity) { • this.humidity = humidity; • } • public int getTemperature() { • return temperature; • } • public int getHumidity() { • return humidity; • } Attribute Setters and Getters Chapter 5 cg 59

  26. ***** SWTJC STEM ***** • public boolean isComforatble( ) { • if (temperature < 80) { • return true; • } • else { • if (humidity <= 60) { • return true; • } • else { • return false; • } • } • } isComfortable Check true T<80? false Comfortable true H≤60? false Warm but Uncomfortable Warm and uncomfortable Chapter 5 cg 59

  27. ***** SWTJC STEM ***** • public String toString () { • if (temperature < 80) { • return "T = " + Integer.toString(temperature) + • ", H = " + Integer.toString(humidity) + • ": It's comfortable."; • } • else { • if (humidity <= 60) { • return "T = " + Integer.toString(temperature) + • ", H = " + Integer.toString(humidity) + • ": It's warm, but comfortable."; • } • else { • return "T = " + Integer.toString(temperature) + • ", H = " + Integer.toString(humidity) + • ": It's warm and uncomfortable!"; • } • } • } toString Method Chapter 5 cg 59

More Related