17
CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Embed Size (px)

Citation preview

Page 1: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

CIS3931 - Intro to JAVA

Lecture Notes Set 7

7-June-05

Page 2: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Maps

• Maps create a way of storing a value that is accessed via a key (mapping a value to a key)

• Keys are unique in maps. There can not be two keys with the same name.

• Each key maps to one value.

• Values are not unique. Multiple keys may have the same value.

Page 3: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Creating a Map

• Map map = new TreeMap();– This creates a Map of type TreeMap

• Map map = new HashMap();– This creates a Map of type HashMap

• Etc …

Page 4: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Map methods

• clear() – Removes all mappings from a map

• remove(Object key) – Removes the key and associated value from the map

• put (Object key, Object value) – Associates the specified value with the specified key

• putAll(Map t) – Copies all of the mappings from the specified map to this map.

Page 5: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Map methods (cont.)

• containsKey(Object key) – Returns true if the maps contains a mapping for the specified key

• containsValue(Object value) – Returns true if this map maps one or more keys to the specified value

• get(Object key) – Returns the value to which this map maps the specified key.c

Page 6: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Map Example

• See MapExample.java

• For more information, see page 942 of your textbook.

Page 7: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Pattern

• Pattern creates a pattern recognizer using a regular expression

• Regular expressions info : http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

Page 8: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Pattern

• To create a pattern– Pattern pattern = Pattern.compile(REGEX);

– Where REGEX is a regular expression

• Example : – Pattern pattern = Pattern.compile(“\\b[a-zA-Z]+\\b”);

– Creates a pattern that will match all words with letters A-Z and a-z.

Page 9: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Matcher

• Uses pattern to grab all the matches out of a string

• Usage : – Matcher matcher = pattern.matcher(STRING);

Page 10: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Matcher - Methods

• You will find the following methods useful when writing Assignment 3…

• find() – attempts to find the next subsequence of the input sequence that matches the pattern.

• group() – Returns the input subsequence matched by the previous match.

Page 11: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Matcher Example

• From previously taught class : – regular_expressions.html

• See PatternMatcherExample.java

Page 12: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Program 2

• Example of finished source code for Program 2…

Page 13: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Program 3

• Posted on the Assignment page…

Page 14: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

GUI – The Graphical User Interface

• Read Chapter 11 of the textbook !!!

• GUI’s will not be on the first midterm. However, they will be on the second midterm and final. Information from Chap. 11 may be in midterm 2 and the final.

Page 15: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

GUI Components

• Buttons• Menus• Title Bars• Menu Bars• Combo Boxes• Scroll Bars• Panels• …etc

Page 16: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

A Simple GUI Example

• GUI based I/O using the JOptionPane

• See GUIAddition.java

Page 17: CIS3931 - Intro to JAVA Lecture Notes Set 7 7-June-05

Questions?