Docstoc

Methods

Document Sample
Methods Powered By Docstoc
					                                                                                                1
Extra practice on methods
This exercise provides practice at the following learning objectives:
      Defining methods with parameters.
      Using methods, by calling them with actual arguments.
      Overloading methods (two methods with the same name but different signatures).
      Statements:
               Conditionals
               Loops
               Blocks
In addition, it reinforces the following learning objectives:
      Declare variables: primitive-type and object-type.
      Give values to variables: primitive-type and object-type.
      Use variables: primitive-type and object-type.
      Statements:
               Assignment
      Creating new objects by using new.
      The dot notation.


Instructions:
   0. Create a new Basic Console project.


   1. Stage 1: Using methods that someone else wrote.
           a. Input a String from the user.
           b. Print:
                          The length of the String
                          The first 3 characters in the String followed immediately by the
                           last 3 characters in the String
                          The first 8 characters of the String, but with all of it in lower-
                           case
                          Something else interesting about the String that you can obtain
                           by using String methods.
                                                                   2
2. Stage 2: Writing your own methods.
      a. Implement and test the following method:
          /**
           * Prints the given String in reverse order.
           *
           * @param string The String to print in reverse order.
           */
          private static void printReverse(String string);



      b. Implement and test the following method:
          /**
           * Returns the given String in reverse order.
           *
           * @param string The String to reverse.
           * @return The given String in reverse order.
           */
          private static String reverse(String string);



      c. Implement and test the following method:
          /**
           * Returns the first n characters of the given String
           * in reverse order, where n is the 2nd parameter.
           *
           * @param string The String to reverse.
           * @param n Only the 1st n characters of the given
           *          String are to be reversed.
           * @return The first n characters of the given String
           *         in reverse order, where n is also given.
           */
          private static String reverse(String string, int n);
                                                                                        3
           d. Implement and test the following method:
              /**
               * Returns the String composed of every other
               * character of the given String.
               * For example, given “Hello”,
               * this method returns “Hlo”.
               *
               * @param string The given String.
               * @return The String composed of every other
               *         character of the given String.
               */
              private static String everyOther(String string);



   3. Stage 3: Using methods that you wrote.
           a. Implement and test the following method:
              /**
               * Returns the String composed of every other
               * character of the given String,
               * starting at the given index.
               * For example:
               *   -- given “HelloThere” and 0, returns “Hlohr”.
               *   -- given “Hello” and 1, returns “elTee”.
               *   -- given “Hello” and 2, returns “lohr”.
               *
               * @param string The given String.
               * @param n The index at which to start.
               *
               * @return The String composed of every other
               *         character of the given String,
               *         starting at the given index.
               */
              private static String everyOther(String string, int n);



During the exercise:
      Ask questions as needed, but focus on learning the concept, not getting the
       exercise completed
      Whenever you ask a question, write down notes to yourself to help you keep the
       concepts and details in your mind. Keep those notes in an organized place and
       bring them with you whenever you do software development.


At the end of this exercise, determine whether you completely understand the learning
objectives listed above. Ask questions as needed. Do further follow-ups as needed.

				
DOCUMENT INFO