Introduction to programming
CMPS 241
1. Flowchart
2. Examples
3. Introduction to Java
Methods of specifying algorithm
Pseudo code - specifies the steps of algorithm using
essentially natural language of superimposed control
structure
Flowchart - a traditional graphical tool with standardized
symbols. Show the sequence of steps in an algorithm
Flow chart
Flow chart
A diagrammatic representation that illustrates the sequence of operations to be
performed to get the solution of a problem
Generally drawn in the early stages of formulating computer solutions
Facilitate communication between programmers and business people/end users
Once the flowchart is drawn it becomes easy to write the program in any high level
language
Must for the better documentation of a complex program
Flow chart
A flow chart can be used to
- Build a step-by-step picture of the process for
analysis, discussion or communication
- Find areas for
improvement in a process
Flowcharting Guidelines
1- The flowchart should flow from top to bottom
2- If the chart becomes complex, utilize connecting blocks
3- Avoid intersecting flow lines
4- Use meaningful description in the symbol
Flowcharting Symbols
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
Draw a flowchart to convert the length in feet to centimetre
(1 foot =30 centimetres)
Example 1
Draw a flowchart to convert the length in feet to centimetre
(1 foot =30 centimetres)
start
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
Input Processing Output
X 1. Enter x z
y 2. Enter y
3. Calculate z
4. Print z
5. End
Example 2
Flow chart start
Enter
X,Y
Z= X*Y
Output
Z
end
Example 3
A program is required to read three numbers, add then
add together and print their total. Draw the IPO &
Flow Chart
Example 3
IPO chart
Let k=x+y+z
Input Processing Output
x 1. Read x k
y 2. Read y
z 3. Read z
3. Calculate k
4. Print k
5. End
Example 3
Flow chart start
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?
Java enables users to develop and deploy applications on
the Internet for servers, desktop computers, and small
hand-held devices.
Java is a general purpose programming language.
28
Java, Web, and Beyond
Java can be used to develop Web applications.
Java Applets
Java Web Applications
Java can also be used to develop applications for
hand-held devices such as Palm and cell phones
29
Popular Java IDEs (Integrated
development environment )
NetBeans Open Source by Sun
Eclipse Open Source by IBM
30
Classes and Objects
The class is the unit of programming
A Java program is a collection of classes
Each class definition (usually) in its own .java file
The file name must match the class name
A class describes objects (instances)
Describes their common characteristics: is a blueprint
Thus all the instances have these same characteristics
These characteristics are:
Data fields for each object
Methods (operations) that do work on the 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
A statement represents an action or a sequence of actions.
33
Print and println statements
Output to a console text window in Java is accomplished through the use of
print and println (pronounced print-line).
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
Two ways to use System.out.println :
• System.out.println("text");
Prints the given message as output.
• 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.
{ public static void main(String[] args)
{ //Print out String of characters
System.out.println("Hello World!");
}
}
Every Java program must have at least one class definition.
Every basic Java statement ends with a semicolon ;
37
Trace a Program Execution
Enter main method
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
38
Trace a Program Execution
Execute statement
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
The println statement ends with a semicolon. It is a complete Java statement
that instructs the computer to display a message on the computer screen.
39
Trace a Program Execution
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
print a message on the
computer screen
Blocks
A pair of braces in a program forms a block that groups
components of a program.
public class Test {
public static void main(String[] args) { Class block
System.out.println("Welcome to Java!"); Method block
}
}
41
Main Method
The main method provides the control of program flow. The Java
interpreter executes the application by invoking the main method.
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
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.
public class Hello {
pooblic static void main(String[] args) {
System.owt.println("Hello, world!")_
}
}
43
Reserved Words
Reserved words or keywords are words that have a specific meaning to
the compiler and cannot be used for other purposes in the program.
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
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println();
System.out.println("This program produces");
System.out.println("four lines of output");
}
}
The code in this program instructs the computer to print
four messages on the screen.
45