Computer Scientists are Pretty Pessimistic

Thursday 16 November 2017

Secant Method


#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)-4*x-10; //Given equation may will be changed
}
int main()
{
    float x1,x2,x3,f1,f2,E;
    printf("Enter initial points X1 and X2: ");
    scanf("%f %f",&x2,&x3);
    printf("Enter the tolerance value: ");
    scanf("%f",&E);

    do
    {
        x1 = x2;
        x2 = x3;

        f1 = f(x1);
        f2 = f(x2);

        x3 = (f2*x1 - f1*x2)/(f2-f1);
    }
    while(fabs((x3-x2)/x3)>E);
    printf("Root is: %.2f\n",x3);

    return 0;
}

//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (153 -154)

Friday 20 October 2017

Newton-Raphson Method

#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)-3*x+2; //Given equation may will be changed
}
double ff(double x)
{
    return 2*x-3; //Given equation may will be changed
}
int main()
{
    float x1,x2,x0,E;
    printf("Enter initial value of X: ");
    scanf("%f",&x1);
    printf("Enter the tolerance value: ");
    scanf("%f",&E); // Tolerance may vary 0.0001
    do
    {
        x0 = x1;
        x1 = x0 - (f(x0)/ff(x0));

    }while(fabs((x1-x0)/x1)>E);
    printf("Root is: %.2f\n",x0);
    return 0;
}
//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (146 -147)

Thursday 12 October 2017

False Position Method

#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)+x-2; //Given equation may will be changed
}
int main()
{
    float x1,x2,x0,E;
    printf("Enter interval: ");
    scanf("%f %f",&x1,&x2);
    printf("Enter the tolerance value: ");
    scanf("%f",&E);
    if(f(x1)*f(x2)>=0)
    {
        printf("Incorrect Interval\n");
        return 0;
    }
    x0 = x1 - (f(x1)*((x2-x1)/(f(x2)-f(x1))));
    if(f(x0)==0)
    {
        printf("Root is: %.2lf\n",x0);
        return 0;
    }
    do
    {
        x0 = x1 - (f(x1)*((x2-x1)/(f(x2)-f(x1))));
        if(f(x1)*f(x0)<0)
        {
            x2 = x0;
        }
        else
        {
            x1 = x0;
        }
    }while(fabs((x2-x1)/x2)>E);
    printf("Root is: %.2f\n",(x1+x2)/2);
    return 0;
}
//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (138 -144)

Tuesday 28 February 2017

Bisection Method

#include <stdio.h>
#include <math.h>
double f(double x)
{
    return pow(x,2)-4*x-10; //Given equation may will be changed
}
int main()
{
    float x1,x2,x0,E;
    printf("Enter interval: ");
    scanf("%f %f",&x1,&x2);
    printf("Enter the tolerance value: ");
    scanf("%f",&E);
    if(f(x1)*f(x2)>=0)
    {
        printf("Incorrect Interval\n");
        return 0;
    }
    x0 = (x1+x2)/2;
    if(f(x0)==0)
    {
        printf("Root is: %.2lf\n",x0);
        return 0;
    }
    do
    {
        x0 = (x1+x2)/2;
        if(f(x1)*f(x0)<0)
        {
            x2 = x0;
        }
        else
        {
            x1 = x0;
        }
    }while(fabs((x2-x1)/x2)>E);
    printf("Root is: %.2f\n",(x1+x2)/2);
    return 0;
}
//Reference : Nemerical Methods by E Balagurusamy (2016-2017) pages (131 -138)

Sunday 28 August 2016

Final Keyword

final variable :-

When a variable declared as final it's value never be changed next time .

final method :-

When a method declared as final it can be overridden .

final class :-

To prevent a class from being inherited the class need to declared as final.

finally block :-

A finally block is a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block . The finally block will execute whether or not an exception is thrown . If an exception is thrown. the finally block will execute even if no catch statement matches the exception .

finalize method :-

When an object is no longer used, it is called "garbage" . The function of the "garbage collector" is to determine objects which are no longer used and reclaim the memory occupied by the object .

Why Abstract Class ?

Sometimes there may be a need to crate a super class that only defines a generalized form that will be shared by all of its sub classes , leaving it to each sub class to fill in the details . Such a class determines the mature of the methods that the sub classes must implement. Abstract class provides a way to solve this type of situation.

Restrictions of abstract class :-

* Constructor never be declared as abstract .

* An object can't be created of abstract class since an abstract  class is not fully defined .

* Can't declare an abstract static methods .

Interfaces in JAVA

Interfaces :-

* Using interface you can specify what a class must do.

* Variables in interface always final and static by default.

* All methods and variables are implicitly public.

* One or more classes can implement an interface.

* A class implements more than one interface.

Some Restriction of Interface :-

* You can not instantiate an interface. (you won't create an object)

* An interface doesn't contain any constructors.

* All of the methods in an interface are abstract.

* An interface can't contain instance fields. The only fields that can appear in an interface must be declared both static and final.

* An interface is not extended by a class, it is implemented by a class.

* An interface can extend multiple interfaces.