Found 33676 Articles for Programming

Write a function that generates one of 3 numbers according to given probabilities in C++

sudhir sharma
Updated on 15-Jul-2020 06:23:43

363 Views

In this problem, we have to create a function that will generate three numbers based on the given probability.For this, we will use the built-in random number generator function which is rand(a, b) which generates random numbers within the range [a, b] with equal probability.Our task is to return only three numbers A, B, C which have the probability of occurrence as P(A), P(B), P(C) respectively and according to definition of probability P(A) + P(B) + P(C) = 1.To create our function using rand(a, b). we will use its feature that the probability of occurrence of any number from a ... Read More

Write a function to delete a Linked List in C++ Programming

sudhir sharma
Updated on 15-Jul-2020 06:21:31

686 Views

Here, we will create a function that will delete all elements of a linked list one by one.In c/c++, there is no specific function to perform this task but in java, the automatic garbage collection is done to ease deleting linked list.Now, let’s see the implementation of this program, Example Live Demo#include using namespace std; class Node{    public:    int data;    Node* next; }; void deleteLinkedList(Node** head_ref){    Node* current = *head_ref;    Node* next;    while (current != NULL){       coutnext = (*head_ref);    (*head_ref) = new_node; } int main(){    Node* head = NULL; ... Read More

Preconditions - Java

AmitDiwan
Updated on 14-Jul-2020 07:08:28

271 Views

Precondition to check if the list passed as parameter is empty or not. Let us see an example −Examplepublic void my_fun(List myList){    if (myList == null){       throw new IllegalArgumentException("List is null");    }    if (myList.isEmpty()){       throw new IllegalArgumentException("List is empty");    }    my_fun(myList); }A void function named ‘my_fun’ is defined that takes a list of objects as its parameters. If the list is null, it prints the relevant message. If the list has no elements in it, a specific message is displayed. The function is called by passing the list as ... Read More

Java program to print matrix in Z form

AmitDiwan
Updated on 29-Oct-2024 18:51:04

723 Views

In this article, we will learn to print a matrix in a "Z" pattern in Java. It starts from the top-left corner, moves horizontally across the top row, then diagonally down through the center, and finally across the bottom row. This pattern creates a "Z" shape with selected elements from the matrix. Problem Statement Write a program in Java to print a matrix in Z form. Input my_arr[][] = { { 34, 67, 89, 0}, { 0, 1, 0, 1 }, { 56, 99, 102, 21 }, {78, 61, 40, 99}} Output The matrix is34 67 89 0 0 99 ... Read More

Java program to print distinct permutations of a string

AmitDiwan
Updated on 19-Sep-2024 21:57:13

895 Views

In this article, we will learn how to generate and print all distinct permutations of a given string using Java. Permutations are rearrangements of characters in a string, and this program will ensure that no duplicate permutations are printed. The method works by checking each rearrangement and avoiding any repeats. Problem Statement Write a program in Java to print distinct permutations of a given string − Input my_str = "mnqm" Output The distinct permutations of the string are[mnqm, nmqm, nqmm, mqnm, qmnm, qnmm, mqmn, qmmn, mnmq, nmmq, mmnq, mmqn] Steps to print distinct permutations of a string Following are the ... Read More

Killing threads in Java

AmitDiwan
Updated on 14-Jul-2020 07:00:12

363 Views

Example Live Demopublic class Main{    static volatile boolean exit = false;    public static void main(String[] args){       System.out.println("Starting the main thread");       new Thread(){          public void run(){             System.out.println("Starting the inner thread");             while (!exit){             }             System.out.println("Exiting the inner thread");          }       }.start();       try{          Thread.sleep(100);       }       catch (InterruptedException e){   ... Read More

Joiner class Guava Java

AmitDiwan
Updated on 14-Jul-2020 06:58:05

117 Views

Joiner provides various methods to handle joining operations on string, objects, etc. Let us see an example −Exampleimport com.google.common.base.Joiner; import java.util.*; public class Demo{    public static void main(String[] args){       String[] my_arr = { "hel", null, "lo", "wo", "r", null, "ld" };       System.out.println("The original array is : "+ Arrays.toString(my_arr));       String my_result = Joiner.on('+').skipNulls().join(my_arr);       System.out.println("The joined string is : " + my_result);    } }OutputThe original array is [hel, null, lo, wo, r, null, ld] The joined string is hel+lo+wo+r+ldA class named Demo contains the main function, which defines ... Read More

Print Single and Multiple variables in Java

AmitDiwan
Updated on 14-Jul-2020 06:57:00

3K+ Views

To print single and multiple variables in Java, the code is as follows −Example Live Demopublic class Demo {    public static void main(String args[]){       String name_1 = "Hello";       String name_2 = "World";       System.out.println("Printing single variable");       System.out.printf("%s", name_1);       System.out.println("Printing multiple variables");       System.out.printf("First Name: %sLast Name: %s",name_1, name_2);    } }OutputPrinting single variable Hello Printing multiple variables First Name: Hello Last Name: WorldA class named Demo contains the main function, which defines two strings. These strings are displayed using the ‘println’ function and using the ‘printf’ function.

LinkedHashMap and LinkedHashSet in Java

Alshifa Hasnain
Updated on 21-Feb-2025 17:31:59

665 Views

In this article, we will learn the LinkedHashMap and LinkedHashSet in Java. LinkedHashMap Hash table and linked list implementation of the Map interface, with predictable iteration order. Example Let us see an example − import java.util.*; public class Demo { public static void main(String args[]){ LinkedHashMap my_set; my_set = new LinkedHashMap(); my_set.put(67, "Joe"); my_set.put(90, "Dev"); my_set.put(null, "Nate"); my_set.put(68, "Sara"); ... Read More

Essential Java Tips and Tricks for Programmers

Deepti S
Updated on 29-Aug-2023 15:34:08

345 Views

Java is a powerful and versatile programming language. It is used in various applications. It's known for its reliability, portability, and safety, which makes it a famous desire for developers. Java is likewise exceptionally easy to analyze, which makes it a terrific desire for novices. However, it's critical to remember that simplicity might operate as an obstacle. If you're not mindful, you could be caught by Java's accessibility and neglect to explore the unique opportunities the language offers. Let's have a look at some tips to assist you develop as a Java developer and improve your language proficiency. Tip 1: ... Read More

Advertisements