Thursday, 19 October 2023

JAVA

Methods in JAVA

Lets get started

  1. A method is a block of code which only runs when it is called.
  2. You can pass data, known as parameters, into a method.
  3. Methods are used to perform certain actions, and they are also known as functions.
  4. Why use methods? To reuse code: define the code once, and use it many times.

https://www.highcpmrevenuegate.com/s4ttwmua2u?key=c02c9252219c2ddb8d72b452016a4796

Creating a Method

A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also create your own methods to perform certain actions:

syntax :

          public class MINtech {
            static void MINtechJava() {
              // code to be executed
            }
          }
        

Explaination --

  1. MINtechJava() is the name of the method
  2. static means that the method belongs to the Main class and not an object of the Main class.
  3. void means that this method does not have a return value.

Calling the Method

To call a method in Java, write the method's name followed by two parentheses () and a semicolon; In the following example, myMethod() is used to print a text (the action), when it is called:

for example

          public class MINtech {
            static void MINtechJava() {
              System.out.println("I just got executed!");
            }
          
            public static void main(String[] args) {
              MINtechJava();
            }
          }
        

Elaborate with a simple example :

 
        
  1. /*Create a Method(function) that will return whether the
  2. given input is a prime number or not */
  3. import java.util.*;
  4. public class MINtech{
  5. public static String MINtechJava(int n){
  6. //writing logic for a prime number
  7. int temp = 0 ;
  8. for(int i = 2 ;i<= n ;i++) {
  9. if(n%i == 0 ){
  10. temp++ ;
  11. }
  12. }
  13. if(temp == 1){
  14. return "Yeah its a prime number " ;
  15. }else{
  16. return "Nope its not a prime number " ;
  17. }
  18. }
  19. public static void main (String[] args) {
  20. Scanner sc = new Scanner(System.in);
  21. System.out.println("Enter a number that you want to check : ");
  22. int n = sc.nextInt();
  23. // calling the fuction MINtechJava
  24. System.out.println(MINtechJava(n));
  25. }
  26. }

Output

      Enter a number that you want to check : 
      199
      Yeah its a prime number 
 
      Enter a number that you want to check : 
      2024
      Nope its not a prime number 

      Enter a number that you want to check : 
      2023
      Nope its not a prime number 

        
https://www.highcpmrevenuegate.com/s4ttwmua2u?key=c02c9252219c2ddb8d72b452016a4796
https://www.highcpmrevenuegate.com/s4ttwmua2u?key=c02c9252219c2ddb8d72b452016a4796
https://www.highcpmrevenuegate.com/s4ttwmua2u?key=c02c9252219c2ddb8d72b452016a4796
https://www.highcpmrevenuegate.com/s4ttwmua2u?key=c02c9252219c2ddb8d72b452016a4796

No comments:

Post a Comment