Found 9321 Articles for Object Oriented Programming

Java Program to implement private constructors

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

193 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

307 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

900 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

302 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

382 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

819 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

Java Program to Display Factors of a Number

AmitDiwan
Updated on 22-Feb-2022 08:15:19

6K+ Views

In this article, we will understand how to display factors of a number. Factor are number that divides another number or expression evenly.Factors are the numbers we multiply to get another number. For example, if we multiply 3 and 5, we get 15. We say, 3 and 5 are factors of 15. Alternatively, factors of a number are those numbers which divide that number without leaving any remainder. For example, 1, 2, 3, 4, 6, and 12 are factors of 12 as all of them divide it evenly.The largest and smallest factors of a number. The largest factor of any ... Read More

Java Program to Display Prime Numbers Between Intervals Using Function

AmitDiwan
Updated on 22-Feb-2022 07:59:51

389 Views

In this article, we will understand how to display prime numbers between intervals using function. 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 the same −InputSuppose our input is ... Read More

Java Program to Display Prime Numbers Between Two Intervals

AmitDiwan
Updated on 22-Feb-2022 07:50:11

2K+ Views

In this article, we will understand how to display prime numbers between two intervals. 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 the same −InputSuppose our input is −Starting ... Read More

Advertisements