
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

696 Views
Let us first create an ArrayList −ArrayListarrList = new ArrayList(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250);Use Comparators interface to order in reverse order with reverseOrder() −Comparator comparator = Collections.reverseOrder(); Now, sort with Collections: Collections.sort(arrList, comparator);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo { public static void main(String[] args) { ArrayListarrList = new ArrayList(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250); arrList.add(100); arrList.add(150); arrList.add(250); Comparator comparator = Collections.reverseOrder(); ... Read More

3K+ Views
Suppose a date is given in LocalDate format, our task is to write a Java program that converts it into java.util.Date. For this problem, we need to add time information to the LocalDate by combining it with a time, such as midnight, or a timezone. Both LocalDate and java.util.Date classes are used to represent a date in Java. But, the LocalDate class, introduced with the release of Java8, represents a date without timezone information, whereas, the Date class is mutable and used for representing date and time with timezone. Let's understand the problem statement with an example. Example Scenario: Input: ... Read More

2K+ Views
In this article, we will learn how to convert a LocalDateTime object to a java.util.Date object in Java. The conversion is essential when working with modern and legacy date/time APIs, ensuring compatibility between them. LocalDateTime Class The java.time.LocalDateTime class represents a specific date and time without including any time-zone details, following the ISO-8601 calendar system. For instance, it can represent values like 2007-12-03T10:15:30. This makes it ideal for scenarios where time-zone information is not required, focusing solely on the local date and time. Java Date class The java.util.Date class represents a specific moment in time, accurate to the ... Read More

555 Views
In this article, we will learn to divide a number into smaller random ints in Java. Dividing a number into smaller random integers is a useful technique in various applications such as resource allocation, gaming, and randomization processes Divide a Number into Random Parts The approach relies on generating unique random dividers within the given number and then computing the differences between consecutive dividers to form the final parts. Following are the steps to divide a number into smaller random ints in Java Create a HashSet to ... Read More

5K+ Views
Heron’s formula gives the area of a triangle when the length of all three sides are already known.Let’s say we have the following three sides of a triangle −s1 = 15191235.0; s2 = 15191235.0; s3 = 1.01235479;Now, use the Heron’s formulae to find the area −area = (s1+s2+s3)/2.0d; resArea = Math.sqrt(area* (area - s1) * (area - s2) * (area - s3));Example Live Demopublic class Demo { public static void main(String[] args) { // sides of a triangle double s1, s2, s3; double area, resArea; // three sides ... Read More

2K+ Views
To convert integer array list to integer array is not a tedious task. First, create an integer array list and add some elements to it −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, assign each value of the integer array list to integer array. We used size() to get the size of the integer array list and placed the same size to the newly created integer array −final int[] arr = new int[arrList.size()]; int index = 0; for (final Integer value: arrList) { arr[index++] = value; }Example Live Demoimport java.util.ArrayList; public ... Read More

396 Views
To find all prime numbers up to any given limit, use the Sieve of Eratosthenes algorithm. At first we have set the value to be checked −int val = 30;Now, we have taken a boolean array with a length one more than the val −boolean[] isprime = new boolean[val + 1];Loop through val and set numbers as TRUE. Also, set 0 and 1 as false since both these number are not prime −isprime[0] = false; isprime[1] = false;Following is an example showing rest of the steps to get prime numbers using the Sieve of Eratosthenes algorithm −Example Live Demopublic class Demo ... Read More

2K+ Views
To create a calculator with Java Swings, try the following code −Exampleimport java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JTextField; public class SwingDemo extends JFrame implements ActionListener { JButton one, two, three, four, five, six, seven, eight, nine, num0, add, sub, div, mult, equalto, exit, point, reset; JTextField textField; String s = "", ope = ""; int flag = 0; double total1; double input1, input2; void total(double input1, double inout2, String ope) { String total; if (ope.equalsIgnoreCase("+")) ... Read More

886 Views
In this article, we will learn two ways to handle decimal values in Java. First, we will use the BigDecimal class to ensure precision when working with decimals. Then, we will use the DecimalFormat class to format decimal values. Both examples will show how to scale decimal values to three places. Different Approaches to Save Decimal Values The following are the two approaches to save decimal values in Java − Using BigDecimal Using DecimalFormat Save Decimal Values Using BigDecimal for Precision Control The BigDecimal class of java.math package is used for ... Read More

3K+ Views
To evaluate mathematical expression in String, use Nashorn JavaScript in Java i.e. scripting. Nashorn invoke dynamics feature, introduced in Java 7 to improve performance.For scripting, use the ScriptEngineManager class for the engine −ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");Now for JavaScript code from string, use eval i.e. execute the script. Here, we are evaluating mathematical expressions in a string −Object ob = scriptEngine.eval("9 + 15 + 30"); System.out.println("Result of evaluating mathematical expressions in String = "+ob);Example Live Demoimport javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Main { public static void main(String[] args) throws Exception { ScriptEngineManager scriptEngineManager ... Read More