compsci201, arrays, arraylist, simulation
TRANSCRIPT
Compsci 201, Arrays, ArrayList, Simulation
Owen [email protected]
http://bit.ly/201spring19January 23, 2019
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 1
D is for …
• Debugging• A key skill in making your programs run
• Digital• All about the 1s and 0s
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation2
Plan for the Week
• How to succeed in finishing Assignment P1, P0• Java and Git knowledge
• Understanding arrays and ArrayList: Tradeoffs• Toward diyad on Friday
• Applying these concepts to P1: Nbody simulation• Solving a problem that requires computational
simulation, not only mathematical analysis
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 3
GroundHog Day
• We had not quite finished SandwichBar APT
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 4
Reading and thinking
• SandwichBar APT writeup• Clue/hint that indexing loop appropriate• Helper function: array of strings contains all
strings. Another: array of strings contains one
• How to access all strings: “cheese tomato bread”• Using String.split(" ") function
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 5
If Only there was an API for that …
• As semester progresses, you'll learn …• In package java.util: ArrayList, HashSet, more• Collections. and Arrays. for help
• Preview – for now solve problems using what you know rather than increasing what you know!
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 6
Questions about APTs and P0
• When are APTs due? What is late policy?
• What about optional APTs and reading
• Both String and Person are immutable
• Can make new objects, cannot change them
• A variable can change what it references, but what it references cannot change
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation7
WOTO Review
http://bit.ly/201spring19-jan18-1
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 8
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• http://bit.ly/5qOjoH
• Software and steam engines
Assignments P0 and P1
• Simple Java class with driver program• How is a program run? What is main?• See documentation and create methods
• Example of 201 work-flow• GitLab for starter code and your code• Using SSH and GitLab together• Submitting via Gradescope, Analysis
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 10
Project P1
• See course website for details• Not quite this, but …
• Read assignment before starting to code• Think before fingers on keys
• Use Piazza and Helper Hours
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 11
What is a static method?
• Objects are instances of a class• Thus objects have instance variables• Typically private, accessed in methods
• Static method belongs to class, not object• No instance variables• Accesses static variables and methods• More on this later, for now? …
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 12
Static Methods
• The class Math has many (java.lang)
• sqrt, cos, abs, …
• The class Arrays has many (java.util)
• sort, fill, toString, …
• Invoke as Math.sqrt(25.0)
• Math is a class. Convention for name?
• There aren't different Math objects, none!• No state needed, all code in methods
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation13
WOTO: Static
http://bit.ly/201spring19-jan23-1
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 14
Charles Isbell
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation15
http://www.pbs.org/newshour/bb/online-graduate-programs-offer-degrees-significant-savings/
For me, the differences are simple to state: Computationalists grok that models, languages and machines are equivalent.
• Context matters: Threads
• Machine learning researcher
• Systems that interact intelligently with many other intelligence agents
• Exec. Assoc. Dean @ Georgia Tech
• Rethinking education: OnlineMasters in Computer Science
Projects in 201: Briefly
• Start with GitLab project, fork repository
• Starter code and cloud-based storage for projects
• Clone repo to your machine: you need Git for this
• You use Duke's Compsci GitLab website
• You manage your code/projects with GitLab
• Make changes, complete project
• Push changes frequently using Git … hourly/daily
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation16
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/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 17
Submission and Grading
• After completing assignment --- and before!
• Push code using Git
• Submit code on Gradescope. Wait a minute
• Connect to GitLab repository, grade
• (alternatives exist, we don't plan to use them)
• Submit before deadline!
• Autograder runs and tells you what happened!
• Don’t submit until you think code works!
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation18
Working on P0 and P1
• Make sure you can start and finish before coding
• What does that mean? Issues with grading?
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation19
Array and ArrayList
• Array can hold primitive or Object types• int[] and String[] work• Fixed size, cannot grow
• Use java.util class ArrayList for growth, more• https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html
• Only hold object types, not primitives• Use .get(),.set() and not [] for indexing
1/23/19 Compsci 201, Spring 2019: Arrays, ArrayList, Simulation 20
Tradeoffs: Algorithmic Approaches
• https://coursework.cs.duke.edu/201spring19/classwork-spring19/tree/master
• Read words from a file, store in ArrayList
• See ArrayListUniqiue.java, list grows as needed, to be investigated
• Create sorted list of unique words in list
• Compare three different algorithmic approaches
• Tradeoffs in understanding code
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation21
Process every word, add to sorted
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation22
• Code in methodA: process each word in list, add X to list of sorted, unique words
• If X already in sorted-list? Nothing to do
• If X greater than all words in list? Add at end
• Some word greater than X? shift to make room
Details of shifting to add X
• In code, s is the String being added to sorted-list, and the sorted-list is variable ret• Shift from end to index > X, then add X
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation23
Code Analysis and Dissection
• What does break do? Use of boolean done?
• What if words in list already in order? …
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation24
Tradeoff: Words Already Sorted
• There are duplicates in list, but it's sorted
• Look at X, each element of list in order, consider adding X to no-dupes sorted list
• X is >= last element of no-dupes
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation25
What if we use API, other classes
• A set contains no duplicates, a TreeSet maintains unique elements in sorted order
• Create set, contains no duplicates
• Create ArrayList from set
1/23/19Compsci 201, Spring 2019: Arrays,
ArrayList, Simulation26