Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Sophisticated Behavior

Similar presentations


Presentation on theme: "More Sophisticated Behavior"— Presentation transcript:

1 More Sophisticated Behavior
Chapter 5 More Sophisticated Behavior

2 JAVA API Documentation
A good programmer does not reinvent the wheel A good programmer uses what someone else has already written and debugged Java supplies us with a very rich class library You are not expected to know all the classes in the library, but you should know how to find out about a class

3 The String Class Let’s look at the String class’s on-line documentation String Class API Documention

4 Interface vs. Implementation
The interface describes what a class does and how it can be used. The implementation of a class is the code that actually does the work. A good class will have a robust interface and allow the user of the class to perform all the necessary activities without needed to know how they were implemented.

5 String methods trim() – removes extra whitespace from the beginning and end of the string startWith() – indicates whether or not the string starts with the given prefix. equals() – used to tell whether or not two strings are the same string, i.e. the same word(s) Note: DO NOT USE == to check string equality This does not do what you think it does split() – splits the string into tokens using the given character as a delimiter

6 Packages and Import Import statements tell the Java compiler that we would like to have access to a particular class. It has the form: import qualified-class-name; import package-name.*; The qualified class name is the of its package, followed by a dot followed by the class name.

7 Class Documentation Documentation of a class should include
The class name A comment describing the overall purpose and characteristics of the class A version number The author’s or authors’ name(s) Documentation for each constructor and method

8 Method 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 return type

9 javadoc BlueJ can create documentation for your projects from the tools menu. It will look for javadoc comments They start with /* and end with */ You can use some tags for key items @version @author @param @return

10 Public vs. Private The key word public and private are two of Java’s access modifiers. In general, fields are private and methods are public There are many reasons why a method would be private There are almost no reason to make a field public

11 Access Modifiers Access modifiers allow Java to implement information hiding Information hiding is the idea that internal details of a class should be hidden from other classes. Information hiding is key to lowering the coupling between classes Coupling is the amount of reliance of a class on the implementation of another class.

12 Class variables and constants
There are times when you want every object of a class to have a value The static key word declares a variable or constant to be shared by every object in the class. private static final int GRAVITY = 3; This example would give every object of this class GRAVITY.

13 Constants The key word static declares a constant or a variable as a class variable or constant. But what makes a variable into a constants? The key word final creates a constant private final int SIZE = 10; Constants must be initialized when they are declared and they cannot be changed


Download ppt "More Sophisticated Behavior"

Similar presentations


Ads by Google