more sophisticated behavior behavior more sophisticated
play

More sophisticated behavior behavior More sophisticated Using - PowerPoint PPT Presentation

More sophisticated behavior behavior More sophisticated Using library classes to implement some more advanced functionality Main concepts to be covered Main concepts to be covered Using library classes Reading documentation


  1. More sophisticated behavior behavior More sophisticated Using library classes to implement some more advanced functionality

  2. Main concepts to be covered Main concepts to be covered • Using library classes • Reading documentation • Writing documentation Lecture 5: More Sophisticated 03/11/2005 2 Behavior

  3. The Java class library The Java class library • Thousands of classes • Tens of thousands of methods • Many useful classes that make life much easier • A competent Java programmer must be able to work with the libraries. Lecture 5: More Sophisticated 03/11/2005 3 Behavior

  4. Working with the library Working with the library You should: • know some important classes by name; • know how to find out about other classes. Remember: • We only need to know the interface, not the implementation. Lecture 5: More Sophisticated 03/11/2005 4 Behavior

  5. A Technical Support System A Technical Support System • A textual dialog system • Idea based on ‘Eliza’ by Joseph Weizenbaum (MIT, 1960s) • This is a system that when entering questions will try to answer them in an “intelligent” way. Lecture 5: More Sophisticated 03/11/2005 5 Behavior

  6. A Technical Support System A Technical Support System Lecture 5: More Sophisticated 03/11/2005 6 Behavior

  7. A Technical Support System A Technical Support System Lecture 5: More Sophisticated 03/11/2005 7 Behavior

  8. Main loop structure Main loop structure // from the start method in SupportSystem boolean finished = false; while(!finished) {// sentinel controlled loop do something if( exit condition ) { finished = true; } else { do something more } } Lecture 5: More Sophisticated 03/11/2005 8 Behavior

  9. Main loop body Main loop body String input = reader.getInput(); ... String response = responder.generateResponse(); System.out.println(response); Lecture 5: More Sophisticated 03/11/2005 9 Behavior

  10. The exit condition The exit condition String input = reader.getInput(); if(input.startsWith("bye")) { finished = true; } • Where does ‘startsWith’ come from? • What is it? What does it do? • How can we find out? Lecture 5: More Sophisticated 03/11/2005 10 Behavior

  11. String Info String Info Lecture 5: More Sophisticated 03/11/2005 11 Behavior

  12. Reading class documentation Reading class documentation • Documentation of the Java libraries in HTML format; • Readable in a web browser • Class API: Application Programmers’ Interface • Interface description for all library classes • http://java.sun.com/j2se/1.3/docs/api/ Lecture 5: More Sophisticated 03/11/2005 12 Behavior

  13. Interface vs vs implementation implementation Interface The documentation includes • the name of the class; • a general description of the class; • a list of constructors and methods • return values and parameters for constructors and methods • a description of the purpose of each constructor and method the interface of the class Lecture 5: More Sophisticated 03/11/2005 13 Behavior

  14. Interface vs vs implementation implementation Interface The documentation does not include • private fields (most fields are private) • private methods • the bodies (source code) for each method the implementation of the class Lecture 5: More Sophisticated 03/11/2005 14 Behavior

  15. Interface vs vs implementation implementation Interface • The interface of a method consists of • signature • access modifier • return type • method name • a list of parameters • comment • discussion of all signature items • purpose of method Lecture 5: More Sophisticated 03/11/2005 15 Behavior

  16. Using library classes Using library classes • Classes from the library must be imported using an import statement (except classes from java.lang ). • They can then be used like classes from the current project. Lecture 5: More Sophisticated 03/11/2005 16 Behavior

  17. Packages and import Packages and import • Classes are organised in packages. • Single classes may be imported: import java.util.ArrayList; • Whole packages can be imported: import java.util.*; Lecture 5: More Sophisticated 03/11/2005 17 Behavior

  18. Side Note 1: Strings Side Note 1: Strings • Strings are immutable objects • immutable objects cannot change content or state once they have been created String input = reader.getInput(); input = input.trim; if(input.startsWith(“bye”)){ finished = true; } else { … Code omitted } Lecture 5: More Sophisticated 03/11/2005 18 Behavior

  19. Side Note 1: StringBuffer StringBuffer Side Note 1: • A string buffer implements a mutable sequence of characters. A string buffer is like a String , but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. Lecture 5: More Sophisticated 03/11/2005 19 Behavior

  20. Side note 2 2: String equality : String equality Side note tests identity if(input == "bye") { ... } tests equality if(input.equals("bye")) { ... } Strings should (almost) always be compared with .equals • Lecture 5: More Sophisticated 03/11/2005 20 Behavior

  21. Identity vs vs equality 1 equality 1 Identity Other (non-String) objects: :Person :Person “Fred” “Jill” person1 person2 person1 == person2 ? Lecture 5: More Sophisticated 03/11/2005 21 Behavior

  22. Identity vs vs equality 2 equality 2 Identity Other (non-String) objects: :Person :Person “Fred” “Fred” person1 person2 person1 == person2 ? Lecture 5: More Sophisticated 03/11/2005 22 Behavior

  23. Identity vs vs equality 3 equality 3 Identity Other (non-String) objects: :Person :Person “Fred” “Fred” person1 person2 person1 == person2 ? Lecture 5: More Sophisticated 03/11/2005 23 Behavior

  24. Identity vs vs equality (Strings) equality (Strings) Identity String input = reader.getInput(); == tests identity if(input == "bye") { ... } :String :String == ? "bye" "bye" input ➠ (may be) false! Lecture 5: More Sophisticated 03/11/2005 24 Behavior

  25. Identity vs vs equality (Strings) equality (Strings) Identity equals tests String input = reader.getInput(); equality if(input.equals("bye")) { ... } :String :String ? equals "bye" "bye" input ➠ true! Lecture 5: More Sophisticated 03/11/2005 25 Behavior

  26. Using Random Using Random • The library class Random can be used to generate random numbers import java.util.Random; ... Random randomGenerator = new Random(); ... int index1 = randomGenerator.nextInt(); int index2 = randomGenerator.nextInt(100); Lecture 5: More Sophisticated 03/11/2005 26 Behavior

  27. Generating random responses Generating random responses public Responder() { randomGenerator = new Random(); responses = new ArrayList(); fillResponses(); } public String generateResponse() { int index = randomGenerator.nextInt(responses.size()); return (String) responses.get(index); } public void fillResponses() ... Lecture 5: More Sophisticated 03/11/2005 27 Behavior

  28. Maps Maps • Maps are collections that contain pairs of values. • Pairs consist of a key and a value. • Lookup works by supplying a key, and retrieving a value. • An example: a telephone book. Lecture 5: More Sophisticated 03/11/2005 28 Behavior

  29. Using maps Using maps • A map with Strings as keys and values :HashMap "Charles Nguyen" "(531) 9392 4587" "Lisa Jones" "(402) 4536 4674" "William H. Smith" "(998) 5488 0123" Lecture 5: More Sophisticated 03/11/2005 29 Behavior

  30. Using maps Using maps HashMap phoneBook = new HashMap(); phoneBook.put("Charles Nguyen", "(531) 9392 4587"); phoneBook.put("Lisa Jones", "(402) 4536 4674"); phoneBook.put("William H. Smith", "(998) 5488 0123"); String number = (String)phoneBook.get("Lisa Jones"); System.out.println(number); Lecture 5: More Sophisticated 03/11/2005 30 Behavior

  31. Maps in TechSupport TechSupport Maps in public class Responder { private HashMap responseMap; … Code Omitted public String generateResponse(String word) { String response = (String) responseMap.get(word); if(response != null) { return response; } else { return pickDefaultResponse(); } } Lecture 5: More Sophisticated 03/11/2005 31 Behavior

  32. Using sets Using sets import java.util.HashSet; import java.util.Iterator; ... HashSet mySet = new HashSet(); Compare this to ArrayList mySet.add("one"); code! mySet.add("two"); mySet.add("three"); Iterator it = mySet.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } Lecture 5: More Sophisticated 03/11/2005 32 Behavior

  33. Sets and List Sets and List • A Set is a collection that stores each individual element at most once. It does not maintain any specific order. • A List is a collection that stores all elements it is been giving regardless of duplication. It maintains a order at all times. Lecture 5: More Sophisticated 03/11/2005 33 Behavior

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend