0% found this document useful (0 votes)
6 views34 pages

Chapter 1

This document is an introductory chapter on programming, specifically focusing on Java. It covers the basics of programming languages, the structure of Java programs, and essential concepts such as syntax, semantics, and debugging. Additionally, it outlines course objectives, resources, and assessment criteria for students at Jazan University.

Uploaded by

f2rvtg7vxt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views34 pages

Chapter 1

This document is an introductory chapter on programming, specifically focusing on Java. It covers the basics of programming languages, the structure of Java programs, and essential concepts such as syntax, semantics, and debugging. Additionally, it outlines course objectives, resources, and assessment criteria for students at Jazan University.

Uploaded by

f2rvtg7vxt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 1: Introduction to

Programming

PROGRAMMING 1
COS 011

Slides prepared by Mnassar Al-yami,


Jazan University
Our Resources
2

Textbook:
Think Java: How to Think Like a Computer Scientist
By Allen B. Downey

 Legally Available online for free at:


http://www.greenteapress.com/thinkapjava/thinkapjava.pdf

 The course approximately follows this book.

Reference book:
 Savitch, W. J. (2008).Absolute Java. Boston, Mass: Addison-Wesley.
Mark Distribution
3

When ? Exams, Quizzes, and Mark


Assignment
Week 5 Mid Exam 10
Deadline Week 10 Assignment 10
Deadline Week 11 Mini project 10
Week 3 & 10 Quiz 1 & 2 10
Week 12 Final Lab * 20
Total 60
Final Theory 40
Final Total 100

* Final Lab has 10 marks assignment and 10 marks exam.


Course Objectives
4

Give an overview of programming language.


Describe the basic elements of Java.
Understand the different Data types and
variables used in Java programming.
Explain the branching mechanism as well as
looping concepts for programming.
Provide an overview of Methods and Arrays.
Let’s Start Chapter 1
5

In this course we don’t just learn Java but


also learn how to solve problems.
 It is an important skill for computer scientist to be able
to formulate problems, think creatively about solutions,
and express a solution clearly and accurately.
 So, we will learn Java programming as a means in

problem-solving.
 As we go further, this skill will become clearer.
Chapter 1, cont.
6

What is a programming language?


 It is a language we use to communicate with

computer.
 However, computer understand only 0 and 1

represented in electric pulses.


 Therefore, computer runs only programs written in

low-level language called machine language or


assembly language.
 Programmers use high-level languages like C++, Java,

and Python which has a human-like syntax.


 Since the computer doesn’t understand the human

language those codes written in high-level language


interpreted into a machine language using compilers.
Origins of the Java Language
7

Created by Sun Microsystems team led by


James Gosling (1991)
Originally designed for programming home
appliances
Translate Java code to Machine Code
8

Compiler
Java code -------- Java byte-code

Interpreter
Java byte-code --- Machine code
9
Program
10

What is a program?

 A program is a sequence of instructions that species how to


perform a computation. The computation might be something
mathematical, like solving a system of equations or finding the
roots of a polynomial, but it can also be a symbolic computation,
like searching and replacing text in a document.

 Program call statements, which looks different in different


programming languages, for instance in C++ we use ( cout …) to
print something whereas we use in Java ( println….) to perform
the same.
Basic Operations in Java
11

input:
 Get data from the keyboard, or a file, or some other device.
output:
 Display data on the screen or send data to a file or other
device.
math:
 Perform basic mathematical operations like addition and
multiplication.
testing:
 Check for certain conditions and run the appropriate
sequence of statements.
repetition:
 Perform some action repeatedly, usually with some variation.
Syntax and Semantics
12

Syntax: The arrangement of words and


punctuations that are legal in a language, the
grammar rules of a language
Semantics: The meaning of things written
while following the syntax rules of a language
What is debugging?
13

Programming errors are called bugs and the


process of tracking them down and
correcting them is called debugging.
3 kinds of errors:

1) Syntax errors:
 A grammatical mistake in a program

System.out.println(Hello, world.);
System.out.println("Hello, world.");
14

2. Run-time errors
 Does not appear until you run the program and it
appears as windows or dialog boxes that contain
information about what happened and what the program
was doing when it happened.
 The compiler cannot detect these errors: an error message is not
generated after compilation, but after execution.

3. Logic errors and semantics


 The program works but gives wrong output.
 For example,
if number<0
print(“Number is positive”);
Formal and natural languages
15

Natural languages are the languages that


people speak, like English, They were not
designed by people.
Formal languages are languages designed by
people for specific applications. For example,
the notation that mathematicians use is a
formal language that is particularly good at
denoting relationships among numbers and
symbols.
Programming languages are formal
languages that have been designed to express
computations.
Cont.
16

Formal languages have strict rules.


3 + 3 = 6
3 $ 3 = 6 Error in Syntax
Syntax rules in formal language come in two
forms, tokens and structure.
 Tokens are the basic elements of the language, like
words and numbers.
 Structure is the way the tokens are arranged.
Formal vs. Natural languages
17
 Ambiguity  Ambiguity
 Formal languages are designed  Natural languages are full of
to be unambiguous, which ambiguity, which people deal
means that any statement has with by using contextual clues
exactly one meaning, and other information.
regardless of context.

 Redundancy
 Redundancy  To make up for ambiguity and
 Formal languages are more reduce misunderstandings,
concise. natural languages are often
redundant.

Literalness Literalness
 Formal languages mean exactly
 Natural languages are full of
what they say. idiom and metaphor.
Java Program
18

 Java programs are made up of class definitions, which


have the form:
A Sample Java Application Program
19
System.out.println
20

Java programs work by having things called


objects perform actions
 System.out: an object used for sending output to the
screen
The actions performed by an object are called
methods
 println: the method or action that the System.out
object performs
System.out.println
21

Invoking or calling a method: When an


object performs an action using a method
 Also called sending a message to the object
 Method invocation syntax (in order): an object, a

dot (period), the method name, and a pair of


parentheses
 Arguments: Zero or more pieces of information

needed by the method that are placed inside the


parentheses
System.out.println("This is an argument");
Println vs. Print
22

println is short for “print line,” it adds a


special character after each line, called a
newline, that moves the cursor to the next
line of the display.
print display the output from multiple print
statements all on one line,
Example
23
The output
24
Comments
25

A line comment begins with the symbols //,


and causes the compiler to ignore the
remainder of the line
 This type of comment is used for the code writer
or for a programmer who modifies the code
A block comment begins with the symbol
pair /*, and ends with the symbol pair */
 The compiler ignores anything in between
 This type of comment can span several lines
 This type of comment provides documentation for
the users of the program
The First program
26

 What is the class name from the above?


 Method?
 Statement?
 Strings?
 Comments?
Escape Sequences
27

A backslash (\) immediately preceding a


character (i.e., without any space) denotes an
escape sequence or an escape character
 The character following the backslash does not have
its usual meaning
 Although it is formed using two symbols, it is regarded
as a single character
Escape Sequences
28
Arithmetic Operators and Expressions
29

As in most languages, expressions can be


formed in Java using variables, constants, and
arithmetic operators
 These operators are + (addition), - (subtraction), *
(multiplication), / (division), and % (modulo,
remainder)
 An expression can be used anyplace it is legal to use a
value of the type produced by the expression
Order of Operations
30

Follows standard math rules:


1. Parentheses
2. Multiplication and division
3. Addition and subtraction
Glossary
31
1. problem-solving 11. print statement
2. high-level language 12. comment
3. low-level language 13. library
4. formal language 14. bug
5. natural language 15. syntax
6. portability 16. semantics
7. interpret 17. parse
8. compile 18. syntax error

9. source code 19. exception

10. statement 20. logic error


21. debugging
Exercises
32

1. Write a Java program to print 'Hello' on screen and then print


your name on a separate line.
2. What is the output of this code?
static void printAsterisk()
{

//System.out.println("*****");
//System.out.println("*****");
//System.out.println("*****");
//System.out.println("*****");
//System.out.println("*****");

}
Knowledge Check
33

1. What is low-level language?


2. What is the source code?
3. What is debugging?
4. Give an example for syntax error.
5. What is wrong with this code?

class Hello
{
/ main: generate some simple output
public static void main(String[] args)
{
System.out.println("Hello, world.”)
}
}
34

Questions ?!

You might also like