25
Compsci 201, Java and APTs Owen Astrachan [email protected] August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs 1

Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan [email protected] August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Compsci 201, Java and APTs

Owen [email protected] 31, 2018

8/31/18 Compsci 201, Fall 2018, Java and APTs 1

Page 2: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Where can you find …

• Compsci/Internet: http://bit.ly/201fall18

• Sakai: https://sakai.duke.edu/portal/site/201fall18

8/31/18 Compsci 201, Fall 2018, Java and APTs 2

Page 3: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

B is for …

• Bug• What you will always have and need to fix

• Bytecode• Java on the “bare metal”

8/30/17 Compsci 201, Fall 2017, First Day 3

Page 4: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Source Code, Byte Code

• High level languages compiled to low level languages• In C/C++ low-level specific to platform• In Java byte code is low-level

• Execute by machine: real or virtual• JVMs must be ported to platform• Android doesn't use JVM• Dalvik and now ARM

• Be grateful!!!!!• High level source code

Page 5: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Goals/Plan for the Day

• Be ready to work on and solve APTs

• Solve an APT as a class, discussion section

• Understand Assignment P0: Java Basics

• Understand logistics as well: Git, Run, Fix, Push, …

• Java basics: syntax, semantics, object-oriented,…

• Readings will help fill in more details

• Think about what you’re reading and programming

9/1/17 Compsci 201, Fall 2017, Java Basics 5

Page 6: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Solving an APT Together

• Totality (see APT page on course site)

http://www.cs.duke.edu/csed/newapt/totality.html

• Solve by hand: a = {20,30,40,50,60} stype=“odd”

• Use what you know, but implement in Java

• Check ideas using jshell (Java 9 and later)

• Command line is your friend!

8/31/18 Compsci 201, Fall 2018, Java and APTs 6

Page 7: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Think Before You Code

• Solve by hand … Check your understanding of examples … think about solution you’ll write ...

• Then think before fingers on keys

8/31/18 Compsci 201, Fall 2018, Java and APTs 7

Page 8: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Learning a new language

• First you’ll translate what you know into the new language

• You’ll view new language in terms of the language you know

• Eventually you’ll program idiomatically and colloquially!

• What do list, vector, and array have in common?

8/31/18 Compsci 201, Fall 2018, Java and APTs 8

Page 9: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Picture worth 210 words?

8/31/18 Compsci 201, Fall 2018, Java and APTs 9

Page 10: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Java Array Basics

• Constructing and initializing …

• int[] a = new int[100];• int[] b = {1,2,3,4,5};

• Indexing starts at 0, exceptions on bad indexing

• a[5] = 7;• b[5] = 7;

8/31/18 Compsci 201, Fall 2018, Java and APTs 10

Page 11: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Indexing for loops and arrays

• Constructing and initializing …

int[] a = new int[100];for(int k=0; k < a.length; k += 1){

a[k] = 99;}

• Let an API call fill in the array: java.util.Arrays;

• Arrays.fill(a,99);

8/31/18 Compsci 201, Fall 2018, Java and APTs 11

Page 12: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

For each loops and arrays

• For each loop: no index, no changing what’s stored

int[] a = {1,2,3,4,5,6,7,8,9,10};int sum1 = 0; int sum2 = 0;for(int k=0; k < a.length; k += 1){

sum1 += a[k];}for(int v : a){

sum2 += v;}System.out.println(sum1 == sum2);

8/31/18 Compsci 201, Fall 2018, Java and APTs 12

Page 13: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Control Construct Summary

• if (boolean) {…}

• Block executed when guard is true

• {.} not needed for single statement, use anyway

• if (boolean) {…} else {…} • Code in else block when negation true

• while(boolean) {…}• Check boolean guard, execute body, repeat

• Guard checked again after body executed

9/1/17 Compsci 201, Fall 2017, Java Basics 13

Page 14: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

For Loop Summary

• for(init; boolean guard; update) {…}• for(int k=0; k < a.length; k+=1) {…}

• Initialization happens once, before guard checked for the first time, never again

• Initialization can introduce variables: loop scope

• Guard checked, if true loop body executes

• After loop body, update executes, guard checked

9/1/17 Compsci 201, Fall 2017, Java Basics 14

Page 15: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

For Each Loop

• for(variable : iterable) {…}• for(String s : array-of-strings} {…}

• Variable introduced in scope of loop body

• Variable takes on each value of iterable, execute loop body for each value

• Arrays are iterable, ArrayList, Set are iterable

• Possible to create iterable using standard API

9/1/17 Compsci 201, Fall 2017, Java Basics 15

Page 16: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Summary of Java-isms

• Loop using indexes over an array• The for-loop: initialize; guard/check; update

• Should we loop over odd indexes only?• In some cases, …

• How do we check for String equality?• .equals compared to ==

• How do we submit an APT?• Test, Grade, REFLECT

8/31/18 Compsci 201, Fall 2018, Java and APTs 16

Page 17: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Coding Interlude

• Working on Totality APT in Eclipse

8/31/18 Compsci 201, Fall 2018, Java and APTs 17

Page 18: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Duke Connection: Fred Brooks '53• What Would FB Say?"The most important single

decision I ever made was to change the IBM 360 series from a 6-bit byte to an 8-bit byte, thereby enabling the use of lowercase letters. That change propagated everywhere."

• "Fred Brooks" by Copyright owned by SD&M (www.sdm.de) - Request for picture sent by email to Fred Brooks by uploader (Mark Pellegrini; user:Raul654) Fred sent this photo back, along with contact information for Carola Lauber at SD&M, who gave copyright permission.. Licensed under CC BY-SA 3.0 via Wikimedia Commons -https://commons.wikimedia.org/wiki/File:Fred_Brooks.jpg#/media/File:Fred_Brooks.jpg

8/31/18 Compsci 201, Fall 2018, Java and APTs 18

Page 19: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Why is Programming Fun?

• First is the sheer joy of making things

• Second is the pleasure of making things that are useful

• Third is the fascination of fashioning complex puzzle-like objects of interlocking moving parts

• Fourth is the joy of always learning

• Finally, there is the delight of working in such a tractable medium. The programmer, like the poet, works only slightly removed from pure thought-stuff.

8/31/18 Compsci 201, Fall 2018, Java and APTs 19

Page 20: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Java Primitives and Objects• Unfortunately Java has two different kinds of

constructs• Primitive: int, double, char, boolean, …• Objects: pointers/references to bigger things

• As we’ll see: array of both kinds, but ArrayList or HashSet only of Object

8/31/18 Compsci 201, Fall 2018, Java and APTs 20

Page 21: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Primitive Values and Variables

• Primitive variables label their storage/memory

• With int x = 5; int storage with 5, label: x

• If int y = x; copy 5 to y’s memory

• Changing value stored in memory for x has no effect on value stored in memory for y

9/1/17 Compsci 201, Fall 2017, Java Basics 21

int x = 5;int y = x;x += 7;// values of x and y are?

Page 22: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Object Values and Variables

• Object variables are labels that reference/point to storage. (labels that are put on boxes)String s = new String("Duke");String t = new String("Duke");// only one if statement is true!!!if (s == t) {they label the same box}if (s.equals(t)) {contents of boxes the same}

9/1/17 Compsci 201, Fall 2017, Java Basics 22

s t

What's in the boxes? "Duke" is in the boxes

Page 23: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Object Values and Variables

• Object variables are pointers, value is a memory location or address. Copying value is copying addressString s = "Duke"String t = s;// both statements are true!!!if (s == t) {they label the same box}if (s.equals(t)) {contents of boxes the same}

9/1/17 Compsci 201, Fall 2017, Java Basics 23

s t

Page 24: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

Summary

• Using jShell we play with wild Java abandon• Let's use int, double, String, and array variables• Look at primitive and object assignment/use

• Primitives aren't pointers/references, Objects are• Understand the distinction• Be able to explain the difference

• Understand differences: == and .equals()

9/1/17 Compsci 201, Fall 2017, Java Basics 24

Page 25: Compsci201, Java and APTs - Duke University · 2018-09-01 · Compsci201, Java and APTs Owen Astrachan ola@cs.duke.edu August 31, 2018 8/31/18 Compsci 201, Fall 2018, Java and APTs

WOTO

http://bit.ly/201fall18-831-1

• 32 or 64 bits for variables. Strings/arrays huge!!!

8/31/18 Compsci 201, Fall 2018, Java and APTs 25