Date post: | 05-Apr-2018 |
Category: |
Documents |
Upload: | noor-halemeh |
View: | 233 times |
Download: | 1 times |
of 41
7/31/2019 1 Intro+Var Data Types
1/41
Object-OrientedProgramming (2)
OOP2
Introduction
Variables, Constants and Built-in Data TypesJava Program Structure
7/31/2019 1 Intro+Var Data Types
2/41
OOP2Page 2
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 canunderstand
Characteristics of Java Variables, Constants and Built-in Data Types
Java Program Structure
7/31/2019 1 Intro+Var Data Types
3/41
OOP2Page 3
What Is a Computer?
Computer
Executes statements (computations/logical decisions)
Hardware :Physical devices of computer system
Software: Programs that run on computers
Control Unit
Arithmetic/Logic Unit
Memory Unit
Input device Output Device
Central Processing Unit
My ProgamMy data
7/31/2019 1 Intro+Var Data Types
4/41
OOP2Page 4
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)
7/31/2019 1 Intro+Var Data Types
5/41
OOP2Page 5
What a computer program is?
For a computer to be able to perform specifictasks (i.e. print what grade a student got onan exam), it must be given instructions to do
the task.
The set of instructions that tells the computerto perform specific tasks is known as acomputer program
7/31/2019 1 Intro+Var Data Types
6/41
OOP2Page 6
Levels of ProgramDevelopment
Human thought
Pseudo-Natural Language (English, Arabic)
High Level Programming Language (C, C++,Java, )
Machine Code
7/31/2019 1 Intro+Var Data Types
7/41
OOP2Page 7
The Programmers Algorithm
An algorithm is a finite sequence ofinstructions that produces a solution toa 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.
7/31/2019 1 Intro+Var Data Types
8/41
OOP2Page 8
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 andinput.
and processing: The statements to achieve. Look for verbs to suggest processing steps.
Keyboard ScreenProcessing
input data output data
7/31/2019 1 Intro+Var Data Types
9/41
OOP2Page 9
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 otherprograms
7/31/2019 1 Intro+Var Data Types
10/41
OOP2Page 10
Example:Area and Perimeter of a circle
Input
Radius
PI
ProcessingArea = PI * Radius * Radius
Perimeter = 2 * PI * Radius
Output
Area Perimeter
7/31/2019 1 Intro+Var Data Types
11/41
OOP2Page 11
Planning the Solution
When planning, algorithms are used tooutline the solution steps usingEnglishlike statements, called
pseudocode.
7/31/2019 1 Intro+Var Data Types
12/41
OOP2Page 12
Coding the Program
Coding is writing the program in a formal languagecalled Programming Language.
Programming Language : A set of rules, symbols and specialwords used to write statements.
The program is written by translating the algorithmsteps into a programming language statements.
The written program is called Source codeand it issaved in a file with .java extension.
Program
Coding
Algorithm Pseudocode
Source Code(The .java)
Translating
7/31/2019 1 Intro+Var Data Types
13/41
OOP2Page 13
Compiling Computer Programs
Computers do not understandprogramswritten in programming languagessuch asC++ and Java
Programsmust first be convertedintomachine codethat the computer can run
A Software that translatesa programminglanguage statements into machine code iscalled a compiler
Machine code
Program Source code
Machine Code
TranslatingCompiling
7/31/2019 1 Intro+Var Data Types
14/41
OOP2Page 14
Programming Language Compiler
A compiler is a software that: Checks the correctnessof the source code
according to the language rules.
Syntax errors are raised if some rules wereviolated.
Translatesthe source code into a machinecode if no errors were found.
7/31/2019 1 Intro+Var Data Types
15/41
OOP2Page 15
Platform dependent Compiling
Because different platforms, or hardwarearchitectures along with the operating systems(Windows, Macs, Unix), require different
machine code, you must compi le mostprograms separately for each platform.
7/31/2019 1 Intro+Var Data Types
16/41
OOP2Page 16
Compiling Java Programs
The Java compiler produces bytecode(a .classfile)not machine code from the source code (the
.java file).
Bytecode is converted into machine code using aJava Interpreter
Source Code Bytecode
7/31/2019 1 Intro+Var Data Types
17/41
OOP2Page 17
Platform IndependentJava Programs Compiling
You can run bytecode on an computer thathas a Java Interpreter installed
Hello.java Hello.class
7/31/2019 1 Intro+Var Data Types
18/41
OOP2Page 18
Multipurpose Java Compiling
7/31/2019 1 Intro+Var Data Types
19/41
OOP2Page 19
Running The Program
ClassLoader
BytecodeInterpreter
BytecodeVerifier
Hardware
Operating System
JVMThe Bytecode(the .class file)
Running
7/31/2019 1 Intro+Var Data Types
20/41
OOP2Page 20
The Java Virtual MachineComponents
The Class Loader stores bytecodes in memory
Bytecode Verifier ensures bytecodes do not violate securityrequirements
Bytecode Interpreter translates bytecodes into machine language
7/31/2019 1 Intro+Var Data Types
21/41
OOP2Page 21
The Java Virtual Machine
The class Loader, the BytecodeVerifierand Interpreter constitute the Java VirtualMachine (JVM).
JVM is platform specific.
The interpreter translates the bytecodes
into specific machine commands.
T ti d D b i th
7/31/2019 1 Intro+Var Data Types
22/41
OOP2Page 22
Testing and Debugging theProgram
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 provideswrong output.
Runtime errors:The program stop runningsuddenly when asking the OS executing a noneaccepted statement (divide by zero, etc).
Debugging
Find, Understand and correct the error
7/31/2019 1 Intro+Var Data Types
23/41
OOP2Page 23
Primary
Memory
.
.
.
.
.
.
Disk
Disk
Disk
Editor
Compiler
Class Loader
Program is created inan editor and stored ondisk in a file ending with.java.
Compiler createsbytecodes and storesthem on disk in a fileending with .class.
Class loader reads.class filescontainingbytecodes from diskand puts thosebytecodes inmemory.
Phase 1
Phase 2
Phase 3
PrimaryMemory
.
.
.
.
.
.
Bytecode Verifier Bytecode verifierconfirms that allbytecodes are validand do not violateJavas securityrestrictions.
Phase 4
PrimaryMemory
.
.
.
.
.
.
InterpreterInterpreter readsbytecodes andtranslates them into alanguage that thecomputer canunderstand, possiblystoring data values asthe programexecutes.
Phase 5
JavaDevelopmentEnvironment
7/31/2019 1 Intro+Var Data Types
24/41
OOP2Page 24
Some Characteristics of Java
Object-Oriented Combines data and behavior into one unitobjects
Provides Data abstraction and encapsulation
Decomposeprogramintoobjects.
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.
7/31/2019 1 Intro+Var Data Types
25/41
OOP2Page 25
Variables, Constants and Built-in Data TypesJava 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 propertypes for numerical data.
Write arithmetic expressions in Java.
Program Structure.
7/31/2019 1 Intro+Var Data Types
26/41
OOP2Page 26
Programs and Data
Most programs require the temporary storage of data.The data to be processed is stored in a temporary storagein the computer's memory: space memory.
A space memory has three characteristics Identifier
Data Type
State
Keyboard ScreenProcessing
input data output data
7/31/2019 1 Intro+Var Data Types
27/41
OOP2Page 27
State of the Space Memory
The state of the space memoryis thecurrent value (data) stored in the spacememory.
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.
7/31/2019 1 Intro+Var Data Types
28/41
OOP2Page 28
Space Memory Identifier Identifier is a sequence of characters that
denotes the name of the space memory tobe 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, thedigits 0 to 9, and the underscore symbol, _.
No spaces or punctuation, except theunderscore symbol, _, are allowed.
7/31/2019 1 Intro+Var Data Types
29/41
OOP2Page 29
Constants:
All uppercase, separating words withina multiword identifier with theunderscore symbol, _.
Variables
All lowercase.
Capitalizing the first letter of each wordin a multiword identifier, except for thefirst word.
Identifier Conventions in Java
7/31/2019 1 Intro+Var Data Types
30/41
OOP2Page 30
Identifiers are Case-Sensitive
Identifiers in Java are case-sensitive. Thus,the identifiers myNumber and mynumber,are seen as two different identifiers by thecompiler.
7/31/2019 1 Intro+Var Data Types
31/41
OOP2Page 31
Data Type
The data type defines what kinds of values aspace memory is allowed to store.
All values stored in the same space memoryshould be of the same data type.
All constants and variables used in a Javaprogram must be defined prior to their use in
the program.
7/31/2019 1 Intro+Var Data Types
32/41
OOP2Page 32
Java built-in Data Types
Constant or Variable
Numeric Character Boolean
Integer Floating-point
short
int
char String boolean
float
double
First Decision Level
Second Decision Level
Third Decision Level
Fourth Decision Level
long
byte
P i iti D t T
7/31/2019 1 Intro+Var Data Types
33/41
OOP2Page 33
Primitive Data TypesType
Size(bits)
Range Description
boolean true, false Stores a value that is eithertrue or false.
char 16 0 to 65535 Stores a single 16-bitUnicode character.
byte 8 -128 to +127 Stores an integer.
short 16 -32768 to +32767 Stores an integer.
int 32 bits -2,147,483,648 to+2,147,483,647
Stores an integer.
long 64 bits -9,223,372,036,854,775,808to+9,223,372,036,854,775,807
Stores an integer.
float 32 bits accurate to 8 significant digits Stores a single-precisionfloating point number.
double 64 bits accurate to 16 significant digits Stores a double-precisionfloating point number.
7/31/2019 1 Intro+Var Data Types
34/41
OOP2Page 34
7/31/2019 1 Intro+Var Data Types
35/41
OOP2Page 35
Variable/Constant Declaration
When the declaration is made, memory spaceis allocated to store the values of the declaredvariable or constant.
The declaration of a variable means allocatinga space memory which state (value) maychange.
The declaration of a constant means allocatinga space memory which state (value) cannotchange.
7/31/2019 1 Intro+Var Data Types
36/41
OOP2Page 36
Constant Declaration
final dataType constIdentifier = literal | expression;
final double PI = 3.14159;final int MONTH_IN_YEAR = 12;final short FARADAY_CONSTANT = 23060;
final int MAX = 1024;
final int MIN = 128;final int AVG = (MAX + MIN) / 2;
These are constants,also called namedconstant.
The reserved wordfinal is used todeclare constants.
These are calledliterals.
This is calledexpression.
7/31/2019 1 Intro+Var Data Types
37/41
OOP2Page 37
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;
Java Program Structure
7/31/2019 1 Intro+Var Data Types
38/41
OOP2Page 38
// import Section import used java libraries
public classMyProgramName {
// main method
public static voidmain( 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
Java Program Structure
Salam Program
7/31/2019 1 Intro+Var Data Types
39/41
OOP2Page 39
// import section: Empty
public classMySalamProgram{
// main method
public static voidmain( String args[] ){
// Declaration section: Empty
// Input section: Empty
// Processing section: Empty
// Output sectionSystem.out.println( SalamaHello);
}// end main
}// end class
Salam Program
S i C ili d R i
7/31/2019 1 Intro+Var Data Types
40/41
OOP2Page 40
Saving, Compiling and RunningJava 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 thisfile is .java.
Salam program should be saved in a file called
MySalamProgram.java. Compiling a Java program.
Call the Java compilerjavac: javacMySalamProgram.java
The Java compiler generates a file called MySalamProgram.class(the bytecode).
Running a Java program Call the Java Virtual Machinejava:
javaMySalamProgram.class
Comments in a Java Program
7/31/2019 1 Intro+Var Data Types
41/41
Comments in a Java Program
Commentsare used to describe what your code doesand 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 commentIt ends at this line. */
//This comment starts here and ends at the end of this line.