Download presentation
Presentation is loading. Please wait.
Published byMarjatta Tamminen Modified over 5 years ago
1
CS 18000 Problem Solving and Object Oriented Programming Spring 2019
Section LE2 Week 2: Lecture 4 January Slides updated: 12:06pm January 16, 2019 Aditya Mathur Professor, Department of Computer Science Purdue University West Lafayette, IN, USA ps://
2
Review: Lecture 3 Primitive types Numbers
Two’s complement representation
3
Summary of primitive types (numbers)
Range Sample byte (8 bits) -128:127 -3, 29, 127 short (16 bits) -32,768: 32767 int (32 bits) -2,147,483,648 :2,147,483,647 3, 29, 127 long (64 bits) -263 : float (32 bits) 1.40e-45 : 3.40e+38 -2.99, 4.56, 32.89E21 double (64 bits) 4.90e-324 : 1.79e+308 1/14/2019 CS Spring Week 2. L3
4
Today Signed magnitude vs. two’s complement representation
Naming, constants, variables Expressions Assignments The String class and strings JOptionPane
5
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 Spring Week 2. L3
6
Representing 0 0000=+0 1000=-0 Do we need two zeros? 1/14/2019
CS Spring Week 2. L3
7
Addition Signed magnitude 0111=+7 1111=-7 0111+1111=?110=?+6 or -6?
2’s complement 0111=+7 1001=-7 =0000 The student who asked this question deserves a big hand!! Clap clap clap! 1/14/2019 CS Spring Week 2. L3
8
Names Used to denote classes, objects, data
Contain characters; must start with a letter, or a $ sign or an underscore (_). Examples: height, area1, 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 Spring Week 2. L3
9
Variables A variable is something whose value may change during program execution. Example: int gradesProcessed; 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 hurricaneCategory; The name is hurricaneCategory and its type is int. Every variable must be declared before it is used. 1/14/2019 CS Spring Week 2. L3
10
Constants A constant is something that cannot change during program execution. Examples: Integer constants: 0, 1, -1, +24, 29, , O14, 0x1B Floating point constants: 0.0, e28, Boolean constants: true, false Character constants: ‘ ‘, ‘a’, ‘A’, ‘$’ String constants: “”, “ ”, “Hi!”, “Alice in Wonderland” 1/14/2019 CS Spring Week 2. L3
11
Declarations int age; float height, area;
String name boolean iAmAlive; int x=1, y=0; String firstName=“Harry”; Why is “String” capitalized while other types are not? 1/14/2019 CS Spring Week 2. L3
12
Named Constants A constant can be named and the name used instead of the constant itself. Examples: final float pi= ; final boolean dogsExist=true; 1/14/2019 CS Spring Week 2. L3
13
[Simple] Expressions A mathematical formula to compute something.
a+b*c-d/e avagadroNum*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 Spring Week 2. L3
14
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 firstName=“Mary”, lastName= “Jones”; firstName+ “ “ +lastName; // Results in a String 1/14/2019 CS Spring Week 2. L3
15
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=avagadroNum*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 Spring Week 2. L3
16
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 firstName=“Mary”, lastName= “Jones”; String name= firstName+” “+lastName; 1/14/2019 CS Spring Week 2. L3
17
Fun exercise Try the following: int p=1; int q=1; int r, z;
r=++p + ++p + ++p; z=q++ + q++ + q++; What are the values of r and z? 1/14/2019 CS Spring Week 2. L3
18
Operations 1/14/2019 CS Spring Week 2. L3
19
Operators: Arithmetic, relational, conditional
Arithmetic operators + - * / % Relational operators == < > <= >= != Boolean/conditional operators || && | & a+b*c-d a/b c%d a==b a<=b a!=b a==b||c<d a<=b&&c>d a!=b &&c>d||p+1<q 1/14/2019 CS Spring Week 2. L3
20
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 Spring Week 2. L3
21
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 & 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 Spring Week 2. L3
22
Division int x=0; System.out.println(3/x); // Will generate exception
System.out.println(3.0/x); // Generates Infinity int x=0; System.out.println(3.0%x); // Generates NaN System.out.println(Math.sqrt(-2)); // Generates NaN NaN: Not a number 1/14/2019 CS Spring Week 2. L3
23
Strings 1/14/2019 CS Spring Week 2. L3
24
Strings: basics A string is any sequence of Unicode characters
You may name a string as in the following: String myDogsName; myDogsName 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 myDogsName. What is the difference between 29 and “29”? 1/14/2019 CS Spring Week 2. L3
25
Strings: assignment You may assign a value to a string object.
myDogsName=“Bently”; // Assuming that myDogsName has been declared String myCarColor=“Red”; All string objects must be declared before they are used. Thus, it would be incorrect to assign a value to myDogsName before it has been declared. 1/14/2019 CS Spring Week 2. L3
26
Strings: Other operations
You may apply a variety of operations to strings. Examples follow. String commend=“Bently,”+ “ good girl!; // String catenation String myCar=“It’s a Porsche”+ “, and I love it!” +”but maintenance is expensive.” // String catenation String firstChar=commend.charAt(0); // Extract character at position 0 1/14/2019 CS Spring Week 2. L3
27
Strings operations There exist a variety of operations on strings.
Statement Operation used String commend=“Bently,”+ “ good girl!”; Catenation char firstChar=commend.charAt(0); Character extraction using charAt() movieName.equals(“Fugitive”) equals() String.valueOf(29) Convert int 29 to String “29” During week 3 experiment with comparing two strings using the == operator. 1/14/2019 CS Spring Week 2. L3
28
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 Spring Week 2. L3
29
The edit, compile, execute cycle
.java file(s) .class file(s) Edit a Java program Compile your program Execute your Syntax error Run time Error or incorrect output No syntax Correct In CS 180 we shall use IntelliJ for editing, compiling and execution. IntelliJ is an Integrated Development Environment also known as an IDE. 1/14/2019 CS Spring Week 2. L3
30
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 m3/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 Spring Week 2. L3
31
Another program (with GUI!)
import javax.swing.*; public class SimpleGUI { public static void main(String [] args){ String title="Got it!"; String question="Enter your password"; String social="Social Engineering"; String password=JOptionPane.showInputDialog(null, question, social, JOptionPane.QUESTION_MESSAGE); JOptionPane.showMessageDialog(null,password, title, JOptionPane.INFORMATION_MESSAGE); } } 1/14/2019 CS Spring Week 2. L3
32
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 Spring Week 2. L3
33
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 Spring Week 2. L3
34
What is Problem solving and OO programming?
Understand the problem 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. Design a solution [Algorithm] Implement the algorithm Test, debug, and correct the program 1/14/2019 CS Spring Week 2. L3
35
Week 2: January 14-18, 2019 Hope you enjoyed this week!
1/14/2019 CS Spring Week 2. L3
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.