04_methods - Java 211 Methods Yosef Mendelsohn Methods It...

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

View Full Document Right Arrow Icon
Java 211 Methods Yosef Mendelsohn Methods It is surely no surprise to you that computer programs can be very long and complex. For this reason, there are many ways of organizing programs. One of the key ways to organize a program is to divide it into specific tasks. Every task (and often, individual parts of a given task) may be included in its own little section called a method . (Some people and programming languages will also call them functions . There are subtle distinctions, but we won’t worry about them here. For now, you may hear me use the terms interchangably). Methods have another valuable use: They are a great way to allow you to repeat code over and over again. For example, suppose you find that in a program you want to ask the user for a number and then determine the square root of that number. Suppose also that you will want to do this over and over again in your program. This is a great time to use a method. In other words, whenever you want to do this little bit of functionality, you don’t have to go back and re-type (or copy-paste) the code into your program. You can simply invoke the method that you have already written. Don’t confuse methods with loops. A loop repeats code over and over again, but only at the current location in a program. A method allows your to repeat code, but at any location in the program. A method is a collection of programming statements that is given a specific name. When you “invoke” a method (by giving its name), you are transferring the flow of control to that group of statements. When the method has completed, flow returns to the location from which the method was called. Pre-Defined Methods Are methods that have been pre-created for us by the people who created Java. For example, the length() method used with String objects is a pre-defined method. So are all of those methods seen in the String API. Let’s look at a few of those methods now: String s = “Hello DePaul!”; System.out.println( s.charAt(0) ); //outputs an ‘H’ System.out.println( s.charAt(12) ); //outputs a ‘!’ System.out.println( s.charAt(13) ); //error – there is no 13 th index! System.out.println( s. charAt (s.length()-1) ); //okay too! String s1 = “Hello DePaul!”; String s2 = “Hello DePaul!”;
Background image of page 1

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

View Full DocumentRight Arrow Icon
if ( s1.equals(s2) ) //returns true if ( s1.equals(“Hello DePaul!”)) //also returns true If you look at a class called ‘Math’, you will see numerous pre-defined methods that are available to us for basic mathematical functionality. For example, to calculate a square root, there is a method called ‘sqrt’ that accepts any numeric data-type and returns it’s square root. Here is the “header” of that method: static double sqrt (double a) This tells you that the method accepts one argument of type double. By reading the information in the API you can also see that the method returns a double corresponding to the square root of ‘a’. Therefore, you should be able to invoke the method by typing something like:
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.

This note was uploaded on 02/15/2011 for the course CSC 211 taught by Professor Stella during the Winter '09 term at Ill. Chicago.

Page1 / 10

04_methods - Java 211 Methods Yosef Mendelsohn Methods It...

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