* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Recitation 1
Object-oriented programming wikipedia , lookup
Scala (programming language) wikipedia , lookup
Java performance wikipedia , lookup
Binary search algorithm wikipedia , lookup
Java ConcurrentMap wikipedia , lookup
Abstraction (computer science) wikipedia , lookup
Corecursion wikipedia , lookup
Go (programming language) wikipedia , lookup
C Sharp syntax wikipedia , lookup
CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi 1 Who am I, and Who are You? Who am I? 2nd Year PhD Student in the CS Dept Did my undergrad in CS and MATH Research Interests Computer Networks and Distributed Systems Other interests Sports (Volleyball, Racket Ball etc) Who are you, and what do you do? Introductions… 2 Other Information Office: 6803 SENSQ Email: [email protected] Office Hours: Wed 2:30PM - 5:40PM (10 min break) or by appointment 3 Agenda for Today’s class Review Exercises 4 Recall: Goals of the Course To write computer programs in Java 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 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 HelloWorld.java javac HelloWorld.java compile source code run java HelloWorld HelloWorld.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 A variable should be declared and initialized Variable Declaration: 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 Integer (4 bytes) short (2 bytes) Short integer long (8 bytes) Long integer float Single precision floating point (4 bytes) 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) 8 bits are called a byte Computers store numbers in binary 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 float number = 23.5; ERROR float number = 23.5f; CORRECT float number = 23.5F; CORRECT long bigNumber = 9223372036854775807; ERROR long bigNumber = 9223372036854775807L; CORRECT long bigNumber = 9223372036854775807l;CORRECT 12 The boolean Data Type a boolean variable can have two values: 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 character literals are enclosed in single quotes Example: char letter; A letter = 'A'; b System.out.println(letter); letter = ‘b'; System.out.println(letter); 14 The char Data Type Characters are internally represented by numbers. Java uses Unicode which is a set of numbers that are used as codes for representing characters Example: 65 is the code for A, 66 is the code for B 15 The char Data Type 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 Returns the remainder of a division operation involving two integers Common operation in computer science Examples; 4%5 is 4 30%6 is 0 22%7 is 1 3205%100 is 5 3205%10 is 5 18 Operator Precedence 1. Evaluate – (unary negation) from right to left 2. Evaluate * / % from left to right 3. Evaluate + - from left to right Note: If equal precedence, evaluate from left to right except for negations where we evaluate from right to left 19 Precedence Examples Polynomial = 1+2*3+ 6/2 – 2; Polynomial has the value of 1+6+3-2=8 Polynomial = –1 + 5 – 2; // 2 Polynomial = –(–3) + –(–5); //8 20 Grouping With Parentheses You can use parentheses to force the evaluation of enclosed operations before others 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 value = Math.pow(x,y); // now value holds x to the power of y 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 public class Swap{ public static void main (String args []) { 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 Note: Make use of Math.pow() and Math.sqrt() Assume double x = 4.0 double y = 5.0 double z = 6.0 25 Solution 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 Determine which of them are prime A prime number is a positive integer p>1 that has no positive integer divisors other than 1 and p itself Hint: Use % (modulus) operator For each number p compute p % 2, p % 3,…, p % (p-1) Examples 5%1=0 5%2=1 7%6=1 27 Thanks !! 28