BIT HELPER UOM

ads header

Search This Blog

Wednesday, 10 January 2024

Advantages of Modular Programming in Java




 A genuine issue is complicated and huge. On the off chance that a solid arrangement is created it represents these issues :

  • Challenging to compose, test and execute one major program
  • Changes after the eventual outcome is conveyed is near unimaginable
  • Upkeep of program undeniably challenging
  • One blunder can stop the entire framework


To defeat these issues, the arrangement ought to be partitioned into more modest parts called modules. The strategy of separating one major arrangement into more modest modules for simplicity of advancement, execution, adjustment and support is called particular procedure of programming or programming improvement.


The following are some of the benefits of modular programming:

  •  Facilitates faster development because each module can be developed in parallel 
  •  Allows for the reuse of modules 
  •  Facilitates faster and more robust testing 
  •  Easier debugging and maintenance of the entire program 
  •  Modules are smaller and less complex, making them simple to comprehend

Because there isn't one right way to identify software modules, it's a difficult task. Here are a few pointers to recognizing modules −

  • Assuming information is the main component of the framework, make modules that handle related information.
  • In the event that help given by the framework is different, separate the framework into utilitarian modules.
  • When in doubt, separate the framework into legitimate modules according to how you might interpret the framework during prerequisite get-together stage.

For coding, every module must be again separated into more modest modules for simplicity of programming. This should again be possible utilizing the three hints shared above, joined with explicit programming rules. For instance, for an item situated programming language like C++ and Java, each class with its information and techniques could shape a solitary module.


Methods

A Java technique is an assortment of proclamations that are gathered to play out an activity. At the point when you call the System.out.println() technique, for instance, the framework really executes a few assertions to show a message on the control center.

You will now understand how to use method abstraction in program design, how to create your own methods with or without return values, how to invoke a method with or without parameters.

Strategy definition comprises of a technique header and a strategy body.
modifier − It characterizes the entrance sort of the technique and it is discretionary to utilize.
returnType − Strategy might return a worth.

nameOfMethod: The method's name here. The technique signature comprises of the strategy name and the boundary list.
Boundary Rundown − The rundown of boundaries, it is the sort, request, and number of boundaries of a technique. These are discretionary, strategy might contain zero boundaries.
strategy body − The technique body characterizes how the technique manages the assertions.


Example: 
modifier returnType nameOfMethod (Parameter List) {
   // method body
}


public static int methodName(int a, int b) {
   // body
}








Creating Method and Method Calling


Here is the source code of the above characterized technique called min(). This method takes two parameters, n1 and n2, and returns the difference between them that is the smallest. A method can be called either "method returns a value" or "method returns nothing" (no return value).

The course of strategy calling is straightforward. When a program calls a method, control of the program is given to the called method. This called technique then returns control to the guest in two circumstances, when −
the return proclamation is executed.
it arrives at the strategy finishing shutting support.


Example:
public class ExampleMinNumber {
   
   public static void main(String[] args) {
      int a = 11;
      int b = 6;
      int c = minFunction(a, b);
      System.out.println("Minimum Value = " + c);
   }

   /** returns the minimum of two numbers */
   public static int minFunction(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;

      return min; 
   }
}



/** the snippet returns the minimum between two numbers */

public static int min(int n1, int n2) {
   int min;
   if (n1 > n2)
      min = n2;
   else
      min = n1;

   return min; 
}




The Void Keyword :

Here is the source code of the above characterized technique called min(). This method takes two parameters, n1 and n2, and returns the difference between them that is the smallest. A method can be called either "method returns a value" or "method returns nothing" (no return value).

The course of strategy calling is straightforward. When a program calls a method, control of the program is given to the called method. This called technique then returns control to the guest in two circumstances, when −
  • the return proclamation is executed.
  • it arrives at the strategy finishing shutting support.

public class ExampleVoid {

   public static void main(String[] args) {
      methodRankPoints(255.7);
   }

   public static void methodRankPoints(double points) {
      if (points >= 202.5) {
         System.out.println("Rank:A1");
      }else if (points >= 122.4) {
         System.out.println("Rank:A2");
      }else {
         System.out.println("Rank:A3");
      }
   }
}


Passing Parameters by Value :

While working under calling process, contentions is to be passed. These ought to be in similar request as their separate boundaries in the strategy determination. Boundaries can be passed by esteem or by reference.

Passing Boundaries by Worth means calling a strategy with a boundary. Through this, the contention esteem is passed to the boundary.

The accompanying project shows an instance of passing boundary by esteem. The upsides of the contentions continues as before even after the technique conjuring.



public class swappingExample {

   public static void main(String[] args) {
      int a = 30;
      int b = 45;
      System.out.println("Before swapping, a = " + a + " and b = " + b);

      // Invoke the swap method
      swapFunction(a, b);
      System.out.println("\n**Now, Before and After swapping values will be same here**:");
      System.out.println("After swapping, a = " + a + " and b is " + b);
   }

   public static void swapFunction(int a, int b) {
      System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
      
      // Swap n1 with n2
      int c = a;
      a = b;
      b = c;
      System.out.println("After swapping(Inside), a = " + a + " b = " + b);
   }
}





Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30

**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45



Method Overloading: 

At the point when a class has at least two techniques by a similar name however various boundaries, it is known as strategy over-burdening.

If, suppose we need to track down the base number of twofold sort. Then, at that point, the idea of over-burdening will be acquainted with make at least two strategies with a similar name however various boundaries.

Over-burdening strategies makes program lucid. Here, two strategies are given by a similar name however with various boundaries. The base number from number and twofold sorts is the outcome





public class ExampleOverloading {

   public static void main(String[] args) {
      int a = 11;
      int b = 6;
      double c = 7.3;
      double d = 9.4;
      int result1 = minFunction(a, b);
      
      // same function name with different parameters
      double result2 = minFunction(c, d);
      System.out.println("Minimum Value = " + result1);
      System.out.println("Minimum Value = " + result2);
   }

   // for integer
   public static int minFunction(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;

      return min; 
   }
   
   // for double
   public static double minFunction(double n1, double n2) {
     double min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;

      return min; 
   }
}








Methods - Using Command-Line Arguments :

In some cases you will need to pass some data into a program when you run it. By supplying main with arguments from the command line, this can be accomplished

An order line contention is the data that straightforwardly follows the program's name on the order line when it is executed. To get to the order line contentions inside a Java program is very simple. In the String array given to main(), they are stored as strings.



public class CommandLine {

   public static void main(String args[]) { 
      for(int i = 0; i<args.length; i++) {
         System.out.println("args[" + i + "]: " +  args[i]);
      }
   }
}



$java CommandLine this is a command line 200 -100


args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100




No comments:

Post a Comment

Powered by Blogger.

Student Management System in C# and MySQL

Student Management System in C# and MySQL Student Management System Developed using C# a...

Sponsor

Ahamed Hather

Popular Posts