In: Computer Science
the importance of providing parameters to methods.
Ans.
Parameters/Arguments, essentially, are values that are passed into functions.
Without parameters, methods become pretty much useless. How would I be able to find the sum of n1 & n2 using the sum() function without knowing n1 & n2? That dilemma is pretty trivial but it can get even more complex.
describe how a parameter is declared within a method header. Give example.
Ans.
A method is just a chunk of code that does a particular job. But methods are set out in a certain way. You have a method header, and a method body. The header is where you tell Java what value type, if any, the method will return (an int value, a double value, a string value, etc). As well as the return type, you need a name for your method, which also goes in the header. You can pass values over to your methods, and these go between a pair of round brackets. The method body is where you code goes.
|
Describe the features of methods.
Ans.
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
To call a method in Java, write the method's name followed by two parentheses () and a semicolon
myMethod()
is the name of the method.
void
means that this method does not have a return
value.
A method can also be called multiple times.
number of methods permitted in a program.
Ans.
No fixed number of methods for a program you can make as much as you want
method naming rules.
Ans.
Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.
eg. run();
runFast();
getBackground();
the composition of a method
Ans.
Composition is one of the fundamental concepts in object-oriented programming. It describes a class that references one or more objects of other classes in instance variables. This allows you to model a has-a association between objects.
eg.
You can find such relationships quite regularly in the real world. A car, for example, has an engine and modern coffee machines often have an integrated grinder and a brewing unit.
uses:
variable scope rules.
Ans.
Variable Scope means - That limit, as far as the variable can be used.
Local variables
A variable that is declared within the method that is called local variables. It is defined in method or other statements, such as defined and used within the cache block, and outside the block or method, the variable cannot be used.
Instance variables
A non-static variable that is declared within the class but not in the method is called instance variable. Instance variables are related to a specific object; they can access class variables.
Class/Static variables
A variable that is declared with static keyword in a class but not in the method is called static or class variable.
eg.
public class Example { int salary; // Instance Variable public void show() { int value = 2; // Local variable value = value + 10; System.out.println("The Value is : " + value); salary = 10000; System.out.println("The salary is : " + salary); } public static void main(String args[]) { Example eg = new Example(); eg.show(); } } |
THANK YOU.