Introduction to Programming in Java - Lecture 25 | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-f8a-1
koofers-user-f8a-1 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 25
Last Time:
We looked at the HashMap class, and in lab you used it to write a phone directory.
What does a HashMap do?
- It has entries that are composed of a key and a value
- You can look up the value of an entry by using the key
- The key and the value can be the same or different types
Tech Support System
Let's go back to our Tech support system and enhance it to use a HashMap to
generate responses. [Use tech-support2 project].
1) Use a HashMap to associate keywords such as "slow", "crash", or "expensive"
with tech support responses.
2) If the user enters a String consisting of one of the keywords, print the response
3) Otherwise, choose a random response.
OK, so what's wrong with this version? It only prints a 'sensible' response if the user
input consists of one word that happens to be a key value.
We'd like to print out a sensible response if any word in the input is one of the keys.
String split method – go on-line and read about the split method in class
String. What does it do?
The split method returns an array of Strings that are extracted from the original
String. The split method expects a regular expression to determine where to break the
original String into parts. For now, just think of the reg-ex as another String.
Examples: Assume String s = "I ate a banana"
s.split(" ") returns an array with 4 elements: "I" "ate" "a" "banana"
s.split("t") returns an array with 2 elements: "I a" "e a banana"
s.split("a") returns an array with 5 elements: "I " "te " " b" "n" "n"
s.split("an") returns an array with 3 elements: "I ate a b" "" "a"
Where do we want to split the user input String in our tech-support system?
OK, so let's modify the tech-support system to use split in the InputReader class. We
will return the array from the getInput method, and pass this to the
pf3

Partial preview of the text

Download Introduction to Programming in Java - Lecture 25 | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 25

Last Time: We looked at the HashMap class, and in lab you used it to write a phone directory. What does a HashMap do?

  • It has entries that are composed of a key and a value
  • You can look up the value of an entry by using the key
  • The key and the value can be the same or different types Tech Support System Let's go back to our Tech support system and enhance it to use a HashMap to generate responses. [Use tech-support2 project].
  1. Use a HashMap to associate keywords such as "slow", "crash", or "expensive" with tech support responses.
  2. If the user enters a String consisting of one of the keywords, print the response
  3. Otherwise, choose a random response. OK, so what's wrong with this version? It only prints a 'sensible' response if the user input consists of one word that happens to be a key value. We'd like to print out a sensible response if any word in the input is one of the keys. String split method – go on-line and read about the split method in class String. What does it do? The split method returns an array of Strings that are extracted from the original String. The split method expects a regular expression to determine where to break the original String into parts. For now, just think of the reg-ex as another String. Examples: Assume String s = "I ate a banana" s.split(" ") returns an array with 4 elements: "I" "ate" "a" "banana" s.split("t") returns an array with 2 elements: "I a" "e a banana" s.split("a") returns an array with 5 elements: "I " "te " " b" "n" "n" s.split("an") returns an array with 3 elements: "I ate a b" "" "a" Where do we want to split the user input String in our tech-support system? OK, so let's modify the tech-support system to use split in the InputReader class. We will return the array from the getInput method, and pass this to the

generateResponse method in the Responder class.