Uploaded by Lusungu Mhango

Lecture 1-C (1)

advertisement
COM 121 :
Introduction to Computer
Programming
Objectives
●
Program Development Life Cycle
○
○
○
○
Problem Definition
Problem Analysis
Algorithm design and representation
■ Pseudocode
■ Flowchart
Coding and debugging
■ Programming Errors
Objectives
●
●
Setting up Development Environment
Creating, Compiling and Executing a Java Program
○
○
○
Explain stages of a java program
Explain the Java Virtual machine
Identify the basic parts of a Java program
The Program Development Life Cycle
1. Problem Definition
2. Problem Analysis
3. Algorithm design and representation (Pseudocode or
flowchart)
4. Coding and debugging
Problem Definition
● Define the problem first before we even try to create a
solution.
● Example problem:
“Create a program that will determine the number of times a
name occurs in a list.”
Problem Analysis
● Formulate most efficient and effective approach to solve
the problem breaking up the problem into smaller and
simpler sub-problems.
● Example Problem:
○
Determine the number of times a name occurs in a list
Input to the program:
list of names, name to look for
Output of the program:
Algorithm Design and Representation
● Express our solution in a step-by-step manner
● An Algorithm is a clear and unambiguous specification of
the steps needed to solve a problem.
○
May be expressed in either:
■ Human language (English, Tagalog, Chichewa)
■ Graphical representation: a flowchart or through a pseudocode - a cross between
human language and a programming language
NB: Pseudocode: a notation resembling a simplified programming language
Human Language
1.
2.
3.
4.
Get the list of names
Get the name to look for, let's call this the keyname
Compare the keyname to each of the names in the list
If the keyname is the same with a name in the list, add 1
to the count
5. If all the names have been compared, output the result
Flowchart
Symbols
https://www.lucidchart.com/pages/flowchart-symbols-meaning
-explained
Pseudocode
Let nameList = List of Names
Let keyName = the name to be sought
Let Count = 0
For each name in NameList do the following
if name == keyName
Count = Count + 1
Display Count
Coding and Debugging
● Coding: writing the chosen programming language.
● Debugging: Fixing errors (also called bugs) that occurs in
the program
● Three types of errors:
○
○
○
Syntax or Compile-Time Errors: result from errors in code construction
Runtime error:They occur while a program is running if the environment detects an operation
that is impossible to carry out
■ E.g dividing by zero
Logic Errors: occur when a program does not perform the way it was intended to
■ Examples: erroneous formula giving wrong results
Exercise
Create an algorithm to accomplish the following task:
1. Getting the largest number in a list of numbers
2. Determining whether a given integer is odd
You may write your algorithms using flowcharts.
Popular High-Level Languages
●
●
●
●
●
●
●
●
●
●
●
●
COBOL (COmmon Business Oriented Language)
FORTRAN (FORmula TRANslation)
BASIC (Beginner All-purpose Symbolic Instructional Code)
Pascal (named for Blaise Pascal)
Ada (named for Ada Lovelace)
C (whose developer designed B first)
Visual Basic (Basic-like visual language developed by Microsoft)
Delphi (Pascal-like visual language developed by Borland)
C++ (an object-oriented language, based on C)
C# (a Java-like language developed by Microsoft)
Java (the language we are going to use in this module)
Python
Why Java
●
●
●
●
●
●
●
●
●
●
●
Java Is Simple
Java Is Object-Oriented
Java Is Distributed
Java Is Interpreted
Java Is Robust
Java Is Secure
Java Is Architecture-Neutral
Java Is Portable
Java's Performance
Java Is Multithreaded
Java Is Dynamic
Setting up Development Environment
Example
A Feel of Java
Basic Structure of a Java program
/*
This is a simple Java program.
Call this file Hello.java
*/
Public class Hello {
//your program begins with the call to the method main()
public static void main(String[] args) {
System.out.println(“ Hello World ”);
}
}
Phases of a Java Program
Compiling the Program
C:\>javac Hello.java
● The javac compiler creates a file called Hello.class
○
contains the bytecode version of the program - instructions the Java Virtual Machine will
execute -
Running the Program
● Use the Java application launcher called java
C:\>java Hello
● When the program is run, the following output is
displayed:
“Hello World”
● When Java source code is compiled, each individual
class is put into its own output file named after the
class and using the .class extension
More programming examples
Download