Lecture 1 : Introduction to Computers and Java · 2016-01-25 · Coding the Program Coding is...

27
Lecture 1 : Introduction to Computers and Java NADA ALZAHRANI CS 2301 1

Transcript of Lecture 1 : Introduction to Computers and Java · 2016-01-25 · Coding the Program Coding is...

Lecture 1 : Introduction to Computers and Java

NADA ALZAHRANI

CS2301

1

Outline What is a computer ?

Computer organization

Early operating systems

What a computer program ?

The Programmer Algorithm

Characteristics of Java

2

What Is a Computer?Computer

◦ Performs computations and makes logical decisions

◦ Millions or billions of times faster than human beings

Computer programs◦ Sets of instructions for which computer processes data

Hardware◦ Physical devices of computer system

Software◦ Programs that run on computers

3

Computer OrganizationSix logical units of computer system

◦ Input unit◦ Mouse, keyboard

◦ Output unit◦ Printer, monitor, audio speakers

◦ Memory unit◦ Retains input and processed information

◦ Arithmetic and logic unit (ALU)◦ Performs calculations

◦ Central processing unit (CPU)◦ Supervises operation of other devices

◦ Secondary storage unit◦ Hard drives, floppy drives

4

Early Operating SystemsBatch processing

◦ One job (task) at a time

Multiprogramming◦ “Simultaneous” jobs

5

What is a computer program?Computer is able to perform specific tasks:

maintaining employee records,

searching the web for information,

displaying videos,

monitoring security systems, etc.

it must be given instructions to do these tasks.

Computers perform three main functions: input, process and output.

6

What is a computer program?• Computers receive a set of instructions and data as input and process the instructions on the data to produce the output.

The set of instructions that is provided as input to the computer represents a computer program.

7

The Programmer Algorithmprogram is a set of statements written by a programmer in a logical order to solve a particular problem.

An algorithm is a finite sequence of instructions that produces a solution to a problem.

The programmer’s algorithm:◦ Define the problem.

◦ Plan the problem solution.

◦ Code the program.

◦ Compile the program.

◦ Run the program.

◦ Test and debug the program.

8

Defining the ProblemThe problem must be defined in terms of:◦ Input: Data to be processed.

◦ Output: The expected result.◦ Look for nouns in the problem statement that suggest output and input.

◦ processing: The statements to achieve. ◦ Look for verbs to suggest processing steps.

9

Keyboard ScreenProcessing

input data output data

Input and OutputInputs◦ Can come from many sources, such as users, files, and other programs

◦ Can take on many forms, such as text, graphics, and sound

Outputs◦ Can also take on many forms, such as numbers, text, graphics, sounds, or

commands to other programs

10

Example 1Area and Perimeter of a rectangleInput◦ Length

◦ width

Processing◦ Area = length*width

◦ Perimeter = 2*( length + width)

Output◦ Area

◦ Perimeter

11

Example 2Sum and Average of 5 numbersInput◦ five number x1, x2, x3, x4, x5

Processing◦ Sum = x1+x2+x3+x4+x5

◦ Average = Sum/5

Output◦ Sum

◦ Average

12

Planning the SolutionWhen planning, algorithms are used to outline the solution steps using English-like statements, called pseudo-code.

13

Coding the ProgramCoding is writing the program in a formal language called Programming Language.

Programming Language : A set of rules, symbols and special words used to write statements.

The program is written by translating the algorithm steps into a programming language statements.

The written program is called Source code and it is saved in a file with “.java” extension.

14

Program

Coding

Algorithm Pseudo-code

Source Code(The “.java”)

Translating

Classification of programming languages

15

Classification of programming languagesMachine language:

• The only programming language the CPU understands.

• Each type of CPU has its own machine language.

• Machine-language instructions are binary-coded and very low level.

• We must provide many machine-language instructions to accomplish a simple task.

• A program written in machine language might look like this:

10110011 00011001

01111010 11010001 10010100

10011111 00011001

16

Classification of programming languagesAssembly Language:

• One level above machine language

• Instead of writing programs as a sequence of bits, assembly language allows programmers to write programs by using symbolic operation codes.

• For example, instead of 10110011, we use MV to move the contents of a memory cell into a register.

• A program written in assembly language might look like this:

MV 0, SUM

MV NUM, AC

ADD SUM, AC

assembler to translate programs written in assembly language into machine-language to be recognized by the CPU

17

Classification of programming languagesHigh-level languages:

• English-like and easy to learn and program.

• For example, the following is a high-level language statement that computesthe area of a circle with radius 5:

area = 5 * 5 * 3.1415;

18

Compiling Computer Programs• Computers do not understand programs written in programming languagessuch as C++ and Java

• Programs must first be converted into machine code that the computer can run

• A Software that translates a programming language statements into machine code is called a compiler

19

Machine code

Program Source code

Machine Code

TranslatingCompiling

Programming Language CompilerA compiler is a software that:◦ Checks the correctness of the source code according to the

language rules.◦ Syntax errors are raised if some rules were violated.

◦ Translates the source code into a machine code if no errors were found.

20

Platform dependent CompilingBecause different platforms, or hardware architectures along with the operating systems (Windows, Macs, Unix), require different machine code, you must compile most programs separately for each platform.

21

Compiling Java ProgramsThe Java compiler produces bytecode (a “.class” file) not machine code from the source code (the “.java” file).

Bytecode is converted into machine code using a Java Interpreter

22

Source Code Bytecode

Platform Independent Java Programs CompilingYou can run bytecode on an computer that has a Java Interpreter installed

23

“Hello.java” “Hello.class”

Running The Program

24

The Java Virtual Machine Components

The Class Loader◦ stores bytecodes in memory

Bytecode Verifier◦ ensures bytecodes do not violate security

requirements

Bytecode Interpreter◦ translates bytecodes into machine language

The Java Virtual MachineThe class Loader, the Bytecode Verifier and Interpreter constitute the Java Virtual Machine (JVM).

JVM is platform specific.

The interpreter translates the bytecodes into specific machine commands.

25

Testing and Debugging the ProgramTesting◦ Be sure that the output of the program conforms with the input.

◦ There are two types of errors:◦ Logical Errors: The program run but provides wrong output.

◦ Runtime errors: The program stop running suddenly when asking the OS executing a non accepted statement (divide by zero, etc).

Debugging◦ Find, Understand and correct the error

26

Some Characteristics of JavaObject-Oriented

◦ Combines data and behavior into one unit objects◦ Provides Data abstraction and encapsulation◦ Decompose program into objects.◦ Programs are collections of interacting and cooperating objects.

Platform-independent◦ Portable◦ Architecture neutral◦ ”Write-once, run-anywhere”

Secure◦ The bytecode verifier of the JVM :

◦ checks untrusted bytecode ◦ controls the permissions for high level actions.

27