. Chapter 1 Introduction to Computers and Programming www.raparinweb.com M hiwa ahmad aziz 1
Ch 1 - Introduction to Computers and Programming Hardware Terminology Main Memory Auxiliary Memory Drives Writing Algorithms Using Pseudo code Programming Language Code The Compilation Process for Non-Java Programs Object Code Portability Java Virtual Machine The Compilation Process for Java Programs History of Java 2
Hardware Terminology Computer system = all of the components shown below. 3
Hardware Terminology I/O = input and output devices Input examples: keyboard, mouse, scanner. Output examples: monitor (screen), printer. CPU = the computer's "brain." Synonyms: central processing unit processor microprocessor popular CPUs: Intel Core 2 Quad Intel Core i7 AMD Phenom II AMD Athlon 64 4
Main Memory When a computer performs calculations, it often needs to save intermediate results. It saves those intermediate results in the main memory storage area. Main memory is often called RAM (Random Access Memory). Memory contains storage boxes, and each storage box contains a piece of information. For example, if a program stores the word Emu, it uses six storage boxes: one for the first half of E, one for the second half of E, one for the first half of m, one for the second half of m, etc. 5
Main Memory Each of the eight storage boxes used to store Emu is a byte. Computers don't understand the alphabet. They only understand 0 s and 1 s. So computers map each alphabet character to a series of sixteen 0's and 1's. For example, the letter E is 00000000 01000101. So in storing the letter E, main memory actually stores 00000000 01000101. Each of the 0's and 1's is called a bit. And each of the eight-bit groupings is a byte. The capacity (size) of memory is described in terms of number of bytes. RAM capacities in a typical computer range from 1 GB (gigabyte) to 8 GB. RAM is volatile data is lost when power is turned off. Address 50,000 50,001 50,002 50,003 50,004 50,005 Memory Contents E m u 6
Vocabulary Hardware - the "machinery" - computer equipment - the CPU, the monitor, the keyboard, the mouse, the external speakers, the scanner, the printer, etc. The physical, touchable parts of a computer system. Software - the program instructions that make the computer do something, such as word processing, database management, games, etc. Your Java programs will be your software. Program - a listing of instructions (code) for the computer to follow written in some programming language. For this course, these instructions will be written in the language of Java. Network - a hardware and software data communication system. Usually a group of computers that are linked to share memory and programs. Control Unit - the unit inside of the CPU which "directs the traffic" - makes decisions. It performs the functions of fetch, decode, execute, and store. Machine Language - the lowest level of computer languages where instructions are given by numeric code. High Level Language - a computer language which is easily read by humans - the code consists of English-like words where each statement corresponds to several machine language instructions. 7
Vocabulary Compiler - converts the source code of a program to machine language placing the result in an object code file. Interpreter - converts a program one line at a time into machine language. Bit - the representation of a 1 or 0 designating power ON or power OFF. A binary digit. Byte - 8 bits. Kilobyte - approximately 1000 bytes (1024 bytes). Megabyte - approximately 1,000,000 bytes (1,048,576 bytes). Gigabyte - approximately a billion bytes (1,073,741,824 bytes). Format/Initialize - to prepare a storage device (diskette or hard disk) for receiving information for a system. The storage device is said to be formatted (into areas called sectors and tracks) when its space has been divided and organized into areas that can be quickly controlled by the system for storage and access. 8
The Mechanics of Creating a Java Program Java Program: a set of instructions for the computer to follow. The source code. Data: data for the Java program. Java Compiler: translates the program from Java to a language that the computer can understand. Compilers differ by make of machine and operating systems. Byte Code Program: the compiler translates Java into a language called byte-code. Byte code is the machine language for a hypothetical computer called the Java Virtual Machine. Byte Code Interpreter: the interpreter translates each instruction of byte code into instructions that the computer in use can execute. Machine Language: is the language the computer in use understands. 9
Writing Algorithms Using Pseudocode A program is a set of instructions that can be used to solve a problem. The first step in writing a program is writing a draft of your program where your focus is on the basic logic, not the specific details. The draft should include instructions that are coherent and logically correct, but there's no need to worry about missing minor steps or misspelling words. Such a draft program is referred to as an algorithm. For example, a Scrambled eggs recipe is an algorithm. Algorithms are written with pseudo code similar to regular programming code except that precise syntax (words, grammar, punctuation) isn't required. Example: Write an algorithm that finds the average miles per hour value for a given car trip. Sample input starting location = 100 ending location = 200 x=vt starting time = 2:00 ending time = 4:00 10
Programming Language Code A programming language is a language that uses specially defined words, grammar, and punctuation that a computer understands. Some of the more popular programming languages are VisualBasic, C++, and Java. Example: Write Java code that finds the average miles per hour value for a given car trip. Initially, programming language code might be harder for you to understand than pseudo code. But after you gain experience with a programming language, you may become so comfortable with it that you're able to skip the algorithm pseudo code step and start coding with the programming language directly. However, for larger programs, I recommend that you do not skip the algorithm pseudo code step. Why? 11
The Compilation Process for Non-Java Programs After writing a program, you'll normally want to have a computer perform the tasks specified by the program. Getting that to work is normally a twostep process: 1) Perform a compile command, 2) Perform a run command. When you perform a compile command, you tell the computer to translate the program's programming language instructions to a binary format (all 0's and 1's). When you perform a run command, you tell the computer to read the binary-format instructions and perform the tasks specified by them. The computer contains a special program called a compiler that's in charge of the compilation process. If you submit programming language instructions to a compiler, the compiler translates them into binary-format instructions. More formally, if you submit source code to a compiler, the compiler compiles the source code and produces object code as the result. Source code is the formal term for programming language instructions. Object code is the formal term for binary-format instructions. 12
The Compilation Process for Non-Java Programs source code (programming language instructions) Programmers write this. Compilers compile source code into object code. object code (binary instructions) Computers run this. 13
The Compilation Process for Java Programs Java source code bytecode object code Java compilers compile source code into bytecode. When a Java program is run, the JVM translates bytecode to object code. 14
Java language features Features instructions Sensitive to Big and small letters for example while is keywords but WHILE is not keywords Each command end with ;. the maximum length of an instruction is 255 characters. Each command can be written in a few lines. Each line can be written in several commands. Description of the * / and / * are placed or after the / / is written 15
Variable and Data types in Java A variable is a named memory location which temporarily stores data that can change while the program is running. Variables of different types occupy different amounts of memory space and are described as having different sizes. Variable Value will change over time For naming From combined Letters a to z or A to Z, Underlined, Numbers. The first character can t be a number and may not use reserved words. Acceptable names count test sum s_1 Unacceptable names 1test high!there grade.1.pcx 16
Java reserved keywords The Java programming language has total of 50 reserved keywords which have special meaning for the compiler and cannot be used as variable names. Following is a list of Java keywords in alphabetical order, click on an individual keyword to see its description and usage example. 17
Variable and Data types in Java Of the eight primitive data types in Java, the four most commonly used are:double, int, boolean, and char. When you learn about objects, you will discuss the differences between primitives and objects. 18
19
Integer and Double This type is numerical value Boolean This type is tow value true and false Boolean type is used for comparison Char For saving the character like 'a', 'b',..., 'y', 'z' is used String For saving Names such as Names of the students: Ali Array An array a collection of data that have the same type. Object Can have every type in java 20
Define the variables Data Type Variable name int a, b; float x, y; char z; There are many ways to give Value for a variable When defining int x=12, y=8; char ch1= a, ch2= b ; float m=11.5; After defining with (=) int s, n ; float f1; s=n=0; f1=13.28; 21
constants are value that there are in program But unlike variables do not change. Variables are defined as constants But before that type placed keyword final placed. final float final boolean final int Invalid instructions pi=3.14; debug = true; id= 12345; pi=3.1445; id=1265; constants debug=false; 22
Operators Operators are symbols that do Certain acts Such as +, * Arithmetic Operators + - * / % ++ -- Int x, y =10; x = y++ ; Int x, y =10 ; x= ++y ; Int m,x =10, y=15 ; m= ++x + y++ ; Relational operators < <= ==!= >= > Logic operators (Boolean) &&! x y X && y X y! x F F F F T F T F T T T F F T F T T T T F Combinatorial operator -= += *= /= %= x + = y => x = x + y 23
Priority operators in general Operator ( ) Highest Priority ++ - -.!. * /. %. + -. < > <= >=. = =!=. &&..?. = += - = / =... lowest Priority 24
Operand with the smaller type converted to the Operand is Greater type. Short s ; Int i ; Float f ; Double d ; Result = (s / i) + (f * d) + (f i) Conversion of type Int x = 10; Float y, m = 3.14 Y = (float) x ; X = (int) m ; int double float but Missing some numbers. double 25
Consider these examples: 26
Sample A small program public class (class name) Declaration class } { } { public static void main(string[] args ) instaction program...... declaration methood main Public class first { public static void main ( String[] args) { System.out.println( java is a powerful program ); System.out.print( let s go to learn java programing ); } } 27