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

Slides:



Advertisements
Similar presentations
Using Java without BlueJ BlueJ projects A BlueJ project is stored in a directory on disk. A BlueJ package is stored in several different files.
Advertisements

Fields, Constructors, Methods
Lab 10: Bank Account II Transaction List, error checking & aggregation.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
More sophisticated behaviour Using library classes to implement some more advanced functionality.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
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.
Objects First with Java A Practical Introduction using BlueJ
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Object interaction Creating cooperating objects 3.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.
Object Interaction 1 Creating cooperating objects.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Object interaction Creating cooperating objects 5.0.
Object interaction Creating cooperating objects 5.0.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
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.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
JAVA LIBRARY CLASSES CITS Main concepts to be covered Using library classes: String, Math, Color Reading documentation Java 7 API is available.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Object Oriented Design: Identifying Objects
Java Packages and Libraries M Taimoor Khan
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
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.
COS 260 DAY 5 Tony Gauvin.
Understanding class definitions
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
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.
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.
Grouping objects Introduction to collections 5.0.
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.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
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.
More-Sophisticated Behavior
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
More Sophisticated Behavior
Objects First with Java Introduction to collections
Libraries CITS1001.
More sophisticated behavior
Understanding class definitions
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
Collections and iterators
Objects First with Java Creating cooperating objects
Objects First with Java Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Collections and iterators
Presentation transcript:

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

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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main Concepts to be Covered Using library classes Using library classes Reading documentation Reading documentation

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling The Java Class Library Thousands of classes Thousands of classes Tens of thousands of methods Tens of thousands of methods Many useful classes to save us work Many useful classes to save us work A competent Java programmer must be able to work with the libraries. 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 We should: We should: –know some important classes by name –know how to find out about other classes Remember: Remember: –We only need to work with the interface –We do not need the implementation * * Documentation, public contructor and method headers ** ** Private fields, contructors, methods and code bodies

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling A Technical Support System A textual dialog system A textual dialog system Idea based on Eliza by Joseph Weizenbaum (MIT, 1960s) Idea based on Eliza by Joseph Weizenbaum (MIT, 1960s) Explore … Explore … See Chapter05, tech-support projects

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Eliza ProjectInputReaderResponder SupportSystem See Chapter05, tech-support projects

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Common Loop Structure boolean finished = false; while (!finished) {... do something if (we want to finish) { finished = true; } else {... do something more } }

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Eliza Main Loop boolean finished = false; while (!finished) { String input = reader.getInput (); if (we want to finish) { finished = true; } else {... do something more } } Chapter05, tech-support1, SupportSystem.start() private InputReader reader;

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling boolean finished = false; while (!finished) { String input = reader.getInput (); if (we want to finish) { finished = true; } else { String response = responder.generateResponse (); System.out.println (response); } } Chapter05, tech-support1, SupportSystem.start() private InputReader reader; private Responder responder; Eliza Main Loop

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling boolean finished = false; while (!finished) { String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } else { String response = responder.generateResponse (); System.out.println (response); } } Chapter05, tech-support1, SupportSystem.start() private InputReader reader; private Responder responder; Eliza Main Loop

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } Where does startsWith come from? Where does startsWith 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 startsWith come from? Where does startsWith 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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Where does startsWith come from? Where does startsWith 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 startsWith come from? Where does startsWith 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? if () { finished = true; } String input = reader.getInput (); if (input.startsWith ("bye")) { finished = true; } Where does startsWith come from? Where does startsWith come from? Its a method invoked on input … Its a method invoked on input … Therefore, its a method of String … Therefore, its a method of String … Where does startsWith come from? Where does startsWith come from? Its a method invoked on input … Its a method invoked on input … Therefore, its a method of String … Therefore, its a method of String … Eliza Main Loop

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 libraries is in HTML format The built-in documentation of the Java libraries is in HTML format Readable in any web browser Readable in any web browser Class API: Application Programmers Interface Class API: Application Programmers Interface Interface description for all library classes 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 documentation includes : the name of the class the name of the class a general description of the class a general description of the class a list of public constructors and methods a list of public constructors and methods parameters for constructors and methods parameters for constructors and methods return values for non- void methods return values for non- void methods the purpose of each constructor and method the purpose of each constructor and method the interface of the class the interface of the class

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling the implementation of the class the implementation of the class The documentation does not include: The documentation does not include: private fields (most fields are private) private fields (most fields are private) private constructors and methods private constructors and methods the bodies (source code) for each method and constructor the bodies (source code) for each method and constructor Interface –vs– Implementation

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). Classes from the library must be imported using an import statement (except classes from java.lang). Then, they can be used like classes from the current project. Then, they can 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. Classes are organised in packages. Single classes may be imported: import java.util.ArrayList; Single classes may be imported: import java.util.ArrayList; Whole packages can be imported: import java.util.*; Whole packages can be imported: import java.util.*;

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;... Random randomGenerator = new Random ();... int index1 = randomGenerator.nextInt (); int index2 = randomGenerator.nextInt (100); returns a random integer between 0 and 99 (inclusive) returns a random integer

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling private void fillResponses () { responses.add ("That sounds odd. Tell me more?"); responses.add ("No one else complained!"); responses.add ("That sounds odd. Tell me more?"); 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 feature!"); responses.add ("That's not a bug - it's a feature!");... etc.... etc.} Generating Random Responses public Responder () { randomGenerator = new Random (); randomGenerator = new Random (); responses = new ArrayList (); responses = new ArrayList (); fillResponses (); fillResponses ();} Chapter05, tech-support2, Responder

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Generating Random Responses public String generateResponse () { int index = randomGenerator.nextInt (responses.size ()); return responses.get (index); int index = randomGenerator.nextInt (responses.size ()); return responses.get (index);} Chapter05, tech-support2, Responder returns a random number in the legal range for indexing responses public Responder () { randomGenerator = new Random (); randomGenerator = new Random (); responses = new ArrayList (); responses = new ArrayList (); fillResponses (); fillResponses ();} private void fillResponses () {......}

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Review Java has an extensive class library Java has an extensive class library A good programmer must be familiar with the basic parts of the library... A good programmer must be familiar 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 need to know to use a class (its interface). The documentation tells us what we need to know to use a class (its interface).