Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jeff Forbes Owen Astrachan September 8, 2017

Similar presentations


Presentation on theme: "Jeff Forbes Owen Astrachan September 8, 2017"— Presentation transcript:

1 Jeff Forbes Owen Astrachan September 8, 2017
More Java Basics Jeff Forbes Owen Astrachan September 8, 2017 9/8/17 Compsci 201, Fall 2017, Java Basics

2 Compsci 201, Fall 2017, Java Basics
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

3 CompSci 201, Fall 2017, Java Basics
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

4 Compsci 201, Fall 2017, Java Basics
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 package More later on these… 9/8/17 Compsci 201, Fall 2017, Java Basics

5 Compsci 201, Fall 2017, Simulation
9/6/17 Compsci 201, Fall 2017, Simulation

6 Compsci 201, Fall 2017, Java Basics
Solving problems See ClassScores APT Prof. Drew Hilton’s 7 step process 9/8/17 Compsci 201, Fall 2017, Java Basics

7 Compsci 201, Fall 2017, Java Basics
Do it on paper! Find the maximally occurring number(s) How do you count? What data structures will you need? Translate to code later 9/8/17 Compsci 201, Fall 2017, Java Basics

8 Compsci 201, Fall 2017, Java Basics
ArrayList An array that does not have fixed length No primitives! Only Objects! ArrayList<String> list = new ArrayList<String>(); // add to ArrayList list.add("hello"); //check if element is in ArrayList boolean inList = list.contains("hello"); //get element at index five String word = list.get(5); 9/8/17 Compsci 201, Fall 2017, Java Basics

9 Compsci 201, Fall 2017, Java Basics
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

10 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 Example String[] words = String[N]; . . . TreeSet<String> wordset = new TreeSet<String>(Arrays.asList(words));

11 Writing & Testing ClassScores
WOTO: 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] How would printArrayList differ? 9/8/17 Compsci 201, Fall 2017, Java Basics

12 Charles Isbell Context matters Machine learning researcher
Systems that interact intelligently with many other intelliggence agents Exec. Assoc. Georgia Tech Rethinking education: Online Masters in Computer Science . For me, the differences are simple to state: Computationalists grok that models, languages and machines are equivalent.

13 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 sets java.util.TreeSet A TreeSet is backed up by a tree structure (future topic) Keeps items sorted (+) Slower than HashSets ?? (-) 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

14 Union 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} 2 3 5 7 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...) © Michael Frank

15 Intersection 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} = ______ 2 3 5 6 4 Think “The intersection of Main St. and 9th St. is just that part of the road surface that lies on both streets.” © Michael Frank

16 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 © Michael Frank

17 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} Note that set difference and complement do not relate to each other like arithmetic difference and negative. In arithmetic, we know that a-b = -(b-a). But in sets, A-B is not generally the same as the complement of B-A. A U © Michael Frank

18 WordCount Again! http://bit.ly/201-f17-0908-2 WOTO!
WordCount3.java has two similar methods that count the number of unique "words" in a file. Would you expect countWordsLoop or countWords to be faster? Why? What would happen if you used a TreeSet rather than a HashSet? How would we write wordsInCommon and the necessary call in main to prints the number of Strings in common between two files. WOTO! CompSci 201

19 Set Questions Continue Quiz 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. the set of engineers taking CompSci 201 the set of engineers who are not taking CompSci 201 the set of students who either are engineers or are taking CompSci 201 the set of students who either are not engineers or are not taking CompSci 201 Continue Quiz CompSci 201

20 Converting from Collection to array
Collections such as ArrayList and TreeSet have a toArray method Syntax a bit awkward Example ArrayList<String> words = new ArrayList<String>(); . . . String[] a = (String[]) words.toArray(new String[0]); or return (String[]) words.toArray(new String[0]);

21 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

22 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 Comments Abstraction: What does it do? Comments for methods and classes Implementation: How does it do it? Inline comments as needed 9/8/17 Compsci 201, Fall 2017, Java Basics

23 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

24 Compsci 201, Fall 2017, Java Basics
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? Discussion warmup: Bring a written solution to discussion Complete and submit ClassScores as warmup 9/8/17 Compsci 201, Fall 2017, Java Basics


Download ppt "Jeff Forbes Owen Astrachan September 8, 2017"

Similar presentations


Ads by Google