CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.

Slides:



Advertisements
Similar presentations
Algorithms and Problem Solving
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
H2-1 University of Washington Computer Programming I Lecture 10: Loop Development and Program Schemas © 2000 UW CSE.
CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem.
By the end of this session you should be able to...
Chapter 5 Loops.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
Chapter 4: Control Structures II
Mixing integer and floating point numbers in an arithmetic operation.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Building java programs, chapter 3 Parameters, Methods and Objects.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC111 Quick Revision.
Introduction to Arrays
2.5 Another Java Application: Adding Integers
Introduction to programming in java
Lecture 07 More Repetition Richard Gesick.
Iterations Programming Condition Controlled Loops (WHILE Loop)
OPERATORS (1) CSC 111.
Building Java Programs
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
Building Java Programs
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Building Java Programs
Building Java Programs
Introduction to Arrays
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Lecture 14 Previous Lecture
CS100J Lecture 15 Previous Lecture This Lecture Sorting
In this class, we will cover:
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
loops revisited I/O arrays
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements –Reading: L&L, Sec ; or S. Sec n This Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem –boundary conditions –validation n Pattern for processing input values up to (but not including) a stopping signal –Example n Processing exam grades –Java Constructs n Casts n Rounding –Reading, Lewis & Loftus, Section 3.9

CS 100Lecture 42 Problem Context n n Input Data: Zero or more exam grades (between 0 and 100), followed by a stopping value of -1. Assume input is well-formed. n n Sample Input Data: n n Degenerate Input Data: n n The General Task: Input the grades and “process” them, i.e., print some information about them.

CS 100Lecture 43 Sample Tasks 1. Print the grades. 2. Print the number of grades. 3. Print the average grade. 4. Print the highest grade. 5. All of the above.

CS 100Lecture 44 Programming Rules of Thumb Learn program patterns of general utility and use a relevant pattern (if you know one) Learn program patterns of general utility and use a relevant pattern (if you know one) for the problem at hand. n n Seek inspiration by systematically working test data by hand. Be introspective; ask yourself: “What am I doing?” n n Declare variables for each piece of information you maintain when working the problem by hand. Write comments that precisely describe the contents of each variable. n n Remember to consider the problem’s boundary conditions. n n Validate your program by tracing it on simple test data.

CS 100Lecture 45 A Useful Pattern /* “Process” integer input values until (but not including) some designated stoppingValue. */  variable = in.readInt(); variable = in.readInt(); while ( variable != stoppingValue ) while ( variable != stoppingValue ) {  variable = in.readInt(); variable = in.readInt(); }  /* Do  for each integer between 1 and n. */  variable = 1; variable = 1; while ( variable <= n ) while ( variable <= n ) {  variable = variable + 1; variable = variable + 1; }  Contrast with:

CS 100Lecture 46 Placeholders in Pattern  variable, , , , and stoppingValue are placeholders variable contains the most recently read input value. Replace it uniformly by a name that is meaningful for the given problem. stoppingValue is the value designated to signal the end of the input data.   is the initialization: statements that initialize variables before any input value is processed.   is where you process each input value except the stoppingValue.   is the finalization: where you perform operations after all input values have been processed.

CS 100Lecture 47 Pattern for “processing” grades /* “Process” grades up to (but not including) a stopping signal of -1. */  grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) {  grade = in.readInt(); grade = in.readInt(); }  int grade; // the grade being processed.

CS 100Lecture 48 Task 1: Print the grades where  :  System.out.println( grade );  /* Print each grade input up to a stopping signal of -1. */  grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) {  grade = in.readInt(); grade = in.readInt(); }  int grade; // the grade being processed.

CS 100Lecture 49 Task 2: Count the grades where  : count = 0;  count = count + 1;  System.out.println( count ); /* Print count of grades input up to a stopping signal of -1. */  grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) {  grade = in.readInt(); grade = in.readInt(); }  int grade; // the grade being processed. int count; // # of grades so far.

CS 100Lecture 410 Task 3: Print the average where  : count = 0; sum = 0;  : count = count + 1; sum = sum + grade;  : System.out.println( sum / count ); /* Print average of grades input up to a stopping signal of -1. */  grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) {  grade = in.readInt(); grade = in.readInt(); }  int grade; // the grade being processed. int count; // # of grades so far. int sum; // sum of grades so far.

CS 100Lecture 411 Casts and Rounding This statement prints the average using integer division, e.g. 7/2 is 3 : This statement prints the average using integer division, e.g. 7/2 is 3 :  : System.out.println ( sum/count ); This statement prints the average using “double precision floating point division”, e.g., (double)7/2 is 3.5 : This statement prints the average using “double precision floating point division”, e.g., (double)7/2 is 3.5 :  ’  : System.out.println ( (double)sum/count (double)sum/count ); ); n An expression of the form (type)expression (type)expression is a cast, which converts the value of expression to a value of the given type, if possible. is a cast, which converts the value of expression to a value of the given type, if possible. This statement prints the average as a “rounded double precision floating point number”, e.g., Math.round((double)7/2) is 4 : This statement prints the average as a “rounded double precision floating point number”, e.g., Math.round((double)7/2) is 4 :  ’’  : System.out.println ( Math.round((double)sum / count) Math.round((double)sum / count) ); );

CS 100Lecture 412 Boundary Conditions n What if no one takes the exam?  ’’’: if ( count == 0 ) if ( count == 0 ) System.out.println(”No grades”); System.out.println(”No grades”); else else System.out.println( System.out.println( ”Average: ” + (double)sum / count ”Average: ” + (double)sum / count ); );

CS 100Lecture 413 Task 4: Print highest grade where  : high = 0;  if (grade > high) high = grade;  ’  high = Math.max(grade, high);  System.out.println( high ); /* Print max of grades input up to a stopping signal of -1. */  grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) {  grade = in.readInt(); grade = in.readInt(); }  int grade; // the grade being processed. int high; // highest grade so far.

CS 100Lecture 414 Boundary Conditions Again n What if no one takes the exam?  ’’’  : if ( count == 0 ) if ( count == 0 ) System.out.println(”No grades”); System.out.println(”No grades”); else else System.out.println(”High: ” + high); System.out.println(”High: ” + high); (where we are anticipating that there will be a variable count in the final program)

CS 100Lecture 415 Task 5: Putting it all together int grade; // the grade being processed. int count; // # of grades so far. int sum; // sum of grades so far. int high; // highest grade so far. /* Print statistics on 0 or more grades in the range 0-100, followed by a stopping signal of -1. */ import java.io.*; public class CUCSApplication { public static void main(String args[]) { declarations // Initialize Text object in to read // from standard input. TokenReader in = new TokenReader(System.in); statements } where declarations are: and where statements are:

CS 100Lecture 416 Task 5: Putting it all together, cont. /* Print statistics on grades input up to, but not including, a stopping signal of -1. */ count = 0; count = 0; sum = 0; sum = 0; high = 0; high = 0; grade = in.readInt(); grade = in.readInt(); while (grade != -1 ) while (grade != -1 ) { System.out.println(”Grade: ” + grade); System.out.println(”Grade: ” + grade); count = count + 1; count = count + 1; sum = sum + grade; sum = sum + grade; high = Math.max(grade, high); high = Math.max(grade, high); grade = in.readInt(); grade = in.readInt(); } if (count == 0) if (count == 0) System.out.println(“No grades”); System.out.println(“No grades”); else { else { System.out.println(”#grades: ” + count); System.out.println(”#grades: ” + count); System.out.println ( System.out.println ( ”Average: ” + (double)sum / count ”Average: ” + (double)sum / count ); ); System.out.println (”High: ” + high); System.out.println (”High: ” + high); }