introduction to programming (java) 1/12 · 2008. 9. 18. · introduction to programming (java) 1/12...
TRANSCRIPT
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Introduction to Programming (Java)1/12
Michal Krátký
Department of Computer ScienceTechnical University of Ostrava
Introduction to Programming (Java)2008/2009
c©2006–2008 Michal Krátký Introduction to Programming (Java) 1/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Michal Krátký
tel.: +420 596 993 239room: A1004e-mail: [email protected]: http://www.cs.vsb.cz/kratky/Czech version:http://www.cs.vsb.cz/kratky/courses/2008-09/upr/
English version:http://www.cs.vsb.cz/kratky/courses/2008-09/upr/exam-en.html
c©2006–2008 Michal Krátký Introduction to Programming (Java) 2/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Breakpoints and Credit
This course is finished by a classified credit. The maximalnumber of points 100.There are three real-time tests:
Test 1: 6th week, 15p, min 8b, 1h,Test 2: 9th week, 25p, min 12b, 1.5h,Test 3: 12th week, 30p, min 16b, 1.5h.
Form: student guesses a task and she/he must developthis task during the defined time. If her/his program will notbe correct, the number of points is 0.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 3/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Breakpoints and Credit 2/2
When you will not execute this test or you do not reach theminimal number of points, you may utilize a possibility of acorrection.
The correction of each test will be held the one week afterthe test. No another correction is possible.
This course is finished by a test. The minimal number ofpoints is 15p from 30p.
Warning: Although the successfulness of this courseis 30%. Students suppose that you can learn to programby reading of books.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 4/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
The Course Content 1/2
1 introduction, algorithm; hardware architectures; numbersystems, operation with numbers
2 introduction to Java programming language3 primitive data types, data types, assign operators;
statements, block4 array, language constructions5 language constructions, methods, pointer vs value,
recursion
c©2006–2008 Michal Krátký Introduction to Programming (Java) 5/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
The Course Content 2/2
1 searching I. Test 12 searching II.3 linear data structures – stack, queue, list4 dynamic memory allocation Test 25 ordering I.6 packages, exceptions, streams, parameters of a command
line7 ordering II. Test 3
c©2006–2008 Michal Krátký Introduction to Programming (Java) 6/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Algorithm
J. Dvorský, E. Ochodková, D. Duráková. Algoritmy.Skripta, VŠB-Technická univerzita Ostrava,http://www.cs.vsb.cz/dvorsky/Opora.html,2007.
The term ’algorithm’ has origin in 9th century in Arabia.Persian mathematician Muhammad ibn Músá al Chwárizmíwrote two books in 800-825.One from these books is titled Algoritmi dicit in Latin (’Itsays al Chwárizmí’ in English).These books describes steps for a computation withnumbers. The ’algorithm’ term was created from the Latinterm ’Al-Gorizmí’.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 7/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Algorithm
Algorithm is a ’prescript’ for a solution of an issue. For example,a computation of an area of a rectangle is a such issue. Wemust do an analysis of the issue and we need three entities:
1 values of input data: size of edges a, b
2 a prescript for solution: equation S = a× b
3 the result (output data): the area of a rectangle (S)
c©2006–2008 Michal Krátký Introduction to Programming (Java) 8/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Algorithm
Algorithm includes steps, it provides output data to inputdata.Algorithm must output the same result for the same inputdata.Each program includes an ordered set of steps. Runtime ofa program is deterministic.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 9/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
A Program
Algorithm is written in a programming language – aprogram is written.
Program is a prescript readable for a computer.
Program must be readable for other programmers as well.Remarks are often used by real programmers.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 10/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Programming
Programming is:
1 Understanding of the task. The goal is the exact analysisof all situations and to design the input and output data.
2 Development of overall solution of the issue.3 We must detect parts to be written as a stand-alone entity.4 Development of a source code in a programming language.5 Debugging and validation of the program.6 Optimization of the work.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 11/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Low-level Programming Languages
1 A microprocessors includes a lotof registers which are used by aprogrammer.
2 Computer code is applied forprogramming of a microprocessor.
3 An assembler may be used forprogramming.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 12/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Example – An Application with GUI
Let us have an application with GUI – Calculator in thiscase.Low-level languages are not appropriate for this purpose.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 13/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Low-level Programming Languages?
Must programmer of an application know the hardwarearchitecture? For example, the core of AMD’s Opteron inthis case.
c© http://www.chip-architect.com/
c©2006–2008 Michal Krátký Introduction to Programming (Java) 14/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Higher Programming Languages
We need programming languages without a knowledge of ahardware architecture. These languages include four tools forthe note of an algorithm:
1 Primitive expressions, i.e. data (numbers, charactersand so on) and operators (addition, multiplication, logicaloperators and so on).
2 Complex expressions include other simpler expressions.3 Named expressions – we can define a variable,
procedure and so on.4 Language Constructions – cycles, branching and so on.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 15/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Labeling of Values
Generally, we use an association of a name with a value in aprogramming language. For example:
int a = 486. The name a is related with the value 486in Decimal system.
int b = a + 37. The name + is related with aprocedure for the addition operation (the value of + is theprocedure).
Consequently, a user utilizes the name in a program instead ofvalues, however, compiler uses the values.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 16/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Number Systems - A Motivation
µ-processor AMD Opteron includes 16 basic 64b registersutilized by a programmer
Although we have not to know the computer architecture inthe case of higher programming languages, we wouldknow a background of a program runtime
32b register means 32 digits in Binary system – minimaland maximal values are defined
Primitive data types and their operations are often mappedto registers and instructions of µ-processors
c©2006–2008 Michal Krátký Introduction to Programming (Java) 17/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
An Integer in a Computer
az = anzn + an−1zn−1 + ... + a2z2 + a1z1 + a0z0
where
az is an integer for a number system with the z basez is the base of the number system, z > 1. The z i value,where i = 0, 1, ..., n is called weigh of the i order.ai are digits, 0 ≤ ai ≤ z − 1. We call the ai value as thevalue of the i order.n is the number of digits
c©2006–2008 Michal Krátký Introduction to Programming (Java) 18/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Decimal System (The Base-10 System)
a10 = an10n + an−110n−1 + ... + a2102 + a1101 + a0100
372510 = 3× 103 + 7× 102 + 2× 101 + 5× 100 =3× 1000 + 7× 100 + 2× 10 + 5× 1
The most important number system.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 19/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Binary, Octant, and Hexadecimal Number Systems
Binary (Base-2), octant (Base-8), hexadecimal (Base-16)number systems.
a2 = an2n + an−12n−1 + ... + a222 + a121 + a020
a8 = an8n + an−18n−1 + ... + a282 + a181 + a080
a16 = an16n + an−116n−1 + ... + a2162 + a1161 + a0160
0010 0110 0001 11012 = 230358 = 261D16
The maximal value in the register with the size n is 2n − 1. E.g.the maximal value for the 8b register is 255.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 20/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Conversion to Decimal System
20123 = 2×33+0×32+1×31+2×30 = 2×27+0+3+2 = 5910
1101102 = 1× 25 + 1× 24 + 0× 23 + 1× 22 + 1× 21 + 0× 20 =32 + 16 + 4 + 2 = 5410
D416 = 13× 161 + 4× 160 = 208 + 4 = 21210
c©2006–2008 Michal Krátký Introduction to Programming (Java) 21/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Conversion from the Base-2 System to the Base-8 andBase-16 Systems
975710
A foursome of bits (from the lowest bit) creates a digit inHexadecimal system (Base-16).0010 | 0110 | 0001 | 1101 = 261D16
Example: 1101 = 8 + 4 + 0 + 1 = 1310 = D16
A triplet of bits (from the lowest bit) creates a digit in Octantsystem (Base-8).0 | 010 | 011 | 000 | 011 | 101 = 0230358 = 230358
Example: 101 = 4 + 0 + 1 = 510 = 58
c©2006–2008 Michal Krátký Introduction to Programming (Java) 22/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Addition of Binary Values
1101 1100 1010 1110 = 5649410+ 0010 0110 1011 0011 = 990710
1 0000 0011 0110 0001 = 6640110
0 + 0 = 00 + 1 = 11 + 1 = 0, a carry to the higher order1 + 1 + 1 = 1, a carry to the higher order
c©2006–2008 Michal Krátký Introduction to Programming (Java) 23/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Bit Shifts
Right and left bit shift. It is not a rotation and it is the unsignedshift!1010 1110 >> 1 = 0101 01110010 1110 << 1 = 0101 11001000 0000 >> 7 = 0000 00010000 0001 << 7 = 1000 0000
Applications:
A computation of the register length.Quick integer multiplication/division by 2n value, where n isthe number of the shift.For example: 1010 1110 >> 1 = 0101 0111, 17410 / 2 =8710.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 24/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Bit OR
1101 1100 1010 1110OR 0010 0110 1011 0011
1111 1110 1011 1111
Truth Table:0 OR 0 = 00 OR 1 = 11 OR 0 = 11 OR 1 = 1
c©2006–2008 Michal Krátký Introduction to Programming (Java) 25/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Bit AND
1101 1100 1010 1110AND 0010 0110 1011 0011
0000 0100 1010 0010
Truth Table:0 AND 0 = 00 AND 1 = 01 AND 0 = 01 AND 1 = 1
c©2006–2008 Michal Krátký Introduction to Programming (Java) 26/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
NOT - Bit Negation
NOT 1101 1100 1010 1110= 0010 0011 0101 0001
Truth Table:NOT 0 = 1NOT 1 = 0
c©2006–2008 Michal Krátký Introduction to Programming (Java) 27/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Underflow/Overflow
Let us have 4b register and the 11112 number. If we addthe 1 value, overflow appears.
Let us have 4b unsigned register and the 00012 number. Ifwe subtract the 2 value, underflow appears.
These issues are often indicated by special processor’sregisters.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 28/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Signed Integers 1/2
The highest bit in a register is the sign bit.
If the size of a register is 32b, then the value is stored inthe lower 31bits. The sign is stored in the 32nd bit (0 –positive value, 1 – negative value).
We need a special code for operations - we utilize theComplementary code: the value is negated and the 1value is added.
990710, NOT 0010 0110 1011 0011 + 1 = 1101 1001 01001101
c©2006–2008 Michal Krátký Introduction to Programming (Java) 29/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Sign Integers 2/2
The addition operation is implemented as the subtract if weutilize the Complementary code.If overflow appears in the case of subtract operation, theoverflow bit is ignored.
1101 1100 1010 1110 = 5649410+ 1101 1001 0100 1101 = 990710
1011 0101 1111 1011 = 4658710, overflow appears
c©2006–2008 Michal Krátký Introduction to Programming (Java) 30/31
Introduction to Programming (Java) 1/12Basic InfoAn Algorithm and ProgramNumber Systems
Real Numbers
zM × 2e:z - sign,m - mantissa,e - exponent.
For example:64b real number: mantissa 52b, exponent 11b.80b real number: mantissa 64b, exponent 15b.
c©2006–2008 Michal Krátký Introduction to Programming (Java) 31/31