compsci201, java and apts - duke university · 2018-09-01 · compsci201, java and apts owen...
TRANSCRIPT
Compsci 201, Java and APTs
Owen [email protected] 31, 2018
8/31/18 Compsci 201, Fall 2018, Java and APTs 1
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
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
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
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
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
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
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
Picture worth 210 words?
8/31/18 Compsci 201, Fall 2018, Java and APTs 9
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
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
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
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
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
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
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
Coding Interlude
• Working on Totality APT in Eclipse
8/31/18 Compsci 201, Fall 2018, Java and APTs 17
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
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
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
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?
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
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
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
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