More sophisticated behavior

Slides:



Advertisements
Similar presentations
More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
More sophisticated behaviour Using library classes to implement some more advanced functionality.
Copyright by Scott GrissomCh 5 Sophisticated Behavior Slide 1 Java Class Library a very large collection of classes for you to use as needed the challenge.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
More Sophisticated Behaviour 2 Using library classes to implement more advanced functionality. Also … class ( static ) fields.
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Grouping Objects 1 Introduction to Collections.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
More sophisticated behavior Using library classes to implement some more advanced functionality.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Java Packages and Libraries M Taimoor Khan
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 5.0.
Grouping objects Introduction to collections 5.0.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Page 1 – Autumn 2009Steffen Vissing Andersen SDJ I1, Autumn 2009 Agenda: Java API Documentation Code Documenting (in javadoc format) Debugging.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 5.0.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
CS1054: Lecture 11 More Sophisticated Behavior (contd..)
1 COS 260 DAY 9 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz –Good results ->All A’s –Threw out question on name overloading 4 th Mini quiz next class.
Programming Fundamentals 2: Libraries/ F II Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
OOP (Java): Libraries/ OOP (Java) Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random Semester.
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
More-Sophisticated Behavior Using library classes to implement some more advanced functionality 6.0.
5 More sophisticated behavior
BlueJ Chapter 5 More sophisticated behavior: Library classes and documentation.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
More-Sophisticated Behavior
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Object-Oriented Concepts
More Sophisticated Behavior
Objects First with Java A Practical Introduction using BlueJ
Libraries CITS1001.
COS 260 DAY 7 Tony Gauvin.
Functional Processing of Collections (Advanced)
Objects First with Java A Practical Introduction using BlueJ
COS 260 DAY 9 Tony Gauvin.
Objects First with Java Introduction to collections
Understanding class definitions
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Objects First with Java Introduction to collections
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
Collections and iterators
F II 6. Using Libraries Objectives
Objects First with Java A Practical Introduction using BlueJ
Classes CS 21a: Introduction to Computing I
Chap 1. Getting Started Objectives
Chap 4. Programming Fundamentals
Collections and iterators
Presentation transcript:

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

Main concepts to be covered Using library classes Reading documentation Writing documentation 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. 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. 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… 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 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); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

The exit condition Where does ‘startsWith’ come from? 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? 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 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 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 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. 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.*; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Side note: String equality SW08 - Object-Oriented Programming and Principles Side note: String equality if(input == "bye") { tests identity ... } if(input.equals("bye")) { tests equality Strings should (almost) always be compared with .equals if desired, include more detailed discussion of identity vs equality here. (That's the next 5 slides, up to "Identity vs equality (Strings)". Skip these if this is not needed now. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © M. Kölling, University of Southern Denmark

SW08 - Object-Oriented Programming and Principles Identity vs equality 1 Other (non-String) objects: :Person :Person “Fred” “Jill” == is not true here (of course) person1 person2 person1 == person2 ? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © M. Kölling, University of Southern Denmark

SW08 - Object-Oriented Programming and Principles Identity vs equality 2 Other (non-String) objects: :Person :Person “Fred” “Fred” == is still not true here (different objects, == tests identity) person1 person2 person1 == person2 ? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © M. Kölling, University of Southern Denmark

SW08 - Object-Oriented Programming and Principles Identity vs equality 3 Other (non-String) objects: :Person :Person “Fred” “Fred” == is true now (same object) person1 person2 person1 == person2 ? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © M. Kölling, University of Southern Denmark

Identity vs equality (Strings) SW08 - Object-Oriented Programming and Principles Identity vs equality (Strings) String input = reader.getInput(); if(input == "bye") { ... } == tests identity :String :String == ? "bye" "bye" == is still not true here (different objects, == tests identity) input  (may be) false! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © M. Kölling, University of Southern Denmark

Identity vs equality (Strings) SW08 - Object-Oriented Programming and Principles Identity vs equality (Strings) String input = reader.getInput(); if(input.equals("bye")) { ... } equals tests equality :String :String ? equals "bye" "bye" == is still not true here (different objects, == tests identity) input  true! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © M. Kölling, University of Southern Denmark

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); 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(); fillResponses(); } public String generateResponse() int index = randomGenerator.nextInt(responses.size()); return (String) responses.get(index); public void fillResponses() ... Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Using maps A map with Strings as keys and values :HashMap "Charles Nguyen" "(531) 9392 4587" "Lisa Jones" "(402) 4536 4674" "William H. Smith" "(998) 5488 0123" Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Using sets Compare this to ArrayList code! 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! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Tokenizing 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; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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'! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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) */ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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) { ... } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Public vs private 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

Constants private: access modifier, as usual static: class variable private static final int gravity = 3; private: access modifier, as usual static: class variable final: constant 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). The implementation is hidden (information hiding). We document our classes so that the interface can be read on its own (class comment, method comments). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling