more sophisticated behaviour 1
Download
Skip this Video
Download Presentation
More Sophisticated Behaviour 1

Loading in 2 Seconds...

play fullscreen
1 / 23

More Sophisticated Behaviour 1 - PowerPoint PPT Presentation


  • 118 Views
  • Uploaded on

More Sophisticated Behaviour 1. Using library classes to implement more advanced functionality. Book Material. Chapter 5, sections 5.1 - 5.5. Main Concepts to be Covered. Using library classes Reading documentation. The Java Class Library. Thousands of classes Tens of thousands of methods

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' More Sophisticated Behaviour 1' - thomas-mclaughlin


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
more sophisticated behaviour 1
More Sophisticated Behaviour 1
  • Using library classes to implement more advanced functionality.
book material
Book Material
  • Chapter 5, sections 5.1 - 5.5
main concepts to be covered
Main Concepts to be Covered
  • Using library classes
  • Reading documentation
the java class library
The Java Class Library
  • Thousands of classes
  • Tens of thousands of methods
  • Many useful classes to save us work
  • A competent Java programmer must be able to work with the libraries.
working with the library

*

**

* Documentation, public contructor and method headers

** Private fields, contructors, methods and code bodies

Working with the Library
  • We should:
    • know some important classes by name
    • know how to find out about other classes
  • Remember:
    • We only need to work with the interface
    • We do not need the implementation
a technical support system
A Technical Support System
  • A textual dialog system
  • Idea based on ‘Eliza’ by Joseph Weizenbaum (MIT, 1960s)
  • Explore …

See Chapter05, tech-support projects

eliza project

InputReader

Responder

SupportSystem

‘Eliza’ Project

See Chapter05, tech-support projects

common loop structure
Common Loop Structure

boolean finished = false;while (!finished) {... do somethingif (we want to finish) {finished = true; } else {... do something more }}

eliza main loop
‘Eliza’ Main Loop

boolean finished = false;while (!finished) {String input = reader.getInput ();if (we want to finish) {finished = true; } else {... do something more }}

private InputReader reader;

Chapter05, tech-support1, SupportSystem.start()

eliza main loop1
‘Eliza’ Main Loop

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); }}

private InputReader reader;private Responder responder;

Chapter05, tech-support1, SupportSystem.start()

eliza main loop2
‘Eliza’ Main Loop

boolean finished = false;while (!finished) {String input = reader.getInput ();if (input.startsWith ("bye")) {finished = true; } else {String response = responder.generateResponse (); System.out.println (response); }}

private InputReader reader;private Responder responder;

Chapter05, tech-support1, SupportSystem.start()

eliza main loop3
‘Eliza’ Main Loop

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?
eliza main loop4
‘Eliza’ Main Loop

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?
  • Where does ‘startsWith’ come from?
  • It’s a method invoked on ‘input’ …
  • Therefore, it’s a method of ‘String’ …
reading class documentation
Reading Class Documentation
  • The built-in documentation of the Java libraries is in HTML format
  • Readable in any web browser
  • Class API: Application Programmers’ Interface
  • Interface description for all library classes
interface vs implementation

the interface of the class

Interface –vs– Implementation
  • The documentation includes:
    • the name of the class
    • a general description of the class
    • a list of public constructors and methods
    • parameters for constructors and methods
    • return values for non-voidmethods
    • the purpose of each constructor and method
interface vs implementation1

the implementation of the class

Interface –vs– Implementation
  • The documentation does not include:
    • private fields (most fields are private)
    • private constructors and methods
    • the bodies (source code) for each method and constructor
using library classes
Using Library Classes
  • 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.
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.*;
using the random class

returns a random integer

returns a random integer between 0 and 99 (inclusive)

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);

generating random responses
Generating Random Responses

public Responder ()

{

randomGenerator = new Random ();

responses = new ArrayList<String> ();

fillResponses ();

}

Chapter05, tech-support2, Responder

private void fillResponses ()

{

responses.add ("That sounds odd. Tell me more?"); responses.add ("No one else complained!");

responses.add ("Interesting. Tell me more?");

responses.add ("Do you have a dll conflict?");

responses.add ("Read the ******* manual!");

responses.add ("That\'s not a bug - it\'s a feature!");

... etc.

}

generating random responses1

returns a random number in the legal range for indexing responses

Generating Random Responses

public Responder ()

{

randomGenerator = new Random ();

responses = new ArrayList<String> ();

fillResponses ();

}

Chapter05, tech-support2, Responder

private void fillResponses ()

{

...

}

public String generateResponse ()

{

int index = randomGenerator.nextInt (responses.size ()); return responses.get (index);

}

review
Review
  • Java has an extensive class library
  • A good programmer must be familiar with the basic parts of the library ...
  • ... and how to look up what you need.
  • The documentation tells us what we need to know to use a class (its interface).
ad