Ch. 6. Methods

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Return Value Type - A method may return a value. The returnValueType is the ____ ____ of the value the method returns. If the method does not return a value, the returnValueType is the keyword ____. - For example, the returnValueType in the ____ method is void.

- A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. - For example, the returnValueType in the main method is void.

Scope of Local Variables - A variable declared in the initial action part of a for loop header has its scope in the _________ ________. - But a variable declared inside a for loop body has its scope limited in the loop body from its __________ and to the __ __ __ ___ that contains the variable.

- A variable declared in the initial action part of a for loop header has its scope in the entire loop. - But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable.

Top-Down and Bottom-Up

- Both top-down and bottom-up methods are fine. - Both approaches implement the methods incrementally and help to isolate programming errors and makes debugging easy. - Sometimes, they can be used together.

Implementation: Bottom-Up

- Bottom-up approach is to implement one method in the structure chart at a time from the bottom to the top. - For each method implemented, write a test program to test it.

Scope of Local Variables (T/F): It is fine to declare i in two nesting blocks

- FALSE

- One of the benefits of methods is for _______. - The max method can be invoked from any ________ besides TestMax. - If you create a new class Test, you can _______ the max method using ________________. (eg. _______________)

- One of the benefits of methods is for reuse. - The max method can be invoked from any class besides TestMax. - If you create a new class Test, you can invoke the max method using ClassName.methodName - (eg. TestMax.max)

4 Benefits of Stepwise Refinement

- Simpler Program - Reusing Methods - Easier Developing, Debugging, and Testing - Better Facilitating Teamwork

What is an Ambiguous Invocation?

- Sometimes there may be two or more possible matches for an invocation of a method, but the compiler cannot determine the most specific match. - This is referred to as ambiguous invocation. - Ambiguous invocation is a compile error.

Scope of Local Variables (T/F): It is fine to declare i in two non-nesting blocks

- TRUE

What is Stepwise Refinement?

- The concept of method abstraction can be applied to the process of developing programs. - When writing a large program, you can use the "divide and conquer" strategy, also known as stepwise refinement, to decompose it into subproblems. - The subproblems can be further decomposed into smaller, more manageable problems.

Find Error: public static int sign(int n) { if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return -1; }

- The method shown is logically correct, but it has a compilation error because the Java compiler thinks it possible that this method does not return any value. - To fix this problem, delete if (n < 0), so that the compiler will see a return statement to be reached regardless of how the if statement is evaluated.

Scope of Local Variables What is Scope?

- The part of the program where the variable can be referenced.

Scope of Local Variables What is the scope of a local variable?

- The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. - A local variable must be declared before it can be used.

Implementation: Top-Down

- Top-down approach is to implement one method in the structure chart at a time from the top to the bottom. - Stubs can be used for the methods waiting to be implemented. A stub is a simple but incomplete version of a method. - The use of stubs enables you to test invoking the method from a caller.

Scope of Local Variables - You can declare a local variable with the ____ _____ multiple times in different ___________ _________ in a method, but you cannot declare a local variable __________ in nested blocks.

- You can declare a local variable with the same name multiple times in different non-nesting blocks in a method, but you cannot declare a local variable twice in nested blocks.

Method Abstraction - You can think of the method body as a black box that contains the detailed _____________ for the ________.

- You can think of the method body as a black box that contains the detailed implementation for the method.

Find Error: public static void someMethod() { int x = 1; int y = 1; // i is declared for (int i = 1; i < 10; i++) { x += i; } // i is declared again for (int i = 1; i < 10; i++) { y += i; } }

// Fine with no errors

1. Define a method 2. Invoke a method

1. public static int max(int num1, int num2) { int result; if (num1 > num2) { result = num1; } else { result = num2; } return result; } -------------------------------------------- 2. int z = max(x, y); where x,y are actual parameters (arguments)

What are Methods?

A method is a collection of statements that are grouped together to perform an operation.

Scope of Local Variables What is a local variable?

A variable defined inside a method.

Method Signature

Method signature is the combination of the method name and the parameter list.

Formal Parameters

The variables defined in the method header are known as formal parameters.

Actual Parameters

When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.

Find Error: public static void someMethod() { int x = 1; int y = 1; for (int i = 1; i < 10; i++) { int x = 0; x += i; } }

With Errors

3 Benefits of Methods:

• Write a method once and reuse it anywhere. • Information hiding. Hide the implementation from the user. • Reduce complexity.


Kaugnay na mga set ng pag-aaral

Bus 100 Production and Operations Management

View Set

palate, nasal cavity, pterygopalatine fossa I and II

View Set

ACT: English Rules (BRN 36) Michael D

View Set