0% found this document useful (0 votes)
4 views45 pages

1-Introduction To Computer, Programming, and Java

The document is a course outline for an introductory programming class at the University of Halabja, focusing on computer basics and the Java programming language. It covers objectives such as understanding computer components, writing Java programs, and the differences between syntax and runtime errors. Additionally, it provides insights into programming languages, the Java development process, and examples of simple Java programs.

Uploaded by

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

1-Introduction To Computer, Programming, and Java

The document is a course outline for an introductory programming class at the University of Halabja, focusing on computer basics and the Java programming language. It covers objectives such as understanding computer components, writing Java programs, and the differences between syntax and runtime errors. Additionally, it provides insights into programming languages, the Java development process, and examples of simple Java programs.

Uploaded by

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

University of Halabja

College of Science
Department of Computer Science
Fundamentals of Programming
First Stage
First Semester

Introduction to Computer,
Programming, and Java

Prepared by: Peshraw Ahmed, M.Sc.


p e s h r a w. a b d a l l a @ u o h . e d u . i q
Objectives

» To understand computer basics, programs, and operating systems.


» To understand the meaning of Java language.
» To write a simple Java program.
» To display output on the console.
» To explain the basic syntax of a Java program.
» To create, compile, and run Java programs.
» To explain the differences between syntax errors and runtime errors.

Liang, Introduction to Java Programming, Eleventh Edition, (c)


2018 Pearson Education, Ltd. 2
What is a Computer?

» A computer is an electronic device that manipulates information, or data. It


has the ability to store, retrieve, and process data. You may already know
that you can use a computer to type documents, send an email, play
games, and browse the Web.
» A computer consists of a CPU, memory, storage devices, I/O devices, and
communication devices.
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

3
CPU

» The central processing unit (CPU) is the brain of a computer.


» It retrieves instructions from memory and executes them.
» The CPU speed is measured in megahertz (MHz), with 1 megahertz
equaling 1 million pulses per second. The speed of the CPU has been
improved continuously (1 gigahertz is 1000 megahertz).
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

4
Memory

» Memory is to store data and program instructions for CPU to execute. A


memory unit is an ordered sequence of bytes, each holds eight bits.
» A program and its data must be transported to memory before they can be
executed.

Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

5
How Data is Stored?

» Data of various kinds, such as


numbers, characters, and strings, Memory address Memory content
are encoded as a series of bits (zeros
and ones). . .
. .
» Computers use zeros and ones . .
because digital devices have two 2000 01001010 Encoding for character ‘J’
stable states, which are referred to 2001 01100001 Encoding for character ‘a’
2002 01110110 Encoding for character ‘v’
as zero and one by convention. 2003 01100001 Encoding for character ‘a’
» For example, character ‘J’ is 2004 00000011 Encoding for number 3

represented by 01001010 in one


byte. A byte is the minimum storage
unit.
6
Storage Devices

» Memory is volatile; because the information is lost when the power is off.
Programs and data are permanently stored on storage devices and are
moved to memory when the computer actually uses them. E.g. Disk drives
(hard disks and floppy disks) and CD drives.

Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

7
Output Devices: Monitor

» An output device is any piece of computer hardware equipment that


converts information into a human-readable form. It can be text, graphics,
tactile, audio, and video.
» The monitor displays information (text and graphics). The resolution and
dot pitch determine the quality of the display.
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

8
Communication Devices

» A communications device is any type of hardware capable of transmitting


data, instructions, and information between a sending device and a
receiving device.
» Examples of communication devices include a NIC (network interface card)
and Wi-Fi.
Bus

Storage Communication Input Output


Memory CPU Devices Devices Devices
Devices
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer

9
Programs

» Computer programs, known as software, are instructions to the computer.


» You tell a computer what to do through programs. Without programs, a
computer is an empty machine. Computers do not understand human
languages, so you need to use computer languages to communicate with
them.
» Programs are written using programming languages.

10
Programming Languages
Machine Language Assembly Language High-Level Language

Machine language is a set of primitive instructions built


into every computer. The instructions are in the form of
binary code, so you have to enter binary codes for various
instructions. Program with native machine language is a
boring process. Moreover, the programs are highly difficult
to read and modify. For example, to add two numbers, you
might write an instruction in binary like this:

1101101010011010
11
Programming Languages
Machine Language Assembly Language High-Level Language

Assembly languages were developed to make programming


easy. Since the computer cannot understand assembly
language, however, a program called an assembler is used
to convert assembly language programs into machine code.
For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3

12
Programming Languages
Machine Language Assembly Language High-Level Language

The high-level languages are English-like and easy to learn


and program. For example, the following is a high-level
language statement that computes the area of a circle with a
radius 5:
area = 5 * 5 * 3.1415;

13
Popular High-Level Languages

Language Description

Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is used mainly in defense projects.
BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily
by beginners.
C Developed at Bell Laboratories. C combines the power of an assembly language with the ease of
use and portability of a high-level language.
C++ C++ is an object-oriented language, based on C.
C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.
COBOL COmmon Business Oriented Language. Used for business applications.
FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications.
Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-
independent Internet applications.
Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a
simple, structured, general-purpose language primarily for teaching programming.
Python A simple general-purpose scripting language good for writing short programs.
Visual Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop
Basic graphical user interfaces.
14
Interpreting/Compiling Source Code

» A program written in a high-level language is called a source program or source code.


» Because a computer cannot understand a source program, a source program must be
translated into machine code for execution.
» The translation can be done using another programming tool called an interpreter or a
compiler.

15
Interpreting Source Code

» An interpreter is a computer program, which converts each high-level program


statement into the machine code.
» An interpreter reads one statement from the source code, translates it to the machine
code or virtual machine code, and then executes it at once, as shown in the following
figure. Note that a statement from the source code may be translated into several
machine instructions.

16
Compiling Source Code

» A compiler is a computer program that translates the entire source code into a machine-
code file, and the machine-code file is then executed, as shown in the following figure.
» A compiler should comply with the syntax rule of that programming language in which
it is written. However, the compiler is only a program and cannot fix errors found in that
program. So, if you make a mistake, you need to make changes in the syntax of your
program. Otherwise, it will not compile.
» Both compiler and interpreters do the same job which is converting higher-level
programming language to machine code. However, a compiler will convert the code into
machine code before the program run.

17
Operating Systems

» The operating system (OS) is a program


that manages and controls a computer’s
activities. The popular operating systems
are Microsoft Windows, Mac OS, and
Linux.
» Application programs, such as a Web
browser or a word processor, cannot run
unless an operating system is installed and
running on the computer.

18
Why Java?

» It is easy to learn
» “Write once run anywhere”, java apps can run on any system (Windows, Mac, or Linux)
» Java programmers are in demand.
» Android which is the most popular mobile OS runs on Java.
» Java is an Object-Oriented Programming Language.
» Large community support, a huge number of supporters on the web.

19
Java programming language

» It was originally developed by Sun Microsystems which was initiated by James Gosling
and released in 1995 as a core component of Sun Microsystems' Java platform (Java 1.0
[J2SE]).
» Java SE 17 is the latest release for the Java SE Platform
» The Java programming language is a high-level language that can be characterized by all
of the following buzzwords:
• Simple: syntax is based on C++ and removed many confusing instructions.

• Portable: We may carry the java bytecode to any platform.

• Object-oriented: It means we organize our software as a combination of different


types of objects that include both data and behavior.

• Robust: Simply means strong. Java uses strong memory management. 20


Creating, Compiling, and Running Programs

21
Java Development Process

» All source code is first written in plain text files ending with the .java extension.
» Those source files are then compiled into .class files by the javac compiler.
» A .class file does not contain code that is native to your processor, it instead
contains bytecodes of the machine language of the Java Virtual Machine (Java VM).
» With Java, you write the program once and compile the source program into a special
type of object code, known as bytecode. The bytecode can then run on any computer
with a Java Virtual Machine, as shown below. Java Virtual Machine is a software that
interprets Java bytecode

22
Creating Java Application

To write a java program, it needs:


» Java SE Development Kit 17.0.1 (JDK 15)
» A text editor

Textpad is a simple editor included with the Windows platforms.


Creating Java Application
» Create a source file: It contains code, written in the Java, Text pad is an example to
create a source file.
» Compile the source file into a .class file: (ctrl + 1) takes your source file and translates
its text into bytecodes.
» Run the program: (ctrl + 2) uses the Java virtual machine to run your application.

23
A Simple Java Program

// This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

} print a message to the console

24
Compiling and Running Java from TextPad

» Open TextPad
» Write the source code
» Ctr + 1 to Compile the code
» Ctr + 2 to the code
» What is the output ?

25
Two More Simple Examples(1)

public class WelcomeWithThreeMessages {


public static void main(String[] args) {
System.out.println("Programming is fun!");
System.out.println("Fundamentals First");
System.out.println("Problem Driven");
}
}

26
Two More Simple Examples(2)

public class ComputeExpression {


public static void main(String[] args) {
System.out.print("(10.5 + 2 * 3) / (45 – 3.5) = ");
System.out.println((10.5 + 2 * 3) / (45 - 3.5));
// Display the result of expression
}
}

27
Anatomy of a Java Program

» Class name
» Main method
» Statements
» Statement terminator
» Reserved words
» Comments
» Blocks

28
Class Name

» Every Java program must have at least one class. Each class has a name. By convention,
class names start with an uppercase letter. In this example, the class name is Welcome.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

29
Main Method

» Line 2 defines the main method. In order to run a class, the class must contain a method
named main. The program is executed from the main method.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

30
Statement

» A statement represents an action or a sequence of actions.


» The statement System.out.println("Welcome to Java!") in the program is a statement
to display the greeting "Welcome to Java!“.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

31
Statement Terminator

» Every statement in Java ends with a semicolon (;).

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

32
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.

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

33
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
}
}

34
Special Symbols

Character Name Description

{} Opening and closing Denotes a block to enclose statements.


braces
() Opening and closing Used with methods.
parentheses
[] Opening and closing Denotes an array.
brackets
// Double slashes Precedes a comment line.

" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
; Semicolon Marks the end of a statement.

35
{ …}

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

36
(…)

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

37
;

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

38
"…"

// This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

39
Programming Errors

» Syntax Errors
• Detected by the compiler
» Runtime Errors
• Causes the program to abort
» Logic Errors
• Produces an incorrect result

40
Syntax Errors
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}

41
Runtime Errors

public class ShowRuntimeErrors {


public static void main(String[] args) {
System.out.println(1 / 0);
}
}

42
Logic Error
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}

43
Exercises

» Q1 - Write a program to display weekend days.


» Q2 - Write a Java program to print the pyramids in java.
#
##
###
####
#####
» Q3 - Write a Java program to display the following pattern.

44
» https://www.w3schools.com/java/default.asp
Read more…
» https://www.tutorialspoint.com/java/index.htm

» https://www.guru99.com/java-tutorial.html

» Download Java SE Development Kit 17.0.1


» https://www.oracle.com/java/technologies/javase/jdk17-
archive-downloads.html
Thank You
Peshraw Ahmed Abdalla » Download TextPad 8.9.0
Lecturer » https://www.textpad.com/download#TextPad890
[email protected]
University of Halabja

You might also like