![Page 1: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/1.jpg)
More Java Basics
Jeff ForbesOwen Astrachan
September 8, 2017
9/8/17 Compsci 201, Fall 2017, Java Basics 1
![Page 2: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/2.jpg)
D is for …• Debugging• A key skill in making your programs run
• Data-first• Data dictates the tradeoffs
• Digital• All about the 1s and 0s
9/1/17 Compsci 201, Fall 2017, Java Basics 2
![Page 3: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/3.jpg)
Review: What is a class?• Encapsulates state and behavior• State is: instance variables, specific to each
object• Behavior is methods: update state, possibly
use or report on state• Class is a blueprint or template: object creation• Call new, invokes constructor, initialize object
• Objects communicate via methods, parameters• Object can pass itself: this refers to self
9/8/17 CompSci 201, Fall 2017, Java Basics 3
![Page 4: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/4.jpg)
Access Control• public: accessible anywhere• private: accessible only within methods of this
class
• protected: accessible to this class or subclasses
• No modifier: accessible within class and packageMorelateronthese…
9/8/17 Compsci 201, Fall 2017, Java Basics 4
![Page 5: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/5.jpg)
9/6/17 Compsci 201, Fall 2017, Simulation 5
![Page 6: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/6.jpg)
Solving problems• See ClassScores APT• Prof. Drew Hilton’s 7 step process
9/8/17 Compsci 201, Fall 2017, Java Basics 6
![Page 7: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/7.jpg)
Do it on paper!
9/8/17 Compsci 201, Fall 2017, Java Basics 7
• Find the maximally occurring number(s)• How do you count?• What data structures will you need?• Translate to code later
![Page 8: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/8.jpg)
ArrayList• An array that does not have fixed length•No primitives! •Only Objects!
ArrayList<String> list = new ArrayList<String>();
// add to ArrayListlist.add("hello");
//check if element is in ArrayListboolean inList = list.contains("hello");
//get element at index fiveString word = list.get(5);
9/8/17 Compsci 201, Fall 2017, Java Basics 8
![Page 9: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/9.jpg)
Java API• Application Programming Interface• Classes that are part of the Java development kit• Data structures• e.g., java.util.ArrayList, java.lang.String, java.util.Scanner
• Utility methods• e.g., java.lang.Math, java.util.Arrays
9/8/17 Compsci 201, Fall 2017, Java Basics 9
![Page 10: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/10.jpg)
Useful methods for Arrays• Arrange elements in natural order
int[] example;Arrays.sort(example);
• Convert from List/Collection to Array• For arrays of objects (such as Strings) use the asList method in the Arrays class.• Pass this into the constructor of your Collection
• ExampleString[] words = String[N];. . .TreeSet<String> wordset = new
TreeSet<String>(Arrays.asList(words));
![Page 11: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/11.jpg)
Writing & Testing ClassScores1. WOTO: http://bit.ly/201-f17-0908-12. How do you print all of the elements in an array
of ints?• Add a method printArray that will print the
elements in standard form[88, 70, 65, 70, 88]
3. How would printArrayList differ?
9/8/17 Compsci 201, Fall 2017, Java Basics 11
![Page 12: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/12.jpg)
Charles Isbell.
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• Machine learning researcher• Systems that interact intelligently
with many other intelliggenceagents
• Exec. Assoc. Dean @ Georgia Tech • Rethinking education: Online
Masters in Computer Science
![Page 13: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/13.jpg)
Sets• Set is an unordered collection of distinct objects
• Items are unique! Only one copy of each item in set!• We will use two different implementations of sets1. java.util.TreeSet
• A TreeSet is backed up by a tree structure (future topic)• Keeps items sorted (+)• Slower than HashSets ?? (-)
2. java.util.HashSet• A HashSet is backed up by a hashing scheme (future topic)• Items not sorted – should seem to be in random order (-)• Faster than TreeSets ?? (+)
CompSci 201
![Page 14: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/14.jpg)
© Michael Frank
• The union of two sets A and B is the set containing elements that are either in A or in B.
• {a,b,c}È{2,3} = {a,b,c,2,3}• {2,3,5}È{3,5,7} = {2,3,5,3,5,7} ={2,3,5,7}
Union
Think “The United States of America includes every person who worked in any U.S. state last year.” (This is how the IRS sees it...)
![Page 15: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/15.jpg)
© Michael Frank
• The intersection of two sets A and B is the set containing elements that are both A and B.
• {a,b,c}Ç{2,3} = ___• {2,4,6}Ç{3,4,5} = ______
Intersection
Think “The intersection of Main St. and 9th St. is just that part of the road surface that lies on both streets.”
![Page 16: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/16.jpg)
© Michael Frank
Set Difference - Venn Diagram• The difference between two sets A and B is the
set containing elements that are in A but not B.• A−B is what’s left after B
“takes a bite out of A”
Set A Set B
![Page 17: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/17.jpg)
© Michael Frank
Set Complements• The complement of a set A is the set of all
elements of the U, universal set (i.e. set containing all objects under consideration) that are not in A.
A = {x | x∉ A}
AU
![Page 18: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/18.jpg)
WordCount Again!• WordCount3.java has two similar methods that count the
number of unique "words" in a file.1. Would you expect countWordsLoop or countWords
to be faster? Why?
2. What would happen if you used a TreeSet rather than a HashSet?
3. How would we write wordsInCommon and the necessary call in main to prints the number of Strings in common between two files.
CompSci 201
WOTO!http://bit.ly/201-f17-0908-2
![Page 19: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/19.jpg)
Set Questions• Let• A is a Set of engineering undergraduate students at
Duke• B is a Set of students taking CompSci 201 at Duke.
• Express each of the following sets in terms of operations on sets A and B.
1. the set of engineers taking CompSci 2012. the set of engineers who are not taking CompSci 2013. the set of students who either are engineers or are taking
CompSci 2014. the set of students who either are not engineers or are
not taking CompSci 201
CompSci 201
ContinueQuiz
![Page 20: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/20.jpg)
Converting from Collection to array
• Collections such as ArrayList and TreeSethave a toArray method• Syntax a bit awkward
• ExampleArrayList<String> words = new ArrayList<String>();
. . .String[] a =
(String[]) words.toArray(new String[0]);
orreturn (String[]) words.toArray(new String[0]);
![Page 21: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/21.jpg)
SetOperations?• Implement set operations for two sets • Union, intersection, difference
• Implement set operations for array of sets• Union, intersection
• Refer to Java API• add, retain, remove• Variants with all
![Page 22: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/22.jpg)
Conventions & Documentation• See Resources/Advice on Sakai• Variable name guidelines• Use nouns that describe what value is being
stored• Don’t reiterate the type involved
• Comments1. Abstraction: What does it do?• Comments for methods and classes
2. Implementation: How does it do it?• Inline comments as needed
9/8/17 Compsci 201, Fall 2017, Java Basics 22
![Page 23: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/23.jpg)
Conventions & Documentation• Classes start with capital letter and then we have:• They’re public, except nested class? • camelCaseForMethods and ForClasses• Fields & instance variables: mySize, myMap, …• Constants (public static) are ALL_CAPS
• Standard identifiers• i, j, k: integer loop counters• n, len, length: integer number of elements in
collection• x, y: Cartesian coordinates (integer or real)• head, current, last: references used to iterate
over lists.
9/8/17 Compsci 201, Fall 2017, Java Basics 23
![Page 24: More Java Basics - Duke University · More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci201, Fall 2017, Java Basics 1](https://reader035.vdocuments.net/reader035/viewer/2022070812/5f0bdf527e708231d432a197/html5/thumbnails/24.jpg)
Summary• Write code to solve problem • Using arrays, files, etc.
• Introduce data structures and methods from Java API
• Reflect –• What’s clear? What’s still muddy?• http://bit.ly/201-f17-reflect
• Discussion warmup:• Bring a written solution to discussion• Complete and submit ClassScores as warmup
9/8/17 Compsci 201, Fall 2017, Java Basics 24