Programming Articles - Page 2492 of 3366

How to create a thread by using anonymous class in Java?

Vivek Verma
Updated on 27-May-2025 16:16:05

4K+ Views

This article will use an anonymous class to create a Thread in Java. An Anonymous class is a class that does not have a name. Thread in Java In Java, a Thread is a part of a program that can be executed independently. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM), when the main() method is invoked. Creating a Thread by Using Anonymous Class In Java, the basic way to create a thread is to either extend the Thread class or implement the Runnable interface. However, we ... Read More

Abundant Number in C ?

sudhir sharma
Updated on 07-Aug-2019 14:44:39

3K+ Views

An abundant Number (also known as excessive number) is a number in the number theory which itself is smaller than the sum of all its proper divisors. For example, 12 is an abundant Number : divisors 1, 2, 3, 4, 6 , sum =16 >12.The difference between the sum of divisors and the number is called abundance. For above example abundance = 4 => 16 - 12 .To check for abundant number we will find all the factors of the number and add them up. This sum compared with the number show that if the number is abundant or not.PROGRAM ... Read More

abs() function for complex number in c++ ?

sudhir sharma
Updated on 07-Aug-2019 14:41:04

478 Views

The abs function in C++ is used to find the absolute value of a complex number. The absolute value of a complex number (also known as modulus) is the distance of that number from the origin in the complex plane. This can be found using the formula −For complex number a+bi:mod|a+bi| = √(a2+b2)The abs() function returns the result of the above calculation in C++. It is defined in the complex library that is needed to be included.PROGRAM TO SHOW USE OF abs() FUNCTION FOR COMPLEX NUMBERS IN C++#include #include using namespace std; int main () {    float ... Read More

A shorthand array notation in C for repeated values?

sudhir sharma
Updated on 07-Aug-2019 14:38:32

179 Views

An array stores number of same data type. For an array there may arise a situation when you need to store 2-3 values that are same i.e. 3,3,3,3 needs to be stored.For this case, the programing language C has made a simple way to create an array with such repeated value to reduce the workload of the programmer.Syntax[startofRepeatingSeq … EndofRepeatingSeq]number Example : For 3 repeated 5 times ; [0 … 4]3Example#include int main() {    int array[10] = {[0 ... 4]3, [6 ... 9]5};    for (int i = 0; i < 10; i++)       printf("%d ", array[i]);    return 0; }Output3 3 3 3 3 0 5 5 5 5

A modified game of Nim in C ?

sudhir sharma
Updated on 07-Aug-2019 14:32:16

693 Views

Modified game of Nim is an optimisation games of arrays. This game predicts the winner based on the starting player and optimal moves.Game Logic − In this game, we are given an array{}, that contains elements. There are generally two players that play the game namly player1 and player2. The aim of both is to make sure that all their numbers are removed from the array. Now, player1 has to remove all the numbers that are divisible by 3 and the player2 has to remove all the numbers that are divisible by 5. The aim is to make sure that ... Read More

A program to check if a binary tree is BST or not in C ?

sudhir sharma
Updated on 07-Aug-2019 14:32:47

408 Views

A binary tree is a tree data structure in which there are two child nodes for each node. The child nodes being two are referred as, left and right child.A BST is a tree structure in which left subtree contains nodes with values lesser than root and right subtree contains nodes with values greater that root.Here, we will check if a binary tree is a BST or not −To check for this we have to check for the BST condition on the binary tree. For a root node check for left child should be less that root, right child should ... Read More

How to remove the HTML tags from a given string in Java?

Vivek Verma
Updated on 28-Aug-2025 17:06:02

20K+ Views

A String is a final class in Java, and it is immutable, it means that we cannot change the object itself, but we can change the reference to the object. The HTML tags can be removed from a given string by using the following approaches: Using replaceAll() Method Using Pattern and Matcher Using replaceAll() Method We can remove the HTML tags from a given string by using the replaceAll() method. The replaceAll() method accepts two parameters: a "regular expression" and a "replacement string". It replaces all substrings that match the ... Read More

0-1 Knapsack Problem in C?

sudhir sharma
Updated on 07-Aug-2019 14:24:15

26K+ Views

A knapsack is a bag. And the knapsack problem deals with the putting items to the bag based on the value of the items. It aim is to maximise the value inside the bag. In 0-1 Knapsack you can either put the item or discard it, there is no concept of putting some part of item in the knapsack.Sample ProblemValue of items = {20, 25, 40} Weights of items = {25, 20, 30} Capacity of the bag = 50Weight distribution25, 20{1, 2} 20, 30 {2, 3} If we use {1, 3} the weight will be above the max allowed value. ... Read More

How to find If a given String contains only letters in Java?

Maruthi Krishna
Updated on 07-Aug-2019 12:13:04

2K+ Views

To verify whether a given String contains only characters −Read the String.Convert all the characters in the given String to lower case using the toLower() method.Convert it into a character array using the toCharArray() method of the String class.Find whether every character in the array is in between a and z, if not, return false.ExampleFollowing Java program accepts a String from the user and displays whether it is valid or not.import java.util.Scanner; public class StringValidation{    public boolean validtaeString(String str) {       str = str.toLowerCase();       char[] charArray = str.toCharArray();       for (int i ... Read More

While chaining, can we throw unchecked exception from a checked exception in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:32:08

781 Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is with out adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); }catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing higher ... Read More

Advertisements