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.

  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
advertisement

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

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.