Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.

Similar presentations


Presentation on theme: "1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi."— Presentation transcript:

1 1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi

2 2 Who am I, and Who are You? Who am I?  2 nd 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…

3 3 Other Information Office: 6803 SENSQ Email: ihsan@cs.pitt.edu Office Hours: Wed 2:30PM - 5:40PM (10 min break) or by appointment

4 4 Agenda for Today’s class Review Exercises

5 5 Recall: Goals of the Course To write computer programs in Java To recognize and create well-written programs in good programming style Hands-on experience with several stages of the life-cycle of a computer program, including planning, implementation, and debugging Set of (computer understandable) instructions to perform a task or a set of tasks A programming language

6 6 Example Java Program Key Words Operator Punctuation Syntax Programmer-Defined Names public class Number { public static void main(String [] args) { int age = 25; int height = 75; int sum = age + height; } }

7 7 Last Time.. // This is a simple Java program. public class Hello { public static void main(String [] args) { System.out.println(“Hello World”); } }

8 8 Compiling and Running HelloWorld.java javac HelloWorld.java java HelloWorld HelloWorld.class compile run bytecode source code

9 9 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.

10 10 Primitive Data Types Java Data TypeDescription Booleantrue or false charA character int (4 bytes) Integer short (2 bytes) Short integer long (8 bytes) Long integer float (4 bytes) Single precision floating point Double (8 bytes) byte (1 byte) Double precision floating point limited range (Integer)

11 11 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

12 12 Special Cases float number = 23.5; float number = 23.5f; float number = 23.5F; long bigNumber = 9223372036854775807; long bigNumber = 9223372036854775807L; long bigNumber = 9223372036854775807l; ERROR CORRECT ERROR CORRECT

13 13 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

14 14 The char Data Type used to store characters character literals are enclosed in single quotes Example: char letter; letter = 'A'; System.out.println(letter); letter = ‘b'; System.out.println(letter); AbAb

15 15 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

16 16 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); ABAB

17 17 Arithmetic operators Operator MeaningTypeExample + AdditionBinarytotal = cost+tax; - SubtractionBinarycost = total-tax; * MultiplicationBinaryTax = cost*rate; / DivisionBinarysaleprice=original/2; % ModulusBinaryremainder=value%3;

18 18 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

19 19 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

20 20 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

21 21 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;

22 22 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

23 23 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;

24 24 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); }} }

25 25 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

26 26 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); }

27 27 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

28 28 Thanks !!


Download ppt "1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi."

Similar presentations


Ads by Google