CompSci 100E 2.1 1 Gambler's Ruin  One approach. Monte Carlo simulation.  Flip digital coins and see what happens. o Pseudorandom number generation ojava.util.Random.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
15-Jun-15 Lists in Java Part of the Collections Framework.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Chapter 18 Java Collections Framework
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
2.2 Libraries and Clients Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CompSci 100e2.1 Today’s Topics l Reading:  Sedgewick & Wayne: , l Topics  APTs  Basic Collections: ArrayLists: the expandable array.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Written by: Dr. JJ Shepherd
CompSci Inheritance and Interfaces  Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape,
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
int [] scores = new int [10];
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
Object Oriented Programming in Java Habib Rostami Lecture 7.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 CS162: Introduction to Computer Science II Abstract Data Types.
Using the Java Collection Libraries COMP 103 # T2
Sets Set is an unordered list of items
Implementing ArrayList Part 1
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists 22-Feb-19.
Collections Framework
Introduction to Computer Science I.
Java Basics – Arrays Should be a very familiar idea
What can you put into an ArrayList?
Presentation transcript:

CompSci 100E Gambler's Ruin  One approach. Monte Carlo simulation.  Flip digital coins and see what happens. o Pseudorandom number generation ojava.util.Random  Repeat and compute statistics. © Sedgewick & Wayne

CompSci 100E 2.2 Tips for Excelling in CompSci 100e  Read the Book  Ask questions  Keep working until it is correct  Seek help when stuck  Visit the professor, TA, and UTAs  Start early!  Get the easy points

CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists use mathematical functions to calculate formulas.  Programmers use functions to build modular programs.  You use functions for both.  Examples.  Built-in functions: Math.random(), Math.abs(), Integer.parseInt().  Our I/O libraries StdDraw.show(), StdAudio.play().  User-defined functions: main().

CompSci 100E Anatomy of a Java Function  Java functions. Easy to write your own. f(x) =  x input … output

CompSci 100E Flow of Control  Flow of control. Functions provide a new way to control the flow of execution of a program. "pass-by-value"

CompSci 100E Libraries  Library. A module whose methods are primarily intended for use by many other programs.  Client. Program that calls a library.  API. Contract between client and implementation.  Implementation. Program that implements the methods in an API.

CompSci 100E Modular Programming  Modular programming.  Divide program into self-contained pieces.  Test each piece individually.  Combine pieces to make program.  Ex. Flip N coins. How many heads?  Read arguments from user.  Flip one fair coin.  Flip N fair coins and count number of heads.  Repeat simulation, counting number of times each outcome occurs.  Plot histogram of empirical results.  Compare with theoretical predictions.

CompSci 100E 2.8 Flashback: Data processing  Scan a large (~ 10 7 bytes) file  Print the words together with counts of how often they occur  Need more specification?  How do you do it?  What is we only wanted the top k (say 20) words?

CompSci 100E 2.9 What can you put into an ArrayList?  Any Object  Use a wrapper class (see java.lang.*)  int, double, char, boolean, …  Integer, Double, Character, Boolean,  Can have your cake and eat it too ArrayList list = new ArrayList (); for (int k = 0; k < 10; k++){ list.add(k*k); } for (Integer jj : list){ System.out.println(jj); }  All made practical by Version 5 of Java

CompSci 100E 2.10 Exploring ArrayLists  Look at the Java 6 API  Note interfaces implemented  Serializable, Cloneable, Iterable  Collection, List, RandomAccess  Note other descriptive text  Regarding performance  Constructors  Methods  Don’t forget methods in parent classes

CompSci 100E 2.11 Exploring ArrayLists  Some Commonly Used Methods  boolean add(E o) // append  void add(int index, E element) // insert  void clear()  boolean contains(Object elem)  E get(int index)  int indexOf(Object elem)  boolean remove(Object o)  E remove(int index)  E set(int index, E elem) // replace  int size()

CompSci 100E 2.12 Exploring ArrayLists  Performance  Constant Time  size, isEmpty, get, set, iterator, listIterator operations  add (amortized)  Linear Time  All of the other operations run in linear time  What does all of this mean?  Why do we care?  Exercise: Implement on an array the equivalent of  void add(int index, E element)  E remove(int index)  Remember: Memory is an array (well sort of)

CompSci 100E 2.13 Amortization: Expanding ArrayLists  Expand capacity of list when add() called  Calling add N times, doubling capacity as needed  What if we grow size by one each time? Item #Resizing costCumulative cost Resizing Cost per item Capacity After add m m+1 2 m+1 2 m+2 -2around 22 m+1

CompSci 100E 2.14 What is a char?  Differences between unicode and ASCII  Why is unicode used? Why should we care? What should we know? How many of the details are important?  A char value can be treated like an int value  Add integer to it, cast back to char  Subtract character from it, get int back counters[s.charAt(k)- ’A’]++;  Anatomy of the statement above??

CompSci 100E 2.15 Inheritance and Interfaces  Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …  Write general programs to understand the abstraction, advantages? void execute(Pixmap target) { // do something }  But a dog is also a quadruped, how can we deal with this?

CompSci 100E 2.16 Single inheritance in Java  A class can extend only one class in Java  All classes extend Object --- it's the root of the inheritance hierarchy tree  Can extend something else (which extends Object), why?  Why do we use inheritance in designing programs/systems?  Facilitate code-reuse (what does that mean?)  Ability to specialize and change behavior o If I could change how method foo() works, bar() is ok  Design methods to call ours, even before we implement o Hollywood principle: don't call us, …

CompSci 100E 2.17 Comparable and Comparator  Both are interfaces, there is no default implementation  Contrast with.equals(), default implementation?  Contrast with.toString(), default?  Where do we define a Comparator?  In its own.java file, nothing wrong with that  Private, used for implementation and not public behavior o Use a nested class, then decide on static or non-static o Non-static is part of an object, access inner fields  How do we use the Comparator?  Sort, Sets, Maps (in the future)  Does hashing (future topic) have similar problems?

CompSci 100E 2.18 Sets  Set is an unordered list of items  Items are unique! Only one copy of each item in set!  We will use two different implementations of sets  TreeSet  A TreeSet is backed up by a tree structure (future topic)  Keeps items sorted (+)  Slower than HashSets ?? (-)  HashSet  A HashSet is backed up by a hashing scheme (future topic)  Items not sorted – should seem to be in random order (-)  Faster than TreeSets ?? (+)

CompSci 100E 2.19 Using Both ArrayList and Sets  You may want to use a set to get rid of duplicates, then put the items in an ArrayList and sort them!  Problem:  Often data comes in the form of an array  How do we go from array to ArrayList or TreeSet?  Problem:  Often we are required to return an array  How do we go from a Collection such as an ArrayList or TreeSet to an array?  Can do it the “hard” way with loops or iterators:  one item at a time  OR:

CompSci 100E 2.20 Story Anyway, I thought you'd be interested to know that in 2 of the 5 technical interviews I had, I recognized problems from CS courses at Duke. Specifically, they asked me to write algorithms for the "intersection of two sets" problem and a variation of the "boggle" problem. I thought that was pretty interesting. … For what it's worth for any of your students interviewing, I prepared for the interview mostly by practicing APT problems from the Duke CS100 course page, and I felt that that prepared me very well for about 80% of the questions that were asked. It certainly helped me get into the mindset of the types of things they ask, especially after a few years of being away from those types of algorithms. - Duke CS ‘07 alum