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

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

More Sophisticated Behaviour 1 Using library classes to implement more advanced functionality.
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.
Improving structure with inheritance
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
Understanding class definitions Looking inside classes 3.0.
More sophisticated behavior Using library classes to implement some more advanced functionality 4.0.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
More Sophisticated Behaviour 2 Using library classes to implement more advanced functionality. Also … class ( static ) fields.
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.
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.
Object Interaction 2 Creating cooperating objects.
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.
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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
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 Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Documentation javadoc. Documentation not a programmer's first love lives in a separate file somewhere usually a deliverable on the schedule often not.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Understanding class definitions
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.
More about inheritance Exploring polymorphism 5.0.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
OOPDA Intro 5.0. Topics Website and Syllabus Rowan VPN and H:drive BlueJ application and projects Programming Style (Appendix J) Javadoc (Appendix I)
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.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
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.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
CS1054: Lecture 11 More Sophisticated Behavior (contd..)
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
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.
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
More Sophisticated Behavior
Objects First with Java A Practical Introduction using BlueJ
Libraries CITS1001.
More sophisticated behavior
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
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
Collections and iterators
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

More sophisticated behaviour Using library classes to implement some more advanced functionality

2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling What’s wrong with this? class NoteBook { private ArrayList notes; public NoteBook() { ArrayList notes = new ArrayList (); } public void addNote(String note) { notes.add(note); }

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

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

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

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

7 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 "Charles Nguyen" :HashMap "(531) " "Lisa Jones""(402) " "William H. Smith""(998) "

8 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) "); phoneBook.put("Lisa Jones", "(402) "); phoneBook.put("William H. Smith", "(998) "); String phoneNumber = phoneBook.get("Lisa Jones"); System.out.println(phoneNumber);

9 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'!

10 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

11 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

12 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. * Michael Kölling and David J. Barnes 1.0 (30.Mar.2006) */

13 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. * prompt A prompt to print to screen. A set of Strings, where each String is * one of the words typed by the user */ public HashSet getInput(String prompt) {... }

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

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

16 Personal class library Often static methods are used when creating other classes and their methods. Consequently it is useful to assemble these more general class definitions in a separate folder so that may then be ‘imported’ as required. In BlueJ this is supported using Tools|Preferences menu option: In libraries, click Add, navigate to the required folder and click Open.

17 Example: FinanceMaths Class: Finance

18 Example – cont’d Class: Customer

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

20 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Constants private static final int gravity = 3; private : access modifier, as usual static : class variable final : constant

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