Object Oriented Programming Articles

Page 353 of 589

Java program to swap two numbers using XOR operator

Arjun Thakur
Arjun Thakur
Updated on 05-Nov-2024 875 Views

In this article, we will learn how to swap two numbers using the XOR bitwise operator in Java. The XOR operator is a powerful tool that allows you to perform bitwise operations, and one of its interesting properties is that it can be used to swap two variables without using a temporary variable. This method is efficient and can be used when you need a fast swapping mechanism. Problem StatementGiven two integers, write a Java program to swap their values using the XOR operator.Input Two integers are provided by the user.Output The values of the two integers after swapping. ...

Read More

Java program to generate a random number from an array

karthikeya Boyini
karthikeya Boyini
Updated on 04-Nov-2024 6K+ Views

In this article, we will learn how to generate a random number from an array of integers in Java by using Random class. The Random class provides methods to generate random numbers, and we will use the nextInt(int bound) method to get a random index within the bounds of our array length. Problem Statement Given an array of integers, we need to randomly select and display one element from the array using Java. Input arr = { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200}; Output Random number from the array = 30 Steps to generate ...

Read More

Java program to get the prime numbers with BigInteger type

Nancy Den
Nancy Den
Updated on 04-Nov-2024 738 Views

In this article, we will learn how to generate prime numbers using the BigInteger class in Java. A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. Here, we have the BigInteger type, which has operations for modular arithmetic, GCD calculation, primality testing, prime generation, etc. Steps to get the prime numbers with BigInteger type Following are the steps to get the prime numbers with BigInteger type − First, we will import the BigInteger from the java.math package. ...

Read More

Java program to subtract week from current date

Samual Sam
Samual Sam
Updated on 04-Nov-2024 984 Views

In this article, we will learn to subtract the week from the current date using Java. This is useful when you need to manipulate dates, such as calculating dates from previous weeks for scheduling or tracking purposes. We'll use two approaches: using the Calendar class and the LocalDate class, showcasing how to easily adjust dates. Problem Statement Write a program in Java to subtract a specified number of weeks from the current date and display the updated date − Input Run the program Output Current Date = Mon Nov 04 09:41:18 IST 2024 Updated Date = Mon Oct 21 09:41:18 ...

Read More

Java program to put value to a Properties list

karthikeya Boyini
karthikeya Boyini
Updated on 30-Oct-2024 478 Views

In this article, we will learn how to use the Properties class in Java to store and display key-value pairs. The Properties class is useful for handling configuration settings or storing data in a structured way, where both keys and values are strings. By using the put() method, we can easily add values to a property list and later retrieve or display these key-value pairs. This approach provides a simple and efficient way to manage data in a key-value format in Java applications. Problem Statement A properties list contains country-year pairs. Write a Java program to store values ...

Read More

Java program to merge duplicates of a List with TreeSet

karthikeya Boyini
karthikeya Boyini
Updated on 30-Oct-2024 694 Views

In this article, we will learn to merge duplicate elements from a List in Java by using a TreeSet. This program will take a List of strings containing duplicate values, transfer the elements into a TreeSet, and display the unique, sorted elements. Using a TreeSet is ideal here because it automatically removes duplicates and orders the elements in natural order. Problem Statement Write a Java program to remove duplicate elements from a list using a TreeSet. The program should take a list with repeated elements, convert it into a TreeSet to eliminate duplicates, and display the unique, sorted elements. Input ...

Read More

Java program to sort a List in case sensitive order

karthikeya Boyini
karthikeya Boyini
Updated on 29-Oct-2024 1K+ Views

In this article, we will learn to sort a List in case-sensitive order in Java. Sorting in case-sensitive order places uppercase letters before lowercase letters (e.g., 'A' before 'a'), meaning that uppercase letters are prioritized in sorting. This program uses Java’s built-in Collections.sort method to arrange elements in this specific order.  Problem Statement Given a list of mixed uppercase and lowercase letters, write a Java program that sorts the list in a case-sensitive order. Input { "P", "W", "g", "K", "H", "t", "E" } Output List = [P, W, g, K, H, t, E] Case Sensitive Sort = [E, H, ...

Read More

Java program to get the reverse of an Integer array with Lambda expressions

Krantik Chavan
Krantik Chavan
Updated on 29-Oct-2024 883 Views

In this article, we will learn to get the reverse of an Integer array with Lambda expressions in Java. By utilizing the Arrays.sort() method along with a custom comparator defined as a lambda expression, we can efficiently reorder the elements of the array in descending order. Lambda expressions are a concise way to represent functional interfaces, allowing you to write cleaner and more readable code. Problem Statement Write a program in Java to get the reverse of an Integer array with Lambda expressions − Input arr = {20, 50, 75, 100, 120, 150, 170, 200} Output Integer Array elements... 20 50 ...

Read More

Java program to set the height of only a single row in a JTable with multiple rows

Smita Kapse
Smita Kapse
Updated on 29-Oct-2024 919 Views

In this article, we create a JTable in Java Swing to display information about different programming technologies and their tutorial availability. The table is set up with specific columns and rows, each containing data on a particular technology. We also demonstrate how to set a custom height for a single row in the table. Specifically, the height of the 4th row is adjusted to 30 pixels to show how you can control row height individually in a JTable. The table is then displayed within a scrollable JFrame window. table.setRowHeight(3, 30); The above sets row height to 30 pixels for row index ...

Read More

How to set preferred size for BoxLayout Manager in Java?

Nishtha Thakur
Nishtha Thakur
Updated on 29-Oct-2024 2K+ Views

In this article, we will learn to set the preferred size for a panel within a JFrame using the BoxLayout manager in Java Swing. By setting the preferred and maximum size for the panel, we can control the layout’s appearance, ensuring that the panel maintains a consistent size as other components are added. This approach is useful when arranging elements vertically or horizontally in a structured format while having specific size requirements. Steps to set preferred size for BoxLayout Manager The following are the steps for setting the preferred size for BoxLayout Manager in Java − ...

Read More
Showing 3521–3530 of 5,881 articles
« Prev 1 351 352 353 354 355 589 Next »
Advertisements