More sophisticated behaviour Using library classes to implement - - PowerPoint PPT Presentation

more sophisticated behaviour
SMART_READER_LITE
LIVE PREVIEW

More sophisticated behaviour Using library classes to implement - - PowerPoint PPT Presentation

Objects First With Java A Practical Introduction Using BlueJ More sophisticated behaviour Using library classes to implement some more advanced functionality 2.0 Main concepts to be covered Using library classes More than just


slide-1
SLIDE 1

Objects First With Java A Practical Introduction Using BlueJ

More sophisticated behaviour

Using library classes to implement some more advanced functionality

2.0

slide-2
SLIDE 2

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

2

Main concepts to be covered

  • Using library classes

– More than just ArrayList …

  • Reading code documentation
  • Writing code documentation
slide-3
SLIDE 3

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

3

The Java class library

  • Thousands of classes
  • Tens of thousands of methods

– Many useful classes that make life much easier – Many you will probably never use …

  • A qualified Java programmer must be

able to work with the library.

– It’s just easier, more reliable, more economic, …

slide-4
SLIDE 4

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

4

Working with the library

You should:

  • know some important classes by name (such as

ArrayList);

  • know how to find out about other classes

(methods, parameters);

  • read the Java library’s documentation (available in

html). Remember:

  • We only need to know the interface, not the

implementation (hiding the details …).

slide-5
SLIDE 5

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

5

The big objective …

  • Prepare your own classes of “library

quality”.

  • Others can use them – just as they

use library classes.

  • That’s typical for real-life software

development (long-term, large-scale, big teams, …).

slide-6
SLIDE 6

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

6

Project: A Technical Support System

  • A textual dialog system

– Provide technical support for customers. – Online communication mimics real support. – That is: let’s cheat again!

  • Idea based on the AI project ‘Eliza’ by Joseph

Weizenbaum (MIT, 1960s)

  • classes: SupportSystem (main class),

InputReader, Responder.

  • In the following: SupportSystem.
  • (Explore in BlueJ …)
slide-7
SLIDE 7

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

7

Main loop structure

(method start in SupportSystem)

boolean finished = false; while(!finished) { do something read next input, e.g. if(exit condition) { everything processed, e.g. finished = true; } else { do something more generate response, e.g. } }

slide-8
SLIDE 8

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

8

Main loop body

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

from class InputReader from class Responder

  • read some input
  • ask responder to generate a response
  • print that response
slide-9
SLIDE 9

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

9

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?
  • What happens with “Bye” or “ bye”?
slide-10
SLIDE 10

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

10

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

slide-11
SLIDE 11

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

11

Interface vs. implementation

The documentation includes

  • the name of the class;
  • a general description of the class’s purpose;
  • a list of the class’s constructors and methods
  • return values (types, classes) and parameters for

each constructor and method

  • a description of the purpose of each constructor

and method the interface of the class (this is “abstraction in action!”)

slide-12
SLIDE 12

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

12

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

slide-13
SLIDE 13

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

13

Side note: String equality

if(input == "bye") { tests identity ... } if(input.equals("bye")) { tests equality ... }

  • Strings should (almost) always be compared with .equals

effect: do left- and right-hand side refer to the same object ? NOT: do they have the same value?

slide-14
SLIDE 14

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

14

Identity vs. equality 1

Other (non-String) objects: person1 == person2 ?

“Fred”

:Person

person1 person2 “Jill”

:Person

slide-15
SLIDE 15

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

15

Identity vs. equality 2

Other (non-String) objects: person1 == person2 ?

“Fred”

:Person

person1 person2 “Fred”

:Person

slide-16
SLIDE 16

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

16

Identity vs. equality 3

Other (non-String) objects: person1 == person2 ?

“Fred”

:Person

person1 person2 “Fred”

:Person

slide-17
SLIDE 17

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

17

Identity vs. equality (Strings)

"bye"

:String

input "bye"

:String String input = reader.getInput(); if(input == "bye") { ... }

== tests identity

== ?

 (may be) false!

slide-18
SLIDE 18

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

18

Identity vs. equality (Strings)

"bye"

:String

input "bye"

:String String input = reader.getInput(); if(input.equals("bye")) { ... }

equals tests equality

equals

?

 true!

slide-19
SLIDE 19

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

19

Using Random

  • The library class Random can be used to

generate (pseudo) random numbers

import java.util.Random; ... Random randomGenerator = new Random(); ... int index1 = randomGenerator.nextInt(); int index2 = randomGenerator.nextInt(100);

… over the whole range of integers … over a limited range of integers (here: 0..99) No need to create a new Random object any time you need a number - just call nextInt!

slide-20
SLIDE 20

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

20

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

… for the random numbers … for storing the possible responses … for creating some possible responses

slide-21
SLIDE 21

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

21

Maps

  • Maps are collections that contain a flexible

number of pairs of values.

  • Pairs consist of a key and a value.
  • Lookup works by supplying a key (instead
  • f an index) and retrieving a value.
  • Efficient implementation of put, get.
  • An example: a telephone book.
slide-22
SLIDE 22

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

22

Using maps

"Charles Nguyen"

:HashMap

"(531) 9392 4587" "Lisa Jones" "(402) 4536 4674" "William H. Smith" "(998) 5488 0123"

  • Particular implementation: HashMap
  • A map with Strings as keys and values
slide-23
SLIDE 23

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

23

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); most important methods of HashMap: put , get casting: any type is possible in a HashMap

slide-24
SLIDE 24

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

24

BlueJ -> example “tech-support- complete”

slide-25
SLIDE 25

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

25

Sets

  • Sets are collections that contain a

flexible number of objects.

  • Each individual element is stored at

most once.

  • Important methods: set, contains
  • A set does not maintain a specific
  • rder.
  • Lookup is done via an Iterator (no
  • rder, no index, no key).
slide-26
SLIDE 26

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

26

Using sets

import java.util.HashSet; import java.util.Iterator; ... HashSet mySet = new HashSet(); mySet.add("one"); mySet.add("two"); mySet.add("three"); mySet.add("one"); effect? Iterator it = mySet.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object }

Compare this to ArrayList code!

  • ne variation of a set: HashSet
slide-27
SLIDE 27

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

27

Collections so far

  • Lists
  • Maps
  • Sets
  • What is different, what is equivalent?
slide-28
SLIDE 28

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

28

Writing class documentation

  • Your own classes should be documented

the same way library classes are.

  • Other people should be able to use your

class without reading the implementation.

  • Reading the documentation must take less

time than reading the complete code does!

  • Crucial: how detailed??
  • Make your class a 'library class'!
slide-29
SLIDE 29

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

29

Elements of documentation

Documentation for a class should include:

  • the class name
  • a comment describing the overall purpose and

characteristics of the class

  • a version number (with date)
  • the authors’ names
  • documentation for each constructor and each

method

  • Rationale: why doing sth this way, what other

ways have been tested/dropped (why?)

slide-30
SLIDE 30

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

30

Elements of documentation

Documentation for each constructor and method should include:

  • the name of the method
  • the return type
  • the parameter names and types
  • a description of the purpose and function of the

method

  • a description of each parameter
  • a description of the value returned
  • Rationale: why doing sth this way, what other

ways have been tested/dropped (why?)

slide-31
SLIDE 31

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

31

Javadoc – the Java documentation generator

Class comment:

/** * The Responder class represents a response * generator object. It is used to generate an * automatic response. * * @author Michael Kölling and David J. Barnes * @version 1.0 (1.Feb.2002) */

javadoc key symbols

slide-32
SLIDE 32

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

32

javadoc

Method comment:

/** * Read a line of text from standard input (the text * terminal), and return it as a set of words. * * @param prompt A prompt to print to screen. * @return A set of Strings, where each String is * one of the words typed by the user */ public HashSet getInput(String prompt) { ... }

slide-33
SLIDE 33

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

33

Creating Java documentation

  • Command Line

– command javadoc

  • In BlueJ

– Use Tools -> Project Documentation

slide-34
SLIDE 34

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

34

Public vs. private

  • Remember access modifiers; they define

the visibility of fields, methods, etc.

  • Public attributes (fields, constructors,

methods) are accessible to other classes.

  • Fields should not be public.
  • Private attributes are accessible only within

the same class.

  • Only methods that are intended for other

classes should be public.

slide-35
SLIDE 35

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

35

Information hiding

  • Data belonging to one object is hidden from
  • ther objects.
  • Know what an object can do, not how it

does it.

  • Information hiding increases the level of

independence.

  • Independence of modules is important for

large systems and maintenance.

  • Things one doesn’t need to know vs. things
  • ne even should not know (side effects!).
slide-36
SLIDE 36

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

36

Class variables

slide-37
SLIDE 37

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

37

Constants

private static final int gravity = 3;

  • private: access modifier, as usual
  • static: class variable

– one (shared) variable (copy) for all instances

  • final:

– initialised only once:

  • when declared (as above) or
  • in the constructor

– value won’t change throughout the execution of an application – similar to a constant

slide-38
SLIDE 38

Objects First with Java - A Practical Introduction using BlueJ, David J. Barnes, Michael Kölling; extensions by HJB, TN and MR

38

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

  • The implementation is hidden (information

hiding).

  • We document our classes so that the

interface can be read on its own (class comment, method comments).