guest speaker - cryptography
DESCRIPTION
Guest Speaker - Cryptography. Wednesday (Apr. 22) – Prof. abhi shelat. Final Exam Review. Built-In Datatypes. Variables. A variable has three parts: Name (e.g., “ x ”) Type (e.g., int , double , String ) Value How does a variable get a value? Can be initialized in the program - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/1.jpg)
Guest Speaker - Cryptography
Wednesday (Apr. 22) – Prof. abhi shelat
![Page 2: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/2.jpg)
Final Exam Review
![Page 3: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/3.jpg)
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 • http://www.cs.Princeton.EDU/IntroCS
Built-In Datatypes
![Page 4: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/4.jpg)
Variables
A variable has three parts:1. Name (e.g., “x”)2. Type (e.g., int, double, String)3. Value
How does a variable get a value?– Can be initialized in the program
E.g., int x = 10– Can be computed when the program is running
E.g., x = y + z;
![Page 5: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/5.jpg)
5
Built-in Data Types
Data type. A set of values and operations defined on those values.
add, subtract, multiply, divide
3.14156.022e23
floating point numbers
double
add, subtract, multiply, divide
1712345integersint
and, or, nottruefalsetruth valuesboolean
sequences of characters
characters
set of values operationsliteral valuestype
compare'A''@'
char
String concatenate"Hello World""CS is fun"
![Page 6: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/6.jpg)
6
Math Library
![Page 7: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/7.jpg)
7
Type Conversion
Type conversion. Convert from one type of data to another. Automatic: no loss of precision; or with strings. Explicit: cast; or method.
![Page 8: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/8.jpg)
Conditionals and Loops
![Page 9: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/9.jpg)
9
Control Flow Summary
Control flow. Sequence of statements that are actually executed in a
program. Conditionals and loops: enables us to choreograph the control
flow.
Straight-lineprograms
All statements areexecuted in the order given.
ConditionalsCertain statements are
executed depending on thevalues of certain variables.
ifif-else
LoopsCertain statements are
executed repeatedly untilcertain conditions are met.
whilefor
do-while
Control Flow Description Examples
![Page 10: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/10.jpg)
Nesting Conditionals and Loops
Nest conditionals within conditionals.
Nest loops within loops. Nest conditionals within loops
within loops.
10
![Page 11: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/11.jpg)
Monte Carlo Simulation
General Code Structure:
class MonteCarloSimulation {
for(i=0; i<num_trials; i++) {
Perform one independent trial {
Generate random input;Deterministic computation on that input;
}
}Compute results aggregated over all trials;
}
11
![Page 12: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/12.jpg)
Single-Dimensional Arrays
![Page 13: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/13.jpg)
13
Arrays in Java
Java has special language support for arrays. To make an array: declare, create, and initialize it. To access element i of array named a, use a[i]. Array indices start at 0.
Compact alternative. Declare, create, and initialize in one statement. Default initialization: all numbers automatically set to zero.
int N = 10; double[] a; // declare the arraya = new double[N]; // create the arrayfor (int i = 0; i < N; i++) // initialize the array a[i] = 0.0; // all to 0.0
int N = 10;double[] a = new double[N]; // declare, create, init
![Page 14: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/14.jpg)
Array Layout in Memory
Memory is split into equal sized units known as “addresses”. (Address
Space)
Arrays occupy consecutive addresses in this linear space:
int[] a = {5, 6, 10};
Key Point: Name of the array indirectly references its starting address.
int[] b = new int[3]; b=a;
//b is an alias to a. REFERENCE TYPE
14
0 1 2 . . . N-2 N-1
0 1 2 3 4 5 N-2 N-1 a[0] a[1] a[2] 5 6 10
Addresses
![Page 15: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/15.jpg)
Array Length
The array length can be obtained using the .length operator
for(i=0; i< myArray.length; i++)
The array length cannot be changed after memory allocation
Can use arraylists to get around this limitation
15
![Page 16: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/16.jpg)
Auto-increment
Auto-increment and auto-decrement operators used with arrays. ++i means “add one to i’s value and use the updated value” i++ means “use i’s value now and then add one to the old
value” Can use on line by itself as a single statement
– In which case they do exactly the same thing! Differences between the auto-increment operators are
important when used in expressions: y = 0; x = y++; y = 0; z = ++y;
16
Y=0; x = y; y = y+1;Y=0; y = y+1; z = y;
![Page 17: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/17.jpg)
Multidimensional Arrays
![Page 18: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/18.jpg)
18
Two Dimensional Arrays in Java
Array access. Use a[i][j] to access element in row i and column j.
Zero-based indexing. Row and column indices start at 0.
double[][] a = new double[10][3];for (int i = 0; i < 10; i++) { for (int j = 0; j < 3; j++) { a[i][j] = 0.0; }}
Declaring and Initializing a 10-by-3 Array
![Page 19: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/19.jpg)
Memory Layout 2-D Arrays
19
Row
array[0]
array[1]
array[2]
array[3]
array[0][0]
array[1][2]
![Page 20: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/20.jpg)
Ragged Arrays
Row lengths can be non-uniform Can use .length
int[][] a = ...;
for (int rows=0; rows < a.length; rows++) {for (int cols=0; cols < a[rows].length; cols++)
System.out.print(" " + a[rows][cols]);System.out.println("");
}
20
Number of rows
Number of columns for a given row
![Page 21: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/21.jpg)
Functions/Static Methods
![Page 22: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/22.jpg)
Components of a Function
![Page 23: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/23.jpg)
23
Flow Control – Call by Value
Call by reference – For reference types (arrays, objects)
![Page 24: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/24.jpg)
Things to Remember About Functions
Functions can be overloaded Different argument types Different number of arguments Different return value is NOT overloading
Scoping Rules for functions and conditional and loop code-blocks
![Page 25: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/25.jpg)
Recursion
![Page 26: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/26.jpg)
26
Recursion
What is recursion? When one function calls itself directly or indirectly.
Gcd. Find largest integer d that evenly divides into p and q.
base casereduction step
public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q);}
base case
reduction step,converges to base case
![Page 27: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/27.jpg)
27
How-To’s on Writing Recursive Functions
Base Case: You must check if we’ve reached the
base case before doing another level of recursion!
Make Progress Towards Base Case: Your recursive calls must be on a smaller
or simpler input. Eventually this must reach the base
case (and not miss it).
Multiple recursive calls: Sometimes more than one recursive call.
– H-Tree, Towers of Hanoi Are their return values chosen,
combined?
![Page 28: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/28.jpg)
Objects
![Page 29: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/29.jpg)
29
Objects
Object. An entity that can take on a data type value.
An object’s “value” can be returned to a client or be changed by one of the data type’s operations.
length, substring, comparesequence of charactersString
OperationsSet of ValuesData Type
get red component, brighten24 bitsColor
Picture get/set color of pixel (i, j)2D array of colors
![Page 30: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/30.jpg)
Things to Remember About Objects
Class API Constructor Instance Methods
Difference between constructors and methods
![Page 31: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/31.jpg)
Assigning one object/array to another object/array results in an alias
Cannot use == operator to check the equality of objects
Use instance method equals(). Can define this method for any class.
Mutable vs. Immutable Objects Immutable – Can’t change the value of an
object after its created Aliases to immutable objects are less
problematic
31
Objects are Reference Types
![Page 32: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/32.jpg)
Principles of Object Oriented Programming
Encapsulation: Combine data and the functions that operate on that data into a single unit (object)
Data Hiding: Clients should not be able to manipulate data in objects directly
Declare instance variables to be “private”– Use getter methods to read data within objects
Any object should be able to invoke the instance methods– Declare instance methods to be “public”
![Page 33: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/33.jpg)
33
Summary of Classes
(“Charge” Class Discussed in the Textbook)
![Page 34: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/34.jpg)
Data Structures
![Page 35: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/35.jpg)
35
Data Structures
Lists A collection of a variable number of items
Typical Operations on Lists Add an item to the list Remove an item from the list Read an item from the list Check whether the list is empty Get the current size of the list
All of these are provided via the Java ArrayList class
![Page 36: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/36.jpg)
36
Generics. Parameterize the datatype used in the data structure.
You need to import the library: import java.util.ArrayList;
36
Generics
ArrayList<Apple> list= new ArrayList<Apple>();Apple a = new Apple();Orange b = new Orange();list.add(a);list.add(b); // compile-time errora = list.get(0); // returns an Apple object
sample client
![Page 37: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/37.jpg)
3737
Autoboxing
Generic ArrayList implementation. Only permits reference types.
Wrapper type. Each primitive type has a wrapper reference type. Ex: Integer is wrapper type for int.
Autoboxing. Automatic cast from primitive type to wrapper type.Autounboxing. Automatic cast from wrapper type to primitive type.
ArrayList<Integer> list= new ArrayList<Integer>();list.add(17); // autobox (int -> Integer)int a = list.get(i); // autounbox (Integer -> int)
![Page 38: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/38.jpg)
38
Lists Can Be Organized in Different Ways
Linked List Linear sequence of elements
Queue Remove the item least recently added. First-In, First-Out (FIFO)
Stack Remove the item most recently added. Last-In, First-Out (LIFO)
Array Implementation vs. ArrayList Implementation
![Page 39: Guest Speaker - Cryptography](https://reader035.vdocument.in/reader035/viewer/2022081504/5681500e550346895dbdef55/html5/thumbnails/39.jpg)
Guest Speaker - Cryptography
Wednesday (Apr. 22) – Prof. abhi shelat