Tuesday, 3 November 2015

What is Constructors,Constructor Overloading and Copy-Constructor?



Constructors: The main purpose of having constructors is to create an instance of a class.They are invoked while creating an instance of a class.

The silent features of Java constructors:

  • Constructors can be public, private or protected.
  • If a constructor with arguments has been defined in a class, you can no longer use a default no-argument constructor. you have to write one.
  • They are called only once when the class is being instantiated.
  • They must have the same name as class itself.
  • They do not return a value and you don not have to specify the keyword void.
  • If you do not create a constructor for the class, Java helps you by using a so called default no-argument constructor.

 public class SampleProgram {  
      String name;  
      // This is the the constructor  
      public SampleProgram(String name) {  
           this.name = name;  
      }  
      public void display() {  
           System.out.println("Name is :" + name);  
      }  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           SampleProgram s = new SampleProgram("Ashish");  
           s.display();  
      }  
 }  


Constructor Overloading:- In constructor overloading ,we pass different number and type of variable as arguments all of which are private variables of the class.

 public class SampleProgram {  
      String name;  
      public SampleProgram() {  
           this.name = "Manish";  
      }  
      // This is the the constructor  
      SampleProgram(String name) {  
           this.name = name;  
      }  
      public void display() {  
           System.out.println("Name is :" + name);  
      }  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           SampleProgram s = new SampleProgram("Ashish");  
           s.display();  
           SampleProgram s1=new SampleProgram();  
           s1.display();  
      }  
 }  


Copy Constructor:- A copy constructor is a type of constructor which constructs the object of the class from another object of the same class.The copy constructor accepts a refernce to tis own class as parameter.

Note:- Java does not support Copy Constructor.So don't get confused.It is supported in C++.


Enjoy Reading:)




Monday, 2 November 2015

Java program to generate Harmonic Series


Write a program to generate Harmonic Series.

Input:- 5

Output:- 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately)

Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class HarmonicSeries {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int num = 5;  
           double result = 0.0;  
           while (num > 0) {  
                result = result + (double) 1 / num;  
                num--;  
           }  
           System.out.println("Output is " + result);  
      }  
 }  

Output  
 Output is 2.283333333333333  

Enjoy Programming.

Java program to find whether given number is Armstrong or not


Write a program to find whether given number is Armstrong or not.

Input:- 153

Output:- 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no.


Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class ArmstrongProgram {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int num = 153;  
           int n = num;   
           int check = 0, remainder;  
           while (num > 0) {  
                remainder = num % 10;  
                check = check + (int) Math.pow(remainder, 3);  
                num = num / 10;  
           }  
           if (check == n)  
                System.out.println(n + " is an Armstrong Number");  
           else  
                System.out.println(n + " is not a Armstrong Number");  
      }  
 }  

 Output  
 153 is an Armstrong Number  

Enjoy Programming.

Java program to convert given number of days into months and days


Write a program to convert given number of days into months and days.(Assume that each month is of 30 days).

Input:-  46

Output:- 46 days = 1 Month and 16 days


Sample Program:-

/**  
  *   
  */  
 /**  
  * @author Dixit  
  *   
  */  
 public class DayMonthProgram {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int num = 46;  
           int days = num % 30;  
           int month = num / 30;  
           System.out.println(num + " days = " + month + " Month and " + days  
                     + " days");  
      }  
 }  

 Output  
 46 days = 1 Month and 16 days  

Enjoy Programming.

Java program to find sum of all integers greater than 1 to 100 that are divisible by 7


Write a program to find sum of all integers greater than 1 and less than 100 that are divisible by 7.

Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class SumOfDigit {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int result = 0;  
           for (int i = 1; i <= 100; i++) {  
                if (i % 7 == 0)  
                     result += i;  
           }  
           System.out.println("Output of Program is : " + result);  
      }  
 }  

 Output  
 Output of Program is : 735  


Enjoy Programming.

Generate Random numbers


Write a program to generate 5 random numbers between 1 to 100, and it should not follow with decimal point.

Input:- 5

Output:- Any 5 random numbers between 1 to 100.


Sample Program:-

 /**  
  * @author Dixit  
  *   
  */  
 public class GenerateRandomNumber {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           for (int i = 1; i <= 5; i++) {  
                System.out.println((int) (Math.random() * 100));  
           }  
      }  
 }  

Output  
 11  
 76  
 27  
 84  
 90  

Enjoy Programming

Reversed pyramid using for loops and decrement operator


This Java pyramid example shows how to generate pyramid or triangle like given below using for loop.

12345
1234
123
12
1


Sample Program:-

 /*  
  This Java Pyramid example shows how to generate pyramid or triangle  
  like given below using for loop.  
  12345  
  1234  
  123  
  12  
  1  
  *   
  *  
  */  
 public class ReversePyramid {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           for (int i = 5; i > 0; i--) {  
                for (int j = 0; j < i; j++) {  
                     System.out.print(j + 1);  
                }  
                System.out.println("");  
           }  
      }  
 }  


 Output  
 12345  
 1234  
 123  
 12  
 1  


Enjoy Programming