principles of programming cm10227
play

Principles of Programming CM10227 Lecture D.8.: Java: Groups of - PowerPoint PPT Presentation

Grouping Objects Object Behaviour Principles of Programming CM10227 Lecture D.8.: Java: Groups of Objects - Object Behaviour Dr. Marina De Vos University of Bath Ext: 5053 Academic Year 2012-2013 Lecture D.8. (MDV) Programming I Academic


  1. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections A Java example ⌥ / ∗ ∗ L i s t a l l notes in the notebook . ∗ ∗ / public void l i s t N o t e s ( ) { for ( String note : notes ) { System . out . p r i n t l n ( note ) ; } } ⌃ ⇧ ⌦ for each note in notes, print out noted Lecture D.8. (MDV) Programming I Academic Year 2012-2013 27 / 110

  2. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The while loop A for-each loop repeats the loop body for each object in a collection. Sometimes we require more variation than this. We can use a boolean condition to decide whether or not to keep going. A while loop provides this control. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 28 / 110

  3. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections While loop pseudo code General form of a while loop while keyword boolean test ⌥ while ( loop condition ) { loop body } ⌃ ⇧ ⌦ Statement(s) to be repeated Pseudo-code expression of the actions of a while loop while we wish to continue, do the things in the loop body. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 29 / 110

  4. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections A Java example ⌥ / ∗ ∗ L i s t a l l notes in the notebook . ∗ ∗ / public void l i s t N o t e s ( ) { int index = 0; while ( index < notes . size ( ) ) { System . out . p r i n t l n ( notes . get ( index ) ) ; index ++; } } ⌃ ⇧ ⌦ while the value of index is less than the size of the collection, print the next note, and then increment index Lecture D.8. (MDV) Programming I Academic Year 2012-2013 30 / 110

  5. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Increments i++ : the value of i is used first before it is being incremented with one ⌥ a = 5; b = a++; a == 6; b == 5; ⌃ ⇧ ⌦ ++i: increments i with one, the new value of i is then used. ⌥ a = 5; b = ++a ; a == 6; b == 6; ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 31 / 110

  6. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections for-each versus while for-each: easier to write. safer: it is guaranteed to stop. while: we do not have to process the whole collection. does not even have to be used with a collection. take care: could be an infinite loop. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 32 / 110

  7. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections While without a collection ⌥ / / P r i n t a l l even numbers from 0 to 30. int index = 0; while ( index < = 30) { System . out . p r i n t l n ( index ) ; index = index + 2; } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 33 / 110

  8. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Searching a collection ⌥ int index = 0; boolean found = false ; while ( index < notes . size ( ) && ! found ) { String note = notes . get ( index ) ; i f ( note . contains ( searchString ) ) { / / We don ’ t need to keep looking . found = true ; } else { index ++; } } / / Either we found i t , or we searched the whole / / c o l l e c t i o n . ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 34 / 110

  9. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Using an Iterator object java.util.Iterator returns an Iterator object ⌥ I t e r a t o r < ElementType > i t = myCollection . i t e r a t o r ( ) ; while ( i t . hasNext ( ) ) { c a l l i t . next ( ) to get the next object do something with that object } ⌃ ⇧ ⌦ ⌥ public void l i s t N o t e s ( ) { I t e r a t o r < String > i t = notes . i t e r a t o r ( ) ; while ( i t . hasNext ( ) ) { System . out . p r i n t l n ( i t . next ( ) ) ; } } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 35 / 110

  10. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Iterators An iterator is an object that provides functionality to iterate over all elements of a collection or container class java. util . Iterator two main methods: hasNext() to check if the Iterator has more elements and next() to take the next object from the Iterator. You can access an iterator for all java collections. Not all collections can be accessed using indexes. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 36 / 110

  11. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Index versus Iterator Ways to iterate over a collection: for-each loop. Use if we want to process every element. while loop. Use if we might want to stop part way through. Use for repetition that doesn’t involve a collection. Iterator object. Use if we might want to stop part way through. Often used with collections where indexed access is not very efficient, or impossible. Iteration is an important programming pattern. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 37 / 110

  12. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The auction project The auction project provides further illustration of collections and iteration. Two further points to follow up: The null value. Casting. Used to store the result of get into a variable: ⌥ String message = ( String ) notes . get (0) ; ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 38 / 110

  13. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The auction project Lecture D.8. (MDV) Programming I Academic Year 2012-2013 39 / 110

  14. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The Lot Class � ⌥ public void bidFor ( Person bidder , long value ) { i f ( ( highestBid == null ) || ( highestBid . getValue ( ) < value ) ) { / / This bid i s the best so f a r . setHighestBid ( new Bid ( bidder , value ) ) ; } else { System . out . p r i n t l n ( ” Lot number : ” + getNumber ( ) + ” ( ” + getDescription ( ) + ” ) ” + ” already has a bid of : ” + highestBid . getValue ( ) ) ; } } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 40 / 110

  15. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The null Keyword highestBid == null; The Java keyword null is used to mean no object when a variable is not currently holding a reference to a particular object. setHighestBid(new Bid(bidder, value)); two things are done here: we create a new object Bid we pass this new object immediately to the method Lecture D.8. (MDV) Programming I Academic Year 2012-2013 41 / 110

  16. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The Auction Class ⌥ public void showLots ( ) { I t e r a t o r i t = l o t s . i t e r a t o r ( ) ; while ( i t . hasNext ( ) ) { Lot l o t = ( Lot ) i t . next ( ) ; System . out . p r i n t l n ( l o t . getNumber ( ) + ” : ” + l o t . getDescription ( ) ) ; / / Include any d e t a i l s of a highest bid . Bid highestBid = l o t . getHighestBid ( ) ; i f ( highestBid != null ) { System . out . p r i n t l n ( ” Bid : ” + highestBid . getValue ( ) ) ; } else { System . out . p r i n t l n ( ” (No bid ) ” ) ; } } } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 42 / 110

  17. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Type Casting I Lot lot = (Lot) it.next(); the return value of the Iterator method next() is an object of type Object. To store this in an object of type Lot we need to explicitly convert it that type. This is called Casting This can only been done if the objects we have added to the container were originally of type Lot. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 43 / 110

  18. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Type Casting II Collections in Java allow for the storage of any type of objects. In order to do so it transforms everything you add into an object of type Object. When retrieving objects from a collection we normally cast them back into their original types. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 44 / 110

  19. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Wrapper Classes Container classes in Java can only contain objects. Primitive types can therefore not be added. To solve this, Java provides wrapper classes: Integer, Float, Double Integer a = new Integer(10); int b = a.intValue() ; Lecture D.8. (MDV) Programming I Academic Year 2012-2013 45 / 110

  20. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The auction project The auction project provides further illustration of collections and iteration. One further point to follow up: the null value. Used to indicate, ’no object’. We can test if an object variable holds the null variable. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 46 / 110

  21. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Review Loop statements allow a block of statements to be repeated. The for − each loop allows iteration over a whole collection. The while loop allows the repetition to be controlled by a boolean expression. All collection classes provide special Iterator objects that provide sequential access to a whole collection. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 47 / 110

  22. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually offer a special fixed-size collection type: an array. Java arrays can store objects or primitive-type values. arrays use a special syntax. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 48 / 110

  23. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The weblog-analyzer project Web server records details of each access. Supports webmasters tasks. Most popular pages. Busiest periods. How much data is being delivered. Broken references. Analyze accesses by hour. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 49 / 110

  24. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Creating an array object ⌥ public class LogAnalyzer { Array variable declaration private int [ ] hourCounts ; private LogfileReader reader ; public LogAnalyzer ( ) { Array object creation hourCounts = new int [ 2 4 ] ; reader = new LogfileReader ( ) ; } . . . } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 50 / 110

  25. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The hourCounts array Lecture D.8. (MDV) Programming I Academic Year 2012-2013 51 / 110

  26. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Using an array Square-bracket notation is used to access an array element: hourCounts[...] Elements are used like ordinary variables. On the left of an assignment: hourCounts[hour] = ...; In an expression: adjusted = hourCounts[hour] − 3; hourCounts[hour]++; Lecture D.8. (MDV) Programming I Academic Year 2012-2013 52 / 110

  27. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections The for loop There are two variations of the for loop, for-each and for. The for loop is often used to iterate a fixed number of times. Often used with a variable that changes a fixed amount on each iteration. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 53 / 110

  28. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections For loop pseudo-code General form of a for loop ⌥ for ( i n i t i a l i z a t i o n ; condition ; post − body action ) { statements to be repeated } ⌃ ⇧ ⌦ Equivalent in while-loop form ⌥ i n i t i a l i z a t i o n ; while ( condition ) { statements to be repeated post − body action } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 54 / 110

  29. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections A Java example General form of a for loop ⌥ for ( int hour = 0; hour < hourCounts . length ; hour ++) { System . out . p r i n t l n ( hour + ” : ” + hourCounts [ hour ] ) ; } ⌃ ⇧ ⌦ Equivalent in while-loop form ⌥ int hour = 0; while ( hour < hourCounts . length ) { System . out . p r i n t l n ( hour + ” : ” + hourCounts [ hour ] ) ; hour ++; } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 55 / 110

  30. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections for loop with bigger step ⌥ / / P r i n t multiples of 3 that are below 40. for ( int num = 3; num < 40; num = num + 3) { System . out . p r i n t l n (num) ; } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 56 / 110

  31. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Review arrays are appropriate where a fixed-size collection is required. arrays use special syntax. For loops offer an alternative to while loops when the number of repetitions is known. For loops are used when an index variable is required. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 57 / 110

  32. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Coding Conventions Iterations Always use { , even if there is only one statement to be repeated Lecture D.8. (MDV) Programming I Academic Year 2012-2013 58 / 110

  33. Class Libraries Grouping Objects Collections Object Behaviour Iteration Fixed-size collections Glossary Java API packages import ArrayList Iterator Generic classes type cast Lecture D.8. (MDV) Programming I Academic Year 2012-2013 59 / 110

  34. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Main concepts to be covered Using library classes Reading documentation Writing documentation Lecture D.8. (MDV) Programming I Academic Year 2012-2013 61 / 110

  35. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 62 / 110

  36. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 63 / 110

  37. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level A Technical Support System A textual dialog system Idea based on ‘Eliza’ by Joseph Weizenbaum (MIT, 1960s) This is a system that when entering questions will try to answer them in an “intelligent” way. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 64 / 110

  38. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level A Technical Support System Lecture D.8. (MDV) Programming I Academic Year 2012-2013 65 / 110

  39. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level A Technical Support System Output Lecture D.8. (MDV) Programming I Academic Year 2012-2013 66 / 110

  40. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Main loop structure ⌥ / / from the s t a r t method in SupportSystem boolean fi ni s h ed = false ; while ( ! f i n i sh e d ) { / / se n t i n e l c o n t r o l l e d loop do something i f ( e x i t condition ) { fi ni s h ed = true ; } else { do something more } } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 67 / 110

  41. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Main loop body ⌥ String input = reader . getInput ( ) ; . . . String response = responder . generateResponse ( ) ; System . out . p r i n t l n ( response ) ; ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 68 / 110

  42. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level The exit condition ⌥ String input = reader . getInput ( ) ; i f ( input . startsWith ( ” bye ” ) ) { fi ni s h ed = true ; } ⌃ ⇧ ⌦ Where does startsWith come from? What is it? What does it do? How can we find out? Lecture D.8. (MDV) Programming I Academic Year 2012-2013 69 / 110

  43. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level String Info Lecture D.8. (MDV) Programming I Academic Year 2012-2013 70 / 110

  44. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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://java.sun.com/j2se/1.5.0/docs/api/ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 71 / 110

  45. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Interface vs implementation I 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 Lecture D.8. (MDV) Programming I Academic Year 2012-2013 72 / 110

  46. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Interface vs implementation II 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 Lecture D.8. (MDV) Programming I Academic Year 2012-2013 73 / 110

  47. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Interface vs implementation The interface of a method consists of signature access modifier return type method name a list of parameters comment discussion of all signature items purpose of method Lecture D.8. (MDV) Programming I Academic Year 2012-2013 74 / 110

  48. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 75 / 110

  49. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Packages and import Classes are organised in packages. Single classes may be imported: ⌥ import java . u t i l . ArrayList ; ⌃ ⇧ ⌦ Whole packages can be imported: ⌥ import java . u t i l . ∗ ; ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 76 / 110

  50. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Side Note 1: Strings Strings are immutable objects - just like in Python immutable objects cannot change content or state once they have been created ⌥ String input = reader . getInput ( ) ; input = input . trim ; i f ( input . startsWith ( ‘ ‘ bye ’ ’ ) ) { fi n i sh e d = true ; } else { Code omitted } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 77 / 110

  51. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Side Note 1: StringBuffer A string buffer implements a mutable sequence of characters. A StringBsuffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 78 / 110

  52. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Side note 2: String equality ⌥ i f ( input == ” bye ” ) { . . . tests identity } ⌃ ⇧ ⌦ ⌥ i f ( input . equals ( ” bye ” ) ) { tests equality . . . } ⌃ ⇧ ⌦ ⇒ Strings should (almost) always be compared with .equals Lecture D.8. (MDV) Programming I Academic Year 2012-2013 79 / 110

  53. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Identity vs equality 1 Other (non-String) objects: :Person :Person “Fred“ “Jill“ person1 == person2 ? Lecture D.8. (MDV) Programming I Academic Year 2012-2013 80 / 110

  54. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Identity vs equality 2 Other (non-String) objects: :Person :Person “Fred“ “Fred“ person1 == person2 ? Lecture D.8. (MDV) Programming I Academic Year 2012-2013 81 / 110

  55. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Identity vs equality 3 Other (non-String) objects: :Person :Person “Fred“ “Fred“ person1 == person2 ? Lecture D.8. (MDV) Programming I Academic Year 2012-2013 82 / 110

  56. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Identity vs equality (Strings) = tests identity ⌥ String input = reader . getInput ( ) ; i f ( input == ” bye ” ) { . . . } ⌃ ⇧ ⌦ :String :String == ?? “bye“ “bye“ ⇒ (may be) false! input Lecture D.8. (MDV) Programming I Academic Year 2012-2013 83 / 110

  57. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Identity vs equality (Strings) equals tests equality (content) ⌥ String input = reader . getInput ( ) ; i f ( input . equals ( ” bye ” ) ) { . . . } ⌃ ⇧ ⌦ :String :String equals ?? “bye“ “bye“ ⇒ true! input Lecture D.8. (MDV) Programming I Academic Year 2012-2013 84 / 110

  58. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Using Random The library class Random can be used to generate random numbers ⌥ import java . u t i l .Random; . . . Random randomGenerator = new Random ( ) ; . . . int index1 = randomGenerator . n e xtIn t ( ) ; int index2 = randomGenerator . n e xtIn t (100) ; ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 85 / 110

  59. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Generating random responses ⌥ public Responder ( ) { randomGenerator = new Random ( ) ; responses = new ArrayList ( ) ; fillResponses ( ) ; } public String generateResponse ( ) { int index = randomGenerator . n e xtIn t ( responses . size ( ) ) ; return ( String ) responses . get ( index ) ; } public void fillResponses ( ) . . . ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 86 / 110

  60. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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. Just like the Python dictionary Lecture D.8. (MDV) Programming I Academic Year 2012-2013 87 / 110

  61. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Using maps A map with Strings as keys and values :HashMap ”Charles Nguyen” ”(531) 9392 4587” ”(402) 4536 4674” ”Lisa Jones” ”(998) 5488 0123” ”William H. Smith” Lecture D.8. (MDV) Programming I Academic Year 2012-2013 88 / 110

  62. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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 . p r i n t l n ( number ) ; ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 89 / 110

  63. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Maps in TechSupport ⌥ public class Responder { private HashMap responseMap ; Code Omitted public String generateResponse ( String word ) { String response = ( String ) responseMap . get ( word ) ; i f ( response != null ) { return response ; } else { return pickDefaultResponse ( ) ; } } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 90 / 110

  64. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Using sets ⌥ import java . u t i l . HashSet ; import java . u t i l . I t e r a t o r ; . . . HashSet mySet = new HashSet ( ) ; mySet . add ( ” one ” ) ; mySet . add ( ” two ” ) ; mySet . add ( ” three ” ) ; I t e r a t o r i t = mySet . i t e r a t o r ( ) ; while ( i t . hasNext ( ) ) { c a l l i t . next ( ) to get the next object do something with that object } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 91 / 110

  65. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Sets and List A Set is a collection that stores each individual element at most once. It does not maintain any specific order. A List is a collection that stores all elements it is been giving regardless of duplication. It maintains a order at all times. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 92 / 110

  66. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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’! Lecture D.8. (MDV) Programming I Academic Year 2012-2013 93 / 110

  67. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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 Lecture D.8. (MDV) Programming I Academic Year 2012-2013 94 / 110

  68. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level javadoc Class comment: ⌥ / ∗ ∗ ∗ The Responder class represents a response ∗ generator object . I t i s used to generate an ∗ automatic response . ∗ ∗ @author Michael K l l i n g and David J . Barnes ∗ @version 1.0 ( 1 . Feb.2002) ∗ / ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 95 / 110

  69. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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 Lecture D.8. (MDV) Programming I Academic Year 2012-2013 96 / 110

  70. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level javadoc Method comment: ⌥ / ∗ ∗ ∗ Read a l i n e of t e x t from standard input ( the t e x t ∗ terminal ) , and return i t as a set of words . ∗ ∗ @param prompt A prompt to p r i n t to screen . ∗ @return A set of Strings , where each String i s one of the words typed by the user ∗ ∗ / public HashSet getInput ( String prompt ) { . . . } ⌃ ⇧ ⌦ Lecture D.8. (MDV) Programming I Academic Year 2012-2013 97 / 110

  71. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Public vs private Public attributes (fields, constructors, methods) are accessible to other classes. Fields should not be public. Data encapsulation Private attributes are accessible only within the same class. Only methods that are intended for other classes should be public. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 98 / 110

  72. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level 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. Lecture D.8. (MDV) Programming I Academic Year 2012-2013 99 / 110

  73. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level The Balls Project Lecture D.8. (MDV) Programming I Academic Year 2012-2013 100 / 110

  74. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Class and Object Level So far we have created classes for the sake of objects which should have each a state of their own. Sometimes, objects should be able to share common properties For People this could be the gravitational pull For Employees of the same company this could be the companys details Lecture D.8. (MDV) Programming I Academic Year 2012-2013 101 / 110

  75. Java API Grouping Objects More Collections Object Behaviour Documentation Class&Object Level Class and Object Level Instead of having these details stored in each object it would be better to store this at one central point: THE CLASS Classes themselves can also have state class variables or static variables Exactly one copy exists of a class variable at all times, independent of the number of created instances The key word static is Javas syntax to define class variables methods at object level can access static variables Lecture D.8. (MDV) Programming I Academic Year 2012-2013 102 / 110

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend