Tugas Introduction To Programming Brayden Assignment 11 PDF
Tugas Introduction To Programming Brayden Assignment 11 PDF
NIM : 2702247973
1. What are the benefits of using a method? How do you define a method? How do you invoke
a method?
a. Benefit
- To reduce redundant code
- To enable code reuse
- To improve the quality of program
- Modularize code
- To let the code readable
- To narrows the scope of debugging
b. How to define a method
To call a method you just need to type the name of method followed by () which you
can fill parameters if it required to.
2. What would be wrong with not writing a return statement in a value-returning method? Can
you have a return statement in a void method? Does the return statement in void method
cause syntax errors?
- You will get an error if you don’t write a return statement in value returning method
In a void method (a method that does not return any value), you can still use a return
statement without a value. This is often used to exit the method prematurely or
indicate that the method has completed its execution.
Or you can use “return true;” , “return a;” if it just “return; “only it will error
3. Define the terms parameters, argument, and method signature
- Parameter = Parameters are variables used in a method or function to receive values
that are passed to it when the method is called.
- Arguments = Arguments are the actual values that are passed to a method or
function when it is called
- Arguments = Arguments are the actual values that are passed to a method or
- function when it is called
4.
Eror =
- Add return statement after public static
- Set variable m in the parameter of method1
- Put integer in method1
- The method2 can’t accept the argument of 4 to put in parameter list, because
method2 only have 1 parameter which is n. so add int m into it
5. In this program there are 2 methos name “ method” that has a same type of parameter, in
java the metode must be divide by each type and by order of the parameter. In this case,
both method “method” has the same parameter ‘int’ this is not allowed in java. So the
solution is we change the name of one of the method or change the type or the amount of
parameter for making the method becomes unique. Example :
6. The code is error because there is a variable ‘n’ declaration occurs twice. First is in ‘n’ is
declared as a parameter of ‘nPrintin’ method and the second ‘n’ is redeclared inside that
method block. This makes the program error.
So, the solution is not to declare the same variable twice and just replace the value, and if
we still wanted to use the ‘n’ variable without changing the the parameter, just remove the
int n declaration from the parameter and just write ‘int n =1;’ inside the method
7.