methods - Methods Why Write Methods? Many times when we are...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Methods Why Write Methods? Many times when we are writing programs we find that we want to repeat the same set of actions multiple times. When we put all the code in a single Main method, we must just write the same lines of code over and over. This leads to programs that are longer than they need to be and “ugly” code. A method is a section of code that does a particular job. When we want to execute those lines of code, we call the method. This means that instead of repeating a bunch of lines of code when we want to perform an action, we just need to include one line to call the appropriate method. This makes our code much easier to read. Declaring a Method This section will discuss the format for declaring a method. All methods must have a return type (if the method is performing a calculation and giving back a result), a name , and a number of parameters (pieces of data that the method needs to perform its calculations). Syntax Methods are declared inside the brackets for the class, but outside the brackets for the main method. For example: public class Example { public static void main(String[] args) { } //Declare method here //Can declare as many methods as you want } Here is the syntax for declaring a method: public static returnType name(params) { //code for method goes here } For now, don’t worry about what the public static part means – just put that at the beginning of all your methods. Next, the returnType is the type of data you want this method to give back. For example, if we were writing a method that computed the area of a square with integer sides, then we would
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
want to return an int – the data type of the area. If you do not want your method to give back a result, then the return type should be void . The name of method should be descriptive of what the method is doing. For example, a method that computes the area of a square should be called something like area or areaSquare . The rules for naming methods are the same as the rules for naming variables: method names can be made up of letters, numbers, and underscores, but they cannot start with a number. Finally, the params are a comma-separated list of values that the method needs to do its calculations. Each parameter is listed with a data type and a name. If the method does not need any values to do its calculations, then the params section is left off the method declaration (but the () at the end of the method is still there). Examples Suppose we want to write a method that computes and returns the area of a rectangle. First, let’s think about what information this method needs to do its computation – these will be the parameters. To compute the area of a rectangle, we need its width and its height. Next, we need to determine the data type of these values. We know that the width and height will be numbers, but they can be either ints or doubles – depending on what kind of values we want to allow. We’ll be more general and make them doubles.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

methods - Methods Why Write Methods? Many times when we are...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online