Com ompsci psci 201 201 Arrays, s, ArrayList yList, , APIs Is - - PowerPoint PPT Presentation

com ompsci psci 201 201 arrays s arraylist ylist apis is
SMART_READER_LITE
LIVE PREVIEW

Com ompsci psci 201 201 Arrays, s, ArrayList yList, , APIs Is - - PowerPoint PPT Presentation

Com ompsci psci 201 201 Arrays, s, ArrayList yList, , APIs Is Susan Rodger January 22, 2020 1/22/2020 Compsci 201, Spring 2020 1 Today is the last day of drop/add It is almost too late You cannot t switch your r section tion


slide-1
SLIDE 1

Com

  • mpsci

psci 201 201 Arrays, s, ArrayList yList, , APIs Is

1/22/2020 Compsci 201, Spring 2020 1

Susan Rodger January 22, 2020

slide-2
SLIDE 2

Today is the last day of drop/add It is almost too late…

  • You cannot

t switch your r section tion withou

  • ut

t a perm rmission ission numbe mber. .

  • DO

DO NOT drop rop your section tion until til you can get et a perm rm numbe mber r for the e ot

  • ther

er section tion

  • Go

Go to to https ps://w //www.cs.d s.duke.ed .edu/under /undergra rad/ d/registr egistrati tion

  • n
  • Scroll
  • ll down

n belo low offic ice e hour

  • urs

s til til you u see Permis missi sion

  • n numbe

mber for Sprin ing 2020 course rses

  • Then

en fill out that at form

  • You must

t use e permi missi sion

  • n numbe

mber to toda day! y!

  • We will

ll not

  • t be hand

nding ng them em out t afte ter r 5pm

1/22/2020 Compsci 201, Spring 2020 2

slide-3
SLIDE 3

D is for …

  • Debugging
  • A key skill in making your programs correc

ect, your program will run without this skill

  • Digital
  • All about the 1s and 0s

1/22/2020 Compsci 201, Spring 2020 3

slide-4
SLIDE 4

Plan for Today

  • What

at work is here, what at work is coming ing

  • P0, P1, APTs
  • Unders

rstanding tanding arra rays s and ArrayList List: : Trad adeoff eoffs

  • APTS always use arrays, convert to ArrayList?
  • Reaso

soning ning empiric irically and analytic ticall ally

  • Working well enough and working at scale

1/22/2020 Compsci 201, Spring 2020 4

slide-5
SLIDE 5

Announcements

  • Assig

ignment nment P0 due due las ast t week

  • No late penalty since first assignment til Jan 24
  • APT-1

1 now du due e Thur urs, s, January nuary 23 23

  • Can still turn in Friday til 11:59pm
  • Discussion

cussion 3 o

  • n January

nuary 27

  • Prediscussion, do before, out by Friday
  • APT-2 out to

today, , du due January ry 28 (shor

  • rt

t 1 due due)

  • Assignme

ignment t P1 o

  • ut

t Frida iday, , du due e Thursd rsday, , Jan 30

  • Readin

ding on cal alen endar ar

  • Slowing down …..
  • try to read by date posted, ok if you need a few

more days

1/22/2020 Compsci 201, Spring 2020 5

slide-6
SLIDE 6

Visualizations Help Understanding?

  • Javatut

utor to to visua uali lize e code: : http tp:// ://pytho thont ntut utor

  • r.com/j

com/java. a.html html

  • Using the java.awt.Color class
  • Both String and Color are immuta

utable ble

  • Once created, cannot ever change

1/22/2020 Compsci 201, Spring 2020 6

slide-7
SLIDE 7

Arrays and APIs

  • Acces

cessing ing a list st of sand ndwic ich h ingred edients ients

https:/ ://w /www2. 2.cs. s.duk uke.e .edu du/cse /csed/ d/newapt/s t/sand ndwich ichbar.h .html tml

  • Ingredients specified as string "lettuce tomato"
  • String split method

String[] d = "lettuce tomato".split(" ")

1/22/2020 Compsci 201, Spring 2020 7

slide-8
SLIDE 8

APTs: parsing/processing data

  • String <-> String[]: parameter and return types

String s = "apple pear lemon orange"; String [] ar = s.split(" “); // ar is ["apple", "pear", "lemon", "orange"]

  • Bridge

ge fro rom arra ray to to string? ring?

String[] a = {"one", "two", "three"} String b = String.join(":",a); // b is "one:two:three"

1/22/2020 Compsci 201, Spring 2020 9

slide-9
SLIDE 9

Helper methods Creating your own API

  • Sand

ndwic ichBar hBar APT writeup up

https://www2.cs.duke.edu/csed/newapt/sandwichbar.html

  • Clue/hint that indexing loop appropriate
  • Helper

er functi ction: array of strings contains all

  • strings. Be creative in wanting an API for that

1/22/2020 Compsci 201, Spring 2020 11

slide-10
SLIDE 10

WOTO SandwichBar

  • How to

to te test st locally? ally?

  • Write main method. Create new object …
  • Faster to debug locally, even use debugger?

1/22/2020 Compsci 201, Spring 2020 12

slide-11
SLIDE 11

Helper methods Creating your own API

  • Sand

ndwic ichBar hBar APT writeup up

  • Helper

er funct ctio ion: array of strings contains all

  • strings. Be creative in wanting an API for that

1/22/2020 Compsci 201, Spring 2020 13

slide-12
SLIDE 12

Scoring WOTOs going forward

  • Each

h regular gular WOTO O fo form is worth h 2 pts

  • If 2 WOTOs

Os in a lect ctur ure, , each ch worth th 2 pt pts

  • Some

e WOTO O fo forms ms correc ectne tness s will count nt and each ch pro roblem lem is worth th pts s (likel ely 1 pt pt each) ch)

  • We are dro

ropping ping all WOTOs Os done befor efore e to today

  • SUBMIT

MIT ALL WOTO O FORM RMS – they count! nt!

  • Work Together

her with h ot

  • thers

ers

1/22/2020 Compsci 201, Spring 2020 16

slide-13
SLIDE 13

WOTO (4 minutes)

http:/ ://bit.l bit.ly/ y/201 201sp sprin ring20 g20-01 0122 22-1

1/22/2020 Compsci 201, Spring 2020 17

slide-14
SLIDE 14

Nancy Leveson: Software Safety

  • (@MIT) Mathematical and engineering

aspects, invented the discipline

  • Air traffic control
  • Microsoft word

“There will always be another software bug; never trust human life solely on software” huffington post?

  • Therac 25: Radiation machine
  • http://en.wikipedia.org/wiki/Therac-

25

  • Paper on this: http://bit.ly/5qOjoH
  • Software and steam engines
slide-15
SLIDE 15

Source Code, Byte Code

  • High level

el languages es compil mpiled d to to low level el lan anguages es

  • In C/C++ low-level specific

ic to to platf tfor

  • rm
  • In Java byte code is low-level
  • Exec

ecute e by machin ine: : real al or virtual al

  • JVMs must

t be ported ed to to plat atform

  • rm
  • Andr

droid

  • id doesn

sn't use e JVM

  • Dalvik and now ARM
  • Be grat

ateful!! l!!!! !!!

  • High level source code

1/22/2020 Compsci 201, Spring 2020 19

slide-16
SLIDE 16

From Hello.java to Hello.class

1/22/2020 Compsci 201, Spring 2020 20

public class Hello { public static void main(String[] args){ System.out.println("hello world"); } } Code: 0: aload_0 1: invokespecial #1 4: return Code: 0: getstatic #2 3: ldc #3 5: invokevirtual #4 8: return

slide-17
SLIDE 17

Project P1 out soon

  • See

e course rse websit ite fo for det etails ails, discus cussio ion n D3

  • Not quite this, but …

https://www.youtube.com/watch?v=DoLe1c-eokI

  • Read as

assignment signment befo efore e star arting ting to to code

  • Think before fingers on keys
  • Use Piazza

za and Helper er Hours

1/22/2020 Compsci 201, Spring 2020 21

slide-18
SLIDE 18

Projects in 201: Review

  • Start with

th GitLab Lab pro rojec ect, t, fork k reposit

  • sitory
  • ry
  • Starter code and cloud-based storage for projects
  • Clone repo to your machine: you need Git for this
  • You use Duke

e Compsc sci GitLab ab websit site

  • You manage your code/projects with GitLab
  • Make

e chan anges es, , comple mplete e pro roject ject

  • Push changes frequently using Git … hourly/daily

1/22/2020 Compsci 201, Spring 2020 22

slide-19
SLIDE 19

What is Git?

  • Git is a free and open source distributed version

control system designed to handle everything from small to very large projects with speed and

  • efficiency. https://git-scm.com/
  • In teams, huge win. Individually, huge win
  • Git is complicated when it doesn’t work, lots of

commands

  • Git relies on SSH to be secure

1/22/2020 Compsci 201, Spring 2020 23

slide-20
SLIDE 20

Assignments P0 and P1

  • Simple

le Java a class ass with driver er pro rogra rams ms

  • How is a program run? What is main?
  • Testing methods and testing classes
  • Examp

mple le of 201 work-flow

  • GitLab for starter code and your code
  • Using SSH and GitLab together
  • Submitting via Gradescope, Analysis

1/22/2020 Compsci 201, Spring 2020 24

slide-21
SLIDE 21

What is a static method?

  • Objects

ects are inst stance ances s of a class ss

  • Thus objects have instance variables
  • Typically private, accessed in methods
  • Static

tic meth ethod d belongs ngs to to class ss, not

  • t obje

bject ct

  • No instance variables
  • Accesses static variables and methods
  • More on this later, for now? …

1/22/2020 Compsci 201, Spring 2020 25

slide-22
SLIDE 22

Static Methods

  • The class

ss Math ath has as many ny (java.lan .lang)

  • sqrt, cos, abs, …
  • The class

ss Arrays s has as many ny (java. a.uti util)

  • sort, fill, toString, asList, …
  • Invoke as

as Math.s ath.sqrt(2 (25.0 5.0) ) or String.j ring.join

  • in(…)
  • Math is a class. Convention for name?
  • There aren't different Math objects, none!
  • No state needed, all code in methods

1/22/2020 Compsci 201, Spring 2020 26

slide-23
SLIDE 23

WOTO: Correctness Counts

http://b ://bit.l it.ly/201spr y/201spring20 ing20-0122 0122-2

1/22/2020 Compsci 201, Spring 2020 27