CS 18000 Problem Solving and Object Oriented Programming
CS 18000 Problem Solving and Object Oriented Programming Spring 2019 Section LE 2 Week 2: Lecture 3 January 14. 2019 Slides updated: 10: 30 am January 15, 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: Week 1 The Play&Learn approach Dissecting a simple Java program Class, method, variable, type, etc.
Today Numbers Primitive types: byte, short, int, long, float, double, char The String class and strings JOption. Pane
Sample programs for Play &Learn Please download Java programs from the following site: https: //www. cs. purdue. edu/homes/apm/courses/CS 180 Spr ing 2019/Java. Code/
What elements of Java did we use in Circle. AC? Program Element Package Class What is it? Example A group of classes A template for creating objects java, util Circle. AC Method A sequence of instructions to accomplish a task main() Variable; name Constant; name A memory location whose contents may change A location whose contents will not change during program execution radius Expression A formula to compute something 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 Math. PI*Math. pow (r, 2) 5
What elements of Java did we use in Circle. AC (contd)? Program Element Statement What is it? Example A construct that enables computation and assignment. a=b+c*4 Type Argument Set of values and operations Input to a method; output from a method double main(String [] args) Scanner To get inout from the keyboard using System. in To print a string on a line To format and print a string Scanner Description to help understand the code. CS 18000. Spring 2019. Week 2. L 3 // One line comment 6 /* Long comment*/ println printf Comment 1/14/2019 println() printf()
Numbers in Java 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 7
Decimal and binary All numbers are represented in memory as 0’s and 1’s. In our day-to-day activities, we use the decimal number system; computers use the binary number system. The base (? ? ) of decimal number system is 10. The base (? ? ) of binary number system is 2. 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 8
Decimal and binary: expansion Decimal (base 10): (127)10=1*102 + 2*101 +7*100 Binary (base 2) (1101)2=1*23 + 1*22 +0*21 + 1*20 =8+4+0+1=(13)10 Most significant bit Least significant bit 3 2 1 0 Position 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 9
Conversion: Decimal to binary Convert 19 to binary. Divide by 2 until quotient is 0. Number (n) (n/2) Quotient (n/2) Remainder 19 9 1 9 4 1 4 2 0 2 1 0 0 0 1 Least significant bit Most significant bit (19)10 =(10011)2=1*24 +0*23 +0*22 +1*21 +1*20 =16+2+1=19 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 10
Representation of binary integers in memory Integers are represented using two’s complement. Consider a byte (8 -bits) in memory Here is how (19)10 will be represented: 0 0 0 1 1 Sign bit: 0 for positive, 1 for negative 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 11
Representation of negative integers in memory How will (-19)10 be represented as a byte in memory? Find the binary equivalent of (19)10: 000010011 Flip the bits: 111101100 Add 1: 111101100+1= 111101101 1 0 1 Sign bit: 0 for positive, 1 for negative 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 12
Range of integers Number of bits=N Max: 2 N-1 -1 Min: -2 N-1 For N=8 Max: 27 -1=127 Min: -2 N-1 =-128 What happens when you add 1 to 127? 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 13
Circle of integers Number of bits=4. Max: 2(4 -1)-1= 7 Min: -2(4 -1) =-8 0 7 6 -1 -8 -7 -2 5 1 -6 -3 -4 -5 2 3 4 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 14
Floating point numbers Numbers that may have a fraction 2. 35, -1. 99, 3. , 2. 0 E 15 Some cannot be represented exactly 0. 3, 0. 1, 7. 33 Scientific notation 0. 23 e 12, 1. 2498 e-14 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 15
Inaccuracy [Try this program] /* This program demonstrates imprecise representation of floating point numbers. Aditya Mathur. January 8, 2019. */ public class Inaccuracy { public static void main(String [] args) { double x = 0. 0; System. out. println(x); x = x + 0. 1; System. out. println(x); x=x+0. 1; System. out. println(x); } } 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 16
Operations on numbers Operators: {+, -, *, /, %, ++, --} int i=0; System. out. println(++i) // Increment; int i=0; System. out. println(--i); // Decrement System. out. println(2+3*4); // Multiplication: higher priority than + or System. out. println((2+3)*4) System. out. println(2+12/3); // Division higher priority than + or System. out. println((2+12)/3); // Division has higher priority than + or 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 17
Types, names, variables, constants 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 18
Types Set of values Set of Operations x a x x c x x 1/14/2019 b CS 18000. Spring 2019. Week 2. L 3 19
Primitive types: byte, short, int, long Set of integers Set of Operations 2010 12 + / * % -14 180 /: Division %: Modulo 1751 Integer. MAX_VALUE: 231 -1 Integer. MIN_VALUE: -231 Long. MAX_VALUE: 263 -1 Long. MIN_VALUE: -263 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 20
Primitive types: float, double Set of real numbers Set of Operations (sample) 2010. 98135 Infinity 12. 77 3. 14 -Infinity. 2010 E 4 180. 0 Na. N + - == * >= > -1751. 0 Float. MAX_VALUE: 3. 40282347 e+38 Float. MIN_VALUE: 1. 40239846 e-45 Double. MAX_VALUE: 1. 79769313486231570 e+308 Double. MIN_VALUE: 4. 94065645841246544 e-324 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 21
Primitive types: boolean Set of logical values true false Set of Operations (sample) == || && | != &&: ||: ^: &: |: 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 Logical AND Logical OR Exclusive OR And but not short circuit OR but not short circuit 22
Primitive types: char Set of characters (sample values shown) Set of Operations (sample) ‘a’ ‘&’ ‘+’ ‘$’ Write a Java Program to discover what Basic operations are allowed on variables of type char. Is the following correct? char b= ‘a’+1; 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 23
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 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 24
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 25
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 26
Variables A variable is something whose value may change during program execution. Example: int num. Students; denotes the number of students whose grads 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 27
[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 28
Assignments A statement that assigns a value a variable. x=a+b*c-d/e; y=avagadro. Num*29/Math. E; z=(x-y%2)/(a+b)*(a+b); i++ 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 29
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 30
End of Slides for Lecture 3 Week 2: January 14 -18, 2019 1/14/2019 CS 18000. Spring 2019. Week 2. L 3 31
- Slides: 31