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

Info icon This 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!”;
Image of page 1

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

View Full Document Right 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’.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern