Guest Speaker - Cryptography Wednesday (Apr. 22) – Prof. abhi shelat.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Programming with Collections Collections in Java Using Arrays Week 9.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Review Java.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
March 2005Java Programming1. March 2005Java Programming2 Why Java? Platform independence Object Oriented design Run-time checks (fewer bugs) Exception.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
A First Book of ANSI C Fourth Edition
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
ArrayList, Multidimensional Arrays
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
From C++ to Java A whirlwind tour of Java for C++ programmers.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
Introduction to Java Java Translation Program Structure
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Understanding Data Types and Collections Lesson 2.
Java Programming Java Basics. Data Types Java has two main categories of data types: –Primitive data types Built in data types Many very similar to C++
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CS-I MidTerm Review Hao Jiang Computer Science Department Boston College.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
Heads-Up – Programming Quiz This Week Announcement will be made via You will be provided with a login to the password-protected website You will.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming Lecture 2: BallWorld.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Introduction to Computer Science / Procedural – 67130
Methods Chapter 6.
Type Conversion, Constants, and the String Object
Data Structures 1 1.
HW-6 Deadline Extended to April 27th
Advanced Programming Behnam Hatami Fall 2017.
Exam 2 Review 1.
Introduction to Data Structure
Arrays.
Presentation transcript:

Guest Speaker - Cryptography Wednesday (Apr. 22) – Prof. abhi shelat

Final Exam Review

CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Built-In Datatypes

Variables n A variable has three parts: 1. Name (e.g., “x”) 2. Type (e.g., int, double, String) 3. Value n 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;

5 Built-in Data Types Data type. A set of values and operations defined on those values. add, subtract, multiply, divide e23 floating point numbers double add, subtract, multiply, divide integers int and, or, not true false truth values boolean sequences of characters characters set of valuesoperationsliteral valuestype compare 'A' char String concatenate "Hello World" "CS is fun"

6 Math Library

7 Type Conversion Type conversion. Convert from one type of data to another. n Automatic: no loss of precision; or with strings. n Explicit: cast; or method.

Conditionals and Loops

9 Control Flow Summary Control flow. n Sequence of statements that are actually executed in a program. n Conditionals and loops: enables us to choreograph the control flow. Straight-line programs All statements are executed in the order given. Conditionals Certain statements are executed depending on the values of certain variables. if if-else Loops Certain statements are executed repeatedly until certain conditions are met. while for do-while Control FlowDescriptionExamples

Nesting Conditionals and Loops Nest conditionals within conditionals. Nest loops within loops. Nest conditionals within loops within loops. 10

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

Single-Dimensional Arrays

13 Arrays in Java Java has special language support for arrays. n 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. n Declare, create, and initialize in one statement. n Default initialization: all numbers automatically set to zero. int N = 10; double[] a; // declare the array a = new double[N]; // create the array for (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

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 N-2 N N-2 N-1 a[0] a[1] a[2] Addresses

Array Length The array length can be obtained using the.length operator n for(i=0; i< myArray.length; i++) The array length cannot be changed after memory allocation n Can use arraylists to get around this limitation 15

Auto-increment Auto-increment and auto-decrement operators used with arrays. n ++i means “add one to i’s value and use the updated value” n i++ means “use i’s value now and then add one to the old value” n Can use on line by itself as a single statement – In which case they do exactly the same thing! n 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;

Multidimensional Arrays

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

Memory Layout 2-D Arrays 19 Row array[0] array[1] array[2] array[3] array[0][0] array[1][2]

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

Functions/Static Methods

Components of a Function

23 Flow Control – Call by Value Call by reference – For reference types (arrays, objects)

Things to Remember About Functions Functions can be overloaded n Different argument types n Different number of arguments n Different return value is NOT overloading Scoping Rules for functions and conditional and loop code-blocks

Recursion

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 case reduction 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

27 How-To’s on Writing Recursive Functions Base Case: n You must check if we’ve reached the base case before doing another level of recursion! Make Progress Towards Base Case: n Your recursive calls must be on a smaller or simpler input. n Eventually this must reach the base case (and not miss it). Multiple recursive calls: n Sometimes more than one recursive call. – H-Tree, Towers of Hanoi n Are their return values chosen, combined?

Objects

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 characters String OperationsSet of ValuesData Type get red component, brighten24 bits Color Picture get/set color of pixel (i, j)2D array of colors

Things to Remember About Objects Class API n Constructor n Instance Methods Difference between constructors and methods

Assigning one object/array to another object/array results in an alias n Cannot use == operator to check the equality of objects n Use instance method equals(). Can define this method for any class. Mutable vs. Immutable Objects n Immutable – Can’t change the value of an object after its created n Aliases to immutable objects are less problematic 31 Objects are Reference Types

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 n Declare instance variables to be “private” – Use getter methods to read data within objects n Any object should be able to invoke the instance methods – Declare instance methods to be “public”

33 Summary of Classes (“Charge” Class Discussed in the Textbook)

Data Structures

35 Data Structures Lists n A collection of a variable number of items Typical Operations on Lists n Add an item to the list n Remove an item from the list n Read an item from the list n Check whether the list is empty n Get the current size of the list All of these are provided via the Java ArrayList class

36 Generics. Parameterize the datatype used in the data structure. You need to import the library: import java.util.ArrayList; 36 Generics ArrayList list= new ArrayList (); Apple a = new Apple(); Orange b = new Orange(); list.add(a); list.add(b); // compile-time error a = list.get(0); // returns an Apple object sample client

37 Autoboxing Generic ArrayList implementation. Only permits reference types. Wrapper type. n Each primitive type has a wrapper reference type. n 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 list= new ArrayList (); list.add(17); // autobox (int -> Integer) int a = list.get(i); // autounbox (Integer -> int)

38 Lists Can Be Organized in Different Ways Linked List n Linear sequence of elements Queue n Remove the item least recently added. n First-In, First-Out (FIFO) Stack n Remove the item most recently added. n Last-In, First-Out (LIFO) Array Implementation vs. ArrayList Implementation

Guest Speaker - Cryptography Wednesday (Apr. 22) – Prof. abhi shelat