Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Wednesday, February 24, 2016

Java Program to calculate factorial by passing object




public class FindFactorial {
int a=0;
  FindFactorial(int i){
    a=i;
  }
  int displayFact(FindFactorial f){
    if(f.a<1)
      return 1;
    else{
         f.a--;
         return (f.a+1)*displayFact(f);
     }
  }

}

 

public class Main {
public static void main(String[] args){
      FindFactorial f1 = new FindFactorial(3);
      System.out.println("factorial ="+ f1.displayFact(f1));
}
}


Output:



WRITE A PROGRAM TO DRAW AN ELLIPSE USING C++ BUILDER


 DRAW AN ELLIPSE
The midpoint algorithm is applied to the first quadrant in two parts, region 1 and region 2. We process by taking unit step in x-coordinate direction and finding the closest value for y for each x steps in region 1.
In the first quadrant at region 1 we start at position (0,Ry) and incrementing X and calculating Y closer to the path along clockwise direction. When the slope becomes -1 then shift unit step in X to Y and compute corresponding X closest to ellipse path at region 2 at the same direction.
  
ALGORITHM:
  1. Start
  2. Input centre co-ordinates of the ellipse
  3. Input x-radius and y-radius
  4. For region 1, Pk=Ry2(Xk+1)2+Rx2(Yk-0.5)2-Rx2Ry2
    1. While px<py, increment x and set Px=Px+2Ry2
    2. If Pk<0, set Pk+1=Pk+Ry2+Px
    3. else set y=y-1, Py=Py-2Rx2 and Pk+1=Pk+Ry2+Px-Py
  5. For region 2, Pk=Ry2(X+0.5)2+Rx2(Y-1)2-Rx2Ry2
    1. While y>0, decrement the value of y and set Py=Py-2Rx2
    2. If p>0, set Pk+1=Pk+Rx2-Py
    3. Else increment Px=Px+2Ry2and set Pk+1=Pk+Rx2+Px-Py
  6. Display ellipse
  7. Stop

      
    SOURCE CODE:


    //---------------------------------------------------------------------------
    #include <vcl\vcl.h>
    #pragma hdrstop

    #include "ellipse.h"
    //---------------------------------------------------------------------------
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    int Xc=StrToInt(Edit1->Text);
    int Yc=StrToInt(Edit2->Text);
    int rx=StrToInt(Edit3->Text);
    int ry=StrToInt(Edit4->Text);
    int rxSq = rx * rx;
    int rySq = ry * ry;
    int X = 0, Y = ry, p;
    int px = 0, py = 2 * rxSq * Y;
    Canvas->Pixels[Xc+X][Yc+Y]=RGB(0,0,255);
    Canvas->Pixels[Xc-X][Yc+Y]=RGB(0,255,0);
    Canvas->Pixels[Xc+X][Yc-Y]=RGB(255,255,0);
    Canvas->Pixels[Xc-X][Yc-Y]=RGB(255,0,255);
    p = rySq - (rxSq * ry) + (0.25 * rxSq);
    while (px < py)
    {
    X++;
    px = px + 2 * rySq;
    if (p < 0)
    p = p + rySq + px;
    else
    {
    Y--;
    py = py - 2 * rxSq;
    p = p + rySq + px - py;
    }
    Canvas->Pixels[Xc+X][Yc+Y]=RGB(0,0,255);
    Canvas->Pixels[Xc-X][Yc+Y]=RGB(0,255,0);
    Canvas->Pixels[Xc+X][Yc-Y]=RGB(255,255,0);
    Canvas->Pixels[Xc-X][Yc-Y]=RGB(255,0,255);
    }
    p = rySq*(X+0.5)*(X+0.5) + rxSq*(Y-1)*(Y-1) - rxSq*rySq;
    while (Y > 0)
    {
    Y--;
    py = py - 2 * rxSq;
    if (p > 0)
    p = p + rxSq - py;
    else
    {
    X++;
    px = px + 2 * rySq;
    p = p + rxSq - py + px;
    }
    Canvas->Pixels[Xc+X][Yc+Y]=RGB(0,0,255);
    Canvas->Pixels[Xc-X][Yc+Y]=RGB(0,255,0);
    Canvas->Pixels[Xc+X][Yc-Y]=RGB(255,255,0);
    Canvas->Pixels[Xc-X][Yc-Y]=RGB(255,0,255);
    }

    }
    //---------------------------------------------------------------------------


    Ouput:
     

C++ program with student as abstract class & create derived classes engineering medicine and science from base class student.


Wap with student as abstract class & create derived classes engineering medicine and science from base class student. Create objects of the derived classes and process them and access them using array of pointers of type base class student.

Source Code:

#include<iostream.h>



#include<conio.h>



class student

{

public:

virtual void getdata()=0;

virtual void display()=0;



};

class engineering:public student

{

int r;

char n[10];

public:

void getdata()

{

cout<<"faculty->Engineering"<<endl;

cout<<"enter name";

cin>>n;

cout<<"enter roll";

cin>>r;

}

void display()

{

cout<<endl<<"name="<<n;

cout<<endl<<"roll="<<r<<endl<<endl;

}

};



class medicine:public student

{

int r;

char n[10];

public:

void getdata()

{ cout<<"Faculty->Medicine";

cout<<endl<<"enter name";

cin>>n;

cout<<"enter roll";

cin>>r;

}

void display()

{

cout<<endl<<"name="<<n;

cout<<endl<<"roll="<<r<<endl<<endl;

}

};



class science:public student

{

int r;

char n[10];

public:

void getdata()

{ cout<<"Faculty->Science";



cout<<endl<<"enter name";

cin>>n;

cout<<"enter roll";

cin>>r;

}

void display()

{

cout<<endl<<"name="<<n;

cout<<endl<<"roll="<<r<<endl<<endl;



}



};





void main()

{

clrscr();



student *ptr[3];

engineering e;

medicine m;

science s;

ptr[0]=&e;

ptr[0]->getdata();

ptr[0]->display();



ptr[1]=&m;

ptr[1]->getdata();

ptr[1]->display();



ptr[2]=&s;

ptr[2]->getdata();

ptr[2]->display();

getch();

}