Preview

06 - Writing Methods

 1. While it is possible to have all code written in the ______________, it is also possible to have several different methods.

  accessor method

  void variable

  constructor method

  main method

 2. __________________ allows us to name a block of code as a method and call it whenever we need it, abstracting away the details of how it works.

  Formative Polymorphism

  Fundamental Encapsulation

  Variable Hiding

  Procedural Abstraction

 3. Read the excerpt below which provides three reasons for which using mulitple methods in your code is beneficial. What is the third?
1.Organization and Reducing Complexity: 
=========================================
organize your program into small sections
of code by function to reduce its complexity. 
Divide a problem into subproblems to solve it a piece at a time.

2. Reusing Code: 
==================
avoid repetition of code. Reuse code by 
putting it in a method and calling it 
whenever needed.

3. ---------------------????????? 
=====================================
smaller methods are easier to debug and 
understand than searching through a large main method

  Efficiency and speed of compilation

  Maintainability and Debugging:

  Derivations and Variable hiding

  Creation of inaccessible methods that are well hidden

 4. When you see repeated code, that is often a signal for you to ___________________.
public static void main(String args[]) {
    System.out.println("This old man, he played one.");
    System.out.println("He played knick knack on my thumb. ");
    System.out.println("With a knick knack paddy whack, give a dog a bone.");
    System.out.println("This old man came rolling home.");
    System.out.println("This old man, he played two.");
    System.out.println("He played knick knack on my shoe. ");
    System.out.println("With a knick knack paddy whack, give a dog a bone.");
    System.out.println("This old man came rolling home.");
}

  make a new method

  delete existing methods

  create new accessor methods

  create additional variables

 5. Step 1 in creating a method is to:
Classname objectName = new Classname();

  declare an object in main or from outside the class

  create the instance variables in the object

  call the object's method

  define the method in the class

 6. Step 2 is to:
objectName.methodName(); //Step 2

  create the instance variables in the object

  declare an object in main or from outside the class

  define the method in the class

  call the object's method

 7. Step 3 is to:
// method header
public void methodName()
{
      // method body for the code
}

  define the method in the class

  declare an object in main or from outside the class

  call the object's method

  create the instance variables in the object

 8. We can make methods even more powerful and more abstract by giving them parameters. What are the parameters below?
public void verse(String number, String rhyme)
{
   System.out.println("This old man, he played " + number);
   System.out.println("He played knick knack on my " + rhyme);
}

  String

  verse, void

  number, rhyme

  verse (only one parameter)

 9. When you create your own method, the variables you define for it in the method header are called _________ parameters

  informal

  formal

  instance

  accurate

 10. When you call the method to do its job, you give or pass in _________ or actual parameters to it that are then saved in these local parameter variables.

  fights

  instances

  abstract variables

  arguments