Server Side Programming Articles - Page 1707 of 2646

Method Overloading and Ambiguity in Varargs in Java

AmitDiwan
Updated on 13-Jul-2020 12:02:11

1K+ Views

There are ambiguities while using variable arguments in Java. This happens because two methods can definitely be valid enough to be called by data values. Due to this, the compiler doesn’t have the knowledge as to which method to call.Example Live Demopublic class Demo {    static void my_fun(double ... my_Val){       System.out.print("fun(double ...): " + "Number of args: " + my_Val.length );       for(double x : my_Val)       System.out.print(x + " ");       System.out.println();    }    static void my_fun(boolean ... my_Val){       System.out.print("fun(boolean ...) " + "The number of ... Read More

How to input multiple values from user in one line in Java?

AmitDiwan
Updated on 13-Jul-2020 12:00:31

14K+ Views

To input multiple values from user in one line, the code is as follows −Example Live Demoimport java.util.Scanner; public class Demo {    public static void main(String[] args) {       System.out.print("Enter two floating point values : ");       Scanner my_scan = new Scanner(System.in);       double double_val = my_scan.nextFloat();       int int_val = my_scan.nextInt();       System.out.println("The floating point value is : " + double_val + " and the integer value is : "       + int_val);    } }Input56.789 99OutputEnter two floating point values : The floating point value is ... Read More

How a thread can interrupt another thread in Java?

AmitDiwan
Updated on 13-Jul-2020 11:57:50

997 Views

The ‘interrupt’ function can be used in Java to interrupt the execution of a thread with the help of the exception InterruptedException.The below example shows how the currently executing thread stops execution (because of a new exception raised in the catch block) once it is interrupted −Example Live Demopublic class Demo extends Thread {    public void run()    {       try       {          Thread.sleep(150);          System.out.println("In the 'run' function inside try block");       }       catch (InterruptedException e)       {       ... Read More

Sum of list with stream filter in Java

AmitDiwan
Updated on 13-Jul-2020 11:53:38

1K+ Views

To get sum of list with stream filter in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args)    {       List my_list = new ArrayList();       my_list.add(11);       my_list.add(35);       my_list.add(56);       my_list.add(78);       my_list.add(91);       System.out.println(sum(my_list));    }    public static int sum(List my_list)    {       System.out.println("In the main function, the sum of list with filter is ");       return my_list.stream().filter(i -> i > 5).mapToInt(i -> i).sum();    } ... Read More

Structure and Members of the Java Program

AmitDiwan
Updated on 13-Jul-2020 11:52:46

385 Views

While writing any piece of code in Java, there is a certain set of rules and regulations that need to be followed, that is considered as a standard. For example − A class contains variables, and functions. The functions can be used to work with the variables. Classes can be extended, and improvised too.Basic structureList of packages that are imported; public class {    Constructor (can be user defined or implicitly created)    {       Operations that the constructor should perform;    }    Data elements/class data members;    User-defined functions/methods;    public static void main (String ... Read More

Handshakes That Don't Cross in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:48:50

350 Views

Suppose we have an even number of people n that stand around a circle and each person shakes hands with someone else, so that there will be n / 2 handshakes total. We have to find the number of ways these handshakes could occur such that none of the handshakes cross. The answers may be very large so return the answer mod 10^9 + 7.So, if the input is like n = 2, then the output will be 1To solve this, we will follow these steps −m := 10^9 + 7Define an array dp of size (n+1)dp[0] := 1for initialize i := 0, when i

Palindrome Removal on C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:46:04

364 Views

Suppose we have an integer array called arr, now in one move we can select a palindromic subarray from index i to j where i

Divide Chocolate in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:43:46

416 Views

Suppose we have one chocolate bar that consists of some chunks. In each chunk it has its own sweetness given by a list called sweetness. If we want to share the chocolate among K friends so we start cutting the chocolate bar into K+1 piece using K cuts, now each piece consists of some consecutive chunks. If we take out the piece with the minimum total sweetness and give the other pieces to our friends. We have to find the maximum total sweetness of the piece we can get by cutting the chocolate bar optimally.So, if the input is like ... Read More

Valid Palindrome III in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:40:52

520 Views

Suppose we have a string s and another number k; we have to check whether the given string is a K-Palindrome or not.A string is said to be K-Palindrome if it can be transformed into a palindrome by removing at most k characters from it.So, if the input is like s = "abcdeca", k = 2, then the output will be true as by removing 'b' and 'e' characters, it will be palindrome.To solve this, we will follow these steps −Define a function lcs(), this will take s, t, n := size of sadd one blank space before sadd one ... Read More

Minimum Time to Build Blocks in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:38:21

214 Views

Suppose we have a list of blocks, if we have blocks[i] = t, this means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker. Single worker can either split into two workers or build a block then go home. These two decisions take some time. The time cost of spliting one worker into two workers is given as a number called split.So, if the input is like blocks = [1, 2], and split = 5, then the output will be 7 as we can split the worker ... Read More

Advertisements