Object-Oriented Programming (2) Oop2: Variables, Constants and Built-In Data Types Java Program Structure

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 41

Object-Oriented Programming (2) OOP2

Introduction Variables, Constants and Built-in Data Types Java Program Structure

Outline
What a computer is What a computer program is The Programmers Algorithm How a program that you write in Java is changed into a form that your computer can understand Characteristics of Java Variables, Constants and Built-in Data Types Java Program Structure

Page 2 OOP2

What Is a Computer?
Computer Executes statements (computations/logical decisions) Hardware :Physical devices of computer system Software: Programs that run on computers
Central Processing Unit

Control Unit Input device Arithmetic/Logic Unit Output Device

Memory Unit

My data

My Progam

Page 3

OOP2

Computer Organization
Six logical units of computer system Input unit (Mouse, keyboard) Output unit (Printer, monitor, audio speakers) Memory unit (Retains input and processed information) Central processing unit (CPU) which consists of:
Control unit (Supervises operation of other devices) Arithmetic and logic unit (ALU) (Performs calculations)

Secondary storage unit (Hard drives, floppy drives)


Page 4 OOP2

What a computer program is?


For a computer to be able to perform specific

tasks (i.e. print what grade a student got on an exam), it must be given instructions to do the task.

The set of instructions that tells the computer

to perform specific tasks is known as a

computer program

Page 5

OOP2

Levels of Program Development


Human thought
Pseudo-Natural Language (English, Arabic) High Level Programming Language (C, C++, Java, ) Machine Code

Page 6

OOP2

The Programmers Algorithm


An algorithm is a finite sequence of

instructions that produces a solution to a problem.

The programmers algorithm: Define the problem Plan the problem solution Code the program. Compile the program. Run the program. Test and debug the program.
Page 7 OOP2

Defining the Problem


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

and processing: The statements to achieve.


Look for verbs to suggest processing steps.

input data Keyboard Processing

output data Screen

Page 8

OOP2

Input and Output


Inputs 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

Page 9

OOP2

Example: Area and Perimeter of a circle


Input

Radius PI Processing Area = PI * Radius * Radius Perimeter = 2 * PI * Radius Output Area Perimeter

Page 10

OOP2

Planning the Solution


When planning, algorithms are used to

outline the solution steps using Englishlike statements, called pseudocode.

Page 11

OOP2

Coding the Program


Coding 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.
Algorithm Pseudocode Translating Source Code (The .java)
OOP2

Coding
Program
Page 12

Compiling Computer Programs


Computers do not understand programs

written in programming languages such 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
Program Compiling Source code Translating

Machine code Machine Code


Page 13 OOP2

Programming Language Compiler


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

Page 14

OOP2

Platform dependent Compiling


Because different platforms , or hardware

architectures along with the operating systems (Windows, Macs, Unix), require different m a c h in e c o d e , y o u m u s t c o m p i l e m o s t programs separately for each platform .

Page 15

OOP2

Compiling Java Programs


The Java compiler produces bytecode (a .class

.java file). Bytecode is converted into machine code using a

file) not machine code from the source code (the Java Interpreter

Source Code

Bytecode

Page 16

OOP2

Platform Independent Java Programs Compiling


You can run bytecode on an computer that

has a Java Interpreter installed

Hello.java

Hello.class

Page 17

OOP2

Multipurpose Java Compiling

Page 18

OOP2

Running The Program


Class Loader

Running
Bytecode Verifier

The Bytecode (the .class file)

Bytecode Interpreter JVM


Operating System

Hardware

Page 19

OOP2

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

Page 20

OOP2

The Java Virtual Machine


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

Page 21

OOP2

Testing and Debugging the Program


Testing 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 none accepted statement (divide by zero, etc).

Debugging Find, Understand and correct the error


Page 22 OOP2

Phase 1

Editor

Disk

Program is created in an editor and stored on disk in a file ending with .java.

Phase 2

Compiler

Disk Primary Memory

Compiler creates bytecodes and stores them on disk in a file ending with .class.

Phase 3

Class Loader

Disk

Class loader reads .class files containing bytecodes from disk and puts those bytecodes in memory. .. . .. .

Java Development Environment

Primary Memory

Phase 4

Bytecode Verifier

Bytecode verifier confirms that all bytecodes are valid and do not violate Javas security restrictions. .. . .. .

Primary Memory

Phase 5

Interpreter

Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes.

.. . .. .

Page 23

OOP2

Some Characteristics of Java


Object-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.
Page 24 OOP2

Variables, Constants and Built-in Data Types Java Program Structure

Discovering what is a variable

Discovering what is a data type


Learning about the basic data types Constants and variables identifiers Get acquainted with how to select proper

types for numerical data. Write arithmetic expressions in Java. Program Structure.
Page 25

OOP2

Programs and Data


input data output data Processing

Keyboard

Screen

Most programs require the temporary storage of data.

The data to be processed is stored in a temporary storage in the computer's memory: space memory.
Identifier Data Type State

A space memory has three characteristics

Page 26

OOP2

State of the Space Memory


The state of the space memory is the

current value (data) stored in the space memory.

The state of the space memory:


May be changed.
In this case the space memory is called variable.

Cannot be changed.
In this case the space memory is called constant.

Page 27

OOP2

Space Memory Identifier


Identifier is a sequence of characters that

denotes the name of the space memory to be used.


This name is unique within a program.

Identifier Rules
It cannot begin with a digit (0 9).
It may contain the letters a to z, A to Z, the digits 0 to 9, and the underscore symbol, _. No spaces or punctuation, except the underscore symbol, _, are allowed.
Page 28 OOP2

Identifier Conventions in Java


Constants: All uppercase, separating words within a multiword identifier with the underscore symbol, _. Variables All lowercase. Capitalizing the first letter of each word in a multiword identifier, except for the first word.
Page 29 OOP2

Identifiers are Case-Sensitive


Identifiers in Java are case-sensitive. Thus, the identifiers myNumber and mynumber, are seen as two different identifiers by the compiler.

Page 30

OOP2

Data Type
The data type defines what kinds of values a space memory is allowed to store. All values stored in the same space memory should be of the same data type. All constants and variables used in a Java program must be defined prior to their use in the program.

Page 31

OOP2

Java built-in Data Types


First Decision Level Constant or Variable

Second Decision Level

Numeric

Character

Boolean

Third Decision Level

Integer

Floating-point

char

String

boolean

byte Fourth Decision Level short int long

float double

Page 32

OOP2

Primitive Data Types


Type boolean char byte short int long 16 8 16 32 bits 64 bits Size (bits) true, false 0 to 65535 -128 to +127 -32768 to +32767 -2,147,483,648 to +2,147,483,647 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 accurate to 8 significant digits Range Description Stores a value that is either true or false. Stores a single 16-bit Unicode character. Stores an integer. Stores an integer. Stores an integer. Stores an integer.

float double

32 bits 64 bits

Stores a single-precision floating point number.

accurate to 16 significant digits Stores a double-precision floating point number.


OOP2

Page 33

Page 34

OOP2

Variable/Constant Declaration
When the declaration is made, memory space

is allocated to store the values of the declared variable or constant. The declaration of a variable means allocating a space memory which state (value) may change. The declaration of a constant means allocating a space memory which state (value) cannot change.
Page 35 OOP2

Constant Declaration
final dataType constIdentifier = literal | expression; final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060;

The reserved word final is used to declare constants.

These are constants, also called named constant.

These are called

literals.

final int MAX final int MIN final int AVG

= 1024; = 128; = (MAX + MIN) / 2;


This is called

expression.
Page 36

OOP2

Variable Declaration
A variable may be declared: With initial value. Without initial value. Variable declaration with initial value;
dataType variableIdentifier = literal | expression;

double avg = 0.0; int i = 1; int x =5, y = 7, z = (x+y)*3;

Variable declaration without initial value;


dataType variableIdentifier;

double avg; int i;


Page 37 OOP2

Java Program Structure


// import Section import used java libraries public class MyProgramName {
// main method public static void main( String args[] ){ // Declaration section Declare needed variables // Input section Enter required data // Processing section Processing Statements

// Output section Display expected results


} // end main } // end class
Page 38 OOP2

Salam Program
// import section: Empty public class MySalamProgram {
// main method public static void main( String args[] ){ // Declaration section: Empty // Input section: Empty // Processing section: Empty

// Output section System.out.println( SalamaHello);


} // end main

} // end class
Page 39 OOP2

Saving, Compiling and Running Java Programs


Saving a Java program. A file having a name same as the class name should be used to save the program. The extension of this file is .java.

Compiling a Java program. Call the Java compiler javac:


javac MySalamProgram.java

Salam program should be saved in a file called MySalamProgram.java.

Running a Java program Call the Java Virtual Machine java:


java MySalamProgram.class

The Java compiler generates a file called MySalamProgram.class (the bytecode).

Page 40

OOP2

Comments in a Java Program


Comments are used to describe what your code does

and aid reading your code. The Java compiler ignores them. Comments are made using

//, which comments to the end of the line, or /* */, everything inside of it is considered a comment (including multiple lines). The comment begins after the first /*. It ends just before the first */.

Examples: /* This comment begins at this line. This line is included in this comment It ends at this line. */
// This comment starts here and ends at the end of this line.
Page 41 OOP2

You might also like