Wednesday 8 December 2010

Generating Pascal's Triangle

Blaise Pascal (AD 1623 - 1662 )


In this program, we'll be generating the below number-triangle:


Pascal's Triangle


Known as Pascal's Triangle, it's a triangular array of Binomial Coefficients named after Blaise Pascal,  a French mathematician, physicist, inventor, writer and Catholic philosopher (a Jansenist apologist, to be precise), which he described in his book Traité du triangle arithmétique of 1653.


File: PascalTriangle.java


We first write a class called BinomialCoefficient for computing the Binomial Coefficient (inclusive of the Factorial class) as:



class Factorial {
static long factorial (byte i)
{
if(i == 0 || i == 1)
return (long) 1;
else 
return (long) i * factorial((byte) (i - 1));
}
}


class BinomialCoefficient {
static long binomialCoefficient(byte n, byte r){
long binCoeff = 0;

binCoeff = (long) (Factorial.factorial(n))/
(Factorial.factorial(r) * Factorial.factorial((byte) (n - r)));

return (long) binCoeff;
}
}

Next we invoke it in the PascalTriangle class below it as:


public class PascalTriangle {

public static void main(String[] args) {
 
  byte numberOfRows = 15;
String[] sumOfBinomialCoefficients = new String[numberOfRows] ;
System.out.println();
for(byte rowIndex = 0;rowIndex < numberOfRows;rowIndex++)
{
sumOfBinomialCoefficients[rowIndex] = "";
for(byte columnIndex = 0;columnIndex <= rowIndex;columnIndex++)
{
  sumOfBinomialCoefficients[rowIndex] +=  
     BinomialCoefficient.binomialCoefficient
     (rowIndex,columnIndex) + " ";
}
}
for(byte rowIndex = 0;rowIndex < numberOfRows;rowIndex++)
{
for(long padding = 0;padding < 
      (sumOfBinomialCoefficients[numberOfRows - 1].length() -    
      sumOfBinomialCoefficients[rowIndex].length())/2;padding++)
{
System.out.print(" ");
}
for(byte columnIndex = 0;columnIndex <= rowIndex; columnIndex++)
{
System.out.print(BinomialCoefficient.binomialCoefficient
    (rowIndex,columnIndex) + " ");
}
System.out.println();
}
}

}


A little note first: I pasted the program written in Eclipse IDE in a notepad and saved it inside the Java Programs folder in drive C:\. I'll be running the program in the Command-Line Interface

Here, I've displayed the triangle upto 15 rows. If you want more number of rows, just assign it to the numberOfRows variable. The output is as:


Cosine Series

The computation of the Cosine Series 



can be done based on the SineSeries.java program in the post below with some minor changes (still assuming that CosineSeries.java is also in the same package where SineSeries.java is). Here we compute the value of the series at x = 45°, which is π/4 in radian measure.

We first define a class called CosineSeriesSummation for summing the series upto a specified number of terms as below:


Next we invoke the method defined above in the CosineSeries class which contains the main() method as:


The output of the program is as:

  The sum of the Cosine Series at x = 0.7853981633974483 upto the    
  first 20 terms is 0.7071067811865475
  Using the cos() method of the java.lang.Math class, the value 
  of the Cosine Function at x = 0.7853981633974483 is 0.7071067811865476

Sine Series

The below Java program computes the Sine Series




at a particular x, upto a specified number of terms. The value of x and the summation required upto a particular number of terms can be assigned to the member variables variable and numberOfTerms inside the main() method. It's assumed that the SineSeries.java file is inside the same package where  ExponetialSeries.java is, for we'll be using the Factorial class in it. Here we compute the value of the series at x = 90°, which is π/2 in radian measure.

class SineSeriesSummation {
    static double sineSeriesSummation(double x,byte n)
    {
       double sineSeries = 0;

       for(byte i = 0;i < n;i++)
       {
          if(i % 2 == 0)
          {
             sineSeries += Math.pow(x,2*i + 1) / (double)                                            Factorial.factorial((byte) (2*i + 1));
          }
          else
          {
             sineSeries -= Math.pow(x,2*i + 1) / (double)                                            Factorial.factorial((byte) (2*i + 1));
          }
       }
      return sineSeries;
   }
}


public class SineSeries {

    public static void main(String[] args) {
       final double PI =  3.141592653589793238462643383279502;

       double variable = PI/2;
       byte numberOfTerms = 32;
       System.out.println("The sum of the Sine Series at x = " 

       + variable + " upto the first " + numberOfTerms + " terms is " 
       + SineSeriesSummation.sineSeriesSummation(variable, numberOfTerms));

   }

}
 

The output will be

 
 The sum of the Sine Series at x = 1.5707963267948966 upto the
  first 32 terms is 0.9999966422044658

This is almost the same value which you'll get by using the method sin() in the java.lang.Math class. You can add the below line just below the last in the above code to check that.
 

  System.out.println("Using the sin() method of the java.lang.Math class, 
  the value of the Sine Function at x = " + variable + " is " + Math.sin 
  (variable)); 


Also instead of defining our own value for π as we did above, we can use it from the java.lang.Math class itself as
  
  double variable = Math.PI/2;


The Sine Series generated by wxMaxima

Monday 6 December 2010

Exponential Series

Below is a Java program for computing the Exponential Series


at a particular x, upto a specified number of terms. The value of x and the summation required upto a particular number of terms can be assigned to the member variables variable and numberOfTerms inside the main() method.

class Factorial {
    static long factorial (byte number)
    {
      if(number == 0 || number == 1)
          return (long) 1;
      else
          return (long) number * factorial((byte) (number - 1));
    }
}

class ExponentialSeriesSummation {                
    static double exponentialSeriesSummation(double x,byte n)
    {
      double exponentialSeries = 0;
      for(byte i = 0;i < n;i++)
      {
           exponentialSeries += Math.pow(x, (int) i) / (double)                                            Factorial.factorial(i);
      }
     
return exponentialSeries;
    }
}

public class ExponentialSeries {

    public static void main(String[] args) {
      double variable = 1;
      byte numberOfTerms = 66;
      

      System.out.println("The sum of the Exponential Series at x = " + variable 
      + " upto the first " + numberOfTerms + " terms is " +
      ExponentialSeriesSummation.exponentialSeriesSummation(variable, numberOfTerms));

   }
}


You'll get the output

     The sum of the Exponential Series at x = 1.0 upto the first 66 terms is 
     2.7182818284590455  

which is the same value generated by the method exp() in the java.lang.Math class. You can add the below line just below the last in the above code to check that.

     ...
     ...
     System.out.println("Using the exp() method of the java.lang.Math class, the value      of the Exponential Function at x = " + variable + " is " + Math.exp(variable));
 


 The Exponential Function ex generated by wxMaxima

Saturday 4 December 2010

Bubble Sort

This is a Java program for Bubble Sort. Input the numbers you want to sort in numberArray[] array (below). File: BubbleSort.java
 
public class BubbleSort {

    public static void main(String[] args) {
      short numberArray[] = {7,99,-4,13,1,675,-37};

      for(byte i = 0;i < numberArray.length - 1;i++)
      {
         for(byte j = (byte) (i + 1);j < numberArray.length;j++)
         {
          if(numberArray[i] > numberArray[j])
          {
            short temporary = numberArray[j];
            numberArray[j] = numberArray[i];
            numberArray[i] = temporary;
          }
         }
      }

      for(byte k = 0;k < numberArray.length;k++)
      {
          System.out.print(numberArray[k] + " ");
      }
   }

}

Friday 3 December 2010

Insertion Sort

This is a Java program for Insertion Sort. Input the numbers you want to sort in numberArray[] array (below). File: InsertionSort.java

 Insertion Sort is analogous to sorting cards

public class InsertionSort {

     
public static void main(String[] args) {
          
short numberArray[] = {7,99,-4,13,1,675,-37};

         
for(byte i = 1; i < numberArray.length;i++)
          {
                 
short key = numberArray[i];

                 
byte j = (byte) (i - 1);

                 
while(j >= 0 && numberArray[j] > key)
                 {
                      numberArray[j + 1] = numberArray[j];
                      j = (
byte) (j - 1);
                 }
                 numberArray[j + 1] = key;
           }

           
for(byte k = 0;k < numberArray.length;k++)
           {
               System.out.print(numberArray[k] + " ");
           }

   }
}

Running your first Java Program in Java EE Eclipse IDE

1. Start the Eclipse Java EE IDE


2. From the menu bar, select File > New > Project...
  

3. In the New Project window select Java > Java Project > Next



 4. In the New Java Project window, type a name in the Project name field, say "Sort"




5. Click Finish




6. Right-click on Sort folder. Select New > Class  




7. In the Name field, fill it with a class name, say HelloWorld. Click Finish





8. Now "redo" the HelloWorld! program in HelloWorld.java file as shown in the Figure above. Save (Ctrl + S). Click Run wizard from the Workbench Toolbar





9. The output will appear in the Console View below