Chapter 5 Methods and Modularity

Ace your homework & exams now with Quizwiz!

Entry conditions

Information about the variables in the formal parameter list- such as the range of values for each parameter- along with any other special conditions that must be set before the method runs.

Method Documentation

Information in a comment describing each method . It should include entry conditions. what the method does and how the method functions exit conditions.

Formal Parameters

Local variables that will hold the values passed to the method when the method is invoked. They are declared in the method header by listing the data types and name of the variables. They may be used in the method just as any other local variable may be used. The scope of the variables declared in a formal parameter list is the body of the method in whose header the list appears.

Inline methods

Methods that return a value, such as Math.sqrt(). THey are called this because they can be invoked in a Java statement "inline".

Return Type

Part of a method header, declares the data type of the value the method returns. A method that returns a value can be called form any place in another Java method where a variable or literal of the same data type can be used. For example, the Math.sqrt() method returns a value of data type double. Therefore, it can be called from any place that a variable type double or a double literal can be used, such as in a math expression. The nextline() method has a return type of String, so it can be used in a Java statement anyplace that a String value can be used.

Void

Part of the method header that indicates the method doesn't return a value.

Actual Parameters

The values specified in this list, are passed on to the variables declared in the formal parameter list. When a method is invoked, the data types of the actual parameters must match those of the formal parameters and the actual parameters must be in the same order as the formal parameters. Parameter Passing works the same with void methods as with value returning methods: the values in the actual parameter list are used to initialize the variables in the format parameter list.

Invoking a Method and Parameter Passing

There are two ways a method may be invoked from another method: A method that returns a value may be used in an expression in a Java statement: A void method may be used in another method as if it's a new user-created instruction in the Java language.

Separating Methods

You should also do something with the documentations to clearly separate one method from another in a class. Programming styles differ, so there are many ways to do this. In the case of overtime method, a line of stars and then a blank line are immediately placed at the end of the method. This makes it easier for a reader to distinguish between methods. Remember, the documentation will be stripped from the code when the software is compiled. It's there to make it easier for people to read and understand the code, including you as the author of the code who may need to come back to a method months or years later and will need to understand how it works. All documentation should be clear and reasonably concise, but ease of understanding is the most important factor. It is worth the time to make sure that readers can understand your code.

Exception list

aka thrown exception clause.

Method declaration

defines a method, with a method header, followed by a block of code marked by braces. The code for each method in Java software is defined in it. The Method declaration has 6 components, the 5 parts of the method header and the method's block of code.

Method header

includes method modifiers (such as public and static), the return type of the method, and the method name, a set of parentheses which contain the method's parameters, and an exception list. The parentheses are blank if there are no parameters. In general parts of a method header look like this [modifier][return type] method name (formal parameter list)[exception list]

Static Method

is associated with the class and not with an instance of the class. It is invoked by using the class name, and not the instance name, as part of it's qualified name. The Math class. Methods such as Math.sqrt() and Math.random() are class level methods that have been invoked using th ename of the class- the Math class. The Scanner class methods we have used were not class level methods. THey were invoked using the name of an instance of the class. We had to declare a new instance of the class associated with a particular input stream before we could use the methods such as nexLine() . If the static modifier is missing, then the method is an instance method. IF the static modifier is present, it is a class method.

Protected access

is similar to private, but methods in subclasses of a class may invoke protected methods.

Public access

means that the method may be invoked from any other software

Private access

means the method may only be invoked from within the class which contains the method

Method modifiers

provide the compiler with information about the nature of the method.

Exit conditions

should describe what value, if any, a method returns and anything else the method does that has an effect outside of the method. Does it open a new I/O stream? Does it turn a hardware device on or off? If it does anything with an effect that will be present after the method runs, then the exit conditions should say so.

Main method

the class in a Java application that has the same name as the application's software package. This class will be the method that is executed when the software application runs. IT WILL CONTAIN ANY CALS OR REFRENCES TO OTHER METHODS USED WITHIN THE APPLICATION. This method must be public, which mean sit is available to run from outside of it's own software package. This is necessary so that the operating system can start the application by running the main method. This means that the access modifier must be public The main method for a java pplication does not return any values. Hence it must be marked with the access modifier void in the method's header, which indicates it does not return any user defined values.

Access Modifier

the most common Method modifier, sets the access level for the method. Java Methods may have public private or protected access


Related study sets

PHI2600 - 3.1 Utilitarianism, The Theory

View Set

Molecular and Cellular Biology Midterm 1 Study Guide Part 1

View Set

Buying on Margin and Short Selling

View Set

Intercultural Communication Exam 3

View Set

EMT: Chapter 27 [face and neck injuries]

View Set

Iggy: Chapter 57 Care of Patients with Inflammatory Intestinal Disorders

View Set