Wednesday, 14 September 2011

Design a class for Complex numbers in Java. In addition to methods for basic operations on complex numbers, provide a method to return the number of active objects created


Program:
import java.io.*;
 class complex
{
int a,b;
public static int c;
public complex(int x,int y)
{
a=x;b=y;
c++;
}
public static String add(complex n1,complex n2)
{
int a1=n1.a+n2.a;
int b1=n1.b+n2.b;
if(b1<0)
return (a1+" "+b1+"i");
else
return (a1+" "+b1+"i");
}
public static String sub(complex n1,complex n2)
{
int a1=n1.a-n2.a;
int b1=n1.b-n2.b;
if(b1<0)
return (a1+" "+b1+"i");
else
return (a1+" "+b1+"i");
}
public static String mul(complex n1,complex n2)
{

int a1=n1.a*n2.a;
int b1=n1.b*n2.b;
int v1=n1.a*n2.b;
int v2=n2.a*n1.b;
int vi=v1+v2;
if(vi<0)
return(a1-b1+" "+vi+"i");
else
return(a1-b1+"+"+vi+"i");
}
}
class com
{
public static void main(String a[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int x,y;
System.out.println("enter the no for complex1:");
x=Integer.parseInt(in.readLine());
y=Integer.parseInt(in.readLine());
System.out.println("enter the no for complex2:");
int m=Integer.parseInt(in.readLine());
int n=Integer.parseInt(in.readLine());
complex c1=new complex(x,y);
complex c2=new complex(m,n);
System.out.println("addition:"+complex.add(c1,c2));
System.out.println("subtraction:"+complex.sub(c1,c2));
System.out.println("multiplication:"+complex.mul(c1,c2));
System.out.println("count="+complex.c);
}
}



Output:


C:\j2sdk1.4.0\bin>javac com.java

C:\j2sdk1.4.0\bin>java com
enter the no for complex1:
21
12
enter the no for complex2:
34
45
addition:55 57i
subtraction:-13 -33i
multiplication:174+1353i
count=2

creating a tree component


import javax.swing.*;
import javax.swing.tree.*;

public class TreeComponent{
public static void main(String[] args) {
JFrame frame = new JFrame("Creating a JTree Component!");
DefaultMutableTreeNode parent = new DefaultMutableTreeNode("Color"true);
DefaultMutableTreeNode black = new DefaultMutableTreeNode("Black");
DefaultMutableTreeNode blue = new DefaultMutableTreeNode("Blue");
DefaultMutableTreeNode nBlue = new DefaultMutableTreeNode("Navy Blue");
DefaultMutableTreeNode dBlue = new DefaultMutableTreeNode("Dark Blue");
DefaultMutableTreeNode green = new DefaultMutableTreeNode("Green");
DefaultMutableTreeNode white = new DefaultMutableTreeNode("White");
parent.add(black);
parent.add(blue);
blue.add(nBlue);
blue.add(dBlue);
parent.add(green );
parent.add(white);
JTree tree = new JTree(parent);
frame.add(tree);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setSize(200,200);
frame.setVisible(true);
  }
}
output:

SHOW DIALOGBOX IN JAVA SWING

import javax.swing.*;
import java.awt.event.*;

public class ShowDialogBox{
  JFrame frame;
  public static void main(String[] args){
  ShowDialogBox db = new ShowDialogBox();
  }

  public ShowDialogBox(){
  frame = new JFrame("Show Message Dialog");
  JButton button = new JButton("Click Me");
  button.addActionListener(new MyAction());
  frame.add(button);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent e){
  JOptionPane.showMessageDialog(frame,"jebaraj.net");
  }
  }
}

output:






Progress Bar in Java Swing

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.html.*;

public class SwingProgressBar{
  final static int interval = 1000;
  int i;
  JLabel label;
  JProgressBar pb;
  Timer timer;
  JButton button;

  public SwingProgressBar() {
  JFrame frame = new JFrame("Swing Progress Bar");
  button = new JButton("Start");
  button.addActionListener(new ButtonListener());

  pb = new JProgressBar(020);
  pb.setValue(0);
  pb.setStringPainted(true);

  label = new JLabel("jebaraj.net");
  
  JPanel panel = new JPanel();
  panel.add(button);
  panel.add(pb);

  JPanel panel1 = new JPanel();
  panel1.setLayout(new BorderLayout());
  panel1.add(panel, BorderLayout.NORTH);
  panel1.add(label, BorderLayout.CENTER);
  panel1.setBorder(BorderFactory.createEmptyBorder(20202020));
  frame.setContentPane(panel1);
  frame.pack();
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //Create a timer.
  timer = new Timer(interval, new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  if (i == 20){
  Toolkit.getDefaultToolkit().beep();
  timer.stop();
  button.setEnabled(true);
  pb.setValue(0);
  String str = "<html>" "<font color=\"#FF0000\">" "<b>" "Downloading completed." "</b>" "</font>" "</html>";
  label.setText(str);
  }
  i = i + 1;
  pb.setValue(i);
  }
  });
  }

  class ButtonListener implements ActionListener {
  public void actionPerformed(ActionEvent ae) {
  button.setEnabled(false);
  i = 0;
  String str = "<html>" "<font color=\"#008000\">" "<b>" "Downloading is in process......." "</b>" "</font>" "</html>";
  label.setText(str);
  timer.start();
  }
  }
  
  public static void main(String[] args) {
  SwingProgressBar spb = new SwingProgressBar();
  }
}

output:

Create a ToolBar in Java


import javax.swing.*;
import java.awt.*;

public class CreateToolbar{
public static void main(String[] args) {
JFrame frame = new JFrame("Create a toolbar Which have three buttons Such as: Cut, Copy, Paste");
JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton cutbutton = new JButton(new ImageIcon("cut.gif"));
toolbar.add(cutbutton);
JButton copybutton = new JButton(new ImageIcon("copy.gif"));
toolbar.add(copybutton);
JButton pastebutton = new JButton(new ImageIcon("paste.gif"));
toolbar.add(pastebutton);
frame.getContentPane().add(toolbar,BorderLayout.NORTH);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setVisible(true);
}
}

JSlider Component of Java Swing

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class CreateSlider{
  JSlider slider;
  JLabel label;
  public static void main(String[] args){
  CreateSlider cs = new CreateSlider();
  }

  public CreateSlider(){
  JFrame frame = new JFrame("Slider Frame");
  slider = new JSlider();
  slider.setValue(70);
  slider.addChangeListener(new MyChangeAction());
  label = new JLabel("jebaraj.net");
  JPanel panel = new JPanel();
  panel.add(slider);
  panel.add(label);
  frame.add(panel, BorderLayout.CENTER);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyChangeAction implements ChangeListener{
  public void stateChanged(ChangeEvent ce){
  int value = slider.getValue();
  String str = Integer.toString(value);
  label.setText(str);
  }
  }
}

output:

Design a Date class similar to the one provided in the java.util package in java


Program:
package date;
public class day
{
int day,month,year,y,m,tot_day=0;
int day_mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
String week[]={"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
public void days(int a,int b,int c)
{
valid(a,b,c);
day=a;
month=b;year=c;
for(y=0;y<year;y++)
if(y%4==0)
tot_day+=366;
else
tot_day+=365;
if(month>2 && year%4==0)
day_mon[1]=29;
for(m=0;m<(month-1);m++)
tot_day+=day_mon[m];
tot_day+=day;
System.out.println(day+"-"+month+"-"+year+"  this is :"+"="+week[(tot_day+4)%7]);
}
public void valid(int dd,int mm,int yy)
{
if(yy<1)
{
System.out.println("invalid....");
System.exit(0);
}
switch(mm)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(dd>31 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
break;
case 4:
case 6:
case 9:
case 11:
if(dd>30 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
break;
case 2:
if(yy%4!=0 && dd>=29 && dd<1)
{
System.out.println("invalid......");
System.exit(0);
}
else if(yy%4==0 || dd>28 || dd<1)
{
System.out.println("invalid......");
System.exit(0);
}break;
default:
System.out.println("invalid......");
System.exit(0);
}
}
}

Main program:

import date.*;
import java.io.*;
class chkd
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
day d=new day();
System.out.println("enter the date:(dd mm yyyy):");
int dd=Integer.parseInt(in.readLine());
int mm=Integer.parseInt(in.readLine());
int yy=Integer.parseInt(in.readLine());
d.days(dd,mm,yy);
}
}

Output:


C:\j2sdk1.4.0\bin>javac chkd.java
Note: chkd.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

C:\j2sdk1.4.0\bin>java chkd
enter the date:(dd mm yyyy):
30
04
1989
30-4-1989  this is :=sunday

C:\j2sdk1.4.0\bin>java chkd
enter the date:(dd mm yyyy):
30
02
2000
invalid......

CPU Scheduling Programs

/* CPU Scheduling*/

#include<stdio.h>
#include<conio.h>
void fcfs()
{
int n,p[10],bt[10],wt[10],ta[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();
printf(" First Come First Served");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i<n;i++)
 {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  p[i]=i ;
 }
wt[0]=0;
for(i=0;i<n;i++)
{
 wt[i+1]=wt[i]+bt[i];
 ta[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=twt/n;
atat=tat/n;
   printf("\n");
   printf("Process_no Burst time Wait time Turn around time\n\n");
   for(i=0;i<n;i++)
    printf("P%d %d %d %d\n",p[i],bt[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);


}
void SJFNP()
{
int n,p[10],bt[10],wt[10],ta[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();
 printf(" Shortest Job First Non Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i<n;i++)
 {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  p[i]=i;
 }

int j,temp;
wt[0]=0;
for(i=0;i<n;i++)
{
 for(j=0;j<n-1;j++)
  {
   if(bt[i]<bt[j])
   {
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
   }
  }
 }
for(i=0;i<n;i++)
{
 wt[i+1]=wt[i]+bt[i];
 ta[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=(float)twt/n;
atat=(float)tat/n;
printf("\n");
  printf("Process_no Burst time Wait time Turn around time\n\n");
   for(i=0;i<n;i++)
    printf("P%d %d %d %d\n",p[i],bt[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
}
void SJFP()
{
int n,p[10],bt[10],wt[10],ta[10],ar[10],pn[10],w[10],bt1[10],ar1[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();

 printf(" Shortest Job First With Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i<n;i++)
 {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  printf("\n Enter Arrival time for P%d:",i);
  scanf("%d",&ar[i]);
  p[i]=i;
  bt1[i]=bt[i];
  ar1[i]=ar[i];

 }

int j,temp,m=0;
wt[-1]=0;
for(i=0;i<n;i++)
{
 for(j=0;j<n;j++)
  {
   if(ar[i]<ar[j])
   {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;

   }
  }
 }
for(i=0;i<n-1;i++)
{
  pn[i]=p[m];
  w[i]=ar[i+1];
  bt[m]=bt[m]-(ar[i+1]-ar[i]);
  if(bt[m]>bt[i+1])
   m=i+1;
}
for(i=0;i<n;i++)
{
 for(j=0;j<n;j++)
  {
   if(bt[i]<bt[j])
   {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;

   }
  }
 }
for(i=0;i<n;i++)
{
 pn[i+n-1]=p[i];
 w[i+n-1]=w[i+n-2]+bt[i];
}
for(i=0;i<(2*n)-1;i++)
  printf("\n pn[%d]=%d \t w[%d]=%d \n",i,pn[i],i,w[i]);
for(i=0;i<n;i++)
{
  for(j=2*(n-1);j>=0;j--)
  {
    if(i==pn[j])
    {
      ta[i]=w[j]-ar1[i];
      break;
    }
  }
}
  for(i=0;i<n;i++)
{
 wt[i]=ta[i]-bt1[i];
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=(float)twt/n;
atat=(float)tat/n;

   printf("\n");
   printf("Process_no Burst time Arrival time Wait time Turn around time\n\n");
   for(i=0;i<n;i++)
    printf("P%d %d %d      %d %d\n",p[i],bt1[i],ar1[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);

}
void PNP()
{
int n,p[10],bt[10],wt[10],ta[10],pr[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
 clrscr();
 printf(" Priority Scheduling With No Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i<n;i++)
 {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  printf("\n Enter Priority for P%d:",i);
  scanf("%d",&pr[i]);
  p[i]=i;
 }

int j,temp;
wt[0]=0;
for(i=0;i<n;i++)
{
 for(j=0;j<n-1;j++)
  {
   if(pr[i]<pr[j])
   {
    temp=pr[i];
    pr[i]=pr[j];
    pr[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
   }
  }
 }
for(i=0;i<n;i++)
{
 wt[i+1]=wt[i]+bt[i];
 ta[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=twt/n;
atat=tat/n;



   printf("\n");
   printf("Process_no Burst time Priority Wait time Turn around time\n\n");
   for(i=0;i<n;i++)
    printf("P%d %d %d %d %d\n",p[i],bt[i],pr[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
}

void PWP()
{
int n,p[10],bt[10],wt[10],ta[10],ar[10],pn[10],w[10],bt1[10],ar1[10],pr[10],pr1[10],twt=0,tat=0;
float awt=0.0,atat=0.0;
clrscr();
 printf(" Priority With Preemption");
 printf("\n\nEnter the number of process:");
 scanf("%d",&n);
 for(int i=0;i<n;i++)
 {
  printf("\n Enter burst time for P%d:",i);
  scanf("%d",&bt[i]);
  printf("\n Enter Arrival time for P%d:",i);
  scanf("%d",&ar[i]);
  printf("\n Enter Priority for P%d:",i);
  scanf("%d",&pr[i]);
  p[i]=i;
  bt1[i]=bt[i];
  ar1[i]=ar[i];
  pr1[i]=pr[i];
 }


int j,temp,m=0;
wt[-1]=0;
for(i=0;i<n;i++)
{
 for(j=0;j<n;j++)
  {
   if(ar[i]<ar[j])
   {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
    temp=pr[i];
    pr[i]=pr[j];
    pr[j]=temp;
   }
  }
 }
for(i=0;i<n-1;i++)
{
  pn[i]=p[m];
  w[i]=ar[i+1];
  bt[m]=bt[m]-(ar[i+1]-ar[i]);
  if(pr[m]>pr[i+1])
   m=i+1;
}
for(i=0;i<n;i++)
{
 for(j=0;j<n;j++)
  {
   if(pr[i]<pr[j])
   {
    temp=ar[i];
    ar[i]=ar[j];
    ar[j]=temp;
    temp=p[i];
    p[i]=p[j];
    p[j]=temp;
    temp=bt[i];
    bt[i]=bt[j];
    bt[j]=temp;
    temp=pr[i];
    pr[i]=pr[j];
    pr[j]=temp;
   }
  }
 }
for(i=0;i<n;i++)
{
 pn[i+n-1]=p[i];
 w[i+n-1]=w[i+n-2]+bt[i];
}
for(i=0;i<(2*n)-1;i++)
  printf("\n pn[%d]=%d \t w[%d]=%d \n",i,pn[i],i,w[i]);
for(i=0;i<n;i++)
{
  for(j=2*(n-1);j>=0;j--)
  {
    if(i==pn[j])
    {
      ta[i]=w[j]-ar1[i];
      break;
    }
  }
}
for(i=0;i<n;i++)
{
 wt[i]=ta[i]-bt1[i];
 twt=twt+wt[i];
 tat=tat+ta[i];
}
awt=(float)twt/n;
atat=(float)tat/n;



   printf("\n");
   printf("Process_no Burst time Arrival time Priority Wait time  TAT\n\n");
   for(i=0;i<n;i++)
    printf("P%d %d %d %d %d    %d\n",p[i],bt1[i],ar1[i],pr1[i],wt[i],ta[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
}

void RR()
{

  int st[10],bt[10],wt[10],tat[10],n,tq;
  int i,count=0,swt=0,stat=0,temp,sq=0;
  float awt=0.0,atat=0.0;
  clrscr();
   printf("\n\n\t\t ROUND ROBIN");
  printf("Enter number of processes:");
  scanf("%d",&n);
   for(i=0;i<n;i++)
   {
     printf("\nEnter burst time for P%d:",i+1);
     scanf("%d",&bt[i]);
     st[i]=bt[i];
   }
   printf("\nEnter time quantum:");
   scanf("%d",&tq);
   while(1)
   {
     for(i=0,count=0;i<n;i++)
     {
       temp=tq;
       if(st[i]==0)
       {
 count++;
 continue;
       }
       if(st[i]>tq)
st[i]=st[i]-tq;
       else
if(st[i]>=0)
{
  temp=st[i];
  st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
     }
     if(n==count)
     break;
   }
   for(i=0;i<n;i++)
   {
    wt[i]=tat[i]-bt[i];
    swt=swt+wt[i];
    stat=stat+tat[i];
   }
   awt=(float)swt/n;
   atat=(float)stat/n;
   printf("\n");
   printf("Process_no Burst time Wait time Turn around time\n\n");
   for(i=0;i<n;i++)
    printf("P%d %d %d %d\n",i+1,bt[i],wt[i],tat[i]);
    printf("Avg wait time is %3.2f\n\nAvg turn around time is %3.2f",awt,atat);
    getch();
}
void main()
{
int ch;
clrscr();
do
{
printf("\n1.FCFS \n2.SJF-With no pre-emption \n3.SJF-With Pre-emption ");
printf("\n4.Priority With no pre-emption \n5.Priority With pre-emption \n6.Round Robin \n7.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
fcfs();
break;
case 2:
SJFNP();
break;
case 3:
SJFP();
break;
case 4:
PNP();
break;
case 5:
PWP();
break;
case 6:
RR();
break;
case 7:
break;
}
}while(ch<7);
}



Output
-------

1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:1
                First Come First Served
Enter the number of process:2
 Enter burst time for P0:2
 Enter burst time for P1:3
Process_no      Burst time      Wait time       Turn around time
P0              2               0               2
P1              3               2               5
Avg wait time is 1.00
Avg turn around time is 3.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:2
                Shortest Job First Non Preemption
Enter the number of process:2
 Enter burst time for P0:2
 Enter burst time for P1:3
Process_no      Burst time      Wait time       Turn around time
P0              2               0               2
P1              3               2               5
Avg wait time is 1.00
Avg turn around time is 3.50
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:3
                Shortest Job First With Preemption
Enter the number of process:2
 Enter burst time for P0:5
 Enter Arrival time for P0:1
 Enter burst time for P1:3
Enter Arrival time for P1:0
 pn[0]=1         w[0]=1
 pn[1]=1         w[1]=3
 pn[2]=0         w[2]=8
Process_no      Burst time      Arrival time    Wait time       Turn around time
P1                   5                 1                         2                        7
P0                           3                 0                         0                        3
Avg wait time is 1.00
Avg turn around time is 5.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:4
                Priority Scheduling With No Preemption
Enter the number of process:2
 Enter burst time for P0:2
 Enter Priority for P0:1
 Enter burst time for P1:3
 Enter Priority for P1:2
Process_no      Burst time      Priority        Wait time       Turn around time
P0               2                1               0                2
P1                3                   2              2                         5
Avg wait time is 1.00
Avg turn around time is 3.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:5
                Priority With Preemption
Enter the number of process:2
 Enter burst time for P0:6
 Enter Arrival time for P0:0
 Enter Priority for P0:2
 Enter burst time for P1:8
 Enter Arrival time for P1:1
 Enter Priority for P1:1
 pn[0]=0         w[0]=1
 pn[1]=1         w[1]=9
 pn[2]=0         w[2]=14
Process_no      Burst time      Arrival time    Priority        Wait time   TAT
P1               6                0                2                8          14
P0               8                1                1                0          8
Avg wait time is 4.00
Avg turn around time is 11.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:6
Enter number of processes:2
Enter burst time for P1:2
Enter burst time for P2:3
Enter time quantum:1
Process_no      Burst time      Wait time       Turn around time
P1               2                1                  3
P2               3                   2                  5
Avg wait time is 1.50
Avg turn around time is 4.00
1.FCFS
2.SJF-With no pre-emption
3.SJF-With Pre-emption
4.Priority With no pre-emption
5.Priority With pre-emption
6.Round Robin
7.Exit
Enter your choice:7

IPC(Interprocess communication)

//Inter process

#include<stdio.h>
int n=3,m,item,buf[3],i=0;
wait()
{
m=1;
printf("\n entering the critical section...");
}
signal()
{
m=0;
printf("\n ...quiting the critical section");
}
producer()
{
wait();
printf("\n enter the data to be produced");
scanf("%d",&item);
buf[i]=item;
i++;
printf("\n data produced");
signal();
}
consumer()
{
wait();
printf("\n The data %d has been consumed",buf[i-1]);
i--;
signal();
}
int main()
{
int ch;
clrscr();
start:
printf("\n1-producer");
printf("\n2-consumer");
printf("\n Enter choice:");
scanf("%d",&ch);
if(ch==1)
{
if(i==n)
printf("\n buffer is full");
else
producer();
goto start;
}

else if(ch==2)
{
if(i==0)
printf("\n no item to consume");
else
consumer();
goto start;
}
else
exit(0);
return 0;
}





Output

 PRODUCER CONSUMER PROBLEM

         Operations Are
1.PRODUCER –write
2.CONSUMER - get
3.BUFFER
4.EXIT

 Enter The choice of Operation1
 Enter The Value To Produce: 2
 Do you want to continue(1/2): 1
 Enter The Value To Produce: 4
 Do you want to continue(1/2): 2

         Operations Are
1.PRODUCER -write
2.CONSUMER - get
3.BUFFER
4.EXIT

 Enter The choice of Operation3
 The Buffer Has:2       4

         Operations Are
1.PRODUCER -write
2.CONSUMER - get
3.BUFFER
4.EXIT

 Enter The choice of Operation2
 Consumer Is Consuming the Item0
 Do You want to Continue(1/2)2

Operations Are
1.PRODUCER -write
2.CONSUMER - get
3.BUFFER
4.EXIT
 Enter The choice of Operation: 4


Develop a scientific calculator using even-driven programming paradigm of Java


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class scientific extends JFrame implements ActionListener
{
JTextField jtx;
double temp,temp1,result,a;
static double m1,m2;
int k=1,x=0,y=0,z=0;
char ch;
JButton
one,two,three,four,five,six,seven,eight,nine,zero,clr,pow2,pow3;
JButton
plus,min,div,lg,rec,mul,eq,plmi,poin,mr,mc,mp,mm,sqrt,sin,cos,tan;
Container cont;
JPanel textPanel,buttonpanel;
scientific()
{
cont=getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel=new JPanel();
Font font=new Font("Arial",Font.PLAIN,18);
jtx=new JTextField(25);
jtx.setFont(font);
jtx.setHorizontalAlignment(SwingConstants.RIGHT);
jtx.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent keyevent)
{
char c=keyevent.getKeyChar();
if(c>='0' && c<='9')
{
}
else
{
keyevent.consume();
}
}
});
textpanel.add(jtx);
buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(5,6));
boolean t=true;

sin=new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
mr=new JButton("MR");
buttonpanel.add(mr);
mr.addActionListener(this);
seven=new JButton("7");
buttonpanel.add(seven);
seven.addActionListener(this);
eight=new JButton("8");
buttonpanel.add(eight);
eight.addActionListener(this);
nine=new JButton("9");
buttonpanel.add(nine);
nine.addActionListener(this);
clr=new JButton("AC");
buttonpanel.add(clr);
clr.addActionListener(this);

cos=new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
mc=new JButton("MC");
buttonpanel.add(mc);
mc.addActionListener(this);
four=new JButton("4");
buttonpanel.add(four);
four.addActionListener(this);
five=new JButton("5");
buttonpanel.add(five);
five.addActionListener(this);
six=new JButton("6");
buttonpanel.add(six);
six.addActionListener(this);
mul=new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);

tan=new JButton("TAN");
buttonpanel.add(tan);
tan.addActionListener(this);
mp=new JButton("M+");
buttonpanel.add(mp);
mp.addActionListener(this);
one=new JButton("1");
buttonpanel.add(one);
one.addActionListener(this);
two=new JButton("2");
buttonpanel.add(two);
two.addActionListener(this);
three=new JButton("3");
buttonpanel.add(three);
three.addActionListener(this);
min=new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);

pow2=new JButton("x^2");
buttonpanel.add(pow2);
pow2.addActionListener(this);
mm=new JButton("M-");
buttonpanel.add(mm);
mm.addActionListener(this);
zero=new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);
plmi=new JButton("+/-");
buttonpanel.add(plmi);
plmi.addActionListener(this);
poin=new JButton(".");
buttonpanel.add(poin);
poin.addActionListener(this);
plus=new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);

pow3=new JButton("x^3");
buttonpanel.add(pow3);
pow3.addActionListener(this);
rec=new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
sqrt=new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
lg=new JButton("log");
buttonpanel.add(lg);
lg.addActionListener(this);
div=new JButton("/");
div.addActionListener(this);
buttonpanel.add(div);
eq=new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);


cont.add("Center",buttonpanel);
cont.add("North",textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();

if(s.equals("1"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"1");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"2");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"3");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"3");
z=0;
}
}
if(s.equals("4"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"4");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"4");
z=0;
}
}
if(s.equals("5"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"5");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"5");
z=0;
}
}
if(s.equals("6"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"6");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"6");
z=0;
}
}
if(s.equals("7"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"7");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"7");
z=0;
}
}
if(s.equals("8"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"8");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"8");
z=0;
}
}
if(s.equals("9"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"9");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"9");
z=0;
}
}
if(s.equals("0"))
{
if(z==0)
{
jtx.setText(jtx.getText()+"0");
}
else
{
jtx.setText("");
jtx.setText(jtx.getText()+"0");
z=0;
}
}
if(s.equals("AC"))
{
jtx.setText("");
x=0;
y=0;
z=0;
}
if(s.equals("log"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.log(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("1/x"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=1/Double.parseDouble(jtx.getText());
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}

if(s.equals("x^2"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),2);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("x^3"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.pow(Double.parseDouble(jtx.getText()),3);
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("+/-"))
{
if(x==0)
{
jtx.setText("-"+jtx.getText());
x=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("."))
{
if(y==0)
{
jtx.setText(jtx.getText()+".");
y=1;
}
else
{
jtx.setText(jtx.getText());
}
}
if(s.equals("+"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='+';
}
else
{
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='+';
y=0;
x=0;
}
jtx.requestFocus();
}
if(s.equals("-"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=0;
ch='-';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
jtx.setText("");
ch='-';
}
jtx.requestFocus();
}
if(s.equals("/"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='/';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='/';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("*"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
temp=1;
ch='*';
}
else
{
x=0;
y=0;
temp=Double.parseDouble(jtx.getText());
ch='*';
jtx.setText("");
}
jtx.requestFocus();
}
if(s.equals("MC"))
{
m1=0;
jtx.setText("");
}
if(s.equals("MR"))
{
jtx.setText("");
jtx.setText(jtx.getText() + m1);
}
if(s.equals("M+"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1+=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("M-"))
{
if(k==1)
{
m1=Double.parseDouble(jtx.getText());
k++;
}
else
{
m1-=Double.parseDouble(jtx.getText());
jtx.setText(""+m1);
}
}
if(s.equals("Sqrt"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sqrt(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("SIN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.sin(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("COS"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.cos(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("TAN"))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
a=Math.tan(Double.parseDouble(jtx.getText()));
jtx.setText("");
jtx.setText(jtx.getText() + a);
}
}
if(s.equals("="))
{
if(jtx.getText().equals(""))
{
jtx.setText("");
}
else
{
temp1 = Double.parseDouble(jtx.getText());
switch(ch)
{
case '+':
result=temp+temp1;
break;
case '-':
result=temp-temp1;
break;
case '/':
result=temp/temp1;
break;
case '*':
result=temp*temp1;
break;
}
jtx.setText("");
jtx.setText(jtx.getText() + result);
z=1;
}
}
jtx.requestFocus();
}
public static void main(String args[])
{
scientific n=new scientific();
n.setTitle("CALCULATOR");
n.setSize(370,250);
n.setResizable(false);
n.setVisible(true);
}
}
output: