Found 33676 Articles for Programming

Java program to convert LocalDateTime to java.util.Date

karthikeya Boyini
Updated on 29-Nov-2024 19:11:15

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

Java program to divide a number into smaller random ints

Alshifa Hasnain
Updated on 24-Feb-2025 18:27:35

554 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

Java Program to calculate the area of a triangle using Heron's Formula

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

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

How to convert Integer array list to integer array in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

Java Program to get prime numbers using the Sieve of Eratosthenes algorithm

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

394 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

Java Program to create a Calculator

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

Java Program to Save Decimal

Alshifa Hasnain
Updated on 13-Dec-2024 21:49:28

883 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

Java Program to evaluate mathematical expressions in String

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

ipaddress - IPv4/IPv6 manipulation library in Python

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

930 Views

Internet Protocol is currently in the process of moving from version 4 to version 6. This is necessitated because version 4 doesn’t provide enough addresses to handle the increasing number of devices with direct connections to the internet.An IPv4 address is composed of 32 bits, represented into four eight bit groups called as "octets". This is a "dotted decimal" format where each eight-bit octet can have a decimal value 0 to 255.For example: 192.168.1.1IPv4 address with CIDR notation: 192.168.1.1/24 where 24 means first three octets identify the network and last octet identifies node.An IPv6 address is 128 bits long. It ... Read More

urllib.robotparser - Parser for robots.txt in Python

Nitya Raut
Updated on 30-Jul-2019 22:30:25

705 Views

Web site owners use the /robots.txt file to give instructions about their site to web robots; this is called The Robots Exclusion Protocol. This file is a simple text-based access control system for computer programs that automatically access web resources. Such programs are called spiders, crawlers, etc. The file specifies the user agent identifier followed by a list of URLs the agent may not access.For example#robots.txt Sitemap: https://example.com/sitemap.xml User-agent: * Disallow: /admin/ Disallow: /downloads/ Disallow: /media/ Disallow: /static/This file is usually put in the top-level directory of your web server.Python's urllib.robotparser module provides RobotFileParser class. It answers questions about whether ... Read More

Advertisements