Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu

2 Lab 6 remarks Learn process –start small, with something that has some minimal functionality (e.g. putting a JFrame up on the screen) –test functionality/behavior of program frequently –build overall functionality incrementally, in small steps Notice that all classes implementing an interface are similar –in lab 6, several tools implement the ITool interface –these tools generally fit into the program the same way –figure out one tool first (e.g. SquareTool), and the rest will follow –works for other projects too! Labs 7 and 8 –we will not spell out detailed steps –you should apply process on your own –seek guidance/assistance as needed, from TAs or instructor

3 Agenda Review from last class Introduction to primitive types The while loop The foreach loop

4 Review Collections –Collection interface add/remove/iterator methods –HashSet class –LinkedList class Iterators –Iterator interface next/hasNext methods

5 Collections: concepts interface: java.util.Collection –E is the type of element contained in the collection – replace by an actual type concrete implementations (examples): –java.util.HashSet at most one of each internal order irrelevant to client –java.util.LinkedList multiples permitted maintains order; client can determine order

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

7 Collections: usage example (continued) To add a String to the HashSet: names.add(“Fred”); To remove a String from the HashSet: names.remove(“Fred”);

8 Primitives in Java Java has eight primitive types; today we will see one of them: –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double, float Values of the primitive types are not objects –no properties –no capabilities

9 boolean values: true, false operations: &&“and” ||“or” !“not”

10 Control structure overview while statement while ( ) true false

11 boolean expressions hasNext()’s return type is boolean call to hasNext() can be used to control while loop

12 Last time java.util.Collection coll; coll = new java.util.LinkedList (); coll.add("Fred"); coll.add("Wilma"); coll.add("Pebbles"); coll.add("Dino"); coll.add("Fred"); System.out.println("The collection: " + coll); java.util.Iterator it; it = coll.iterator(); System.out.println(it.next());

13 Notice repetition java.util.Collection coll; coll = new java.util.LinkedList (); coll.add("Fred"); coll.add("Wilma"); coll.add("Pebbles"); coll.add("Dino"); coll.add("Fred"); System.out.println("The collection: " + coll); java.util.Iterator it; it = coll.iterator(); System.out.println(it.next());

14 replace repetition with while loop java.util.Collection coll; coll = new java.util.LinkedList (); coll.add("Fred"); coll.add("Wilma"); coll.add("Pebbles"); coll.add("Dino"); coll.add("Fred"); System.out.println("The collection: " + coll); java.util.Iterator it; it = coll.iterator(); while (it.hasNext()) { System.out.println(it.next()); }

15 for-each loop – process each element of a collection of elements of type E: for (E item : collection) { // do something with item } Example: to print out each String in coll: for (String s : coll ) { System.out.println(s); } Declared as: HashSet Declared as: HashSet

16 while loop java.util.Collection coll; coll = new java.util.LinkedList (); coll.add("Fred"); coll.add("Wilma"); coll.add("Pebbles"); coll.add("Dino"); coll.add("Fred"); System.out.println("The collection: " + coll); java.util.Iterator it; it = coll.iterator(); while (it.hasNext()) { System.out.println(it.next()); }

17 for-each loop: no explicit iterator java.util.Collection coll; coll = new java.util.LinkedList (); coll.add("Fred"); coll.add("Wilma"); coll.add("Pebbles"); coll.add("Dino"); coll.add("Fred"); System.out.println("The collection: " + coll); for (String s : coll) { System.out.println(s); }

18 for-each loop vs. while loop java.util.Collection coll; coll = new java.util.LinkedList (); coll.add("Fred"); coll.add("Wilma"); coll.add("Pebbles"); coll.add("Dino"); coll.add("Fred"); System.out.println("The collection: " + coll); Iterator it; it = coll.iterator(); for (String s : coll) { while (it.hasNext()) { System.out.println(s);String s = it.next(); } System.out.println(s); }


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739"

Similar presentations


Ads by Google