Introduction To Programming CMPS 241
Introduction To Programming CMPS 241
CMPS 241
1. Flowchart
2. Examples
3. Introduction to Java
Methods of specifying algorithm
Once the flowchart is drawn it becomes easy to write the program in any high level
language
Terminal Symbol:
indicates the starting or stopping point in the logic.
Input/Output Symbol:
Represents an input or output process in an algorithm
Process Symbol:
Represents any single process in an algorithm
Decision Symbol:
Represents a decision in the logic involving the comparison Of two values.
Flow line :
denotes the direction of logic flow in the program
Flow chart
Flow chart
Flow chart
For example, assume you are calculating pay at an hourly rate and overtime pay (over 40 hours) at 1.5 times
the hourly rate. The decision to calculate pay would be stated in the following way: If the hours are greater
than 40, Then the pay is calculated for overtime, or Else the pay is calculated in the usual way
Examples
Example 1
Input
Lft
Lcm =
lft*30
Output
Lcm end
Example 2
A simple calculator can multiply two numbers x and y. Draw the IPO &
Flow Chart for the multiplication problem
Example 2
IPO chart
Let z=x*y
Enter
X,Y
Z= X*Y
Output
Z
end
Example 3
IPO chart
Let k=x+y+z
Read
X,Y,Z
k= X+Y+Z
Print
k
end
Example 4
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4 Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL” GRADE(M1+M2+M3+M4)/4
else
Print “PASS” N IS Y
endif GRADE<50
Print Print
“PASS” “FAIL”
STOP
Example 5
Draw a flowchart to find the largest of three numbers A,B
and C and print out the number
Example 5
Example 6
In the software development cycle, you basically design the system, code it
and test it. When an error is found, it must be determined whether the
error is a design error or a coding one. Coding errors can quickly be fixed
by going back to the coding phase but design errors may take longer when
revising the design phase
Example 6
Flow chart
Introduce first program in Java
Introduction to Java Programming
Java language
Facilitates structured and disciplined approach to computer
program design
Following several examples
Illustrate many important features of Java
Each analyzed one statement at a time
Structured programming
Object-oriented programming
27
Why Java?
28
Java, Web, and Beyond
29
Popular Java IDEs (Integrated
development environment )
30
Classes and Objects
31
A Simple Program:
Printing a Line of Text
Comments
Document programs
Improve program readability
Ignored by compiler
Single-line comment
Begin with //
or enclosed between /* and */ in one or multiple lines
When the compiler sees //, it ignores all text after // in the same line.
When it sees /*, it scans for the next */ and ignores any text between /*
and */.
32
Statements
33
Print and println statements
print and println are two operations that can be called. These operations
output text information to the console window in Eclipse.
print and println statements
System.out.println();
Will print out the value of the thing in the parentheses and a new line
System.out.print();
To print just the thing in the parentheses without a new line
• System.out.println();
Prints a blank line of output.
Console Output Exercise
Use System.out.println() to print the results of
expression to the console
System.out.println(3 * 28);
System.out.println(14 – 7);
System.out.println(10 / 2);
System.out.println(128 + 234);
System.out.println("Hi" + "There");
System.out.println("128 + 234");
Try using System.out.print() instead
What is the difference?
A Simple Java Program
No semicolon here! The
public class Hello class header is only the
beginning of the class
This is the method header for
the method named main. No
definition. semicolon.
38
Trace a Program Execution
Execute statement
39
Trace a Program Execution
41
Main Method
The main method provides the control of program flow. The Java
interpreter executes the application by invoking the main method.
42
Syntax and syntax errors
syntax: The set of legal structures and commands that
can be used in a particular programming language.
syntax error or compiler error: A problem in the
structure of a program that causes the compiler to fail.
If you type your Java program incorrectly, you may violate
Java's syntax and see a syntax error.
43
Reserved Words
For example, when the compiler sees the word class, it understands that
the word after class is the name for the class. Other reserved words in
Example 1.1 are public, static, and void. Their use will be introduced later.
44
Another Java program
45