Libraries CITS1001.

Slides:



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

Fields, Constructors, Methods
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
More sophisticated behaviour Using library classes to implement some more advanced functionality.
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.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
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.
Understanding class definitions Looking inside classes.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
More sophisticated behavior Using library classes to implement some more advanced functionality.
JAVA LIBRARY CLASSES CITS Main concepts to be covered Using library classes: String, Math, Color Reading documentation Java 7 API is available.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Java Packages and Libraries M Taimoor Khan
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?
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.
Understanding class definitions
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
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.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Grouping objects Introduction to collections 5.0.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
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.
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(…)
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
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
Using the Java Collection Libraries COMP 103 # T2
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
Methods Chapter 6.
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Week 6 Discussion Word Cloud.
More sophisticated behavior
Understanding class definitions
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
Applying OO Concepts Using Java
Understanding class definitions
Collections and iterators
F II 6. Using Libraries Objectives
Classes CS 21a: Introduction to Computing I
Using java libraries CGS3416 spring 2019.
Collections and iterators
Presentation transcript:

Libraries CITS1001

Objects First with Java Scope of this lecture Using library classes to implement some more advanced functionality 5.0 © David J. Barnes and Michael Kölling

Main concepts to be covered Objects First with Java 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 © David J. Barnes and Michael Kölling

Objects First with Java 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. Library classes are often inter-related. Arranged into packages. © David J. Barnes and Michael Kölling

Working with the library Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Working with the library A competent Java programmer must be able to work with the libraries. 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. © David J. Barnes and Michael Kölling

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

Objects First with Java 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 A common iteration pattern. © David J. Barnes and Michael Kölling

Objects First with Java 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); © David J. Barnes and Michael Kölling

Objects First with Java 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? © David J. Barnes and Michael Kölling

Reading class documentation Objects First with Java 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 http://docs.oracle.com/javase/8/docs/api © David J. Barnes and Michael Kölling

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling http://docs.oracle.com/javase/8/docs/api/index.html?java/lang/String.html

Interface vs implementation Objects First with Java 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 © David J. Barnes and Michael Kölling

Interface vs implementation Objects First with Java 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) of methods the implementation of the class © David J. Barnes and Michael Kölling

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

Methods from String contains endsWith indexOf substring toUpperCase Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Methods from String contains endsWith indexOf substring toUpperCase Trim Beware: strings are immutable! That is, once it is created a String object cannot be changed. The String class has a number of methods that appear to modify strings. Since Strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. See https://docs.oracle.com/javase/tutorial/java/data/strings.html

Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using library classes Classes organized into packages. Classes from the library must be imported using an import statement (except classes from the java.lang package). They can then be used like classes from the current project. © David J. Barnes and Michael Kölling

Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Packages and import Single classes may be imported: import java.util.ArrayList; Whole packages can be imported: import java.util.*; Importation does not involve source code insertion. © David J. Barnes and Michael Kölling

Objects First with Java 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 rand = new Random(); int num = rand.nextInt(); int value = 1 + rand.nextInt(100); int index = rand.nextInt(list.size()); © David J. Barnes and Michael Kölling

Selecting random responses Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Selecting random responses public Responder() { randomGenerator = new Random(); responses = new ArrayList<String>(); fillResponses(); } public void fillResponses() fill responses with a selection of response strings public String generateResponse() int index = randomGenerator.nextInt(responses.size()); return responses.get(index); © David J. Barnes and Michael Kölling

Parameterized classes Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Parameterized classes The documentation includes provision for a type parameter: ArrayList<E> These type names reappear in the parameters and return types: E get(int index) boolean add(E e)

Parameterized classes Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Parameterized classes The types in the documentation are placeholders for the types we use in practice: An ArrayList<TicketMachine> actually has methods: TicketMachine get(int index) boolean add(TicketMachine e)

Objects First with Java 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 (its interface). Some classes are parameterized with additional types. Parameterized classes are also known as generic classes or generic types. © David J. Barnes and Michael Kölling

More sophisticated behavior Objects First with Java More sophisticated behavior Using library classes to implement some more advanced functionality © David J. Barnes and Michael Kölling

Main concepts to be covered Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Further library classes Set Map Writing documentation javadoc © David J. Barnes and Michael Kölling

Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using sets import java.util.HashSet; ... HashSet<String> mySet = new HashSet<String>(); mySet.add("one"); mySet.add("two"); mySet.add("three"); for(String element : mySet) { do something with element } Compare with code for an ArrayList! © David J. Barnes and Michael Kölling

Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Tokenising Strings public HashSet<String> getInput() { System.out.print("> "); String inputLine = reader.nextLine().trim().toLowerCase(); String[] wordArray = inputLine.split(" "); HashSet<String> words = new HashSet<String>(); for(String word : wordArray) { words.add(word); } return words; © David J. Barnes and Michael Kölling

Objects First with Java 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. Example: a telephone book. © David J. Barnes and Michael Kölling

Objects First with Java 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" © David J. Barnes and Michael Kölling

Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using maps HashMap <String, String> phoneBook = new HashMap<String, String>(); phoneBook.put("Charles Nguyen", "(531) 9392 4587"); phoneBook.put("Lisa Jones", "(402) 4536 4674"); phoneBook.put("William H. Smith", "(998) 5488 0123"); String phoneNumber = phoneBook.get("Lisa Jones"); System.out.println(phoneNumber); © David J. Barnes and Michael Kölling

List, Map and Set Alternative ways to group objects. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling List, Map and Set Alternative ways to group objects. Varying implementations available: ArrayList, LinkedList HashSet, TreeSet But HashMap is unrelated to HashSet, despite similar names. The second word reveals organizational relatedness.

Writing class documentation Objects First with Java 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 potential 'library class'! © David J. Barnes and Michael Kölling

Elements of documentation Objects First with Java 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 © David J. Barnes and Michael Kölling

Elements of documentation Objects First with Java 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 © David J. Barnes and Michael Kölling

Objects First with Java 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 (2011.07.31) */ © David J. Barnes and Michael Kölling

Objects First with Java 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<String> getInput(String prompt) { ... } © David J. Barnes and Michael Kölling

Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Public vs private Public elements are accessible to objects of other classes: Fields, constructors and methods Fields should not be public. Private elements are accessible only to objects of the same class. Only methods that are intended for other classes should be public. © David J. Barnes and Michael Kölling

Objects First with Java 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. © David J. Barnes and Michael Kölling

Code completion The BlueJ editor supports lookup of methods. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Code completion The BlueJ editor supports lookup of methods. Use Ctrl-space after a method-call dot to bring up a list of available methods. Use Return to select a highlighted method.

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

Objects First with Java 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). © David J. Barnes and Michael Kölling

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

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Class variables A class variable is shared between all instances of the class. In fact, it belongs to the class and exists independent of any instances. Designated by the static keyword. Public static variables are accessed via the class name; e.g.: Thermometer.boilingPoint

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

Constants A variable, once set, can have its value fixed. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Constants A variable, once set, can have its value fixed. Designated by the final keyword. final int max = list.size(); Final fields must be set in their declaration or the constructor. Combing static and final is common.

Objects First with Java Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Class constants static: class variable final: constant private static final int gravity = 3; Public visibility is less of an issue with final fields. Upper-case names often used for class constants: public static final int BOILING_POINT = 100; © David J. Barnes and Michael Kölling

Using the class Math Whenever you need a mathematical function, it will (probably) be in the class Math java.lang.Math (can be referred to just as Math) For example, Java does not have a built-in power operator, but it is available in Math public static double circleArea(double radius) { double area = 3.14159 * Math.pow(radius,2); return area; } Aside: Why is this is poor-quality code?

Math.random() public static double random() Returns a double x such that 0.0 <= x < 1.0 Try it in the BlueJ Code Pad Example boolean isheads = Math.random() < 0.5;

Math Constants Class variables are often used to provide access to constants – values that are frequently used but not changed Constants can be numerical values Math.PI Math.E public static double circleArea(double radius) { return Math.PI * Math.pow(radius,2); }

Utility Classes A class like Math that contains only static methods is sometimes called a utility class, because it just provides “utility” methods for use in other classes There is no point in ever creating an object of the class Math because it can never do anything that the existing methods cannot do In fact it has been made impossible to create an object in the class Math! this is done by giving a dummy constructor, but making it private

Constant Objects: Colours import java.awt.Color; (before the class header) The class java.awt.Color makes available a number of “pre-constructed’’ objects Color.RED Color.BLUE … Color.BLACK You can use these colours without having to construct them from scratch See http://teaching.csse.uwa.edu.au/units/CITS1001/colorinfo.html for more information about colors in Java

Practice Write constant declarations for the following: A public variable to measure tolerance, with the value 0.001 A private variable to indicate a pass mark, with integer value of 40 A public character variable that is used to indicate that the help command is ‘h’. What constant names are defined in the java.lang.Math class? Why do you think the methods in the Math class are static? Could they be written as instance methods? In a program that uses 73.28166 in ten different places, give reasons why it makes sense to associate this value with a variable name?