Wednesday, December 8, 2010

Calculator Design


import java.util.Random;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


/*

<applet code=MyApplet width = 400 height= 200>

</applet>

*/

public class MyApplet extends Applet implements ActionListener,Runnable {

    Random rand;
    Button bt[];
    Panel pnl1;
    TextField ipTextField;
    String fnum,snum;
    boolean aflag,sflag,mflag,dflag,dotflag,sinflag,cosflag,tanflag;

    public void run(){
    }

    public void init(){
        setBackground(Color.white);
        rand = new Random();
        fnum = "0";
        snum = "0";
        int nb=25;
        bt = new Button[28];

        pnl1 = new Panel();
        ipTextField = new TextField(10);

        setLayout(new BorderLayout());

        pnl1.setLayout(new GridLayout(7,4));

        for(int i=0;i<=9;i++)
            bt[i] = new Button( new Integer(i).toString());

        bt[10] = new Button("+");
        bt[11] = new Button("-");
        bt[12] = new Button("X");
        bt[13] = new Button("/");
        bt[14] = new Button("=");
        bt[15] = new Button(".");
        bt[16] = new Button("AC");
        bt[17] = new Button("sin");
        bt[18] = new Button("cos");
        bt[19] = new Button("tan");
        bt[20] = new Button("Rand");
        bt[21] = new Button("Backspace");
        bt[22] = new Button("Sqrt");
        bt[23] = new Button("X^2");
        bt[24] = new Button("X!");
        bt[25] = new Button("1/X");
        bt[26] = new Button("PI");
        bt[27] = new Button("X^3");

        for(int i=0;i<28;i++) {
            pnl1.add(bt[i]);
            bt[i].addActionListener(this);
            }

        add(ipTextField,BorderLayout.NORTH);
        add(pnl1,BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent ae) {
    String msg;
    msg = ae.getActionCommand();

    String tmsg = ipTextField.getText();

    if(msg.equals("0"))
        tmsg = tmsg + "0";
    else if(msg.equals("1"))
        tmsg = tmsg + "1";
    else if(msg.equals("2"))
        tmsg = tmsg + "2";
    else if(msg.equals("3"))
        tmsg = tmsg + "3";
    else if(msg.equals("4"))
        tmsg = tmsg + "4";
    else if(msg.equals("5"))
        tmsg = tmsg + "5";
    else if(msg.equals("6"))
        tmsg = tmsg + "6";
    else if(msg.equals("7"))
        tmsg = tmsg + "7";
    else if(msg.equals("8"))
        tmsg = tmsg + "8";
    else if(msg.equals("9"))
         tmsg = tmsg + "9";
    else if(msg.equals("AC")) { // All clear button.............
         fnum = "";
         snum = "";
         tmsg = "";

         aflag = false;
         sflag = false;
         mflag = false;
         dflag = false;
         dotflag = false;
    }
    else if(msg.equals("Backspace")) {
        fnum = ipTextField.getText();

        if(fnum == null)
            tmsg = "";
        else
            tmsg = fnum.substring(0,fnum.length()-1);
    }
    else if(msg.equals("+")) {
        fnum = ipTextField.getText();

        tmsg = "";
        aflag = true;
        dotflag = false;
    }
    else if(msg.equals("-")) {
        fnum = ipTextField.getText();
        tmsg = "";
        sflag = true;
        dotflag = false;
    }
    else if(msg.equals("X"))
{

fnum = ipTextField.getText();

tmsg = "";

mflag = true;

dotflag = false;

}

else if(msg.equals("/"))
{

fnum = ipTextField.getText();

tmsg = "";

dflag = true;

dotflag = false;

}

else if(msg.equals("sin"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.sin(dval)+"";

}

else if(msg.equals("cos"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.cos(dval)+"";

}

else if(msg.equals("tan"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.tan(dval)+"";

}

else if(msg.equals("X^2"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = (dval*dval)+"";

}

else if(msg.equals("X^3"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = (dval*dval*dval)+"";

}

else if(msg.equals("Sqrt"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();

tmsg = Math.sqrt(dval)+"";

}

else if(msg.equals("X!"))

{

fnum = ipTextField.getText();

if(fnum==null) fnum = "0.0";

tmsg = "";

dotflag = false;

Double dobj;

double dval;

dobj = Double.valueOf(fnum);

dval = dobj.doubleValue();




long li=1;

for(int i = 1; i<=(int)dval;i++)

li *= i;

tmsg = li+"";




}

else if(msg.equals(".")&&(dotflag==false))

{

tmsg = tmsg + ".";

dotflag = true;

}

else if(msg.equals("PI"))

{

tmsg = Math.PI+"";

dotflag = true;

}

else if(msg.equals("Rand"))

{

tmsg = rand.nextFloat()+"";

dotflag = true;

}

else if(msg.equals("1/X"))

{

fnum = ipTextField.getText();

double d1;

Double do1;

do1 = Double.valueOf(fnum);

d1 = do1.doubleValue();



if(d1==0.0)

{

try
{

ipTextField.setText("Divide by Zero Attempted.");

Thread.sleep(1000);
}

catch(InterruptedException ie)
{
}


tmsg = "";

}

else

{

tmsg =( (1/d1) + "" );

dotflag = true;

}

}

else if(msg.equals("="))
{

snum = ipTextField.getText();

if(snum == null) snum = "0";

if(fnum == null) fnum = "0";



double n1,n2;

Double do1,do2;

do1 = Double.valueOf(fnum);

do2 = Double.valueOf(snum);


n1 = do1.doubleValue();

n2 = do2.doubleValue();



if(aflag==true)

{

tmsg = (n1+n2+"");

}


if(sflag==true)

{

tmsg = (n1-n2+"");

}


if(mflag==true)

{

tmsg = (n1*n2+"");

}

if(dflag==true)


{

tmsg = (n1/n2+"");

}

aflag = false;

sflag = false;

mflag = false;

dflag = false;

dotflag = true;

}

ipTextField.setText(tmsg);

}

public void paint(Graphics g)

{

}

}

PROGRAM TO IMPLEMENT ROUND ROBIN SCHEDULING

import java.io.DataInputStream;

import java.io.IOException;

import datastruct.LinkedQueue;

import datastruct.Node;


/*

@author athiruban

PROGRAM TO IMPLEMENT ROUND ROBIN SCHEDULING

***/



public class App

{

static String[] process_name=null;

static int[] burst_time=null;

static int[] waiting_time=null;

static int[] turnaroundtime =null;

static int noofproc=0;

static DataInputStream dis = new DataInputStream(System.in);

static LinkedQueue lq;

static int exectime=0;

static boolean processed=false;

static int timeslice=0;

public static void readInfo()

{

processed = false;

System.out.print("\n\tEnter No Of Process : ");

try{

noofproc = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }

//allocate memory


process_name = new String[noofproc];

burst_time = new int[noofproc];

waiting_time = new int[noofproc];

turnaroundtime = new int[noofproc];


lq = new LinkedQueue();


for(int i = 0;i<noofproc;i++)

{

System.out.print("\n\tEnter Process Name : ");



try{

process_name[i] = dis.readLine(); } catch(IOException ioe){ }


System.out.print("\n\tEnter Burst time : ");



try{

burst_time[i] = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }


lq.addLast(process_name[i],burst_time[i]);

}


}


public static int getBurstByName(String str)

{

for(int i = 0 ;i<noofproc;i++) { if( str.equals(process_name[i]) ) return burst_time[i]; }

return 0;

}


public static int getIdByName(String str)

{

for(int i = 0 ;i<noofproc;i++) { if( str.equals(process_name[i]) ) return i; }

return 0;

}


public static void displayStatistics()

{

double wt=0;

double tt = 0;

if(processed==false){ System.out.println("Locked"); return; }


System.out.println("\nProcess Execution Statistics");

System.out.println("----------------------------");

System.out.println("\nProcessName\t"+"BurstTime\t"+"waitingTime\t"+"Turnaround Time");

for(int i =0;i<noofproc;i++)

{

System.out.println(process_name[i]+"\t\t"+burst_time[i]+"\t\t"+waiting_time[i]+"\t\t"+turnaroundtime[i]);

wt += waiting_time[i];

tt += turnaroundtime[i];

}

System.out.println("\n\tCPU Time Slice is :"+timeslice+" ms");

System.out.println("\n\tAverage Waiting Time is :"+(wt/noofproc)+" ms");

System.out.println("\n\tAverage Turnaround Time is :"+(tt/noofproc)+" ms");

System.out.println("\n\tTotal Execution Time is : "+exectime+" ms");


}


public static void process()

{

Node tnode; // reference


if(process_name == null){System.out.println("Locked");return;}


System.out.println("INITIAL STAGE");

lq.display();

System.out.print("\n\tENTER TIME SLICE > ");

try{

timeslice = Integer.parseInt(dis.readLine());

}catch(IOException ioe){ }



while(lq.getSize()>0)

{


tnode = lq.removeFirst();

if(tnode.p_no >= timeslice) {

exectime += timeslice;

tnode.p_no -= timeslice;

}

else if(tnode.p_no > 0 && tnode.p_no < timeslice) {

exectime += tnode.p_no;

tnode.p_no =0;

}


if(tnode.p_no > 0)

lq.addLast(tnode.name,tnode.p_no);

else {

System.out.println("\tP r o c e s s "+tnode.name+" i s t e r m i n a t e d");

waiting_time[getIdByName(tnode.name)]=exectime-getBurstByName(tnode.name);

turnaroundtime[getIdByName(tnode.name)]


=waiting_time[getIdByName(tnode.name)]+burst_time[getIdByName(tnode.name)];

}

lq.display();

System.out.println("\t=--------------------------------------------------=");

try{ dis.read(); } catch(IOException ioe){ }

}

processed = true;

}


public static void main(String args[])

{

int ichoice=0;


L1:

do{

System.out.println("\n\n");

System.out.println("\tIMPLEMENTING ROUND ROBIN SCHEDULING");

System.out.println("\t-----------------------------------");

System.out.println("\t1 TO READ DETAILS");

System.out.println("\t2 TO PROCESS QUEUE");

System.out.println("\t3 TO DISPLAY STATISTICS");

System.out.println("\t4 TO EXIT");


System.out.print("\n\tYour Choice > ");

try{ ichoice = Integer.parseInt(dis.readLine() ); } catch(IOException ioe){ }


switch(ichoice)

{

case 1: readInfo();

break;

case 2: process();

break;

case 3: displayStatistics();

break;

case 4:

break L1;

default:

break;

}


} while(true);


}

}


Implementing Graph Traversal (Breath First and Depth First)

Program Coding


// Include necessary headers

int main()


{



int ichoice,i,no=0,j;

int **graph=NULL;

char *symbl=NULL,gt,c,src,dest;

int getindex(char *,char ,int );


do{

    printf("\n\n\tGraph\n");

    printf("\n\t 1 to Create Graph \n");

    printf("\n\t 2 to Depth First Search \n");

    printf("\n\t 3 to Breath First Search\n");

    printf("\n\t 4 to Exit the program \n");

    printf("\n Your Choice -> ");



    scanf("%d",&ichoice);



    switch(ichoice) {

        case 1:
            if(graph!=NULL) {
                for(i=0;i<no;i++) 
                    free(graph[i]);
                free(graph);
                free(symbl);
            }

            printf("\n\tHow many nodes -> ");
            scanf("%d",&no);

            graph = malloc(no*sizeof(int *));
            symbl = malloc(no*sizeof(char));

            for(i=0;i<no;i++)
                graph[i] = (int *)malloc(no*sizeof(int));

            printf("\n\nGraph Type Directed or undirected -> ");
            getchar();

            gt = getchar();

            printf("\n\tEnter Nodes Avoid Repetition\n\t");

            for(i=0;i<no;i++) {
                scanf(" %c",&c);
                symbl[i] = c;
            }

            printf("\n\nEnter Edge pair\n");

            while(1) {
                scanf(" %c %c",&src,&dest);

                i=getindex(symbl,src,no);
                j=getindex(symbl,dest,no);

                if(i==-1||j==-1) break;

                if(gt =='d' || gt== 'D')
                    graph[i][j]=1;
                else
                    graph[i][j]=graph[j][i]=1;
            }

            printf("\n\nEdges created\n\n");
            break;
        case 2:
            dfs(graph,symbl,no);
            break;
        case 3:
            bfs(graph,symbl,no);
            break;
        case 4:
            exit(0);
    }
}while(1);

return 0;

}

int getindex(char *sy,char c,int n) { 
 
    int i;

    for(i=0;i<n;i++){
        if(sy[i]==c) 
            return i; 
    }
    return -1;
}

bfs(int **graph,char *symb,int n) {

    int x=0,*visited,i,que[20];
    int front=-1,rear=-1;
    char cc;

    visited = (int *) malloc(sizeof(int) * n);
    //Get Where to begin

    printf("\n\nWhere to Begin Traverse\n");
    getchar();

    cc = getchar();

    x=getindex(symb,cc,n);

    if(x==-1) {
        printf("\n\n\tInvalid Node....");
        return;
    }
 
    for(i=0;i<n;i++) 
        visited[i]=0;

    printf("\n\nBreath First Traversal\n");
    printf("%c ",symb[x]);

    visited[x]=1;

    rear++; 
    front++;
    que[rear]=x;

    while( front <= rear ) {
        x = que[front];
        front ++;
  
        for(i=0;i<n;i++) {
            if( (graph[x][i] == 1) && (visited[i] == 0) ) {
                printf("%c ",symb[i]);
                visited[i]=1;
                rear++;
                que[rear]=i;
            }
        }

    }     
}



dfs(int **graph,char *symb,int n) {

    int i,top=-1,stack[20],pop_v,j,t,*visited;
    int x=0;
    char cc;

    visited = (int *) malloc(sizeof(int ) * n); 
 
    for(i=0;i<n;i++)
        visited[i] = 0;
 
    printf("\n\nWhere to Begin Traverse\n");
    getchar();
    cc = getchar();
    x = getindex(symb,cc,n);

    if(x==-1) {
        printf("\n\tInvalid Node...");
        return ;
    }
    top++;
    stack[top] = x;
    printf("\n\nDepth First Traversal\n");

    while( top >= 0) {
        pop_v = stack[top];
        top--;
        if( visited[pop_v] == 0) {
            printf("%c ",symb[pop_v]);
            visited[pop_v] = 1;
        }
        else continue;
 
        for(i=n-1;i>=0;i--) {
            if( (graph[pop_v][i] == 1) && (visited[i] == 0) ) {
                top++;
                stack[top]=i;
            }
        } 
    }
}



The author is not liable for any discrepancies......