Found 4330 Articles for Java 8

Java Program to divide a number into smaller random ints

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

363 Views

We have considered a number 10 here, which will divided into 8 random ints with Random class. The number we have set as HashSet collection −HashSetset = new HashSet(); set.add(0); set.add(0); set.add(0); set.add(number);Now use nextInt to get the next random integer −intarrSize = parts + 1; while (set.size() < arrSize) {    set.add(1 + randNum.nextInt(number - 1)); } Integer[] dividers = set.toArray(new Integer[arrSize]); Arrays.sort(dividers); int[] res = new int[parts]; for(int i = 1, j = 0; i < dividers.length; ++i, ++j) {    res[j] = dividers[i] - dividers[j]; }Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Random; public class Demo {   ... 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

3K+ 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

280 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

1K+ 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

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

558 Views

Let’s say the value is 0.10705921712947473 and you want to save only the first 3 decimal digits. At first let us declare the values −double val = 320.0 / 2989.0; BigDecimal big = new BigDecimal(val);Now, use setScale() and set the parameter as 3 to save 3 decimal places −big = big.setScale(3, RoundingMode.HALF_DOWN);Example Live Demoimport java.math.BigDecimal; import java.math.RoundingMode; public class Demo {    public static void main(String[] args) {       double val = 320.0 / 2989.0;       BigDecimal big = new BigDecimal(val);       System.out.println(String.format("Value = %s", val));       // scale       big ... 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

How to make an Android device vibrate?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

867 Views

In android, using vibrate service, we can vibrate android mobile. This example demonstrate about how to make an Android device vibrateStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have taken textview, when you click on textview. it will vibrate.Step 3 − Add the following code to src/MainActivity.javapackage com.example.andy.myapplication; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.VibrationEffect; import android.os.Vibrator; import android.support.v7.app.AppCompatActivity; import android.view.View; import ... Read More

How to Launch an application from another application on Android

Chandu yadav
Updated on 30-Jul-2019 22:30:25

6K+ Views

In android, we can lunch other applications using packing name. This example demonstrate about How to Launch an application from another application on Android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have created one textview. When you click on textview, it will open YouTube.Step 3 − Add the following code to src/MainActivity.javapackage com.example.andy.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import ... Read More

How to Go back to previous activity in android

George John
Updated on 30-Jul-2019 22:30:25

4K+ Views

If you wants to go back from one activity to another activity, This example demonstrate about how to go back to previous activity in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     In the above code, we have given text view, when the user click on text view, it will open new activity.Step 3 − Add the following code to src/MainActivity.javapackage com.example.andy.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import ... Read More

Advertisements