CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Similar documents
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

OO software systems are systems of interacting objects.

CSE115 Introduction to Computer Science I Homework 2, part 1retrospective Fall 2017

Lists using ArrayList

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Big O & ArrayList Fall 2018 Margaret Reid-Miller

Algorithms. Produced by. Eamonn de Leastar

Garbage Collection (1)

CS349/SE382 A1 C Programming Tutorial

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Java Collection Framework

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

CSE115 Lab 9 Fall 2016

More on collec)ons and sor)ng

CSE 115 / 503 INTRODUCTION TO COMPUTER SCIENCE I. Dr. Carl Alphonce Dr. Jesse Hartloff

CSE 143 Au03 Midterm 2 Page 1 of 7

COMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)

CS2110: Software Development Methods. Maps and Sets in Java

ArrayLists. Readings and References. Collections in the Real World. How can we manage lists of objects? Reading. Other References

ArrayLists. CSE 142, Summer 2002 Computer Programming 1.

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Review. CSE 143 Java. A Magical Strategy. Hash Function Example. Want to implement Sets of objects Want fast contains( ), add( )

More sophisticated behaviour Lecture 09

H212 Introduction to Software Systems Honors

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Lab Assignment Three

Section Lists and Sets

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

Lesson 36: for() Loops (W11D1)

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

CIS 120 Final Exam May 3, Name (printed): Pennkey (login id):

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Programming Languages and Techniques (CIS120)

CS111: PROGRAMMING LANGUAGE II

COMP String and Console I/O. Yi Hong May 18, 2015

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Programming Karel the Robot

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Object-oriented programming in...

APCS Semester #1 Final Exam Practice Problems

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList

JAVA OPERATORS GENERAL

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

CSE 115 / 503 INTRODUCTION TO COMPUTER SCIENCE I. Dr. Carl Alphonce Dr. Jesse Hartloff

Tutorial 06. Conditional statement: if then, if else, switch

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Collections and Maps

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

CS1 Lecture 3 Jan. 18, 2019

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

CS171:Introduction to Computer Science II

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

Fall 2017 Mentoring 7: October 9, Abstract Data Types

Photo credit: Andrew Kennedy JAVA GENERICS

Java Basic Programming Constructs

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

CS171:Introduction to Computer Science II

CS 302 ALGORITHMS AND DATA STRUCTURES

Collections, Maps and Generics

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Set<Integer> s = new TreeSet<Integer>(); s.add( 7 ); s.add( 7 ); System.out.println( s.size() );

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Topic 10: The Java Collections Framework (and Iterators)

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Programming Languages and Techniques (CIS120)

Exam 2, Version 2. For the following code, mark True or False for statements 1.8 to 1.10.

Programming Languages and Techniques (CIS120)

Interfaces, collections and comparisons

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

More on collec)ons and sor)ng. CSCI 136: Fundamentals of Computer Science II Keith Vertanen

Letterkenny Institute of Technology

COMP 110 Introduction to Programming. What did we discuss?

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CS260 Intro to Java & Android 03.Java Language Basics

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Algorithms & Datastructures Laboratory Exercise Sheet 1

Java 8 OCA Webinar. A Taste of Java Classes. A Taste of Java Collections. A Taste of Java Lambdas

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Transcription:

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu Office hours: Tuesday 10:00 AM 12:00 PM * Wednesday 4:00 PM 5:00 PM Friday 11:00 AM 12:00 PM OR request appointment via e-mail *Tuesday adjustments: 11:00 AM 1:00 PM on 10/11, 11/1 and 12/6

Dr. Carl Alphonce Last time Collections (intro) ROADMAP Today Collections (continued) Coding exercise Coming up Iterators

REVIEW Dr. Carl Alphonce

java.util.collection<e> interface All collection classes in Java are subtypes of the java.util.collection<e> interface. <E> is new syntax E is a type variable, and denote the element type of the collection: Collection<String> denotes a collection of String objects Collection<ActionListener> denotes a collection of ActionListener objects

java.util.collection<e> interface Among the methods specified in the interface: boolean add(e item) --- tries to add item to the collection; if this is successful, true is returned, false otherwise boolean remove(object item) --- tries to remove (one occurrence of) item from the collection; if this is successful, true is returned, false otherwise boolean contains(object item) --- returns true if item is in the collection, false otherwise int size() --- return the number of items currently in the collection

Two specific collections (defined in java.util) ArrayList<E> permits duplicates allows client to control order of elements HashSet<E> does not permit duplicates does not allow client to control order of elements

Collections: usage example To declare a variable of type HashSet of String: HashSet<String> names; To create a HashSet of String object, and assign its reference to the variable declared above: names = new HashSet<String>();

MOVING ON Dr. Carl Alphonce

Eclipse Live coding demo

Enhanced for statement PROGRAM: ArrayList<String> words; words = new ArrayList<String>(); words.add("cat"); words.add("dog"); words.add("rabbit"); for (String w : words) { System.out.println("A "+w+" is a good pet"); } OUTPUT: A cat is a good pet A dog is a good pet A rabbit is a good pet

Dr. Carl Alphonce An ArrayList<E> object holds a sequence of E values. String The get(i) method returns the value stored at location i, as in: names.get(0) à "Fred"

for ( <stmt i > ; <expr> ; <stmt u > ) <stmt b > for statement <stmt i > true <expr> <stmt b > <stmt u > false

PROGRAM: ArrayList<String> words; words = new ArrayList<String>(); for loop words.add("cat"); words.add("dog"); words.add("rabbit"); for (int i=0; i<words.size(); i=i+1) { String w = words.get(i); System.out.println("A "+w+" is a good pet"); } OUTPUT: A cat is a good pet A dog is a good pet A rabbit is a good pet

Dr. Carl Alphonce A String object holds a sequence of char values. String The length() method returns the number of charaters in the String. dog.length() à 3 rabbit.length() à 6

printing String lengths PROGRAM: ArrayList<String> words; words = new ArrayList<String>(); words.add("cat"); words.add("dog"); words.add("rabbit"); for (int i=0; i<words.size(); i=i+1) { String w = words.get(i); System.out.println("The length of "+w+" is " +w.length()"); } OUTPUT: The length of cat is 3 The length of dog is 3 The length of rabbit is 6

Dr. Carl Alphonce A String object holds a sequence of char values. String Each char value has a position (index) within the String. An index is an integer between 0 and the length() of the string, inclusive at 0, exclusive at length(). The charat(int) method of String returns the char at the given index: dog.charat(0) à d dog.charat(2) à g

looking at characters in String public void characterinstring(string s) { System.out.println("The String " + s + " consists of these characters: "); for (int i=0; i<s.length(); i=i+1) { char ch = s.charat(i); System.out.println(" '"+ch+"'"); } }

looking at characters in String PROGRAM: ArrayList<String> words; words = new ArrayList<String>(); words.add("cat"); words.add("dog"); words.add("rabbit"); for (int i=0; i<words.size(); i=i+1) { String w = words.get(i); characterinstring(w); }

looking at characters in String OUTPUT: The String cat consists of these characters: 'c' 'a' 't' The String dog consists of these characters: 'd' 'o' 'g' The String rabbit consists of these characters: 'r' 'a' 'b' 'b' 'i' 't

PAIR CODING EXERCISE CHECK OUT Exercise-04 FROM REPO TO GET TESTS Dr. Carl Alphonce Define method named answer in quiz.question. Define this method so that it takes an ArrayList<String> as argument and returns true if the first String has length 5, false otherwise. If the argument is null or if the ArrayList is empty, return false. Submit to Exercise-04 in Web-CAT enter the usernames of everyone in your group!

PAIR CODING EXERCISE CHECK OUT Exercise-04 FROM REPO TO GET TESTS Dr. Carl Alphonce Define method named answer in quiz.question. Define this method so that it takes an ArrayList<String> as argument and returns true if the first String has length 5, false otherwise. If the argument is null or if the ArrayList is empty, return false. Submit to Exercise-04 in Web-CAT enter the usernames of everyone in your group! SUBMIT NO LATER THAN 6:00 PM TOMORROW (SATURDAY)

Dr. Carl Alphonce HAVE A GREAT WEEKEND MONDAY IS HALLOWEEN WHO IS DRESSING UP FOR CLASS?