Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.

Similar presentations


Presentation on theme: "More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality."— Presentation transcript:

1 More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.

2

3 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Book Material Chapter 5, sections 5.1 - 5.5 Chapter 5, sections 5.1 - 5.5

4 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main Concepts to be Covered Using library classes Using library classes Reading documentation Reading documentation

5 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The Java Class Library Thousands of classes Thousands of classes Tens of thousands of methods Tens of thousands of methods Many useful classes to save us work Many useful classes to save us work A competent Java programmer must be able to work with the libraries. A competent Java programmer must be able to work with the libraries.

6 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Working with the Library We should: We should: –know some important classes by name –know how to find out about other classes Remember: Remember: –We only need to work with the interface –We do not need the implementation * * Documentation, public contructor and method headers ** ** Private fields, contructors, methods and code bodies

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A Technical Support System A textual dialog system A textual dialog system Idea based on Eliza by Joseph Weizenbaum (MIT, 1960s) Idea based on Eliza by Joseph Weizenbaum (MIT, 1960s) Explore … Explore … See Chapter05, tech-support projects

8 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Eliza ProjectInputReaderResponder SupportSystem See Chapter05, tech-support projects

9 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Common Loop Structure boolean finished = false; while (!finished) {... do something if (we want to finish) { finished = true; } else {... do something more } }

10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Eliza Main Loop boolean finished = false; while (!finished) { String input = reader.getInput (); if (we want to finish) { finished = true; } else {... do something more } } Chapter05, tech-support1, SupportSystem.start() private InputReader reader;

11 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling boolean finished = false; while (!finished) { String input = reader.getInput (); if (we want to finish) { finished = true; } else { String response = responder.generateResponse (); System.out.println (response); } } Chapter05, tech-support1, SupportSystem.start() private InputReader reader; private Responder responder; Eliza Main Loop

12 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling boolean finished = false; while (!finished) { String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } else { String response = responder.generateResponse (); System.out.println (response); } } Chapter05, tech-support1, SupportSystem.start() private InputReader reader; private Responder responder; Eliza Main Loop

13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } Where does startsWith come from? Where does startsWith come from? What is it? What does it do? What is it? What does it do? How can we find out? How can we find out? Where does startsWith come from? Where does startsWith come from? What is it? What does it do? What is it? What does it do? How can we find out? How can we find out? Eliza Main Loop

14 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Where does startsWith come from? Where does startsWith come from? What is it? What does it do? What is it? What does it do? How can we find out? How can we find out? Where does startsWith come from? Where does startsWith come from? What is it? What does it do? What is it? What does it do? How can we find out? How can we find out? if () { finished = true; } String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } Where does startsWith come from? Where does startsWith come from? Its a method invoked on input … Its a method invoked on input … Therefore, its a method of String … Therefore, its a method of String … Where does startsWith come from? Where does startsWith come from? Its a method invoked on input … Its a method invoked on input … Therefore, its a method of String … Therefore, its a method of String … Eliza Main Loop

15 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Reading Class Documentation The built-in documentation of the Java libraries is in HTML format The built-in documentation of the Java libraries is in HTML format Readable in any web browser Readable in any web browser Class API: Application Programmers Interface Class API: Application Programmers Interface Interface description for all library classes Interface description for all library classes

16 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Interface –vs– Implementation The documentation includes : The documentation includes : the name of the class the name of the class a general description of the class a general description of the class a list of public constructors and methods a list of public constructors and methods parameters for constructors and methods parameters for constructors and methods return values for non- void methods return values for non- void methods the purpose of each constructor and method the purpose of each constructor and method the interface of the class the interface of the class

17 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling the implementation of the class the implementation of the class The documentation does not include: The documentation does not include: private fields (most fields are private) private fields (most fields are private) private constructors and methods private constructors and methods the bodies (source code) for each method and constructor the bodies (source code) for each method and constructor Interface –vs– Implementation

18 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using Library Classes Classes from the library must be imported using an import statement (except classes from java.lang). Classes from the library must be imported using an import statement (except classes from java.lang). Then, they can be used like classes from the current project. Then, they can be used like classes from the current project.

19 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Packages and Import Classes are organised in packages. Classes are organised in packages. Single classes may be imported: import java.util.ArrayList; Single classes may be imported: import java.util.ArrayList; Whole packages can be imported: import java.util.*; Whole packages can be imported: import java.util.*;

20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using the Random Class 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); returns a random integer between 0 and 99 (inclusive) returns a random integer

21 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling private void fillResponses () { responses.add ("That sounds odd. Tell me more?"); responses.add ("No one else complained!"); responses.add ("That sounds odd. Tell me more?"); responses.add ("No one else complained!"); responses.add ("Interesting. Tell me more?"); responses.add ("Interesting. Tell me more?"); responses.add ("Do you have a dll conflict?"); responses.add ("Do you have a dll conflict?"); responses.add ("Read the ******* manual!"); responses.add ("Read the ******* manual!"); responses.add ("That's not a bug - it's a feature!"); responses.add ("That's not a bug - it's a feature!");... etc.... etc.} Generating Random Responses public Responder () { randomGenerator = new Random (); randomGenerator = new Random (); responses = new ArrayList (); responses = new ArrayList (); fillResponses (); fillResponses ();} Chapter05, tech-support2, Responder

22 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Generating Random Responses public String generateResponse () { int index = randomGenerator.nextInt (responses.size ()); return responses.get (index); int index = randomGenerator.nextInt (responses.size ()); return responses.get (index);} Chapter05, tech-support2, Responder returns a random number in the legal range for indexing responses public Responder () { randomGenerator = new Random (); randomGenerator = new Random (); responses = new ArrayList (); responses = new ArrayList (); fillResponses (); fillResponses ();} private void fillResponses () {......}

23 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review Java has an extensive class library Java has an extensive class library A good programmer must be familiar with the basic parts of the library... A good programmer must be familiar with the basic parts of the library...... and how to look up what you need.... and how to look up what you need. The documentation tells us what we need to know to use a class (its interface). The documentation tells us what we need to know to use a class (its interface).


Download ppt "More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality."

Similar presentations


Ads by Google