0 Advanced Selection CE00371-1: Introduction to Software Development Week 3 Lecture 2.

Slides:



Advertisements
Similar presentations
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Algorithms and Problem Solving
 2005 Pearson Education, Inc. All rights reserved Introduction.
Fundamentals of Software Development 1Slide 1 Today’s Summary Hello World concepts – –main, static, console Programming patterns involving assignment and.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
15 Sorting1June Sorting CE : Fundamental Programming Techniques.
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
18 File handling1June File handling CE : Fundamental Programming Techniques.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
11 Methods1June Methods CE : Fundamental Programming Techniques.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 1 Program Design
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Chapter 1 Pseudocode & Flowcharts
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Java Programming: From the Ground Up
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 2: Classes and Objects, using Scanner and String.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Computer Science 101 Introduction to Programming.
BY Lecturer: Aisha Dawood.  an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 5 Loops.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CSC Programming I Lecture 6 September 4, 2002.
The Software Development Process
1 Program Planning and Design Important stages before actual program is written.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
ALGORITHMS.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Introduction to C Programming CE Lecture 5 Program Design in C.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
The Hashemite University Computer Engineering Department
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to Computers and Programming Lecture 7:
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Algorithms and Pseudocode
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 10 Using Menus and Validating Input.
Loops A loop is: Java provides three forms for explicit loops:
CS1010 Programming Methodology
Chapter 2 Clarifications
Introduction to programming in java
Chapter 7 Part 1 Edited by JJ Shepherd
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Problem Solving Techniques
SELECTION STATEMENTS (2)
MSIS 655 Advanced Business Applications Programming
Algorithm Discovery and Design
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Looping III (do … while statement)
Week 4 Lecture-2 Chapter 6 (Methods).
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Presentation transcript:

0 Advanced Selection CE : Introduction to Software Development Week 3 Lecture 2

1 In a moment we look at a selection example where we need to be more strategic with the way we design our program. Pseudo-Code A technique called pseudo-code - a highly formal form of English that corresponds to programming language is used for program development. The three variables are now sorted! Orange Box Convention On this module all pseudo-code has an orange box around it. eg give a prompt; read integers a, b, and c ;

2 Pseudo-Code is Generic The three variables are now sorted! give a prompt; read integers a, b, and c ; System.out.println("a,b,c ?"); a = keybd.nextInt( ); b = keybd.nextInt( ); c = keybd.nextInt( ); printf("a,b,c ?"); scanf("%d%d%d", &a, &b, &c); Java C Pseudo-Code

3 Properties of Pseudo-Code The three variables are now sorted! outlines a basic strategy. cannot be compiled. is generic. can be translated into comments ! is concise

4 Sort Example The three variables are now sorted! The requirement is to sort three Integers a,b and c into increasing order. We need to arrange that :- a <= b <= c

5 The requirement is to arrange that three integers a,b and c hold values in ascending order. Namely, a ends up with the smallest value, b the middle one, and c the largest one. Sorting Three Integers To solve this problem, a strategy has to be thought out and then translated into our target programming language. After working with pen and paper on a few examples, the following strategy (algorithm) might occur to you. The three variables are now sorted! before :- a 10 b 14 a 5 b 10 c 5 c 14 after :-

6 Sorting a, b, and c Shuffle the largest value into c as follows:-. (i) Compare a and b and swap if a is larger. (ii) Compare b and c and swap if b is larger. Shuffle the second largest value into b as follows:- (iii) Compare a and b and swap if a is larger. The above strategy is called an algorithm - it works for all possible values of a,b, and c. On the next page we try it out.

7 Dry Running after (i) - no change a 10 b 14 c 5 a 10 b 5 c 14 after (ii) - exchange after (iii) - exchange a 5 b 10 c 14 We’ll check out the strategy for the original data set. NB The state of variables after each step is shown.

8 Read in three ints a, b and c. (i) if (a >b) { swap a and b ; } (ii) if (b >c) { swap b and c ; } (iii) if (a >b) { swap a and b ; } Print out a,b and c. Rough Draft of Sort Remember :- To swap a and b, we do t = a ; a=b; b = t ; Similarly swap b and c, is t = b ; b=c; c = t ; pseudo-code

9 Step-wise Refinement Read in three ints a,b, c. Prompt for input ; Read a ; Read b ; Read c ; // Prompt for input ; System.out.println("Please enter a,b and c"); a = kybd.nextInt( ); // Read a b = kybd.nextInt( ); // Read b c = kybd.nextInt( ); // Read c pseudo-code java pseudo-code

10 Step-wise Refinement if ( a > b ). Swap a and b ; if ( a > b ) { t = a ; a = b ; b = t } And similarly for the other two if then s. pseudo-code java

11 Step-wise Refinement Print out a,b and c. System.out.println( " a = " + a + " b = " + b + " c = " + c ) ; pseudo-code java

12 import java.util.* ; public class SortThree { public static void main (String[ ] args ) { int a, b, c, t ; Scanner kybd = new Scanner(System.in) ; // Read in a,b and c. System.out.println("Please enter a, b, and c") ; a = kybd. nextInt( ) ; b = kybd. nextInt( ) ; c = kybd. nextInt( ) ; Complete Java Sort for Three integers … continued overpage

13 if ( a>b ) // swap (i) { t = a ; a = b ; b= t ; } if ( b>c ) // swap (ii) { t = b; b = c ; c = t ; } if ( a>b ) // swap (iii) { t = a ; a = b ; b= t ; } System.out.println ( "a = " + a + " b= " + b + " c = " + c ) ; }

14 Input Validation One of the more important uses of selection is in the area of validation. A user might not interact properly with a program and this should always be checked for. Is data in range ? Is data of the correct type ? The first check above is fairly easy, but we should also bear in mind the second kind of error. For our simple programs we don't consider the latter.

15 Range Validation Fragment System.out.println("Please enter Grade ?"); grade = keybd.nextInt( ); if ( grade >=0 && grade <= 100) { System.out.println(" A valid grade "); } else { System.out.println("Error : this is " + "an invalid grade"); } Below we show the validation of a grade which should be in the range The user gets an error message if the input is invalid.

16 Looped Validation When we meet iteration next week, a user can be re-prompted if their input is invalid. So our programs can be made more robust. NB In industry a huge percentage of the code is "defensive" and complex sequences of if statements protect the "positive" code, in other words, most of the code looks after bad inputs.