Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pseudocode and Flowcharts

Similar presentations


Presentation on theme: "Pseudocode and Flowcharts"— Presentation transcript:

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

2 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

3 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

4 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

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

6 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

7 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

8 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

9 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

10 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

11 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

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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 Introduction to Object-Oriented Programming with Java--Wu
Chapter 3 Implicit Type Casting Consider the following expression: double x = 3 + 5; The result of 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

33 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 = ; 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

34 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 set to by default? How do we make it a float? (See pages 98-99) float number; number = ; 11/18/2018 Introduction to Object-Oriented Programming with Java--Wu

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

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

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

47 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

48 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


Download ppt "Pseudocode and Flowcharts"

Similar presentations


Ads by Google