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

Slides:



Advertisements
Similar presentations
Understanding class definitions Looking inside classes 5.0.
Advertisements

More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
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.
Grouping Objects 3 Iterators, collections and the while loop.
Objects First with Java A Practical Introduction using BlueJ
Using the Java API
Java Library Java provides a huge library or collection of useful programs A gold mine of well-tested code that can save you countless hours of development.
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.
29-Jun-15 Using the Java API
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.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
University of Limerick1 Work with API’s. University of Limerick2 Learning OO programming u Learning a programming language can be broadly split into two.
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
Week1 Using the Library (the Java API) Classes are grouped together in packages –To use a class you have to know which package it is in –Every package.
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?
COS 260 DAY 5 Tony Gauvin.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
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(…)
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
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
More Sophisticated Behavior
Libraries CITS1001.
More sophisticated behavior
Understanding class definitions
Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Understanding class definitions
Collections and iterators
Creating cooperating objects
Objects First with Java Creating cooperating objects
COS 260 DAY 4 Tony Gauvin.
Objects First with Java Creating cooperating objects
Objects First with Java A Practical Introduction using BlueJ
Collections and iterators
Presentation transcript:

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

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.

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.

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…

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 }

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

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?

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

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 classes Java Classes

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

12 String documentation The documentation for string draws attention to a variety of available methods: Note: – the description –parameter list, and –the return type.

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

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.

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

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

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 (); fillResponses(); } public String generateResponse() { int index = randomGenerator.nextInt(responses.size()); return responses.get(index); } public void fillResponses() {... }

18 Random -class methods

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