Found 9326 Articles for Object Oriented Programming

Java Program to Find Sum of Digits of a Number using Recursion

AmitDiwan
Updated on 22-Feb-2022 10:26:35

673 Views

In this article, we will understand how to find sum of digits of a number using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. ... Read More

Java Program to pass method call as arguments to another method

AmitDiwan
Updated on 14-Jun-2024 14:49:44

20K+ Views

In this article, we will understand how to pass method call as arguments to another method. We can call a method from another class by just creating an object of that class inside another class. After creating an object, call methods using the object reference variable.Below is a demonstration of the same −InputSuppose our input is −Enter two numbers : 2 and 3OutputThe desired output would be −The cube of the sum of two numbers is: 125AlgorithmStep 1 - START Step 2 - Declare two variables values namely my_input_1 and my_input_2 Step 3 - We define a function that takes ... Read More

Java Program to pass lambda expression as a method argument

AmitDiwan
Updated on 22-Feb-2022 10:10:53

431 Views

In this article, we will understand how to pass lambda expression as a method argument. A lambda expression is a short block of code which takes in parameters and returns a value.Below is a demonstration of the same −InputSuppose our input is −("Apple", "Orange", "Grapes")OutputThe desired output would be −elppA, egnarO, separG AlgorithmStep 1 - START Step 2 - We import the required packages. Step 3 - In the main function, we define an ‘ArrayList’ of data. Step 4 - This is displayed on the console. Step 5 - Now, a ‘forEach’ loop is used to iterate over the elements ... Read More

Java Program to implement private constructors

AmitDiwan
Updated on 22-Feb-2022 10:06:46

191 Views

In this article, we will understand how to implement private constructors. Private constructors allow us to restrict the instantiation of a class.Below is a demonstration of the same −InputSuppose our input is −Run the programOutputThe desired output would be −Private constructor is being called AlgorithmStep 1 - Start Step 2 - We define a private constructor using the ‘private’ keyword. Step 3 - Making a constructor private ensures that an object of that class can’t be created. Step 4 - A private constructor can be used with static functions which are inside the same class. Step 5 - The private ... Read More

Java Program to Call One Constructor from another

AmitDiwan
Updated on 22-Feb-2022 10:00:43

306 Views

In this article, we will understand how to call one constructor from another. The keyword 'this()' is used to invoke a constructor.Below is a demonstration of the same. We will displaying sum and product of two numbers while using this() −InputSuppose our input is −The numbers are defined as 12 and 30OutputThe desired output would be −The sum is: 42 The product is: 360AlgorithmStep 1 - START Step 2 - Declare an integer value namely my_sum Step 3 - In the main class, we define a ‘this’ reference to the numbers which would be used as input. Step 4 - This ... Read More

Java Program to calculate the power using recursion

AmitDiwan
Updated on 22-Feb-2022 09:57:25

877 Views

In this article, we will understand how to calculate the power using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.A recursive function is a function that calls itself multiple times until a particular condition is satisfied.Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or ... Read More

Java Program to Reverse a Sentence Using Recursion

AmitDiwan
Updated on 22-Feb-2022 09:53:19

300 Views

In this article, we will understand how to reverse a sentence using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.A recursive function is a function that calls itself multiple times until a particular condition is satisfied.Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or ... Read More

Java Program to Find Factorial of a Number Using Recursion

AmitDiwan
Updated on 22-Feb-2022 09:43:15

373 Views

In this article, we will understand how to find factorial of a number using recursion. Factorial of a number is the product of itself with each of its lower numbers. Factorial is a function applied to natural numbers greater than zero. The symbol for the factorial function is an exclamation mark after a number, like this: 5!A recursive function is a function that calls itself multiple times until a particular condition is satisfied. Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, ... Read More

Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

AmitDiwan
Updated on 22-Feb-2022 09:28:18

803 Views

In this article, we will understand how to check whether a number can be expressed as sum of two prime numbers. Prime numbers are special numbers who have only two factors 1 and itself and cannot be divided by any other number.A number is a prime number if its only factors are 1 and itself. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13 and so on. 2 is the only even prime number. All other prime numbers are odd numbers.Below is a demonstration of ... Read More

Java Program to Make a Simple Calculator Using switch...case

AmitDiwan
Updated on 22-Feb-2022 09:22:12

4K+ Views

In this article, we will understand how to construct a simple calculator using switch-case. The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.Following are the arithmetic operations we are going to perform.AdditionSubtractionMultiplicationDivisionFloor DivisionModuloBelow is a demonstration of the same −InputSuppose our input is −The two inputs: 40.0 and 12.0 Operator:%OutputThe desired output would be −The result is 40.0 % 12.0 = 4.0AlgorithmStep 1 - START Step 2 - Declare three values namely my_input_1, my_input_2 and my_result and declare a character value namely operator. Step 3 - Read the required ... Read More

Advertisements