Find the Area of a Square in Java

Shriansh Kumar
Updated on 11-Sep-2024 11:05:11

9K+ Views

Given a square whose length of all its sides are l, write a Java program to find its area. A square is a rectangle whose length and breadth are same. Therefore, area of this type of rectangle is the square of its length. To calculate the area of square in Java, you simply need to multiply the given length of square with the length itself and store the result in another variable. Java Program to Find The Area of a Square A Java program that illustrates how to find the area of the square is shown below − public class ... Read More

Print Months in Different Formats in Java

Saba Hilal
Updated on 11-Sep-2024 11:02:56

840 Views

In this article, we are going to use various approaches for formatting the months using different libraries of Java programming language. There are several ways to display months. Sometimes the months are written as numbers, sometimes the months are written in long form or they are written in short forms. Using java.time.Month In this approach, the months are printed by specifying the month number that starts from the number 1. For instance -> Month.of(1) will give JANUARY. Example In this Java program, we are printing all 12 months name. import java.time.Month; public class ShowMonth { ... Read More

Find Smallest of Three Numbers Using Ternary Operators in Java

Shriansh Kumar
Updated on 11-Sep-2024 11:02:27

2K+ Views

To find the smallest from three given numbers using ternary operator, create a temporary variable to store the result of first comparison operation between two numbers. Then, perform second comparison operation between the result of first operation (i.e. temp) and third number to get the final result. Let's understand the problem statement with an example − Example Scenario: Input: nums = 20, 35, 10; Output: res = 10 What is Ternary Operator? The conditional operator in Java is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal ... Read More

Check If a String Contains Only Certain Characters in Java

Shriansh Kumar
Updated on 11-Sep-2024 11:00:41

2K+ Views

For a given string, say "str", write a Java program to check if it contains each character from a specified character array. If it contains print "found" otherwise "not found". A String in Java is a class that represents a contiguous block of characters. Example Scenario: Input 1: String str = "pqrst"; Input 2: char[] chSearch = {'p', 'q', r'}; Output: res = Character p, q and r found in given string In the string "pqrst", we are searching for the character set {'p', 'q', r'}. Using Iteration Here, use for loop to iterate till the length of the ... Read More

Filter Associative Array Using Another Array in JavaScript

Shriansh Kumar
Updated on 11-Sep-2024 10:59:11

1K+ Views

In this problem statement, our task is to write a JavaScript program by which we can simply filter an associative array with the help of another array. Before discussing the solution for the given problem, let's familiarize ourselves with associative array first. What is an associative array? An associative array is a data structure which is used to store the key and value pairs. In this each key is associated with a value given to the key. The keys in this array are unique identifiers that can be used to retrieve their respective values. In an associative array the keys ... Read More

Replace Each Element of Array with Its Next Element in Java

Shriansh Kumar
Updated on 11-Sep-2024 10:56:49

1K+ Views

As per the problem statement we have to write a Java program to replace each element of the array with its next element. In Java, the array is an object. It is a non-primitive data type which stores values of similar data types. Before discussing the solution for the given problem, let's understand the problem statement with an example − Example Scenario: Input: arr[] = {12, 23, 11, 64} Output: updated array = {23, 11, 64, 12} Algorithm Follow the steps below to replace each element of the array with its next element − Step 1 − Declare ... Read More

Java Substring Comparisons

Shriansh Kumar
Updated on 11-Sep-2024 10:37:01

2K+ Views

Given a string and its substring(s) of length k, write a Java program to compare and find whether the substrings are equal or not. Substring is a small portion of characters from a large string. In Java, a String is a class that represents a contiguous block of characters. Using compareTo() Method The compareTo() method belongs to the String class of java.lang package. It compares two strings based on the Unicode value of each character contained in the strings. It returns 0 if the specified strings are equal. Example In this example, we are using the compareTo() method to ... Read More

Count the Number of Digits in a Given Integer in Java

Shriansh Kumar
Updated on 11-Sep-2024 10:36:31

23K+ Views

Suppose an integer number is given as an input, our task is to write a Java program to count the number of digits in that integer. For this problem, create a counter variable and initialize it with 0. Divide the given integer value with 10 till the integer becomes 0 and increment the counter variable for each turn. Using while Loop A while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. For the given problem, we use this loop to check whether the specified integer value is ... Read More

Static Blocks in Java with Example

Shriansh Kumar
Updated on 11-Sep-2024 10:35:18

5K+ Views

A block of code that is associated with the static keyword is called as static block. This block executes when classloader loads the class. Remember, if your code contains any static block, it will be invoked before the main() method. In this article, we will learn how to create and invoke static blocks in Java along with its use case. But, before that let's understand the static keyword. What is Static Keyword? The static keyword in Java is a non-access modifier. This keyword is used with variables, methods, blocks of code and classes. A class, method or variable declared with ... Read More

Illustrate Escaping Characters in Regex with Java

Shriansh Kumar
Updated on 11-Sep-2024 10:23:59

1K+ Views

The special characters, also known as metacharacters, in Java Regex holds a specific meaning within the regex syntax and must be escaped if you want to use them as regular characters. Here, we will demonstrate escaping characters in Regex through Java Program. But, before diving deep into the topic, let us get familiar with the term Regex in Java. What is Regex? It is an acronym for a Regular expression. It is an API that offers users to define String patterns that are useful for finding, modifying, and editing strings. A couple of areas of strings where Regex is ... Read More

Advertisements