590 likes | 1.38k Views
Pseudocode and Flowcharts. Expressing Algorithms to Solve Programming Challenges. 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
E N D
Pseudocode and Flowcharts Expressing Algorithms to Solve Programming Challenges 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 Introduction to Object-Oriented Programming with Java--Wu
Define the Problem • Inputs, Processes, Outputs • Defining Diagram Introduction to Object-Oriented Programming with Java--Wu
Outline the Solution • Main logic • Major processing steps • Variables • Control structures • Subtasks Introduction to Object-Oriented Programming with Java--Wu
Develop an Algorithm • Pseudocode • Flowcharts 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. 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 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 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. Introduction to Object-Oriented Programming with Java--Wu
Flowcharts • Can use Word for these symbols • Let’s put our algorithm in a Flowchart 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. Introduction to Object-Oriented Programming with Java--Wu
Chapter 3 Numerical Data Introduction to Object-Oriented Programming with Java--Wu
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. Introduction to Object-Oriented Programming with Java--Wu
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; 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; Introduction to Object-Oriented Programming with Java--Wu
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; 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. 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? 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(); 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; Introduction to Object-Oriented Programming with Java--Wu
int firstNumber, secondNumber; A. Variables are allocated in memory. firstNumber = 234; secondNumber = 87; firstNumber 234 secondNumber A int firstNumber, secondNumber; 87 firstNumber = 234; secondNumber = 87; B B. Values are assigned to variables. Primitive Data Declaration and Assignments int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; State of Memory Code Introduction to Object-Oriented Programming with Java--Wu
number 237 35 A. The variable is allocated in memory. A int number; B number = 237; B. The value 237 is assigned to number. C number = 35; C. The value 35 overwrites the previous value 237. Assigning Numerical Data int number; number = 237; number = 35; int number; number = 237; number = 35; State of Memory Code Introduction to Object-Oriented Programming with Java--Wu
customer Customer Customer A B A. The variable is allocated in memory. Customer customer; customer = new Customer( ); B. The reference to the new object is assigned to customer. customer = new Customer( ); C C. The reference to another object overwrites the reference in customer. Assigning Objects Customer customer; customer = new Customer( ); customer = new Customer( ); Customer customer; customer = new Customer( ); customer = new Customer( ); State of Memory Code Introduction to Object-Oriented Programming with Java--Wu
This is an integer division where the fractional part is truncated. Arithmetic Operators • The following table summarizes the arithmetic operators available in Java. 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 Introduction to Object-Oriented Programming with Java--Wu
clemens twain Customer A B A. Variables are allocated in memory. Customer clemens, twain; clemens = new Customer( ); B. The reference to the new object is assigned to clemens. twain = clemens; C C. The reference in twain is assigned to customer. Having Two References to a Single Object Customer clemens, twain; clemens = new Customer( ); twain = clemens; Customer clemens, twain, clemens = new Customer( ); twain = clemens; State of Memory Code 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 Introduction to Object-Oriented Programming with Java--Wu
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. Introduction to Object-Oriented Programming with Java--Wu
Precedence Rules Introduction to Object-Oriented Programming with Java--Wu
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. Introduction to Object-Oriented Programming with Java--Wu
Type case x to float and then divide it by 3. Type cast the result of the expression x / y * 3.0 to int. 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) Introduction to Object-Oriented Programming with Java--Wu
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; A higher precision value cannot be assigned to a lower precision variable. Introduction to Object-Oriented Programming with Java--Wu
The reserved word final is used to declare constants. These are constants, also called named constant. These are called literal constant. 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; Introduction to Object-Oriented Programming with 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; 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 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( ); Introduction to Object-Oriented Programming with Java--Wu
InputBox—Error Message • If an invalid value is entered, an error message is displayed. Introduction to Object-Oriented Programming with Java--Wu
InputBox—Custom Message • A programmer-designated prompt is possible. inputBox.getInteger(“Enter your age:”); Introduction to Object-Oriented Programming with Java--Wu
InputBox Methods • See the complete documentation for more details. Introduction to Object-Oriented Programming with Java--Wu
OutputBox • The OutputBox class is used to display text. OutputBox outputBox; outputBox = new OutputBox( mainWindow ); outputBox.print(“Hello, Dr. Caffeine” ); 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” ); 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” ); Introduction to Object-Oriented Programming with Java--Wu
OutputBox Methods • See the complete documentation for more details. Introduction to Object-Oriented Programming with Java--Wu
L – loan amount R – monthly interest rate N – number of payments 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 Introduction to Object-Oriented Programming with Java--Wu
Loan Calculator – Design Introduction to Object-Oriented Programming with Java--Wu
Loan Calculator – Object Diagram 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? Introduction to Object-Oriented Programming with Java--Wu
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. Introduction to Object-Oriented Programming with Java--Wu