• No results found

Compsci 201, Collections, Hashing, Objects

N/A
N/A
Protected

Academic year: 2022

Share "Compsci 201, Collections, Hashing, Objects"

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

Compsci 201,

Collections, Hashing, Objects

Owen Astrachan [email protected]

http://bit.ly/201spring19 January 30, 2019

(2)

F is for …

Folder

• aka Directory – where things are stored in Git

Function

• Abstraction – a method in Java

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 2

(3)

PFLWoJ

• Mundane Java-isms

• From char to autoboxing: primitives

What is this?

• Generic classes: ArrayList to HashSet

• From Arrays to HashSet to Collections to …

• From Object.equals to Object.hashCode

• Everything is an Object, what can an object do?

(4)

NBody Review

• Class CelestialBody represents Celestial Body

• Why do we use myXPos and my…

• Review: getters and mutators

• Floating point issues, problems, quandaries

• When is (a + b) + c != a + (b + c)

• When is a/b * c != a*c / b

Watch for this in Gradescope tests!!

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 4

(5)

The Object Concept

Every instance variable and every non-static method accessed/called after Object.Dot

• b.getX(), b.calcForcExertedBy(other)

From within a class, e.g., CelestialBody

• myXPos, getX(),this.myXPos,

All are equivalent as is this.getX()

Some prefer always using this. – clearer?

(6)

Debugging Arithmetic

• Order of operations with floating point values can result in overflow, underflow, more

• Small number + Big number …

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 6

(7)

Debugging double Arithmetic

• Integer values are not the same as Double values

• 1/0 is … whereas 1.0/0 is …

(8)

WOTO

http://bit.ly/201spring19-jan25-2

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 8

(9)

Donald Knuth

• aka “The Donald”

• Turing award (and others)

• Author of “The Art of

Computer Programming”

Arguably most important book written in Computer Science

First publication: Mad Magazine If you optimize everything you will always be unhappy.

Everyday life is like programming, I guess. If you love something you can put beauty into it.

https://www.youtube.com/watch?v=cK7yyjXfbc4

(10)

From Array to ArrayList

• Have int[], String[], CelestialBody[]

• Array of any type, but doesn't grow

Can't use .contains with array, can't print

• The java.utils.Arrays class has some help

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 10

(11)

java.util.ArrayList

• Growable array with many useful methods

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html

• Can only contain Object types (no primitives)

• Convert from array?

• Arrays.asList

It's a List!

• String yes, int no

(12)

From Array to ArrayList

• Can make conversion with Object, e.g., String

• Use Arrays.asList as a bridge, be careful

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 12

(13)

Primitive Array? do it yourself

• No bridge from Arrays.asList since primitive

• Loop and use autoboxing/unboxing

Conversion of int to Integer and vice versa

(14)

-- real world APT?

https://leetcode.com/problems/unique-morse-code-words/

• "a" > ".-", "b" > "-..."

• "z" > "--.."

• Note "gin" > "--...-." and "zen" > "--...-."

• Given an array of strings, how many unique encodings are there?

• Also given String[] of 26 Morse codes, where code[0] = ".-" for "a"

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 14

(15)

From Nothing to Done

• Basic ideas: how do we access encodings in an array where code[1] is for 'b', "-…"

• Arithmetic with char values, 'b' – 'a' == 1

• What about (int) 'b' == 97?

https://www.youtube.com/watch?v=xLpfbcXTeo8

• Loop over characters in a String?

Index k with s.charAt(k)

Or for(char ch : s.toCharArray()){

(16)

WOTO with Live/Leet Code

• Ideas for solving LeetCode problem

• Given array of Strings, return number of unique Morse code encodings

• How is a set useful here? Doable without?

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 16

(17)

Solution

• This was added after class, developed together during class

(18)

ArrayList<…>

• Generic aka parameterized type

• Any Object subtype can be in ArrayList<..>

• Integer, Double, Char, Boolean are wrapper classes for primitives

• Mostly these work. But immutable. Cannot increment an Integer, can create new one

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 18

(19)

DIYAD ArrayList

• Do It Yourself Algorithm and Datastructure

• SimpleStringArrayList: some methods

• GrowableStringArrayList: more methods

• Differences between +10, +100, and *2

Helper methods are private: checkSize()

https://coursework.cs.duke.edu/201spring19/diyad201

(20)

DIYAD Ideas

• Move from String to GrowableString to Generic

• Lots of work to fit in with Collections hierarchy

• For our own work? Easier! All of Java? Harder!

• Differences between +10, +10000, *2 and * 1.2

• How do we measure empirically

• How do we measure analytically

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 20

(21)

Measurement and Analysis

• We measured runtimes empirically

• Same on ola's laptop tomorrow? Next year?

• What about your computer, super computer?

• Mathematical analysis of runtimes

• Machine independent

• Compare algorithms without timing them!

(22)

Analysis via Pictures

• Growing array by adding +1 (or + 100) each time

• Create/copy 1, 2, 3, 4, …, N

• 1+2+ … + N = N(N+1)/2

• Roughly N2

• Square with side N?

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 22

(23)

Analysis via Pictures Again

• Growing array by doubling each time

• Create/copy 1, 2, 4, 8, 16, … 2N

• If X = 2N, we've created 2x2N-1, or 2X-1

• Roughly X, where "roughly" defined later

(24)

WOTO

http://bit.ly/201spring19-jan30-1

1/30//19 Compsci 201, Spring 2019: Collections,

Hashing 24

References

Related documents

Among the strengths of the Tyrolean breast cancer screening program is its implementation: within a short time and with minimal additional resources it was possible to set up

However, it is noted that the results are somewhat different in the central area. As mentioned above, the teaching methodology used in Chemistry I was completely traditional and not

Internal impact affects the distribution of power among the member states and external impact affects power relations between the main decision-making bodies in the EU.. One of the

Table of Contents Table of Contents Introduction Background Capabilities Capital Acquisition Loan Issuing Loan Underwriting Accounting Loan Servicing Loan Management

“ Anita Borg sought to revolutionize the world and the way we think about technology and its impact on our

2/26/2020 CompSci 201, Spring 2020 9.. Linked list with one

void nag_2d_cheb_eval (Integer mfirst, Integer mlast, Integer k, Integer l, const double x[], double xmin, double xmax, double y, double ymin, double ymax, double ff[], const

Chapter 6: Effects of brand information and context on yoghurt product experience Table 6.9 Summary of the results for the comparison of the sensory profiling task with