Pseudocode and Flowcharts

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CMT Programming Software Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Program Design and Development
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Simple Program Design Third Edition A Step-by-Step Approach
Chapter 2 part #4 Operator
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3 Numerical Data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double.
Variables, Operators, and Expressions
Chapter # 2 Part 2 Programs And data
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Expressions.
Chapter 4 Assignment Statement
Lecture 3 Java Operators.
Expressions.
Chapter 2 Elementary Programming
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Intro to OOP with Java, C. Thomas Wu Numerical Data
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Assignment and Arithmetic expressions
Primitive and Reference Data Values
The Selection Structure
Variables, Expressions, and IO
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 4 Defining Instantiable Classes
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Lecture 3 Expressions Richard Gesick.
Chapter 2: Basic Elements of Java
Chapter 3 Numerical Data
Chapter 2 Variables.
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
CS2011 Introduction to Programming I Elementary Programming
Data Types and Expressions
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 6 Selection Statements
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Pseudocode and Flowcharts Expressing Algorithms to Solve Programming Challenges 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Program Development Define the problem Outline the solution Develop the outline into an algorithm Test the algorithm (desk check) Code the algorithm Run the program and debug it Document and maintain the program 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Define the Problem Inputs, Processes, Outputs Defining Diagram 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Outline the Solution Main logic Major processing steps Variables Control structures Subtasks 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Develop an Algorithm Pseudocode Flowcharts 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Code, Debug, Document and Maintain Don’t code until you’ve tested the algorithm in your pseudocode or flowchart. Debugging is easier the more developed your algorithm is. Document as you code. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Pseudocode Rules Statements are written in simple English subject verb predicate (sometimes an adjective) Each instruction on a separate line Keywords and Indentation signify control structures Each set of instructions written top to bottom with one entry and one exit Groups of statements are formed into a module and named 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Algorithms Developing good algorithms can save you hours to days worth of time Make sure to use defining diagrams to plot out you input, processing, and output 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Club Check Develop a program pseudocode that gets the year a person was born. If they are 21 or older they are admitted to the club. If they are under 21 they aren’t. Create a defining diagram and pseudocode. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Flowcharts Can use Word for these symbols Let’s put our algorithm in a Flowchart 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Reference Make sure to keep this handout for reference as we move through the programs. It will come in handy. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Chapter 3 Numerical Data 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Chapter 3 Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write arithmetic expressions in Java. Evaluate arithmetic expressions using the precedence rules. Describe how the memory allocation works for objects and primitive data values. Write mathematical expressions using methods in the Math class. Write programs that input and output data using the InputBox and OutputBox classes from the javabook package. Apply the incremental development technique in writing programs. (Optional) Describe how the integers and real numbers are represented in memory. In this chapter, we will study how to manipulate numerical data in computer programs. We also will introduce two classes—InputBox and OutputBox—from the javabook package. We use an InputBox object to get input data from the user and an OutputBox object to output data. Lastly, we will present a technique for writing programs called incremental development. With this technique we implement programs in small incremental steps, an approach indispensable in developing large programs. We will implement a sample program using the incremental development technique. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Manipulating Numbers In Java, to add two numbers x and y, we write x + y But before the actual addition of the two numbers takes place, we must declare their data type. If x and y are integers, we write int x, y; or int x; int y; 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Variables When the declaration is made, memory space is allocated to store the values of x and y. x and y are called variables. A variable has three properties: A memory location to store the value, The type of data stored in the memory location, and The name used to refer to the memory location. Sample variable declarations: int x; int v, w, y; 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Numerical Data Types There are six numerical data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne, numberTwo; long bigInteger; double bigNumber; At the time a variable is declared, it also can be initialized. For example, we may initialize the integer variables count and height to 10 and 34 as int count = 10, height = 34; In the same way that you can initialize variables at the time you declare them, you can declare and create an object at the same time. For example, the declaration MainWindow mainWindow = new MainWindow(); is equivalent to MainWindow mainWindow; mainWindow = new MainWindow(); 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Data Type Precisions The six data types differ in the precision of values they can store in memory. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Using Numerical Data Types Choosing the data type has memory consequences in large programs Don’t use double when int will do Why? In most cases, we’ll use int (4 bytes) for integers and double for real numbers (higher precision) What’s higher precision? 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Declaring and Creating Objects We have used: MainWindow mainWindow; --declare mainWindow = new MainWindow(); -- create Just use: MainWindow mainWindow = new MainWindow(); 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Assignment Statements We assign a value to a variable using an assignment statements. The syntax is <variable> = <expression> ; Examples: sum = firstNumber + secondNumber; avg = (one + two + three) / 3.0; 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Primitive Data Declaration and Assignments Chapter 3 Primitive Data Declaration and Assignments int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; firstNumber secondNumber A. Variables are allocated in memory. B. Values are assigned to variables. 234 87 A int firstNumber, secondNumber; int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; B firstNumber = 234; secondNumber = 87; Code State of Memory 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Assigning Numerical Data Chapter 3 Assigning Numerical Data int number; number = 237; number = 35; number A. The variable is allocated in memory. B. The value 237 is assigned to number. 237 C. The value 35 overwrites the previous value 237. 35 int number; number = 237; number = 35; A int number; B number = 237; C number = 35; See the note page on the next slide. Code State of Memory 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Assigning Objects customer A. The variable is allocated in memory. Customer customer; customer = new Customer( ); B. The reference to the new object is assigned to customer. Customer C. The reference to another object overwrites the reference in customer. Customer A Customer customer; B customer = new Customer( ); Customer customer; customer = new Customer( ); C customer = new Customer( ); Code State of Memory 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Arithmetic Operators The following table summarizes the arithmetic operators available in Java. This is an integer division where the fractional part is truncated. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

State of Memory Differences Look at page 89 number versus customer variables number variable contains value object variable contains a pointer to memory location 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Having Two References to a Single Object Chapter 3 Having Two References to a Single Object Customer clemens, twain; clemens = new Customer( ); twain = clemens; A. Variables are allocated in memory. clemens twain B. The reference to the new object is assigned to clemens. Customer C. The reference in twain is assigned to customer. A Customer clemens, twain; B clemens = new Customer( ); Customer clemens, twain, clemens = new Customer( ); twain = clemens; C twain = clemens; Code State of Memory 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Different Data Types Numerical data types contain the space necessary to store data when they are created Objects only create a new object reference with the reserved word new Let’s look at page 90 Because of this… Objects are reference data types Numbers are primitive data types 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Arithmetic Expression Chapter 3 Arithmetic Expression How does the expression x + 3 * y get evaluated? Answer: x is added to 3*y. We determine the order of evaluation by following the precedence rules. A higher precedence operator is evaluated before the lower one. If two operators are the same precedence, then they are evaluated left to right for most operators. You need to write the expression as (x + 3) * y if you want to multiply y to the sum of x and 3. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Precedence Rules How would be the result of the following expression? 4 * 3 / 2 + 5 Express the following formula in Java. -x(y + z) 4w 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Type Casting If x is a float and y is an int, what will be the data type of the following expression? x * y The answer is float. The above expression is called a mixed expression. The data types of the operands in mixed expressions are converted based on the promotion rules. The promotion rules ensure that the data type of the expression will be the same as the data type of an operand whose type has the highest precision. When an arithmetic expression consists of variables and constants of the same data type, then the result of the expression will be that data type also. When the data types of variables and constants in an arithmetic expression are of different data types, then a casting conversion will take place. A casting conversion, or type casting, is a process that converts a value of one data type to another data type. Two types of casting conversions in Java are implicit and explicit. An implicit conversion called numeric promotion is applied to the operands of an arithmetic operator. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Explicit Type Casting Instead of relying on the promotion rules, we can make an explicit type cast by prefixing the operand with the data type using the following syntax: ( <data type> ) <expression> Example (float) x / 3 (int) (x / y * 3.0) Type case x to float and then divide it by 3. Type cast the result of the expression x / y * 3.0 to int. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Implicit Type Casting Consider the following expression: double x = 3 + 5; The result of 3 + 5 is of type int. However, since the variable x is double, the value 8 (type int) is promoted to 8.0 (type double) before being assigned to x. Notice that it is a promotion. Demotion is not allowed. int x = 3.5; You can always type cast the value explicitly if you want to assign a higher precision value to a lower precision variable, for example, int x = (int) (14.0 / 5.0); What value will be assigned to the following float variable x? x = 14 / 5; A higher precision value cannot be assigned to a lower precision variable. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 Constants We can change the value of a variable. If we want the value to remain the same, we use a constant. final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060; These are constants, also called named constant. The reserved word final is used to declare constants. These are called literal constant. The data type of literal constant 3.45 is double and the data type of literal constant 398 is int. To designate other data types for literal constants, we suffix the literal constants with the designators F or f for float and L or l for long. For example, 2.45f 1453L 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Working with Literal Constants The literal constant 2 is set to what data type by default? How do we make it of data type long? What is 345.79 set to by default? How do we make it a float? (See pages 98-99) float number; number = 345.79; 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu The Math Class The Math class in the java.lang package includes many common and useful mathematical functions such sin, cos, tan, square root, exponentiation, and others. The mathematical formula is expressed in Java as Math.abs( Math.sin( Math.PI / 4.0) * x ) See Table 3.5 page 101 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu InputBox The InputBox class is used to get input values from the user. InputBox inputBox; inputBox = new InputBox( mainWindow ); inputBox.getInteger( ); 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

InputBox—Error Message If an invalid value is entered, an error message is displayed. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

InputBox—Custom Message Chapter 3 InputBox—Custom Message A programmer-designated prompt is possible. inputBox.getInteger(“Enter your age:”); 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 InputBox Methods See the complete documentation for more details. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 3 OutputBox The OutputBox class is used to display text. OutputBox outputBox; outputBox = new OutputBox( mainWindow ); outputBox.print(“Hello, Dr. Caffeine” ); 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu OutputBox—printLine Unlike MessageBox, OutputBox is capable of displaying multiple lines of text. outputBox.printLine(“one” ); outputBox.printLine(“two” ); outputBox.printLine(“three” ); 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu OutputBox—print Unlike MessageBox, OutputBox is capable of displaying multiple lines of text. outputBox.print(“one ” ); outputBox.print(“two ” ); outputBox.print(“three” ); 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu OutputBox Methods See the complete documentation for more details. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Sample Program: Loan Calculator Problem Statement Write a loan calculator program that computes both monthly and total payments for a given loan amount, annual interest rate, and loan period. Major Tasks Get three input values Compute the monthly and total payments Output the results Key Formula L – loan amount R – monthly interest rate N – number of payments 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Loan Calculator – Design 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Loan Calculator – Object Diagram 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

Loan Calculator – Development Steps Start with a program skeleton. Add code to accept three input values. Add code to output the results. Add code to compute the monthly and total payments. What’s the above remind you of? 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

You are ______ years old. Let’s Try One… Create a program called AgeCalculator Get the user’s year of birth and then display: You were born in ______. You are ______ years old. 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu