Constructor Overloading in Java

Sravani S
Updated on 05-Mar-2020 12:22:53

7K+ Views

Yes! Java supports constructor overloading. In constructor loading, we create multiple constructors with the same name but with different parameters types or with different no of parameters.ExampleLive Demopublic class Tester {      private String message;      public Tester(){       message = "Hello World!";    }    public Tester(String message){       this.message = message;    }      public String getMessage(){       return message ;    }      public void setMessage(String message){       this.message = message;    }      public static void main(String[] args) {       Tester tester = new Tester();       System.out.println(tester.getMessage());           Tester tester1 = new Tester("Welcome");       System.out.println(tester1.getMessage());      } }   OutputHello World! Welcome

StringTokenizer Class in Java

V Jyothi
Updated on 05-Mar-2020 12:18:39

606 Views

The StringTokenizer class of the java.util package allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.This class methods do not even recognize and skip comments.ExampleLive Demoimport java.util.*;   public class Sample {    public static void main(String[] args) {         // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");         // checking next token       System.out.println("Next token is : " + st.nextToken());   }     }OutputNext token is : Come

Fibonacci Series Program in Java Using Recursion

V Jyothi
Updated on 05-Mar-2020 12:16:09

2K+ Views

Following is the required program.ExampleLive Demopublic class Tester {    static int n1 = 0, n2 = 1, n3 = 0;    static void fibbonacci(int count) {       if (count > 0) {          n3 = n1 + n2;          n1 = n2;          n2 = n3;          System.out.print(" " + n3);          fibbonacci(count - 1);       }    }    public static void main(String args[]) {       int count = 5;       System.out.print(n1 + " " + n2);       fibbonacci(count - 2);    } }Output0 1 1 2 3

Eliminate Numbers in a String in Python

Arjun Thakur
Updated on 05-Mar-2020 11:25:01

213 Views

You can create an array to keep track of all non digit characters in a string. Then finally join this array using "".join method. examplemy_str = 'qwerty123asdf32' non_digits = [] for c in my_str:    if not c.isdigit():       non_digits.append(c) result = ''.join(non_digits) print(result)OutputThis will give the outputqwertyasdfexampleYou can also achieve this using a python list comprehension in a single line. my_str = 'qwerty123asdf32' result = ''.join([c for c in my_str if not c.isdigit()]) print(result)OutputThis will give the outputqwertyasdf

Find Keith Numbers Using Python

Priya Pallavi
Updated on 05-Mar-2020 11:23:28

805 Views

You can use the following code to find if a number is a keith number in python −Exampledef is_keith_number(n):    # Find sum of digits by first getting an array of all digits then adding them    c = str(n)    a = list(map(int, c))    b = sum(a)    # Now check if the number is a keith number    # For example, 14 is a keith number because:    # 1+4 = 5    # 4+5 = 9    # 5+9 = 14    while b < n:       a = a[1:] + [b]       b = sum(a)    return (b == n) & (len(c) > 1) print(is_keith_number(14))OutputThis will give the output −True

Print Objects in Array using Java Method

Every ; Thing
Updated on 05-Mar-2020 11:21:10

164 Views

Exampleclass Shape{     private String shapeName;     private int numSides;      Shape(String shapeName, int numSides){         this.shapeName = shapeName;         this.numSides = numSides;     }     public String toString(){         return shapeName + " has " + numSides + " sides.";     } } class ObjectList{     private Object[] list = new Object[10];     private int numElement = 0;      public void add(Object next){         list[numElement] = next;         numElement++;     }      @Override     public String toString(){         String str="";         int i=0;         while(list[i] != null){             str += list[i]+"";             i++;         }         return str;     } } public class Driver{     public static void main(String[] args){         ObjectList list = new ObjectList();         Shape square = new Shape("square", 4);         Shape hex = new Shape("hexagon", 6);         list.add(hex);         list.add(square);         System.out.println(list);     } }

Queue Emplace in C++ STL

Sunidhi Bansal
Updated on 05-Mar-2020 11:17:40

898 Views

In this article we will be discussing the working, syntax and examples of queue::emplace() function in C++ STL.What is a queue in C++ STL?Queue is a simple sequence or data structure defined in the C++ STL which does insertion and deletion of the data in FIFO(First In First Out) fashion. The data in a queue is stored in continuous manner. The elements are inserted at the end and removed from the starting of the queue. In C++ STL there is already a predefined template of queue, which inserts and removes the data in the similar fashion of a queue.What is ... Read More

Add and Subtract Large Numbers Using Python

karthikeya Boyini
Updated on 05-Mar-2020 11:15:16

2K+ Views

You can add/subtract large numbers in python directly without worrying about speed. Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate from the int type, but the interpreter will automatically use whichever is more appropriate.As long as you have version 2.5 or better, just perform standard math operations and any number which exceeds the boundaries of 32-bit math will be automatically (and transparently) converted to a bignum.examplea = 182841384165841685416854134135 b = 135481653441354138548413384135 print(a - b)OutputThis will give the output −47359730724487546868440750000

Deque Shrink to Fit in C++ STL

Sunidhi Bansal
Updated on 05-Mar-2020 11:14:14

285 Views

In this article we will be discussing the working, syntax and examples of deque::shrink_to_fit() function in C++ STL.What is Deque?Deque is the Double Ended Queues that are the sequence containers which provides the functionality of expansion and contraction on both the ends. A queue data structure allow user to insert data only at the END and delete data from the FRONT. Let’s take the analogy of queues at bus stops where the person can be inserted to a queue from the END only and the person standing in the FRONT is the first to be removed whereas in Double ended ... Read More

Find Kaprekar Numbers within a Given Range using Python

Nikitha N
Updated on 05-Mar-2020 11:13:49

1K+ Views

A modified Kaprekar number is a positive whole number n with d digits, such that when we split its square into two pieces - a right hand piece r with d digits and a left hand piece l that contains the remaining d or d−1 digits, the sum of the pieces is equal to the original number (i.e. l + r = n).You can find Kaprekar numbers within a given range by testing each number for the given condition in the given range. exampledef print_Kaprekar_nums(start, end):    for i in range(start, end + 1):       # Get the digits ... Read More

Advertisements