Object Oriented Programming Articles

Page 350 of 589

Java program to find largest of the three numbers using ternary operators

Arjun Thakur
Arjun Thakur
Updated on 11-Dec-2024 2K+ Views

In this article, we’ll learn to find the largest of three numbers using the ternary operator in Java. This simple yet effective approach allows you to write concise and readable code. Finding the largest of three numbers is a common programming task, and Java provides an efficient way to do this using the ternary operator. This concise approach reduces code complexity while maintaining readability. What is the Ternary Operator in Java?  The ternary operator is a shorthand for if-else statements in Java. It uses the syntax: condition ? value_if_true : value_if_false This operator is useful for simple conditional checks and ...

Read More

Java Connection setSavepoint() method with example

Alshifa Hasnain
Alshifa Hasnain
Updated on 11-Dec-2024 1K+ Views

When working with databases in Java, there are scenarios where you might want to perform complex transactions involving multiple steps. To maintain control and flexibility during such operations, Savepoints come into play. The setSavepoint() method in the java.sql.Connection interface allows you to create a savepoint within a transaction, providing a mechanism to roll back to a specific point if needed. What is Savepoint? A Savepoint is a marker within a database transaction. It enables partial rollbacks, meaning you can undo a subset of changes made during a transaction without affecting the entire operation. This is particularly useful in scenarios where ...

Read More

Find All the Subarrays of a Given Array in Java

Mr. Satyabrata
Mr. Satyabrata
Updated on 10-Dec-2024 56K+ Views

An array is a linear data structure in which elements are stored in contiguous memory locations. As per problem statement, we have to find all the subarrays of a given array. Subarrays are part or a section of an array. When we talk about all subarrays of an array, we talk about the total number of combinations that can be made using all the elements of the array without repeating. Let’s explore the article to see how it can be done by using Java programming language. To Show you Some Instances Instance-1 Suppose we have the below array [10, ...

Read More

Java Program for Gnome Sort

AmitDiwan
AmitDiwan
Updated on 04-Dec-2024 505 Views

Gnome Sort, also known as Stupid Sort, is a simple sorting algorithm that works by iterating through a list, comparing adjacent elements, and swapping them if they are in the wrong order. If a swap occurs, the algorithm moves one step backward to recheck the order, otherwise, it moves forward. This process continues until the array is sorted. In this article, we will explain the working of Gnome Sort using Java. Gnome Sort Approach Start from the first element. Compare the current element with the previous one: ...

Read More

Java program to sort string stream with reversed comparator

Krantik Chavan
Krantik Chavan
Updated on 23-Nov-2024 806 Views

In this article, we will learn how to sort a stream of strings using a reversed comparator in Java. Java 8 introduced the Stream API, which allows powerful operations like sorting using custom comparators. Java Comparator A Comparator is a functional interface in Java that defines custom sorting logic. It compares two objects and returns a result based on the comparison. Java Stream A Stream is a sequence of elements that can be processed in parallel or sequentially, supporting methods like sorting, filtering, and mapping. Sorting string stream with a reversed comparator The following are the steps for sorting a ...

Read More

Java Program to Represent Linear Equations in Matrix Form

Rudradev Das
Rudradev Das
Updated on 23-Nov-2024 724 Views

In this segment of Java programming, we are going to learn and discover certain programs by which we can represent linear equations in Matrix form. To do these programs, we first have to learn about linear equations and Matrix forms, their types, and how they are solved by simple mathematical methods. We will learn how to integrate a scanner class of java.util package to take input from the user using Java build code. The array will initialize to store some variables as input for the problem matrix. Then, it will be converted into a loop by which the problem equation will ...

Read More

Java program to remove duplicate elements from a List

AmitDiwan
AmitDiwan
Updated on 23-Nov-2024 918 Views

In this article, we will learn to remove duplicate elements from a List in Java. We will be using two methods: LinkedHashSet and the Stream API. First, we'll create a list with duplicate values and use LinkedHashSet to remove them while maintaining the order. Then, we’ll use the Stream API to filter out duplicates using distinct(). By the end, you’ll see how both methods work to clean up the list while keeping the original order. List in Java The List interface in Java, part of the Collection framework, represents an ordered collection of elements that allows duplicates and provides index-based ...

Read More

Java program to add and remove elements from a set which maintains the insertion order

karthikeya Boyini
karthikeya Boyini
Updated on 19-Nov-2024 741 Views

In this article, we will learn how to use a LinkedHashSet in Java to add and remove elements while maintaining the insertion order. You will see how elements are stored, removed, and updated in a LinkedHashSet without altering the sequence in which they were added. Problem Statement Write a Java program to add elements to a LinkedHashSet, remove specific elements, and display the updated set while ensuring the insertion order of the elements remains unchanged. Below is a demonstration of the same − Input Set = [20, 60, 80, 120, 150, 200, 220, 260, 380] Output Set = [20, 60, ...

Read More

Java Program for Common Divisors of Two Numbers

AmitDiwan
AmitDiwan
Updated on 18-Nov-2024 805 Views

In this article, we will learn to find the common divisors of two numbers using Java. The program will use a recursive method to calculate the greatest common divisor (GCD) of the two numbers, and then determine how many divisors are shared by both numbers. The output will display the total number of common divisors. Problem Statement Write a Java program to find and count the common divisors of two given numbers. Below is a demonstration of the same − Input val_1= 68 val_2= 34 Output The common divisors between the two numbers is4 Steps to find the common divisors ...

Read More

Java program to print a Fibonacci series

Samual Sam
Samual Sam
Updated on 18-Nov-2024 3K+ Views

The Fibonacci Series generates subsequent numbers by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken as 0, 1, or 1, 1 respectively. Fn = Fn-1 + Fn-2 Problem Statement Write a program in Java program to print a Fibonacci series. Input Run the program Output 1 1 2 3 5 8 13 21 34 55 The above output is obtained when the values of n=10. Different approaches to print a Fibonacci series Following are the different approaches to printing a Fibonacci series − ...

Read More
Showing 3491–3500 of 5,881 articles
« Prev 1 348 349 350 351 352 589 Next »
Advertisements