Found 33676 Articles for Programming

Java Program to Toss a Coin

Samual Sam
Updated on 28-Jun-2024 11:20:56

5K+ Views

Tossing a coin is flipping a coin into the air and letting it fall back down. When you toss a coin, it's like a game where you can choose heads or tails, and the side that lands facing up is the result. This method is used when we want to make decisions or settle things randomly. Problem Statement Create a Java program to simulate the flipping of a coin 10 times, recording the number of times the result is "heads" and "tails". Output Chances = 10 Heads: 3 Tails: 7 Algorithms Step-1: Create an instance of the "Toss" ... Read More

Java program to get random letters

karthikeya Boyini
Updated on 05-Sep-2024 11:24:47

2K+ Views

In this article, we will learn to write a program in Java to get the random letters in both lowercase and uppercase. We will be using Random class from java.util package.The program will first create random lowercase letters by selecting them from the alphabet, and then it will generate random uppercase letters in a similar way.  Problem Statement Write a program in Java to get the random letters both uppercase and lower case. Below is the demostration − Output Lowercase random letters...dlhjsUppercase random letters...BKIZN Step to get random letters Following are the steps to get the random letter using Java ... Read More

How can I generate random booleans in Java?

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

345 Views

To generate random booleans like TRUE or FALSE, at first create a new Random object −Random randNum = new Random();Now, loop through the count of booleans you want and generate random booleans with nextBooleans() method −for (int i = 1; i

How to convert Float array list to float array in Java?

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

3K+ Views

Let us first create a float array list −ArrayList < Float > arrList = new ArrayList < Float > (); arrList.add(5.2 f); arrList.add(10.3 f); arrList.add(15.3 f); arrList.add(20.4 f);Now, convert the float array list to float array. At first, we have set the same size to float array i.e. the same number of elements. After that, we have assigned each value −final float[] arr = new float[arrList.size()]; int index = 0; for (final Float value: arrList) {    arr[index++] = value; }Example Live Demoimport java.util.ArrayList; public class Demo {    public static void main(String[] args) {       ArrayListarrList = new ... Read More

How to generate large random numbers in Java?

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

678 Views

For large random numbers, use BigInteger type in Java. At first, create a Random object −Random randNum = new Random();Now, declare a byte array and generate random bytes −byte[] b = new byte[max]; randNum.nextBytes(b);Now, generate a large random number with BigInteger type −BigInteger bigInt = new BigInteger(b);Example Live Demoimport java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String... a) {       int max = 10;       Random randNum = new Random();       byte[] b = new byte[max];       randNum.nextBytes(b);       // BigInteger type       BigInteger bigInt ... Read More

Program to iterate over a List using Java 8 Lambda

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

3K+ Views

Let us first create a List and add elements −ArrayListarrayList = new ArrayList(); arrayList.add("100"); arrayList.add("200"); arrayList.add("300"); arrayList.add("400"); arrayList.add("500");Now, iterate over it with Lambda Expressions −ArrayListlist = arrayList; System.out.println("Iterating..."); list.stream().forEach(elem -> System.out.println(elem));Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       ArrayListarrayList = new ArrayList();       arrayList.add("100");       arrayList.add("200");       arrayList.add("300");       arrayList.add("400");       arrayList.add("500");       arrayList.add("600");       arrayList.add("700");       arrayList.add("800");       arrayList.add("900");       arrayList.add("1000");       System.out.println("ArrayList...");       ... Read More

How to test if a List is an Unmodifable List in Java?

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

1K+ Views

We will first set a list to unmodifiable and after that test if it is unmodifiable or not. Let us create a List and add elements −List list = new LinkedList (); list.add(10); list.add(20); list.add(30); list.add(40); list.add(50);Set the above list to unmodifiable −Listunmodifiable = Collections.unmodifiableList(list);Now, use If-else, to check whether the list in unmodifiable or not −if (unmodifiable.getClass().getName().contains("UnmodifiableList"))    System.out.println("This is an UnmodifiableList" ); else    System.out.println("This is not an UnmodifiableList" );Example Live Demoimport java.util.Collections; import java.util.LinkedList; import java.util.List; public class Demo {    public static void main(String[] args) {       Listlist = new LinkedList();     ... Read More

How to shuffle a List in Java

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

227 Views

First, create an Integer array −Integer[] strArray = new Integer[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };Now, convert it into a List −Listlist = Arrays.asList(strArray);Use Collections to shuffle as shown below −Collections.shuffle(list);Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) {       Integer[] strArray = new Integer[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };       Listlist = Arrays.asList(strArray);       System.out.println("List = "+list);       Collections.shuffle(list);       System.out.println("Shuffled List = "+list);    } }OutputList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Shuffled List = [100, 90, 40, 70, 20, 10, 80, 30, 60, 50]

How to retain elements from a Collection in another Collection

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

139 Views

Let’s say the following is our Collection i.e. ArrayList −Listlist = new ArrayList(); list.add(100); list.add(200); list.add(200); list.add(200); list.add(300); list.add(400); list.add(400); list.add(500);Now, create another Collection −List list2 = new ArrayList(); list2.add(100); list2.add(200); list2.add(300); list2.add(400);To retain all elements from a Collection in another Collection, try the following with list and list2 as our two Collections −list.retainAll(list2);Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       Listlist = new ArrayList();       list.add(100);       list.add(200);       list.add(200);       list.add(200);       list.add(300);   ... Read More

How to remove all elements from a Collection in another Collection

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

579 Views

Let’s say the following is our Collection i.e. ArrayList −Listlist = new ArrayList(); list.add(100); list.add(200); list.add(200); list.add(200); list.add(300); list.add(400); list.add(400); list.add(500);Now, create another Collection −List list2 = new ArrayList(); list2.add(100); list2.add(200); list2.add(300); list2.add(400);To remove all elements from a Collection in another Collection, try the following with list and list2 as our two Collections −list.removeAll(list2);Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String args[]) throws Exception {       Listlist = new ArrayList();       list.add(100);       list.add(200);       list.add(200);       list.add(200);       list.add(300);     ... Read More

Advertisements