Saturday, 28 October 2017

Pascal triangle in java

Program: Write a program to enter number of rows and create a Pascal Triangle.
Input: 5
Output:
     1               row=1
    1 1              row=2
   1 2 1             row=3
  1 3 3 1            row=4
 1 4 6 4 1           row=5



Source FilePascalTriangle.java

package triangle;

public class PascalTriangle
{
      public static void main(String args[])
      {
            int rows, i, k, number=1, j;
            rows = 5;  //change number of rows as per need

            for(i=0;i<rows;i++)
            {
                  for(k=rows; k>i; k--)
                  {
                        System.out.print(" ");
                  }
                  number = 1;
                  for(j=0;j<=i;j++)
                  {
                        System.out.print(number+ " ");
                        number = number * (i - j) / (j + 1);
                  }
                  System.out.println();
            }
      }
}



OUTPUT:

     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1




 You may also like to read about:

Friday, 20 October 2017

Calculator Application in java

Project Detail: Create calculator application which accepts two numbers from user and perform addition, subtraction, multiplication and division on given numbers.
                                     --------------------------  *  -----------------------------


SourceCalculation.java

package calculator;

public class Calculation {
      int result;
      //Method for addition
      public int add(int num1, int num2){
            result = num1+num2;
            return result;
      }
      //Method for subtraction
      public int substract(int num1, int num2){
            result = num1-num2;
            return result;
      }
      //Method for multiplication
      public int multiply(int num1, int num2){
            result = num1*num2;
            return result;
      }
      //Method for division
      public float divide(int num1, int num2){
            result = num1/num2;
            return result;
      }
     
}

SourceCalculatorVO.java

package calculator;

public class CalculatorVO {
      private int firstNumber;
      private int secondNumber;
     
      public int getFirstNumber() {
            return firstNumber;
      }
      public void setFirstNumber(int firstNumber) {
            this.firstNumber = firstNumber;
      }
      public int getSecondNumber() {
            return secondNumber;
      }
      public void setSecondNumber(int secondNumber) {
            this.secondNumber = secondNumber;
      }
     

}

SourceMainClass.java

package calculator;

public class MainClass {

      public static void main(String[] args) {
           
            CalculatorVO calVO = new CalculatorVO();
            //Input can be taken from user
            calVO.setFirstNumber(100);
            calVO.setSecondNumber(11);
           
            Calculation cal = new Calculation();
           
            int addResult = cal.add(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Addition Result ="+addResult);
           
            int subResult = cal.substract(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Subtraction Result ="+subResult);
           
            int mulResult = cal.multiply(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Multiplication Result ="+mulResult);
           
            float divResult = cal.divide(calVO.getFirstNumber(), calVO.getSecondNumber());
            System.out.println("Division Result ="+divResult);

      }

}


OUTPUT:
Addition Result =111
Subtraction Result =89
Multiplication Result =1100
Division Result =9.0

Tuesday, 17 October 2017

Can we overload main() in java?


Yes, the main() method in java can be overloaded.

Given below is the sample code snippet where main() method has been overloaded twice. If you want you can experiment by overloading main() as per your choice and requirement.

To prove our point that main() can be overloaded, below code is sufficient. Have a look,

Source FileMainOverloading.java 

package basic;

public class MainOverloading {

      public static void main(String[] args) {
            System.out.println("Overloaded methods will not be executed here");

      }
     
      public static void main(int x){
            System.out.println("x="+ x);
            main(5,54);
      }
     
      public static void main(int a, int b){
            System.out.println("a="+ a);
            System.out.println("b="+ b);
      }

}


OUTPUT:
Overloaded methods will not be executed here 

---------------------------------------------------------------------------------------------------

In the above code snippet JVM searches for public static void main(String[] args) type signature as regular. And anything inside public static void main(String[] args)  will be executed first. So if you want your overloaded main() method to get executed, you need to call it from inside actual main() which is entry point for all java program. 

Given below is modified version of above code snippet. Here overloaded main() is executed. Have a look,
---------------------------------------------------------------------------------------------------

Source FileMainOverloading.java 

package basic;

public class MainOverloading {

      public static void main(String[] args) {
        main(1);       //calling overloaded main

      }
     
      public static void main(int x){
            System.out.println("x="x);
            main(5,54);
      }
     
      public static void main(int aint b){
            System.out.println("a="a);
            System.out.println("b="b);
      }

}


OUTPUT:
x=1    //output of main(int x)
a=5    //output of main(int aint b) 
b=54
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.