In Java, super refers to the parent class instance, and to inherit members of the super class to the child class, we use the extends keyword. Before writing a Java program to allocate and initialize super class members using a constructor, let's go through some concepts we are going to use in this article. What is Constructor? A Java constructor is a class member that initializes instance variables of a class. It is very similar to a method, but the difference is that methods can return a value, but a constructor does not return any value because it can't have any return ... Read More
Constants in Java A constant is a variable whose value cannot be changed once it has been initialized. Java doesn't have built-in support for constants. To define a variable as a constant, we just need to add the keyword "final" in front of the variable declaration. It is not mandatory that we assign values to constants during declaration. Syntax of Java Constant final float pi = 3.14f; The above statement declares the float variable "pi" as a constant with a value of 3.14f. We cannot change the value of "pi" at any point in time in the program. ... Read More
In number theory, an Armstrong number is a pattern-based number whose sum of each digit raised to the power of total number of digits is equal to the given number itself. This article aims to explain Java programs that checks whether the given number is Armstrong or not. How to Check Armstrong Number? To check whether a number is an Armstrong or not, first, determine the total number of digits and assume it 'n'. Then separate each digit and raise them to the power of 'n'. In the last step, calculate the power of each digit and add all ... Read More
When the sum of factors of a given number (after discarding the given number) is equal to the number itself is called a perfect number. The sum of these divisors is known as the aliquot sum. Therefore, we can say that a number is perfect if it matches its aliquot sum. All known perfect numbers are even. Problem Statement In this article, we will create Java programs to check if a given number is perfect or not. Let's try to understand the given problem through some examples: Example Scenarios: Input: number = 496 Output: 496 is a perfect number ... Read More
We are given an integer number as input, and our task is to write a Java program to check whether that number is even or odd. If a number is divisible by 2, then it is an even number otherwise, it is odd. To verify a given number is even or odd in Java, we can use 3 different ways one with the modulus operator (%), another with the bitwise AND operator (&), and a third one using a switch statement. Example Scenarios: Let's understand the problem statement with some example scenarios: Input: number = 45 Output: result = odd ... Read More
The octal number system has a base value of 8 as it contains digits from 0 to 7, i.e., 8. On the other hand, the decimal number system has 10 digits from 0 to 9. Hence, its base value is 10. In this article, we will learn to convert decimals to the octal number system in Java. Problem Statement The goal is to write a program to convert a given decimal number (base 10) into its equivalent octal number (base 8). Example Scenario: Input: int decimalNumber = 8 Output: The octal value is = 10 Converting Decimal to Octal in ... Read More
A JSONArray is a class provided by the org.json package that represents a collection of JSON values. These values can be of any type, such as strings, numbers, booleans, or even nested objects or arrays. If you do not know what JSON is, then you can read the JSON tutorial. Converting JSON Array to String ArrayWe can convert a JSONArray to String Array by using a simple loop as shown in the example below - import org.json.*; import java.util.*; public class JsonArraytoStringArrayTest { public static void main(String[] args) { ... Read More
In this article we are given a sorted array, our task is to find if there exists any pair of elements such that their sum is equal to a given number. Users must be familiar with array, loop and nested loops, and if/else statement. Two Pointers Technique Two pointers technique is a commonly used algorithmic approach to solve various problems that require linear time complexity. This technique is used to find a solution for problems that involve searching, sorting, or manipulating arrays, strings, or linked lists. In this article, we are going to solve the pair sum problem using brute ... Read More
To find the lost element from a duplicated array in JavaScript, we will be discussing various approaches.Prerequisite to solve this problem requires an understanding of JavaScript arrays, loops, set object and binary search. In this article we are having two arrays where one array is duplicate of other with a missing element, our task is to find the lost element from a duplicated array in JavaScript. Example Input: array1 = [1, 2, 3, 4, 5, 6] array2 = [1, 2, 3, 5, 6] Output: Missing element: 4 Approaches to Find the Lost Element Here ... Read More
Midpoint of a Line The midpoint refers to a point that is exactly in the middle of the line segment, which is joined through two points. These are the endpoints of a line segment and the midpoint lying in between these two points. The following diagram will provide a better understanding: Midpoint Formula The midpoint formula is defined for the points in the coordinate axes (i.e., x and y). Let's consider (x1, y1) and (x2, y2) are the endpoints of a line segment. The midpoint will be equal to half of the sum of the x-coordinates of the two ... Read More