When the method finishes executing code execution

This preview shows page 2 - 4 out of 4 pages.

When the method finishes executing, code execution returns to the point of call 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Ex01 – What is the output? private void test1(int years) { System.out.println("4. " + years); years = 20; } private void test2(int years) { years = years + 10; System.out.println("3. " + years); } public void start() { int years = 5; test1(years); System.out.println("1. " + years); test2(years); System.out.println("2. " + years); } 4. 5 1. 5 3. 15 2. 5 9 Reasons for Using Methods Ideally when we write code we would like to achieve:: Modularity. Divide up the project into logical parts. A large task can be broken up into a number of smaller tasks. Avoiding Code Repetition. Rather than cutting and pasting multiple copies of code within a program, write the code once, but call the code as many times as you need. Code Reuse. Call a method that has already been written and tested to accomplish a task rather than rewrite the code. Easy Testing. Make the code easier to test. Once a method has been tested we can use it over and over again knowing that it works correctly. 10 Example - Guess the word >java GuessApp ANAGRAM TO GUESS ***************** OUCAPTS ***************** Enter guess: caputos OCTAPUS, your guess: CAPUTOS, good guess: false >java GuessApp ANAGRAM TO GUESS ***************** PCTOASU ***************** Enter guess: octapus OCTAPUS, your guess: OCTAPUS, good guess: true 11 Example - Guess the word It is a good idea to divide large tasks into a number of smaller tasks. In this way each task is a single small programming task which is clear and easy to write. Think about the tasks involved in writing the code for the programming assignment from the previous slide. To solve the programming problem on the previous slide I have written eight java statements shown on the next slide. Put the statements in the correct order in the start() method. 12 Ex02 - Guess the word public class GuessProgram { public void start() { } } 1. userGuess = getUserGuess ("Enter guess"); 2. checkGuess = checkUserGuess (theWord, userGuess); 3. theWord = getRandomWord (); 4. displayResult (theWord, userGuess, checkGuess); 5. String theWord, anagramWord, userGuess; 6. displayAnagram (anagramWord); 7. boolean checkGuess; 8. anagramWord = createAnagram (theWord);
Image of page 2

Subscribe to view the full document.

CS101 Lecture 9 3 13 Code Reuse
Image of page 3
Image of page 4
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Subroutine, public void, public class

{[ 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