More sophisticated behavior behavior More sophisticated Using - - PowerPoint PPT Presentation

more sophisticated behavior behavior more sophisticated
SMART_READER_LITE
LIVE PREVIEW

More sophisticated behavior behavior More sophisticated Using - - PowerPoint PPT Presentation

More sophisticated behavior behavior More sophisticated Using library classes to implement some more advanced functionality Main concepts to be covered Main concepts to be covered Using library classes Reading documentation


slide-1
SLIDE 1

More sophisticated More sophisticated behavior behavior

Using library classes to implement some more advanced functionality

slide-2
SLIDE 2

03/11/2005 Lecture 5: More Sophisticated Behavior 2

Main concepts to be covered Main concepts to be covered

  • Using library classes
  • Reading documentation
  • Writing documentation
slide-3
SLIDE 3

03/11/2005 Lecture 5: More Sophisticated Behavior 3

The Java class library 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.

slide-4
SLIDE 4

03/11/2005 Lecture 5: More Sophisticated Behavior 4

Working with the library 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.

slide-5
SLIDE 5

03/11/2005 Lecture 5: More Sophisticated Behavior 5

A Technical Support System A Technical Support System

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

Weizenbaum (MIT, 1960s)

  • This is a system that when entering

questions will try to answer them in an “intelligent” way.

slide-6
SLIDE 6

03/11/2005 Lecture 5: More Sophisticated Behavior 6

A Technical Support System A Technical Support System

slide-7
SLIDE 7

03/11/2005 Lecture 5: More Sophisticated Behavior 7

A Technical Support System A Technical Support System

slide-8
SLIDE 8

03/11/2005 Lecture 5: More Sophisticated Behavior 8

Main loop structure Main loop structure

// from the start method in SupportSystem boolean finished = false; while(!finished) {// sentinel controlled loop do something if(exit condition) { finished = true; } else { do something more } }

slide-9
SLIDE 9

03/11/2005 Lecture 5: More Sophisticated Behavior 9

Main loop body Main loop body

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

slide-10
SLIDE 10

03/11/2005 Lecture 5: More Sophisticated Behavior 10

The exit condition 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?
slide-11
SLIDE 11

03/11/2005 Lecture 5: More Sophisticated Behavior 11

String Info String Info

slide-12
SLIDE 12

03/11/2005 Lecture 5: More Sophisticated Behavior 12

Reading class documentation 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
  • http://java.sun.com/j2se/1.3/docs/api/
slide-13
SLIDE 13

03/11/2005 Lecture 5: More Sophisticated Behavior 13

Interface Interface vs vs implementation 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

slide-14
SLIDE 14

03/11/2005 Lecture 5: More Sophisticated Behavior 14

Interface Interface vs vs implementation 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-15
SLIDE 15

03/11/2005 Lecture 5: More Sophisticated Behavior 15

Interface Interface vs vs implementation implementation

  • The interface of a method consists of
  • signature
  • access modifier
  • return type
  • method name
  • a list of parameters
  • comment
  • discussion of all signature items
  • purpose of method
slide-16
SLIDE 16

03/11/2005 Lecture 5: More Sophisticated Behavior 16

Using library classes 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.

slide-17
SLIDE 17

03/11/2005 Lecture 5: More Sophisticated Behavior 17

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

slide-18
SLIDE 18

03/11/2005 Lecture 5: More Sophisticated Behavior 18

Side Note 1: Strings Side Note 1: Strings

  • Strings are immutable objects
  • immutable objects cannot change content or

state once they have been created

String input = reader.getInput(); input = input.trim; if(input.startsWith(“bye”)){ finished = true; } else { … Code omitted }

slide-19
SLIDE 19

03/11/2005 Lecture 5: More Sophisticated Behavior 19

Side Note 1: Side Note 1: StringBuffer StringBuffer

  • A string buffer implements a mutable

sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

slide-20
SLIDE 20

03/11/2005 Lecture 5: More Sophisticated Behavior 20

Side note Side note 2 2: String equality : String equality

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

  • Strings should (almost) always be compared with .equals
slide-21
SLIDE 21

03/11/2005 Lecture 5: More Sophisticated Behavior 21

Identity Identity vs vs equality 1 equality 1

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

“Fred”

:Person

person1 person2 “Jill”

:Person

slide-22
SLIDE 22

03/11/2005 Lecture 5: More Sophisticated Behavior 22

Identity Identity vs vs equality 2 equality 2

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

“Fred”

:Person

person1 person2 “Fred”

:Person

slide-23
SLIDE 23

03/11/2005 Lecture 5: More Sophisticated Behavior 23

Identity Identity vs vs equality 3 equality 3

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

“Fred”

:Person

person1 person2 “Fred”

:Person

slide-24
SLIDE 24

03/11/2005 Lecture 5: More Sophisticated Behavior 24

Identity Identity vs vs equality (Strings) equality (Strings)

"bye"

:String

input "bye"

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

== tests identity

== ?

➠ (may be) false!

slide-25
SLIDE 25

03/11/2005 Lecture 5: More Sophisticated Behavior 25

Identity Identity vs vs equality (Strings) equality (Strings)

"bye"

:String

input "bye"

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

equals tests equality

equals

?

➠ true!

slide-26
SLIDE 26

03/11/2005 Lecture 5: More Sophisticated Behavior 26

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

slide-27
SLIDE 27

03/11/2005 Lecture 5: More Sophisticated Behavior 27

Generating random responses 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() ...

slide-28
SLIDE 28

03/11/2005 Lecture 5: More Sophisticated Behavior 28

Maps Maps

  • Maps are collections that contain pairs of

values.

  • Pairs consist of a key and a value.
  • Lookup works by supplying a key, and

retrieving a value.

  • An example: a telephone book.
slide-29
SLIDE 29

03/11/2005 Lecture 5: More Sophisticated Behavior 29

Using maps Using maps

  • A map with Strings as keys and values

"Charles Nguyen"

:HashMap

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

slide-30
SLIDE 30

03/11/2005 Lecture 5: More Sophisticated Behavior 30

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

slide-31
SLIDE 31

03/11/2005 Lecture 5: More Sophisticated Behavior 31

Maps in Maps in TechSupport TechSupport

public class Responder { private HashMap responseMap; … Code Omitted public String generateResponse(String word) { String response = (String) responseMap.get(word); if(response != null) { return response; } else { return pickDefaultResponse(); } }

slide-32
SLIDE 32

03/11/2005 Lecture 5: More Sophisticated Behavior 32

Using sets Using sets

import java.util.HashSet; import java.util.Iterator; ... HashSet mySet = new HashSet(); mySet.add("one"); mySet.add("two"); mySet.add("three"); 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!

slide-33
SLIDE 33

03/11/2005 Lecture 5: More Sophisticated Behavior 33

Sets and List Sets and List

  • A Set is a collection that stores each

individual element at most once. It does not maintain any specific order.

  • A List is a collection that stores all

elements it is been giving regardless of

  • duplication. It maintains a order at all times.
slide-34
SLIDE 34

03/11/2005 Lecture 5: More Sophisticated Behavior 34

Tokenizing Tokenizing Strings Strings

public HashSet getInput() { System.out.print("> "); String inputLine = readInputLine().trim().toLowerCase(); StringTokenizer tokenizer = new StringTokenizer(inputLine); HashSet words = new HashSet(); while(tokenizer.hasMoreTokens()) { words.add(tokenizer.nextToken()); } return words; }

slide-35
SLIDE 35

03/11/2005 Lecture 5: More Sophisticated Behavior 35

Writing class documentation 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.

  • Make your class a 'library class'!
slide-36
SLIDE 36

03/11/2005 Lecture 5: More Sophisticated Behavior 36

Elements of documentation 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
  • the authors’ names
  • documentation for each constructor and

each method

slide-37
SLIDE 37

03/11/2005 Lecture 5: More Sophisticated Behavior 37

javadoc javadoc

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) */

slide-38
SLIDE 38

03/11/2005 Lecture 5: More Sophisticated Behavior 38

Elements of documentation Elements of documentation

The 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
slide-39
SLIDE 39

03/11/2005 Lecture 5: More Sophisticated Behavior 39

javadoc 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-40
SLIDE 40

03/11/2005 Lecture 5: More Sophisticated Behavior 40

Public Public vs vs private private

  • Public attributes (fields, constructors,

methods) are accessible to other classes.

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

the same class.

  • Only methods that are intended for other

classes should be public.

slide-41
SLIDE 41

03/11/2005 Lecture 5: More Sophisticated Behavior 41

Information hiding Information hiding

  • Data belonging to one object is hidden

from other 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.

slide-42
SLIDE 42

03/11/2005 Lecture 5: More Sophisticated Behavior 42

The Balls Project The Balls Project

slide-43
SLIDE 43

03/11/2005 Lecture 5: More Sophisticated Behavior 43

Class and Object Level Class and Object Level

  • So far we have created classes for the sake
  • f objects which should have each a state of

their own.

  • Sometimes, objects should be able to share

common properties

  • For People this could be the gravitational pull
  • For Employees of the same company this could be

the company’s details

slide-44
SLIDE 44

03/11/2005 Lecture 5: More Sophisticated Behavior 44

Class and Object Level Class and Object Level

  • Instead of having these details stored in each
  • bject it would be better to store this at one central

point:

  • THE CLASS
  • Classes themselves can also have state
  • class variables or static variables
  • Exactly one copy exists of a class variable at all times,

independent of the number of created instances

  • The key word static is Java’s syntax to define class

variables

  • methods at object level can access static variables
slide-45
SLIDE 45

03/11/2005 Lecture 5: More Sophisticated Behavior 45

Class and Object Level Class and Object Level

public class BouncingBall { private static final int gravity = 3; // effect of gravity private int ballDegradation = 2; private Ellipse2D.Double circle; private Color color; private int diameter; private int xPosition; private int yPosition; private final int groundPosition; // y position of ground private Canvas canvas; private int ySpeed = 1;

slide-46
SLIDE 46

03/11/2005 Lecture 5: More Sophisticated Behavior 46

Class variables Class variables

slide-47
SLIDE 47

03/11/2005 Lecture 5: More Sophisticated Behavior 47

Constants Constants

  • Variables can change their value by means of

assignment.

  • Sometimes, when programming you need a

mechanism that can guarantee you that the value does not change at all

  • Constants provide you a way in doing so
  • similar to maths or physics
  • pi, i, …
  • The key word final is Java’s syntax to express constants
  • Once given a value, initialized, they can change value
  • Trying to do so will result in a syntax error.
slide-48
SLIDE 48

03/11/2005 Lecture 5: More Sophisticated Behavior 48

Constants Constants

private static final int gravity = 3;

  • private: access modifier, as usual
  • static: class variable
  • final: constant
slide-49
SLIDE 49

03/11/2005 Lecture 5: More Sophisticated Behavior 49

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

slide-50
SLIDE 50

03/11/2005 Lecture 5: More Sophisticated Behavior 50

Concepts Concepts

  • Interface
  • class level
  • static variables
  • class variables
  • object level
  • constants
  • Signature
  • javadoc
  • documentation
  • final
  • static