Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
 2002 Prentice Hall. All rights reserved. 1 Chapter 3 – Control Structures Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Chapter 4 Control Statements: Part I
The University of Texas – Pan American
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
PSEUDOCODE C Programming Technique – Firdaus-Harun.com.
C Lecture Notes 1 Structured Program Development.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Chapter 4: Control Structures II
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved. 1.
1 Lecture 3 Control Structures else/if and while.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
CSC111 Quick Revision.
REPETITION CONTROL STRUCTURE
Control Statements: Part 1
Chapter 5: Control Structures II
Repetition-Counter control Loop
Chapter 4 – Control Structures Part 1
Chapter 4- part 2 Control Statements: Loops 1
Chapter 4 Control Statements: Loops 1
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 3 - Structured Program Development
Chapter 3 – Control Structures
Chapter 3 - Structured Program Development
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Control Statements:.
Presentation transcript:

Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3

Algorithm Finite ordered list of well-defined actions that can be used to solve a problem in a finite amount of time Algorithm – to explore logic: pseudocode state – transition diagrams activity diagrams Method findLargestNumber input: indexed list of numbers, first index is zero, called arr output: index of largest number store arr[0] in variable high initialize variable x with 0 loop from i = 1 to length of arr if (arr[i] > high) x = i high = arr[i] end if end loop return x end method

Object Oriented vs. Structured Object Oriented for arranging objects and interactions Structured for dealing with detailed logic of how things are done can be affected by design of the objects. Example: Car Object But there is a sequence of steps that occur when you press the gas pedal.

Our 2 nd Diagram – Activity Diagram 4 Activity Diagram models logic can be used at various levels of detail (30,000 foot view or microscopic view of) Somewhat similar to flowcharting Sample Activity Diagram

Main Activity Diagram Artifacts 5 Initial Node – indicates starting point of logic Final Node – indicates stopping point of logic Activity – rounded rectangle – represents the activities that are taking place Flow - arrow indicating flow of logic Decision – Diamond with one flow entering and two or more exiting Ambler, Scott. UML 2 Activity Diagrams, August

Main Activity Diagram Artifacts (2) 6 Merge – Diamond – two or more flows entering and one exiting Condition – text in square brackets indicated a condition that must be satisfied to traverse its flow. [credentials valid] [credentials not valid] Ambler, Scott. UML 2 Activity Diagrams, August

Examples with pseudocode 7 If x is greater than 60 print “YOU PASSED” Guard not required. Why?

Examples with pseudocode 8 Loop while value is less than 10 value = value + 2 What would the guards be for flows out of this decision?

Conditional Constructs Execute code based on a condition if (x > 5) System.out.println(“ YAY! “); switch (myVar) { case 1: x = 5; break; case 2: y = 10; break; default: System.out.println(“Read the directions”); } if (x 5) System.out.println(“Whats up?”); else System.out.println(“Go Away!”);

New Conditional Construct Conditional Operator (… ? …. : …. ) Shortcut for simple if…else statement Java’s only Ternary Operator x > 10 ? y = 5 : z = 3; if ( x > 10) y = 5; else z = 3; shortcut for System.out.println(studentGrade >= 60 ? “Passed” : “Failed”);

Don’t forget about … … the dangling else problem if (x > 5) if (y > 10) System.out.println(“You Win!”) else System.out.println(“You Lose!”) What will print if x = 3 and y = 15? … if x = 10 and y = 20?

Block Set of statements that are contained within a pair of braces Can have nested blocks main method’s body is a block can have a block as part of if inside main public static void main (String [] args) { int x = 3; int y = 0; if (x == 3) { x++; y++ } Main Block if block

Loops Counter-controlled loops Loops that iterate for a specified number of iterations uses counter variable or loop control variable to control iterations The program shall read 10 grades and produce their average Sentinel-controlled loops Number of iterations of loop is not known end of data entry usually controlled by sentinel value The program shall read an arbitrary number of grades and the provide the average

Adding method to class Gradebook public void determineAverage() { Scanner input = new Scanner(System.in); int total=0, gradeCounter=1, grade, average; while (gradeCounter <= 10) { System.out.print(“Enter Grade: “); grade = input.nextInt(); total = total + grade; gradeCounter++; } average = total / 10; //remember: int division System.out.printf(“\nTotal: %d”, total); System.out.printf(“\nAverage: %d”, average); } Add a method to GradeBook to enter 10 grades and the output the total and average. counter controlled

Adding method to class Gradebook public void determineAverage() { Scanner input = new Scanner(System.in); int total=0, gradeCounter=1, grade; double average; //ask user for first grade – could be sentinel System.out.println(“Enter grade or -1 to quit: “); grade = input.nextInt(); while (grade != -1){ total = total + grade; gradeCounter = gradeCounter + 1; System.out.println(“Enter grade or -1 to quit: “); grade = input.nextInt(); } if (gradeCounter != 0) { average = (double)total/gradeCounter; //FP arithmetic System.out.printf(“Average is %.2f\n”, average); } else System.out.println(“No grades entered!”); } Add method to GradeBook to determine the average of arbitrary number of grades. Sentinel Controlled

Arithmetic Review int x, y = 5; x = y / 2; System.out.println(x); double x, y = 5; x = y / 2; System.out.println(x);

Casting Implicit casting happens for you behind the scenes int x = 2; double y = 5, z; z = y / x; // x is implicitly converted to double //(promoted) in this case Explicit casting programmer can indicate that casting should happen. place the target data type in parentheses int x = 2, y = 5; double z; z = (double) y / x; //forcing y to become double //so that we get FP answer

Your turn! Modify calculateAverage (either one) such that it counts the number of passing scores and failing scores and reports this information as well to the user.

Software Engineering Top – down, Stepwise Refinement Start at the top and work down until sufficient detail exists to implement in Java Top represents a single statement that conveys the overall function of the program. Consider the calculateAverage problem - Possible Top: Determine the class average for a graded assignment Above is obviously insufficient: Initialize Variables Input, sum and count the grades Calculate and print the class average Each step/refinement is a complete representation of the problem/algorithm … only level of detail varies

Second Refinement Initialize variables can be broken down to: Initialize total to zero Initialize counter to zero Input, sum and count grades can be refined to: Prompt user to enter first grade Input the first grade ( may be sentinel) While the user has not yet entered the sentinel Add this grade into the running total Add one to the grade counter Prompt the user to enter the next grade Input the next grade (may be sentinel) Calculate and print – refined to: if counter is not equal to zero Set the average to the total divided by the counter Print the average else Print “no grades recorded” This is of sufficient detail to implement in Java More refinements may be necessary for more complex problems