CS 18000 Problem Solving and Object Oriented Programming
CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE 2 Week 2: Lecture 4 January 14. 2019 Slides updated: 12: 06 pm January 16, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA ps: //www. cs. purdue. edu/homes/apm/courses/CS 180_Java/CS 180 Spring 2019/
Review: Lecture 3 Primitive types Numbers Two’s complement representation
Summary of primitive types (numbers) Type Range Sample byte (8 bits) -128: 127 -3, 29, 127 short (16 bits) -32, 768: 32767 -3, 29, 127 int (32 bits) -2, 147, 483, 648 : 2, 147, 483, 647 3, 29, 127 long (64 bits) -263 : -263 -1 3, 29, 127 float (32 bits) 1. 40 e-45 : 3. 40 e+38 -2. 99, 4. 56, 32. 89 E 21 double (64 bits) 4. 90 e-324 : 1. 79 e+308 -2. 99, 4. 56, 32. 89 E 21 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 3
Today Signed magnitude vs. two’s complement representation Naming, constants, variables Expressions Assignments The String class and strings JOption. Pane
Signed-magnitude vs 2’s complement representation Consider 4 -bits to represent numbers. Signed magnitude representation (leftmost bit is the sign bit): 0111=7 1111=-7 2’s complement representation 0111=7 1111=-8 Range of numbers: Signed magnitude: -7 to +7 2’s complement: -8 to +7 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 5
Representing 0 0000=+0 1000=-0 Do we need two zeros? 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 6
Addition Signed magnitude 0111=+7 1111=-7 0111+1111=? 110=? +6 or -6? 1001=-7 0111+1001=0000 2’s complement 0111=+7 The student who asked this question deserves a big hand!! Clap clap! 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 7
Names Used to denote classes, objects, data Contain characters; must start with a letter, or a $ sign or an underscore (_). Examples: height, area 1, Dog, $great, _init_ Length: unlimited, case sensitive. Dog and dog are different names. Convention: All class names begin with an uppercase letter; all other names begin with a lower case letter. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 8
Variables A variable is something whose value may change during program execution. Example: int grades. Processed; denotes the number of students whose grades have been processed. Its value changes as each student’s grade is processed by a grade processing program. Every variable has a name and a type. Example: int hurricane. Category; The name is hurricane. Category and its type is int. Every variable must be declared before it is used. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 9
Constants A constant is something that cannot change during program execution. Examples: Integer constants: 0, 1, -1, +24, 29, 300009998, O 14, 0 x 1 B Floating point constants: 0. 0, -2. 345 e 28, -0. 000976512 Boolean constants: true, false Character constants: ‘ ‘, ‘a’, ‘A’, ‘$’ String constants: “”, “Hi!”, “Alice in Wonderland” 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 10
Declarations int age; Why is “String” capitalized while other types are not? float height, area; String name boolean i. Am. Alive; int x=1, y=0; String first. Name=“Harry”; 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 11
Named Constants A constant can be named and the name used instead of the constant itself. Examples: final float pi=3. 14159; final boolean dogs. Exist=true; 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 12
[Simple] Expressions A mathematical formula to compute something. a+b*c-d/e avagadro. Num*29/Math. E (x-y%2)/(a+b)*(a+b) Expressions are evaluated left to right. Operator priority: * and / have higher priority than + and -. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 13
Simple expressions Expressions are used to compute “something”. float x, y, z; // Declare x, y, z as variables of type float x*y+z; // Arithmetic expression, results in float value x<y; // Boolean expression, results in boolean value String first. Name=“Mary”, last. Name= “Jones”; first. Name+ “ “ +last. Name; // Results in a String 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 14
Assignments A statement that assigns a value to a variable. = denotes the assignment operator; x=a+b*c-d/e; // Compute the value of the expression on the right and assign to x y=avagadro. Num*29/Math. E; z=(x-y%2)/((a+b)*(a+b)); ++i and i++ // Both increment i by 1 but there is a difference; can you figure that out? 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 15
More assignments float p=x*y+z; // p gets the value of x*y+z boolean q=x<y; // q gets the value of x<y String first. Name=“Mary”, last. Name= “Jones”; String name= first. Name+” “+last. Name; 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 16
Fun exercise Try the following: int p=1; int q=1; int r, z; r=++p + ++p; z=q++ + q++; What are the values of r and z? 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 17
Operations 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 18
Operators: Arithmetic, relational, conditional Arithmetic operators + * / % a+b*c-d a/b c%d 1/14/2019 Relational operators == < > <= >= != Boolean/conditional operators || && | & a==b a<=b a!=b a==b||c<d a<=b&&c>d a!=b &&c>d||p+1<q CS 18000. Spring 2019. Week 2. L 3 19
Operators: bitwise Bitwise operators &: bitwise AND |: bitwise OR ^: bitwise exclusive OR ~: bitwise complement Bitwise shift operators <<: bitwise left shift >>: bitwise right shift >>>: unsigned right shift a & b: logical and of a and b a|b: logical OR of a and b a <<3: shift bit pattern of a left by 3 bits a>>2: shift bit pattern of a to the right by 2 bits 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 20
Short circuit operations int a=1, b=2, c=3, d=0; int p, q; boolean x, y; x=a>b && c>3; y=a>b & a>c/d; p=a <<2: shift bit pattern of a left by 3 bits. q=a>>2: shift bit pattern of a to the right by 2 bits What should we get in each case? float z=2. 0; p=z>>2; What should we get in this case? 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 21
Division int x=0; System. out. println(3/x); // Will generate exception int x=0; System. out. println(3. 0/x); // Generates Infinity int x=0; System. out. println(3. 0%x); // Generates Na. N System. out. println(Math. sqrt(-2)); // Generates Na. N: Not a number 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 22
Strings 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 23
Strings: basics A string is any sequence of Unicode characters You may name a string as in the following: String my. Dogs. Name; my. Dogs. Name is an object of type String. It can take any string as its value. For example, “Max”, “Bently”, “Jake” and “Raja” are possible values of my. Dogs. Name. What is the difference between 29 and “ 29”? 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 24
Strings: assignment You may assign a value to a string object. my. Dogs. Name=“Bently”; // Assuming that my. Dogs. Name has been declared String my. Car. Color=“Red”; All string objects must be declared before they are used. Thus, it would be incorrect to assign a value to my. Dogs. Name before it has been declared. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 25
Strings: Other operations You may apply a variety of operations to strings. Examples follow. String commend=“Bently, ”+ “ good girl!; // String catenation String my. Car=“It’s a Porsche”+ “, and I love it!” +”but maintenance is expensive. ” // String catenation String first. Char=commend. char. At(0); // Extract character at position 0 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 26
Strings operations There exist a variety of operations on strings. Statement Operation used String commend=“Bently, ”+ “ good girl!”; Catenation char first. Char=commend. char. At(0); Character extraction using char. At() movie. Name. equals(“Fugitive”) String. value. Of(29) equals() Convert int 29 to String “ 29” During week 3 experiment with comparing two strings using the == operator. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 27
Complex Numbers Not a primitive type But you can define a class and do arithmetic on complex numbers. For an example, visit: Class Complex 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 28
The edit, compile, execute cycle. java file(s) Edit a Java program . class file(s) Compile your program Syntax error No syntax error Execute your program Correct program Run time Error or incorrect output In CS 180 we shall use Intelli. J for editing, compiling and execution. Intelli. J is an Integrated Development Environment also known as an IDE. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 29
Illustration: Problem [Try yourself after the class] Write a Java program that performs the following tasks: Inputs Radius r (meters) and height h (meters) of a circular tank. level L of water in the tank in meters. inflow rate f in m 3/sec. duration d for which water flows into the tank. Computes the level L of water after d minutes. Print, nicely formatted, L at time t=0 and L at time t=d. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 30
Another program (with GUI!) import javax. swing. *; public class Simple. GUI { public static void main(String [] args){ String title="Got it!"; String question="Enter your password"; String social="Social Engineering"; String password=JOption. Pane. show. Input. Dialog(null, question, social, JOption. Pane. QUESTION_MESSAGE); JOption. Pane. show. Message. Dialog(null, password, title, JOption. Pane. INFORMATION_MESSAGE); } } 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 31
Did you learn “problem solving? ” “Problem solving” refers to a set of activities performed in order to solve a given problem. This is a generic term and applies to all disciplines, not only to Computer Science. Sequence of steps for solving a problem as proposed by George Polya in the 1950’s : Understand the problem Devise a plan [Design] Execute the plan [Code, Test etc] Review solution 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 32
Did you learn “what is OO programming? ” OO, or Object Oriented, programming refers to a set of activities that lead to a computer program, written in an object-oriented language, that when executed on a computer will solve a problem. Java is an OO language used in CS 180. Other OO languages include C++, C#, Delphi, Modula, Oberon, Objective C, Simula, Smalltalk, and many more! 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 33
What is Problem solving and OO programming? Problem solving and OO programming refers to a set of activities that allow the mapping of a problem to a computer program, written in an object-oriented language, that when executed on a computer will solve the problem. Understand the problem Design a solution [Algorithm] Implement the algorithm Test, debug, and correct the program 34 1/14/2019 CS 18000. Spring 2019. Week 2. L 3
Week 2: January 14 -18, 2019 Hope you enjoyed this week! 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 35
- Slides: 35