more sophisticated behavior using library classes to implement some more advanced functionality 4.0

19
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

Post on 21-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

More sophisticated behavior

Using library classes to implement some more advanced

functionality

4.0

Page 2: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

Main concepts to be covered

• Using library classes• Reading documentation

Page 3: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

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.

Page 4: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

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.

Page 5: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

A Technical Support System

• A textual dialog system• Idea based on ‘Eliza’ by Joseph

Weizenbaum (MIT, 1960s)• Explore…

Page 6: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

Main loop structure

boolean finished = false;

while(!finished) {

do something

if(exit condition) { finished = true; } else { do something more }}

Page 7: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

Main loop body

String input = reader.getInput();...String response = responder.generateResponse();

System.out.println(response);

Page 8: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

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?

Page 9: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

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

Page 10: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

10

Class documentation

The web browser displays three frames:

• top left - a list of packages• below - all classes in the Java

library• right - displays details of

selected packages or classesJava Classes

Page 11: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

Interface vs implementation

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

Page 12: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

12

String documentation• The documentation for string draws attention

to a variety of available methods:

Note: – the description– parameter list, and – the return type.

Page 13: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

Interface vs implementation

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

Page 14: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

14 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).

• They can then be used like classes from the current project.

Page 15: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

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.*;

Page 16: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

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

Page 17: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

Generating random responses

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

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

public void fillResponses(){ ...}

Page 18: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

18

Random -class methods

Page 19: More sophisticated behavior Using library classes to implement some more advanced functionality 4.0

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

Review

• Java has an extensive class library.• A good programmer must be familiar

with the library.• The documentation tells us what we

need to know to use a class (interface).