Java Program to Illustrate Use of Methods in a Class

«
»
This is a Java Program to Illustrate Use of Methods in a Class.

Here we have made different methods to perform different arithamatic operations. Hence we call these methods from main method to get the output accordingly.

Here is the source code of the Java Program to Illustrate Use of Methods in a Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. public class Method
  2. {
  3.     void addition(int a,int b)
  4.     {
  5.         int c = a + b;
  6.         System.out.println("Result:"+c);
  7.     }
  8.     void subtraction(int a,int b)
  9.     {
  10.         int c = a - b;
  11.         System.out.println("Result:"+c);
  12.     }
  13.     void multiplication(int a,int b)
  14.     {
  15.         int c = a * b;
  16.         System.out.println("Result:"+c);
  17.     }
  18.     void division(int a,int b)
  19.     {
  20.         double c =(double)a / b;
  21.         System.out.println("Result:"+c);
  22.     }
  23.     public static void main(String args[])
  24.     {
  25.  	Method obj = new Method();
  26.         obj.addition(10, 4);
  27.         obj.subtraction(10, 4);
  28.         obj.multiplication(10, 4);
  29.         obj.division(10, 4);
  30.     }
  31. }

Output:

$ javac Method.java
$ java Method 
 
Result:14
Result:6
Result:40
Result:2.5

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn