One of the techniques used in Computer Science for solving a complex problem, consists of breaking the problem into small, specific tasks, or sub-problems. This strategy known as top-down design, helps by allowing the solution of complex problems to become the easier compilation of solutions for smaller, more manageable sub-problems.
In OOP, this approach to problem solving is addressed by the principle of method writing: "One task, One method", is a principle that facilitates problem solving, while at the same time linking to the encapsulation and modularity techniques of OOP.
A method or sub-procedure in programming, is a collection of statements that are grouped together to perform an operation or function.
Classes are formed by attributes (or instance variables) that define the state of an object, and by methods that define the actions the object can execute.
Using a method is referred to as invoking or calling a method. When you use statements such us:
System.out.println("Hello");
You are invoking or calling the method println with the parameter: "Hello". A method is called in different ways depending on the type of method.
To create new methods, you first need to write the Signature of your method. This is the heading line that will define the type, name and parameters you will require for your method to be called:
public void displayName()
is an example of a method signature that can be found inside of the definition of a class.
There are different types of methods you could add to a class. Constructors, Accessors and Mutators are some of the most common ones.
Also known as "get" methods, will give you access to the current value of an object instance variable.
Accessor methods are return methods, as they return or give back the value stored in the instance variable (attribute).
Also known as "set" methods, are the ones created to set or modify the current value of an object instance variable.
Mutator methods are normally void, as they do not return a value, but instead assign a value to the corresponding instance variable (attribute).
Mutator methods normally contain parameters with the values that will be assigned to the instance variables they modify.
Java programs can contain three types of comments:
/* */ are used to enclose single or multiline comments. These comments are appropriate at the beginning of a program to describe the application and where necessary to clarify a segment of code.
// are used for adding a comment to the end of a statement or to create a single line comment. The // is also useful for debugging a program, which will be discussed in a later chapter.
/** */ are used for documentation. The javadoc tool copies these comments into a separate HTML document to create an instructional manual or external documentation. Documentation comments are appropriate for describing a class or method.
Multiline comments that describe a program, class, or method are sometimes referred to as a comment block. Tips throughout the text will provide additional pointers about commenting code
Methods should be carefully commented so that a reader of the program understands what task the method is performing and what data, if any, will be returned by the method. Method documentation is in the form of documentation comments (/** */) that appear just above the method declaration. For example, the drawBar() method with documentation:
/**
* Print a bar of asterisks across the screen.
* pre: length > 0
* post: Bar drawn of length characters, insertion
* point moved to next line.
*/
public static void drawBar(int length) {
for (int i = 0; i < length; i++) {
System.out.print("*");
}
System.out.println();
}
The assumptions, or initial requirements, of a method are stated in the documentation in a section called the precondition, or just pre. Note that the pre for drawBar() states that length must be greater than 0, but not that length must be an int. Information that the compiler will verify, such as data types, should not be stated in method documentation.
The postcondition section of the documentation, or post, states what must be true after the method has been executed. However, the post should not state how the method accomplished its task. A method may not have a precondition, but every method must have a postcondition. For example, below is the cubeOf() method with documentation:
/**
* Calculates the cube of a number.
* pre: none
* post: x cubed returned
*/
public static double cubeOf(double x) {
double xCubed;
xCubed = x * x * x;
return(xCubed);
}