CS 007 Introduction to Computer Programming Ihsan Ayyub
CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi 1
Who am I, and Who are You? l Who am I? ¡ 2 nd Year Ph. D Student in the CS Dept ¡ Did my undergrad in CS and MATH ¡ Research Interests l Computer Networks and Distributed Systems ¡ Other l interests Sports (Volleyball, Racket Ball etc) l Who are you, and what do you do? ¡ Introductions… 2
Other Information Office: 6803 SENSQ Email: ihsan@cs. pitt. edu Office Hours: Wed 2: 30 PM - 5: 40 PM (10 min break) or by appointment 3
Agenda for Today’s class l Review l Exercises 4
Recall: Goals of the Course l To write computer programs in Java l To recognize and create well-written Set of (computer understandable) A programming language instructions to perform a task or a programs in good programming style set of tasks l Hands-on experience with several stages of the life-cycle of a computer program, including planning, implementation, and debugging 5
Example Java Program public class Number { public static void main(String [] args) { int age = 25; int height = 75; int sum = age + height; } } Punctuation Key Words Names Programmer-Defined Syntax Operator 6
Last Time. . // This is a simple Java program. public class Hello { public static void main(String [] args) { System. out. println(“Hello World”); } } 7
Compiling and Running Hello. World. javac Hello. World. java compile source code run java Hello. World. class bytecode 8
Variables In a computer program a variable is a named storage location in the computer’s memory. Each variable has a type and a name l A variable should be declared and initialized l Variable Declaration: l ¡ ¡ ¡ l char a; char is the variable type and a is the variable name Variables must be declared before they can be used Variable Initialization: ¡ ¡ a = ‘z’; // Assigning value to a variable The equal sign is an operator that stores the value on its right into the variable named on its left. 9
Primitive Data Types Java Data Type Description Boolean true or false char A character int (4 bytes) Integer short Short integer long (2 bytes) (8 bytes) float (4 bytes) Long integer Single precision floating point Double (8 bytes) Double precision floating point byte (1 byte) limited range (Integer) 10
What Is A Byte? The smallest data unit in a computer is a bit (1 or 0) l 8 bits are called a byte l Computers store numbers in binary l ¡ ¡ ¡ l 0 = 0 in binary 1 = 1 in binary 2 = 10 in binary 3 = 11 in binary 4 = 100 in binary Difference between counting using decimal umbers and binary numbers 11
Special Cases l float number = 23. 5; ERROR l float number = 23. 5 f; CORRECT l float number = 23. 5 F; CORRECT long big. Number = 9223372036854775807; ERROR l long big. Number = 9223372036854775807 L; CORRECT l long big. Number = 9223372036854775807 l; CORRECT l 12
The boolean Data Type l a boolean variable can have two values: ¡ ¡ l true false Example: boolean bool; bool = true; System. out. println(bool); bool = false; System. out. println(bool); true false 13
The char Data Type used to store characters l character literals are enclosed in single quotes l Example: char letter; A letter = 'A'; b System. out. println(letter); letter = ‘b'; System. out. println(letter); l 14
The char Data Type l Characters are internally represented by numbers. l Java uses Unicode which is a set of numbers that are used as codes for representing characters ¡ Example: for B 65 is the code for A, 66 is the code 15
The char Data Type l variables of char type can be defined using either literal characters or their corresponding numeric codes ¡ Example: char letter; letter = 65; System. out. println(letter) letter = 66; System. out. println(letter); A B 16
Arithmetic operators Operator Meaning Type Example + Addition Binary total = cost+tax; - Subtraction Binary cost = total-tax; * Multiplication Binary Tax = cost*rate; / Division Binary saleprice=original/2; % Modulus Binary remainder=value%3; 17
The % Operator l Returns the remainder of a division operation involving two integers l Common operation in computer science l Examples; ¡ 4%5 is 4 ¡ 30%6 is 0 ¡ 22%7 is 1 ¡ 3205%100 is 5 ¡ 3205%10 is 5 18
Operator Precedence Evaluate – (unary negation) from right to left 2. Evaluate * / % from left to right 3. Evaluate + - from left to right l Note: If equal precedence, evaluate from left to right except for negations where we evaluate from right to left 1. 19
Precedence Examples l Polynomial ¡ Polynomial = 1+2*3+ 6/2 – 2; has the value of 1+6+3 -2=8 l Polynomial = – 1 + 5 – 2; // 2 l Polynomial = –(– 3) + –(– 5); //8 20
Grouping With Parentheses l You can use parentheses to force the evaluation of enclosed operations before others l Examples: ¡x * ( y + z*z ) instead of x*y + z*z ¡ x * ( y * ( z + 165 ) + 85 ) – 65 ¡ Average = (a +b +c ) /3; 21
The Math class l value = Math. pow(x, y); // now value holds x to the power of y l value = Math. sqrt(x); //now value holds the square root of x 22
Combined Assignment Operators += x += 1; x = x + 1; –= x –= 1; x = x – 1; *= x *= 1; x = x * 1; /= x /= 1; x = x / 1; %= x %= 1; x = x % 1; 23
Exercise 1: Swap the values of two integers l public class Swap{ ¡ ¡ public static void main (String args []) { l l l l ¡ l } } l int x, y, z; x = 5; y = 8; System. out. println("x="+x+" y="+y); z = x; x = y; Y = z; System. out. println("x="+x+" y="+y); 24
Exercise 2: Compute the average and Standard Deviation of three numbers l Note: Make use of Math. pow() and Math. sqrt() l Assume ¡ double x = 4. 0 ¡ double y = 5. 0 ¡ double z = 6. 0 25
Solution l l l l public class Multiply{ public static void main (String args []) { double x=4. 0, y=5. 0, z=6. 0; double av, sd; av = (x+y+z)/3. 0; sd = Math. pow((x-av), 2) + Math. pow((y-av), 2) + Math. pow((z-av), 2); sd = sd/3. 0; sd = Math. sqrt(sd); System. out. println("Average = "+av); System. out. println("Standard Deviation = "+sd); } } 26
Exercise 3: Determining Prime Numbers Assume that you are given numbers 5, 6, … 10 l Determine which of them are prime l A prime number is a positive integer p>1 that has no positive integer divisors other than 1 and p itself l Hint: l ¡ ¡ Use % (modulus) operator For each number p compute l ¡ p % 2, p % 3, …, p % (p-1) Examples l l l 5%1=0 5%2=1 7%6=1 27
Thanks !! 28
- Slides: 28