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

More Sophisticated Behaviour 1

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: More Sophisticated Behaviour 1

More Sophisticated Behaviour 1

Using library classes to implement Using library classes to implement more advanced functionality.more advanced functionality.

Page 2: More Sophisticated Behaviour 1
Page 3: More Sophisticated Behaviour 1

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

Page 4: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Main Concepts to be Covered

• Using library classesUsing library classes

• Reading documentationReading documentation

Page 5: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

The Java Class Library

• Thousands of classesThousands of classes

• Tens of thousands of methodsTens of thousands of methods

• Many useful classes to save us workMany useful classes to save us work

• A competent Java programmer must A competent Java programmer must be able to work with the libraries.be able to work with the libraries.

Page 6: More Sophisticated Behaviour 1

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 nameknow some important classes by name– know how to find out about other classesknow how to find out about other classes

• Remember:Remember:– We only need to work with the We only need to work with the interfaceinterface– We do not need the We do not need the implementationimplementation

**

* Documentation, public contructor and method headers* Documentation, public contructor and method headers* Documentation, public contructor and method headers* Documentation, public contructor and method headers

****

** Private fields, contructors, methods and code bodies** Private fields, contructors, methods and code bodies** Private fields, contructors, methods and code bodies** Private fields, contructors, methods and code bodies

Page 7: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

A Technical Support System

• A textual dialog systemA textual dialog system

• Idea based on Idea based on ‘Eliza’‘Eliza’ by Joseph by Joseph Weizenbaum (MIT, 1960s)Weizenbaum (MIT, 1960s)

• Explore …Explore …

See Chapter05, tech-support projectsSee Chapter05, tech-support projectsSee Chapter05, tech-support projectsSee Chapter05, tech-support projects

Page 8: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

‘Eliza’ Project

InputReaderInputReader ResponderResponder

SupportSystemSupportSystem

See Chapter05, tech-support projectsSee Chapter05, tech-support projectsSee Chapter05, tech-support projectsSee Chapter05, tech-support projects

Page 9: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Common Loop Structureboolean finished = false;boolean finished = false;

while while (!finished(!finished) {) {

... ... do somethingdo something

if (if (we want to finishwe want to finish) {) { finished = true;finished = true; } } else { else { ... ... do something moredo something more } }

}}

Page 10: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

‘Eliza’ Main Loopboolean finished = false;boolean finished = false;

while while (!finished(!finished) {) {

String input = reader.getInput ();String input = reader.getInput ();

if (if (we want to finishwe want to finish) {) { finished = true;finished = true; } } else { else { ... ... do something moredo something more } }

}}

Chapter05, tech-support1, Chapter05, tech-support1, SupportSystem.start()SupportSystem.start()Chapter05, tech-support1, Chapter05, tech-support1, SupportSystem.start()SupportSystem.start()

private InputReader reader;private InputReader reader;private InputReader reader;private InputReader reader;

Page 11: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

boolean finished = false;boolean finished = false;

while while (!finished(!finished) {) {

String input = reader.getInput ();String input = reader.getInput ();

if (if (we want to finishwe want to finish) {) { finished = true;finished = true; } } else { else { String response =String response = responder.generateResponse (); responder.generateResponse (); System.out.println (response); System.out.println (response); } }

}}Chapter05, tech-support1, Chapter05, tech-support1, SupportSystem.start()SupportSystem.start()Chapter05, tech-support1, Chapter05, tech-support1, SupportSystem.start()SupportSystem.start()

private InputReader reader;private InputReader reader;private Responder responder;private Responder responder;private InputReader reader;private InputReader reader;private Responder responder;private Responder responder;

‘Eliza’ Main Loop

Page 12: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

boolean finished = false;boolean finished = false;

while while (!finished(!finished) {) {

String input = reader.getInput ();String input = reader.getInput ();

if (if (input.startsWith ("bye")input.startsWith ("bye")) {) { finished = true;finished = true; } } else { else { String response =String response = responder.generateResponse (); responder.generateResponse (); System.out.println (response); System.out.println (response); } }

}}Chapter05, tech-support1, Chapter05, tech-support1, SupportSystem.start()SupportSystem.start()Chapter05, tech-support1, Chapter05, tech-support1, SupportSystem.start()SupportSystem.start()

private InputReader reader;private InputReader reader;private Responder responder;private Responder responder;private InputReader reader;private InputReader reader;private Responder responder;private Responder responder;

‘Eliza’ Main Loop

Page 13: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

String input = reader.getInput ();String input = reader.getInput ();

if (if (input.startsWith ("bye")input.startsWith ("bye")) {) { finished = true;finished = true; } }

• Where does ‘Where does ‘startsWithstartsWith’ come from?’ 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 ‘Where does ‘startsWithstartsWith’ come from?’ 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

Page 14: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

• Where does ‘Where does ‘startsWithstartsWith’ come from?’ 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 ‘Where does ‘startsWithstartsWith’ come from?’ 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?

String input = reader.getInput ();

if (if (input.startsWith ("bye")) {) { finished = true;finished = true; } }

• Where does ‘Where does ‘startsWithstartsWith’ come from?’ come from?

• It’s a method invoked on ‘It’s a method invoked on ‘inputinput’ …’ …

• Therefore, it’s a method of ‘Therefore, it’s a method of ‘StringString’ …’ …

• Where does ‘Where does ‘startsWithstartsWith’ come from?’ come from?

• It’s a method invoked on ‘It’s a method invoked on ‘inputinput’ …’ …

• Therefore, it’s a method of ‘Therefore, it’s a method of ‘StringString’ …’ …

‘Eliza’ Main Loop

Page 15: More Sophisticated Behaviour 1

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 The built-in documentation of the Java libraries is in HTML formatlibraries is in HTML format

• Readable in any web browserReadable in any web browser

• Class API: Class API: Application Programmers’ Application Programmers’ InterfaceInterface

• InterfaceInterface description for all library description for all library classesclasses

Page 16: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Interface –vs– Implementation

• The documentation includesThe documentation includes::

─ the name of the classthe name of the class

─ a general description of the classa general description of the class

─ a list of a list of publicpublic constructors and constructors and methodsmethods

─ parameters for constructors and parameters for constructors and methodsmethods

─ return values for return values for non-non-voidvoid methodsmethods

─ the purpose of each constructor and the purpose of each constructor and methodmethod

the the interfaceinterface of the class of the class the the interfaceinterface of the class of the class

Page 17: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

the the implementationimplementation of the class of the class the the implementationimplementation of the class of the class

• The documentation The documentation does notdoes not include: include:

─ privateprivate fields (most fields are fields (most fields are private)private)

─ privateprivate constructors and methods constructors and methods

─ the bodies (source code) for each the bodies (source code) for each method method and constructor and constructor

Interface –vs– Implementation

Page 18: More Sophisticated Behaviour 1

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 Classes from the library must be imported using an using an importimport statement (except classes statement (except classes from from java.langjava.lang).).

• Then, they can be used like classes from Then, they can be used like classes from the current project.the current project.

Page 19: More Sophisticated Behaviour 1

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:Single classes may be imported:

import java.util.ArrayList;import java.util.ArrayList;

• Whole packages can be imported:Whole packages can be imported:

import java.util.*;import java.util.*;

Page 20: More Sophisticated Behaviour 1

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;import java.util.Random;

......

Random randomGenerator = new Random ();Random randomGenerator = new Random ();

......

int index1 = randomGenerator.nextInt ();int index1 = randomGenerator.nextInt ();int index2 = randomGenerator.nextInt (100);int index2 = randomGenerator.nextInt (100);

returns a random integer between 0 and 99 (inclusive)returns a random integer between 0 and 99 (inclusive)

returns a random integerreturns a random integer

Page 21: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

private void fillResponses ()private void fillResponses (){{ responses.add ("That sounds odd. Tell me more?");responses.add ("That sounds odd. Tell me more?"); responses.add ("No one else complained!"); 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 responses.add ("That's not a bug - it's a feature!");feature!"); ... etc.... etc.}}

Generating Random Responses

public Responder ()public Responder (){{ randomGenerator = new Random ();randomGenerator = new Random (); responses = new ArrayList<String> ();responses = new ArrayList<String> (); fillResponses ();fillResponses ();}}

Chapter05, tech-support2, Chapter05, tech-support2, ResponderResponderChapter05, tech-support2, Chapter05, tech-support2, ResponderResponder

Page 22: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Generating Random Responses

public String generateResponse ()public String generateResponse (){{ int index =int index = randomGenerator.nextInt (responses.size ()); randomGenerator.nextInt (responses.size ()); return responses.get (index); return responses.get (index);}}

Chapter05, tech-support2, Chapter05, tech-support2, ResponderResponderChapter05, tech-support2, Chapter05, tech-support2, ResponderResponder

returns a random returns a random number in the legal number in the legal range for indexing range for indexing responsesresponses

public Responder ()public Responder (){{ randomGenerator = new Random ();randomGenerator = new Random (); responses = new ArrayList<String> ();responses = new ArrayList<String> (); fillResponses ();fillResponses ();}}

private void fillResponses ()private void fillResponses (){{ ......}}

Page 23: More Sophisticated Behaviour 1

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Review

• Java has an extensive class libraryJava has an extensive class library

• A good programmer must be familiar A good programmer must be familiar with the basic parts of the library ...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 The documentation tells us what we need to know to use a class need to know to use a class (its (its interface)interface)..