(I m not printing out these notes! Take your own.)

Similar documents
DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Midterms Save the Dates!

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

QUICK EXCEL TUTORIAL. The Very Basics

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

Lecture 3. Input, Output and Data Types

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

3. Simple Types, Variables, and Constants

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Com S 127x - Lab 6 1. READING FLOWCHARTS WITH CONDITIONAL ACTIONS!

Section 0.3 The Order of Operations

T H E I N T E R A C T I V E S H E L L

Modular Arithmetic. Marizza Bailey. December 14, 2015

Lecture Transcript While and Do While Statements in C++

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CS1 Lecture 3 Jan. 22, 2018

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Chapter 2: Input, Processing, and Output

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

ENGR 101 Engineering Design Workshop

n = 1 What problems are interesting when n is just 1?

Lecture 2: SML Basics

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Section we will not cover section 2.11 feel free to read it on your own

Variables, expressions and statements

SNS COLLEGE OF ENGINEERING

CS1 Lecture 3 Jan. 18, 2019

Topic 2: Decimals. Topic 1 Integers. Topic 2 Decimals. Topic 3 Fractions. Topic 4 Ratios. Topic 5 Percentages. Topic 6 Algebra

Mr G s Java Jive. #11: Formatting Numbers

2 A little on Spreadsheets

Properties and Definitions

Our Strategy for Learning Fortran 90

Spring 2017 CMSC 140 Programming Project 7: Payroll

Part II Composition of Functions

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

KNOWLEDGE OF NUMBER SENSE, CONCEPTS, AND OPERATIONS

CHAPTER 1: INTEGERS. Image from CHAPTER 1 CONTENTS

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

CS102: Variables and Expressions

Topic Notes: Bits and Bytes and Numbers

ITSE 1411 Beg. Web Programming

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

AP Computer Science A

Chapter 2: Data and Expressions

CS1 Lecture 5 Jan. 25, 2019

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Algorithms and Programming I. Lecture#12 Spring 2015

Computer Science 210: Data Structures

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Expressions and Casting

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Topic Notes: Bits and Bytes and Numbers

Chapter 17. Fundamental Concepts Expressed in JavaScript

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Programming Language 2 (PL2)

Computing Seminar Introduction Oct

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Introduction to Computer Use II

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Post Experiment Interview Questions

GENERAL MATH FOR PASSING

Civil Engineering Computation

1 Anatomy of a Program 4

Introduction to C Final Review Chapters 1-6 & 13

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

EC121 Mathematical Techniques A Revision Notes

SECTION 3. ROUNDING, ESTIMATING, AND USING A CALCULATOR

Solving Equations with Inverse Operations

Introduction to Scientific Computing

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 2 Working with Data Types and Operators

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Intro. Scheme Basics. scm> 5 5. scm>

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Rational Number is a number that can be written as a quotient of two integers. DECIMALS are special fractions whose denominators are powers of 10.

If Statements, For Loops, Functions

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Add Subtract Multiply Divide

Text Input and Conditionals

Lesson Plan. Preparation

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs

Transcription:

PT1420 Week 2: Software Program Design I (I m not printing out these notes! Take your own.) Today we'll be discussing designing programs: Algorithms and errors Flowcharts and pseudocode Sequence structures Variables, constants, and the order of operations We re in the first part of Chapter 2, from pages 29 55: 2.1: Designing a Program, pages 29 34; 2.2: Output, Input, and Variables, pages 34 43; and 2.3: Variable Assignment and Calculations, pages 43 55. If you want a paper copy of the textbook, or of the PowerPoint I m not using, it s up at the front by the sign in sheet. We ll be working, to start, with a program which does the following: If I know how many hours I worked, and I know how much I get paid per hour, how much money am I owed? We ll later be adjusting this to tell me not only how much am I owed, but how much will I get after taxes. (We are probably not going to deal yet with overtime, but if we don t we ll discuss why.) The Three Step Process of Programming Broadly speaking, there are three big steps which programs do when they run. 1. They take some sort of input. (This step is sometimes, rarely, skipped.) 2. They do something (to it) some sort of process. 3. They produce some sort of output.

This is illustrated on page 34, the Input > Process > Output steps. We re looking today at the Process step. We ll take input from the keyboard and output to the screen. We ll use Raptor to make a flowchart and run it. The Software Development Process The software development cycle is really high level illustrated on page 30: The design the program step is the hard one. (This is part contract/specification to protect you from disagreements/lawsuits.) This is also where you d perhaps want to start making flowcharts or using pseudocode. You d then write code. The first type of error you might find are syntax errors. This is an error in programming which prevents the program from even running (or compiling). 3+ is an example of a math expression with a syntax error; that plus sign needs to have something after it. Once your program runs (there are no more syntax errors) it may still have bugs. (It will have bugs.) These will be logic errors. The program is doing what you said as opposed to what you meant.

This is an important distinction, syntax errors versus logic errors. Logic errors are harder to find. The design step involves working out the algorithm you intend your program to use. These are the steps you re going to use to use to perform the tasks. There s an example on page 31: The problem we had above (with the paycheck) could have an algorithm like this: 1. Ask the user how many hours he worked. 2. (Store that number of hours in a variable.) 3. Ask the user how much he makes per hour. 4. (Store that number, of dollars and cents, in a different variable.) 5. Calculate the pay, by multiplying those two variables together and storing that product in a third variable. 6. Display the pay the user should expect (that third variable). This algorithm is a sequential structure because the steps are performed in order straight down. The other two control structures are decision structures (if then branching) and repetition structures (loops). This algorithm has three variables: The number of hours worked The amount of pay per hour

Gross pay There are rules and good practice about naming variables. You can t start a variable name with a number. You can t have a space in a variable name. You can t have weird characters in variable names. For example, a%b is not a valid variable name, but it may not produce a syntax error because in some programming languages a%b means the remainder when a is divided by b (it s called modulo). You do want to make your variable names useful. HoursWorked is a good variable name in this program. Don t make them really long. You may see variables always starting with lower case, for example hoursworked. (This is called camel case.) You may be able to use an underscore, Hours_Worked, if that s useful. You don t want to use ALL CAPS: HOURSWORKED. In most environments, all caps means that a variable is a constant. For example, PI=3.1415 could be a definition of the PI variable as a constant 3.1415. We ll talk more about constants. You only want to use short variables like i, a, b, x, when you re doing something really short and obvious. For example, for (i=1, i<10, i++) { print i; } is a loop which just prints the numbers 12345678910. Using i is ok here because its use is brief and obvious. Back to this algorithm with variables: The number of hours worked: HoursWorked [number with decimals, a floating point number] The amount of pay per hour: PayPerHour [number with decimals, a floating point number]

Gross pay: GrossPay [number with decimals, a floating point number] We re going to look at making pseudocode for this task, and also at using Raptor to make a flowchart and running it. Pseudocode could look like: 1. Print How many hours did you work? 2. Input HoursWorked 3. Print How much is your hourly pay? 4. Input PayPerHour 5. Set GrossPay = HoursWorked * PayPerHour 6. Print Your paycheck should be for $, GrossPay One last topic: Variables, constants, and the order of operations We ve spoken briefly about variables. You want to name your variables sensibly. You want to figure out in advance which variables you need, and what sort of data is stored in them. Some common sorts of data are integers, floating point numbers, character, string (of characters), or boolean (true/false bits). Sometimes you define a variable as a constant, which means (1) the value won t change, (2) the value can t change, and (3) the compiler may do things to make your use of that variable more efficient. const PI=3.1415 const COMPANY_NAME= ITT Technical Institute, Inc. const MINIMUM_WAGE=7.25 Programs also use constants. Constants can be numbers. 1, 2, 3.14. Think carefully about using constants in your code should they be constants?

String constants are common. When we said How many hours did you work? we created a string constant. When we want to set or change variables, we use the Order of Operations (PEMDAS). First comes parentheses, from the inside out. Use parentheses every time you think you re doing something confusing. GrossPay = (HourlyWage * HoursWorked) Next comes exponents. Usually this uses the character ^ or **. 2^3 would be 8 (2x2x2=8). Some languages would say 2**3. Multiplication * and Division / left to right. Multiplication uses the * character. Division uses the / character. Think carefully about using parentheses here if your math is complicated. Addition and Subtraction, left to right. Usual symbols, + and. Again, use parentheses if your math is complicated. The syntax for setting a variable s value is Variable Name = some expression and the variable s name must be alone on the left hand side. If I have a statement (p. 49) Set outcome = 12 + 6 / 3 I might want to write it Set outcome = 12 + (6 / 3) because even though the parentheses are unnecessary, putting them there makes it clear to me when I look at it that that part happens first. and maybe what I wanted was Set outcome = (12 + 6) / 6