Lecture 1 3.10.2023
Lecture 1 3.10.2023
Lecture 1 3.10.2023
Programming I
1
Text Book
2
Evaluation
Category Percentage Location Date
Lab 10% In Lab Weekly
Assignments
Mid 20% In Class 8th week
Term/Quizzes
Final Lab Exam 20% In Lab 14th week
3
Course Objectives
• Help students to understand the fundamentals
of programming such as variables, conditional
and iterative statements, methods, recursion,
arrays, etc.
• Develop the student’s ability to write a well-
structured computer program to solve specified
problems using Java
• Teach students to use the Java SDK environment
to create, debug and run simple Java programs
4
What is Programming?
5
What is programming?
• program: A set of instructions
to be carried out by a computer.
7
Programming Life Cycle Phases
4. Test and verify the completed program.
• Run the program several times using different sets of data, making
sure that it works correctly for every situation in the algorithm .
• if it does not work correctly, then you must find out what is
wrong with your program or algorithm and fix it--this is
called DEBUGGING
8
Algorithm Basic Control
Structures
• a sequence is a series of statements that execute one after
another
9
Control Structures
Sequences
10
Control structures
Selection (Branching)
Statement1
Statement
Condition ...
Statement2
11
Control structures
Loop (Repetition)
False
...
Condition
Statement
12
Control structures
Subprogram (Function)
PROGRAM1 ...
SUBPROGRAM1
a meaningful collection
of SEQUENCE,
SELECTION, LOOP,
SUBPROGRAM
13
Sample Problem
Company payroll case study
A small company needs an interactive
program to figure its weekly payroll.
14
Sample Problem
15
Algorithm for Company
Payroll Program
• initialize total company payroll to 0.0
• repeat these steps for each employee:
1. Get the employee’s ID empNum
2. Get the employee’s hourly payRate
3. Get the hours worked this week
4. Calculate this week’s wages
5. Add wages to total company payroll
• write total company payroll on screen
16
How to calculate the weekly
wages ?
If hours are less than 40.0, then
otherwise
Example:
If h=50, r=10, then
w = 40*rate + (h-40)*rate*1.5
W= 40*10+(50-40)*10*1.5 = ?
17
18
Programming languages
• Some influential ones:
– FORTRAN
• science / engineering
– COBOL
• business data
– LISP
• logic and AI
– BASIC
• a simple language
19
Some modern languages
• procedural languages: programs are a series of commands
– Pascal (1970): designed for education
– C (1972): low-level operating systems and device drivers
• functional programming: functions map inputs to outputs
– Lisp (1958) / Scheme (1975), ML (1973), Haskell (1990)
• object-oriented languages: programs use interacting "objects"
– Smalltalk (1980): first major object-oriented language
– C++ (1985): "object-oriented" improvements to C
• successful in industry; used to build major OSes such as Windows
– Java (1995): designed for web apps/servers
• Created by James Gosling and released by Sun microsystems
• Runs on many platforms (Windows, Mac, Linux, cell phones...)
20
21
Process of Programming
• Code: describes the act of programming (e.g., “Let’s code this into
Java”)
• The process of execution is often called running
• Program is stored in the computer as a series of binary numbers
known as machine language
– Machine language programs are executable programs
– Modern programmers use high-level programming languages such as
C++ and Java.
Source Running
Editor Compiler
File Byte code JVM Program
(IDE) (javac)
(.java)
MyProgram.java MyProgram.class
Output
24
IDE (Editor)
25
Basic Java programs with
println statements
26
A Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
• Its output:
Hello, world!
28
Compile/run a program
1. Write it.
– code or source code: The set of instructions in a program.
2. Compile it.
– javac: translates the program from Java to bytecode
– bytecode: runs on many computer types (any computer with JVM)
29
System.out.println
• A statement that prints a line of output on the console.
– pronounced "print-linn"
– sometimes called a "println statement" for short
• System.out.println();
Prints a blank line of output.
30
Another Java program
public class Hello {
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");
}
}
• Its output:
Hello, world!
This program produces
four lines of output
31
Names and identifiers
• You must give your program a name.
public class MyClass {
– Naming convention: capitalize each word (e.g. MyClass)
– Your program's file must match exactly (MyClass.java)
• includes capitalization (Java is "case-sensitive")
33
Syntax
• Syntax: The set of legal structures and commands that can
be used in a particular language.
– Every basic Java statement ends with a semicolon ;
– The contents of a class or method occur between { and }
• Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors
35
Other types of Errors
36
Strings
• string: A sequence of characters to be printed.
– Starts and ends with a " quote " character.
• The quotes do not appear in the output.
– Examples:
"hello"
"This is a string. It's very long!"
• Restrictions:
– May not span multiple lines.
"This is not
a legal String."
37
Escape sequences
• escape sequence: A special sequence of characters used
to represent certain special characters in a string.
\t tab character
\n new line character
\" quotation mark character
\\ backslash character
– Example:
System.out.println("\\hello\nhow\tare \"you\"?\\\\");
– Output:
\hello
how are "you"?\\
38
Questions
• What is the output of the following println
statements?
System.out.println("\ta\tb\tc");
System.out.println("\\\\");
System.out.println("'");
System.out.println("\"\"\"");
System.out.println("C:\nin\the downward
spiral");
39
Answers
• Output of each println statement:
a b c
\\
'
"""
C:
in he downward spiral
40
Questions
• What println statements will generate this output?
This program prints a
quote from the Gettysburg Address.
"Four score and seven years ago,
our 'fore fathers' brought forth on
this continent a new nation."
41
Answers
• println statements to generate the output:
System.out.println("This program prints a");
System.out.println("quote from the Gettysburg Address.");
System.out.println();
System.out.println("\"Four score and seven years ago,");
System.out.println("our 'fore fathers' brought forth on");
System.out.println("this continent a new nation.\"");
42
Comments
• comment: A note written in source code by the
programmer to describe or clarify the code.
– Comments are not executed when your program runs.
• Syntax:
// comment text, on one line
or,
/* comment text; may span multiple lines */
• Examples:
// This is a one-line comment.
/* This is a very long
multi-line comment. */
43
Using comments
• Where to place comments:
– at the top of each file (a "comment header")
– at the start of every method (seen later)
– to explain complex pieces of code
// second verse
System.out.println("diggy said the boogy");
System.out.println("said up jump the boogy");
}
}
45