Jeff Forbes Owen Astrachan September 8, 2017

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Road Map Introduction to object oriented programming. Classes
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CompSci 100E Gambler's Ruin  One approach. Monte Carlo simulation.  Flip digital coins and see what happens. o Pseudorandom number generation ojava.util.Random.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Chapter 18 Java Collections Framework
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
CompSci 100e2.1 Today’s Topics l Reading:  Sedgewick & Wayne: , l Topics  APTs  Basic Collections: ArrayLists: the expandable array.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
Collections Dwight Deugo Nesa Matic
Unified Modeling Language (UML)
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
Lecture 5:Interfaces and Abstract Classes
Using the Java Collection Libraries COMP 103 # T2
Sort & Search Algorithms
Molecular Ball Simulation
Sixth Lecture ArrayList Abstract Class and Interface
Sets Set is an unordered list of items
EECE 310: Software Engineering
More Sophisticated Behavior
Collections.
Chapter 19 Java Data Structures
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Objects as a programming concept
Java Primer 1: Types, Classes and Operators
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Compsci 201 Priority Queues & Autocomplete
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
CS313D: Advanced Programming Language
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
ArrayLists.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
int [] scores = new int [10];
Dynamic Data Structures and Generics
Object Oriented Programming in java
Welcome to CSE 143! Go to pollev.com/cse143.
CS2110: Software Development Methods
int [] scores = new int [10];
CSC 221: Computer Programming I Fall 2009
Collections Framework
Building Java Programs
Dynamic Data Structures and Generics
Java Programming Language
Creating and Modifying Text part 3
Chapter 19 Generics.
String Class.
Java String Class String is a class
Java Basics – Arrays Should be a very familiar idea
Presentation transcript:

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

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

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

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

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

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

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

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

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

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));

Writing & Testing ClassScores WOTO: http://bit.ly/201-f17-0908-1 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

Charles Isbell Context matters Machine learning researcher Systems that interact intelligently with many other intelliggence agents Exec. Assoc. Dean @ Georgia Tech Rethinking education: Online Masters in Computer Science . 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.

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

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

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

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

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

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! http://bit.ly/201-f17-0908-2 CompSci 201

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

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]);

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

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

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

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? 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