User Defined Methods in Java Class 10 | All Programs with Output
Chapter 3: User Defined Methods Question 1: WAP in Java to create a method to accept any two values from the user and multiply them. Store and display the result. Solution: import java.util.Scanner; class MultiplyValues { // Method to multiply two numbers void multiply(int a, int b) { int result = a * b; System.out.println("Multiplication = " + result); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); // Creating object MultiplyValues obj = new MultiplyValues(); // Accepting values from user System.out.print("Enter first value: "); int x = sc.nextInt(); System.out.print("Enter second value: "); int y = sc.nextInt(); // Calling method obj.multiply(x, y); } } Input: Enter first value: 10 Enter second value: 2 Output: Multiplication =20 Question 2: WAP for method overloading by creating a method ...