Found 9326 Articles for Object Oriented Programming

Break Any Outer Nested Loop by Referencing its Name in Java

Neetika Khandelwal
Updated on 12-Jul-2023 12:44:02

477 Views

Programming is all about coming up with the best and most efficient ways to solve the real−world problems. There are situations when you want to exit multiple loops simultaneously. This can be accomplished in Java by simply referencing the name of the loop you want to exit. In this tutorial, we'll look at how to break any outer nested loop in Java by referencing its name. Referencing Loop Names in Java You can break out of the Java nested loop by labelling the outer loop. This can be accomplished by using a label before the outer loop followed by a ... Read More

Java Program to Find Length of the Longest Substring Without Repeating Characters

Prabhdeep Singh
Updated on 11-Jul-2023 16:49:26

818 Views

The substrings are part of the string which contains the continuous character of the string of any length from 1 to complete string. We are given a string and we have to find the length of the largest substring from the given string that only contains the unique characters. We will see three types of methods: finding every substring, sliding windows, and two-pointers. Method 1 (Finding every Substring) In this approach, we will get every substring and then will check if there are any repeated characters present or not. Example public class Solution{ // function ... Read More

Java Program To Write Your Own atoi()

Prabhdeep Singh
Updated on 11-Jul-2023 16:37:42

390 Views

atoi() function is used in C programming language and used to convert the string which is passed as the parameter to it into an integer value if the string is a valid integer otherwise it shows the undefined behavior. We will implement the atoi() function in the Java programming language. Input string str = "123" Output 123 Explanation We are given a string that represents a number so we have just got the same output. Input string str = "897c7" Output Invalid Input Explanation The given string is not a valid integer, so we have given the ... Read More

Java Program to Find Longest Common Prefix Using Word by Word Matching

Prabhdeep Singh
Updated on 11-Jul-2023 16:31:33

295 Views

We are given a set of strings and we have to find the common prefix among all of them. Prefix is a substring for a string that contains the zero index and could be of any length from 1 to a complete string. We will implement a Java program with an explanation and a discussion of the time and space complexity. Input string arr[] = {"abcdefgh", "abcdefij", "abcdzy", "abcdabacd"}; Output abcd Explanation From all the given strings, we have the first four characters the same and the remaining characters are not same for all of them. Input string arr[] ... Read More

Java Program to Check if all Rows of a Matrix are Circular Rotations of Each Other

Prabhdeep Singh
Updated on 11-Jul-2023 14:14:22

109 Views

Matrix consists of rows and columns to form a rectangular array. And circular rotations mean rotating the array's elements so that one rotation places the last element in the first position and the rest of the elements to the right. In this problem, we have given a matrix of n * n, and our task is to check if all rows of a matrix are circular rotations of each other then print “YES” otherwise print “NO”. Let's see examples with explanations below to understand the problem in a better way. Input 1 mat = [ [ 1, 5, 6], ... Read More

Java Program for Converting Roman Numerals to Decimal Lying Between 1 to 3999

Prabhdeep Singh
Updated on 11-Jul-2023 14:05:25

300 Views

The characters used in an arrangement of number notation based on the pre-Roman Roman system is called Roman numerals. The letters M, D, C, L, X, V, and I stand for 1000, 500, 1000, 50, 10, 5, and 1, respectively, and will discuss all main symbols in the below section. In this problem, we are given a string of Roman numerals and our task is to convert Roman numerals to decimals in the range of 1 to 3999. Let’s see examples with explanations below to understand the problem in a better way. Input 1 str = "MCMIX" Output 1 1909 ... Read More

Final vs Immutability in Java

Deepti S
Updated on 11-Jul-2023 11:05:02

814 Views

The "final" keyword in Java may be employed to define a constant value as well as prevent a variable, method, or class from being changed or overridden. On the other side, immutability describes an object's characteristic of keeping a constant state across the course of its existence. The values of an object don't change after it is formed. Variables, methods, and classes are constrained by the "final" keyword, but immutability goes a step farther by guaranteeing that the object's whole state is preserved. Let us learn the key differences between final vs immutability in this article. Final in Java ... Read More

Few Tricky Programs in Java

Deepti S
Updated on 11-Jul-2023 10:37:08

322 Views

Confounding Java questions stem from loops, multithreading, overloading, overriding, and more, making them challenging to navigate. Occasionally, seemingly simple questions confound us, leading to haphazard code instead of straightforward solutions. With analytical thinking, we can crack these questions even without prior knowledge. Join us as we explore tricky programs in Java. Methods Used Comments that work Named loops Method 1: Comments that work In the realm of programming, Java comments are textual statements within a program that hold no significance in terms of execution by the compiler or interpreter. The purpose behind incorporating comments into code is multifold. ... Read More

Filter Pattern in Java

Deepti S
Updated on 11-Jul-2023 10:33:38

272 Views

The Filter Design Pattern, also known as the Criteria Design Pattern, is a structural design pattern used by developers to filter objects based on different criteria. It enables decoupled filtering and logical operations by chaining multiple criteria into a single criterion. It provides two techniques for creating filters: filtering for the entire collection or filtering for a particular collection member. To apply criteria to a class, you can follow these steps: Create a class that requires filtering. Develop the criteria's interface. Implement concrete classes that meet the interface's requirements. Filter out certain objects by using a variety of criteria ... Read More

Java Program for Left Rotation and Right Rotation of a String

Prabhdeep Singh
Updated on 11-Jul-2023 08:55:14

2K+ Views

Rotation means we have to shift each character either in a forward direction or backward direction. Forward direction means right rotation (Or anticlockwise) and backward direction means left rotation (Or clockwise). In this problem, we have given a string of characters of size n and integer d. Here d is less than n. Our task is to print the left rotated string or right rotated string by d integer. Only the permutation of the current string changes, not the length or frequency of the characters in the given string. Input 1 str = “apple”, d = 2 Output 1 Left ... Read More

Advertisements