1
 Understand the documentation for library class.  Implement TechSupport system.  Present reading class documentation.  Differentiate between interface and implementation.  Identify class methods.  Illustrate library class random.  Understand the packages and Import.  Present maps types and differentiate them from list.  Illustrate how you document your own class.  Distinguish public and private keywords.  Understand the static and final keywords. 2
 Java library means the Java standard class library contains many classes that are very useful.  It consists of thousands of classes, each of which has many methods, both with and without parameters, and with and without return types. Many useful classes that make life much easier.  Library classes are often inter-related.  The library is arranged into packages.  A good Java programmer should know: ◦ some of the most important classes and their methods from the library by name (ArrayListis one of those important ones) and ◦ how to find out about other classes and look up the details (such as methods and parameters).  Each programmer should be able to use the classes of other team members as if they were library classes, making informed use of them without the need to know how they work internally. 3
 We use the TechSupport application. You can find it in the Week8- Tutorial Projects folder under the name tech-support1.  TechSupport is a program intended to provide technical support for customers of the fictitious DodgySoft software company. It is a textual, interactive dialog system.  Customers call to get advice and help with their technical problems with the DodgySoft software products. Customers can communicate with the technical-support system online.  The project diagram shows us three classes: SupportSystem, InputReader, and Responder. 4
5
The main loop structure as the following: boolean finished = false; while(!finished) { do something if(exit condition) { finished = true; } else { do something more } } 6
 The main loop body as the following: String input = reader.getInput(); ... String response = responder.generateResponse(); System.out.println(response);  The exist condition as the following: String input = reader.getInput(); if(input.startsWith("bye")) { finished = true; } 7
 The Java class library documentation shows details about all classes in the library. Using this documentation is essential in order to make good use of library classes.  Documentation of the Java libraries in HTML format.  Readable in a web browser.  Class API: Application Programmers’ Interface  The Java class library documentation presented in the following figure. 8
 The interface of a class describes what a class does and how it can be used without showing the implementation.  The documentation includes different pieces of information that are: • 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  This information, taken together, is called the interface of a class. 9
 The complete source code that defines a class is called the implementation of that class.  The documentation doesn’t include: • Public/private fields (most fields are private) • Public/private methods • the bodies (source code) of methods 10
 The interface terminology is also used for individual methods. For example, the String documentation shows us the interface of the length method: ◦ public int length()  Returns the length of this string. The length is equal to the number of Unicode code units in the string. ◦ specified by:  length in interface CharSequence ◦ returns:  the length of the sequence of characters represented by this object 11
 The interface of a method consists of the signature of the method and a comment (shown here in italics). The signature of a method includes (in this order): ◦ an access modifier (here public), which we shall discuss below ◦ the return type of the method (here int) ◦ the method name ◦ a list of parameters (which is empty in this example) 12
 Immutable objects: an object is said to be immutable if its contents or state cannot be changed once it has been created.  Strings are an example of immutable objects. 13
14
15
16
17
18
 The nextInt(int n) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.  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()); 19
20
21
 Classes organized into packages.  Classes from the library must be imported using an import statement  Single classes may be imported: import java.util.ArrayList;  Java also allows us to import complete packages with statements of the form Import package-name.*;  Whole packages can be imported: import java.util.*; 22
 A map is a collection that stores key/value pairs as entries. Values can be looked up by providing the key.  Map versus ArrayList: ◦ As with the ArrayList, a map can store a flexible number of entries. ◦ One difference between the ArrayList and a map is that with a map each entry is not an object, but a pair of objects. This pair consists of a key object and a value object. 23
 Example: 24
 Example: 25
• 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. 26
The documentation of a class should at least include: ◦ the class name ◦ a comment describing the overall purpose and characteristics of the class ◦ a version number ◦ the author’s name (or authors’ names) ◦ documentation for each constructor and each method 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 27
 Access modifiers define the visibility of a field, constructor, or method.  Public elements are accessible from inside the same class and from other classes.  Private elements are accessible only from within the same class.  Fields should not be public.  Only methods that are intended for other classes should be public. 28
Private Access Modifier - private:  Methods, variables and constructors that are declared private can only be accessed within the declared class itself.  Private access modifier is the most restrictive access level. Class and interfaces cannot be private.  Variables that are declared private can be accessed outside the class if public getter methods are present in the class.  Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world. 29
Public Access Modifier - public:  A class, method, constructor, interface etc. declared public can be accessed from any other class.  However, if the public class we are trying to access is in a different package, then the public class still need to be imported.  Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. 30
 Code completion is a function that is available in BlueJ’s editor when the cursor is behind the dot of a method call.  In this situation, typing CTRL-space will bring up a pop-up listing all methods in the interface of the object we are using in the call.  When the code completion pop-up is displayed, we can type the beginning of the method name to narrow down the method list.  Hitting Return enters the selected method call into our source code.  Code completion can also be used without a preceding object to call local methods. 31
32
 Class variables, also known as static variables, are declared with the static keyword in a class, but outside a method, constructor or a block.  There would only be one copy of each class variable per class, regardless of how many objects are created from it.  Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static.  Static variables are stored in static memory.  Static variables are created when the program starts and destroyed when the program stops. 33
 Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class.  Default values are same as instance variables. Static variables can be accessed by calling with the class name ClassName.VariableName.  When declaring class variables as public static final, then variables names (constants) are all in upper case.  If the static variables are not public and final the naming syntax is the same as instance and local variables.  Classes can have fields. These are known as class variables or static variables. 34
35
 One frequent use for the static keyword is to define constants.  Constants are similar to variables, but they cannot change their value during the execution of an application.  In Java, constants are defined with the keyword final. For example: private final int SIZE = 10;  We notice that constant declarations look similar to field declarations, with two differences: ◦ they include the keyword final before the type name ◦ they must be initialized with a value at the point of declaration 36
 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.  The implementation is hidden (information hiding).  We document our classes so that the interface can be read on its own (class comment, method comments). 37

Lecture 8 Library classes

  • 1.
  • 2.
     Understand thedocumentation for library class.  Implement TechSupport system.  Present reading class documentation.  Differentiate between interface and implementation.  Identify class methods.  Illustrate library class random.  Understand the packages and Import.  Present maps types and differentiate them from list.  Illustrate how you document your own class.  Distinguish public and private keywords.  Understand the static and final keywords. 2
  • 3.
     Java librarymeans the Java standard class library contains many classes that are very useful.  It consists of thousands of classes, each of which has many methods, both with and without parameters, and with and without return types. Many useful classes that make life much easier.  Library classes are often inter-related.  The library is arranged into packages.  A good Java programmer should know: ◦ some of the most important classes and their methods from the library by name (ArrayListis one of those important ones) and ◦ how to find out about other classes and look up the details (such as methods and parameters).  Each programmer should be able to use the classes of other team members as if they were library classes, making informed use of them without the need to know how they work internally. 3
  • 4.
     We usethe TechSupport application. You can find it in the Week8- Tutorial Projects folder under the name tech-support1.  TechSupport is a program intended to provide technical support for customers of the fictitious DodgySoft software company. It is a textual, interactive dialog system.  Customers call to get advice and help with their technical problems with the DodgySoft software products. Customers can communicate with the technical-support system online.  The project diagram shows us three classes: SupportSystem, InputReader, and Responder. 4
  • 5.
  • 6.
    The main loopstructure as the following: boolean finished = false; while(!finished) { do something if(exit condition) { finished = true; } else { do something more } } 6
  • 7.
     The mainloop body as the following: String input = reader.getInput(); ... String response = responder.generateResponse(); System.out.println(response);  The exist condition as the following: String input = reader.getInput(); if(input.startsWith("bye")) { finished = true; } 7
  • 8.
     The Javaclass library documentation shows details about all classes in the library. Using this documentation is essential in order to make good use of library classes.  Documentation of the Java libraries in HTML format.  Readable in a web browser.  Class API: Application Programmers’ Interface  The Java class library documentation presented in the following figure. 8
  • 9.
     The interfaceof a class describes what a class does and how it can be used without showing the implementation.  The documentation includes different pieces of information that are: • 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  This information, taken together, is called the interface of a class. 9
  • 10.
     The completesource code that defines a class is called the implementation of that class.  The documentation doesn’t include: • Public/private fields (most fields are private) • Public/private methods • the bodies (source code) of methods 10
  • 11.
     The interfaceterminology is also used for individual methods. For example, the String documentation shows us the interface of the length method: ◦ public int length()  Returns the length of this string. The length is equal to the number of Unicode code units in the string. ◦ specified by:  length in interface CharSequence ◦ returns:  the length of the sequence of characters represented by this object 11
  • 12.
     The interfaceof a method consists of the signature of the method and a comment (shown here in italics). The signature of a method includes (in this order): ◦ an access modifier (here public), which we shall discuss below ◦ the return type of the method (here int) ◦ the method name ◦ a list of parameters (which is empty in this example) 12
  • 13.
     Immutable objects:an object is said to be immutable if its contents or state cannot be changed once it has been created.  Strings are an example of immutable objects. 13
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
     The nextInt(intn) method is used to get a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.  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()); 19
  • 20.
  • 21.
  • 22.
     Classes organizedinto packages.  Classes from the library must be imported using an import statement  Single classes may be imported: import java.util.ArrayList;  Java also allows us to import complete packages with statements of the form Import package-name.*;  Whole packages can be imported: import java.util.*; 22
  • 23.
     A mapis a collection that stores key/value pairs as entries. Values can be looked up by providing the key.  Map versus ArrayList: ◦ As with the ArrayList, a map can store a flexible number of entries. ◦ One difference between the ArrayList and a map is that with a map each entry is not an object, but a pair of objects. This pair consists of a key object and a value object. 23
  • 24.
  • 25.
  • 26.
    • Your ownclasses should be documented the same way library classes are. • Other people should be able to use your class without reading the implementation. 26
  • 27.
    The documentation ofa class should at least include: ◦ the class name ◦ a comment describing the overall purpose and characteristics of the class ◦ a version number ◦ the author’s name (or authors’ names) ◦ documentation for each constructor and each method 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 27
  • 28.
     Access modifiersdefine the visibility of a field, constructor, or method.  Public elements are accessible from inside the same class and from other classes.  Private elements are accessible only from within the same class.  Fields should not be public.  Only methods that are intended for other classes should be public. 28
  • 29.
    Private Access Modifier- private:  Methods, variables and constructors that are declared private can only be accessed within the declared class itself.  Private access modifier is the most restrictive access level. Class and interfaces cannot be private.  Variables that are declared private can be accessed outside the class if public getter methods are present in the class.  Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world. 29
  • 30.
    Public Access Modifier- public:  A class, method, constructor, interface etc. declared public can be accessed from any other class.  However, if the public class we are trying to access is in a different package, then the public class still need to be imported.  Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. 30
  • 31.
     Code completionis a function that is available in BlueJ’s editor when the cursor is behind the dot of a method call.  In this situation, typing CTRL-space will bring up a pop-up listing all methods in the interface of the object we are using in the call.  When the code completion pop-up is displayed, we can type the beginning of the method name to narrow down the method list.  Hitting Return enters the selected method call into our source code.  Code completion can also be used without a preceding object to call local methods. 31
  • 32.
  • 33.
     Class variables,also known as static variables, are declared with the static keyword in a class, but outside a method, constructor or a block.  There would only be one copy of each class variable per class, regardless of how many objects are created from it.  Static variables are rarely used other than being declared as constants. Constants are variables that are declared as public/private, final and static.  Static variables are stored in static memory.  Static variables are created when the program starts and destroyed when the program stops. 33
  • 34.
     Visibility issimilar to instance variables. However, most static variables are declared public since they must be available for users of the class.  Default values are same as instance variables. Static variables can be accessed by calling with the class name ClassName.VariableName.  When declaring class variables as public static final, then variables names (constants) are all in upper case.  If the static variables are not public and final the naming syntax is the same as instance and local variables.  Classes can have fields. These are known as class variables or static variables. 34
  • 35.
  • 36.
     One frequentuse for the static keyword is to define constants.  Constants are similar to variables, but they cannot change their value during the execution of an application.  In Java, constants are defined with the keyword final. For example: private final int SIZE = 10;  We notice that constant declarations look similar to field declarations, with two differences: ◦ they include the keyword final before the type name ◦ they must be initialized with a value at the point of declaration 36
  • 37.
     Java hasan 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.  The implementation is hidden (information hiding).  We document our classes so that the interface can be read on its own (class comment, method comments). 37