Tuesday, March 13, 2012

Seperation of terminals,non-terminals and productions in the CFG in a compiler Design

Let us consider the productions of grammer G, that may contain any number of productions then the code is,

PROGRAM:

#include<iostream>
#include<string.h>
using namespace std;
class production
{
private:
char left;
char right[10][10];
int noa,i,j;
public:
void setproduction(char str[15])
{
j=0;noa=0;
left=str[0];
for(i=3;str[i]!='\0';i++){
if(str[i]!='/'){
   right[noa][j++]=str[i];
right[noa][j]='\0';
}
else{
   right[noa][j]='\0';noa++;j=0;
}
}
}
void printproduction()
{
cout<<left<<"->";
for(i=0;i<=noa;i++)
{
cout<<right[i];
if(i!=noa){cout<<"/";}
}
}
friend class CFG;
};

Sunday, January 1, 2012

Puzzle: Time for a change JAVA

Consider the following problem:
     Ram goes to the auto parts store to buy a spark plug that costs $1.10, but all he has in his wallet are two-dollar bills. How much change should he get if he pays for the spark plug with a two-dollar bill?

Here is a problem that attempts to solve the word problem. What does it print?

CODE:
public class Change
{
  public static void main(String args[])
  {
     System.out.println(2.00-1.10);
  }
}
Output:
0.8999999999999999

Here is a concept of BigDecimal :

Friday, December 30, 2011

Calculator simple calculator using java:JAVA

Save below code as calculator.java

CODE:
import javax.swing.JOptionPane;
public class calculator
{
  public static void main(String args[])
  {
    String s1,s2,s3,n1;
int op2=1;
int a,b,op1,n,i;
float re=0;
while(op2!=0)
{
n1=JOptionPane.showInputDialog("Enter what operation you want to perform using this calculator:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division\n5.Reminder\nAny other number to exit");
int op=Integer.parseInt(n1);
if((op>0)&&(op<6))
{
 switch(op)
 {
   case 1:
{
 s2=JOptionPane.showInputDialog("Enter number of values you want to add:");
 op1=Integer.parseInt(s2);
 for(i=0;i<op1;i++)
 {
   s3=JOptionPane.showInputDialog("Enter number:");
a=Integer.parseInt(s3);
re=re+a;
 }
 JOptionPane.showMessageDialog(null,"The sum of given "+op1+" values is: "+re,"Addition",JOptionPane.PLAIN_MESSAGE);
 break;
        }
        case 2:
        {
 s2=JOptionPane.showInputDialog("Enter number of values you want to subtract:");
 op1=Integer.parseInt(s2);
 for(i=0;i<op1;i++)
 { 
   if(i==0)
   { 
 s3=JOptionPane.showInputDialog("Enter number:");
 a=Integer.parseInt(s3);
 re=a;
}
            else
            {
     s3=JOptionPane.showInputDialog("Enter number:");
 a=Integer.parseInt(s3);
 re=re-a;
   }
 }
 JOptionPane.showMessageDialog(null,"The differance of given "+op1+" values is: "+re,"Substraction",JOptionPane.PLAIN_MESSAGE);
 break;
}
        case 3:
{
 s2=JOptionPane.showInputDialog("Enter number of values you want to Multiply:");
 op1=Integer.parseInt(s2);
 for(i=0;i<op1;i++)
 {  
   if(i==0)
   { 
 s3=JOptionPane.showInputDialog("Enter number:");
 a=Integer.parseInt(s3);
 re=a;
}
else
{
   s3=JOptionPane.showInputDialog("Enter number:");
a=Integer.parseInt(s3);
re=re*a;
}
 }
 JOptionPane.showMessageDialog(null,"The Product of given "+op1+" values is: "+re,"Multiplication",JOptionPane.PLAIN_MESSAGE);
 break;
        }
case 4:
{
 s2=JOptionPane.showInputDialog("Enter divident value:");
 a=Integer.parseInt(s2);
 s3=JOptionPane.showInputDialog("Enter divisor: ");
 b=Integer.parseInt(s3);
 re=(float)a/b;
 JOptionPane.showMessageDialog(null,"The result of division is: "+re,"Division",JOptionPane.PLAIN_MESSAGE);
 break;
}
        case 5:
{
 s2=JOptionPane.showInputDialog("Enter divident value:");
 a=Integer.parseInt(s2);
 s3=JOptionPane.showInputDialog("Enter divisor: ");
 b=Integer.parseInt(s3);
 re=(float)a%b;
 JOptionPane.showMessageDialog(null,"The result of division is: "+re,"Reminder",JOptionPane.PLAIN_MESSAGE);
 break;
}
      }
    }
    else
    {
      JOptionPane.showMessageDialog(null,"Calculator is terminated as you entered wrong number.","End",JOptionPane.PLAIN_MESSAGE);
 op2=0;
}
    }
  }
}  

//End of program

Thursday, December 29, 2011

Program to find roots of quadratic equation using java

Save below code as roots.java


CODE:


import java.io.*;
import java.math.*;
import javax.swing.JOptionPane;
class roots
{
public static void main(String args[])
{
String n1,n2,n3;
float a,b,c,r1,r2;
double root1,root2,d;
n1=JOptionPane.showInputDialog("Enter the value of a in ax^2+bx+c=0");
n2=JOptionPane.showInputDialog("Enter the value of b in ax^2+bx+c=0");
    n3=JOptionPane.showInputDialog("Enter the value of c in ax^2+bx+c=0");
a=new Float(n1);
b=new Float(n2);
c=new Float(n3);
d=b*b-4*a*c;
if(d<0)
{  
  d=Math.sqrt(-d);
  JOptionPane.showMessageDialog(null,"The root1 is :(-"+b+"+i("+d+"))/"+2*a,"Result",JOptionPane.PLAIN_MESSAGE);
  JOptionPane.showMessageDialog(null,"The root1 is :(-"+b+"-i("+d+"))/"+2*a,"Result",JOptionPane.PLAIN_MESSAGE);
}
else
{
  root1=(Math.sqrt(d)-b)/2*a;
  root2=-(Math.sqrt(d)+b)/2*a;
  JOptionPane.showMessageDialog(null,"The root1 is "+root1,"Result",JOptionPane.PLAIN_MESSAGE);
  JOptionPane.showMessageDialog(null,"The root2 is "+root2,"Result",JOptionPane.PLAIN_MESSAGE);
    }
}     
}

//End of program

Wednesday, December 28, 2011

Finding roots of quadratic equations in java:JAVA

Save below code as palindrome.java


CODE:



import java.util.*;
import javax.swing.JOptionPane;
class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse="";
      original=JOptionPane.showInputDialog("Enter a string to check if it is a palindrome");
      int length = original.length();
      for ( int i = length - 1 ; i >= 0 ; i-- )
 {
         reverse = reverse + original.charAt(i);
      }
 if (original.equals(reverse))
      {
   JOptionPane.showMessageDialog(null,"Entered string is a palindrome","Result",JOptionPane.PLAIN_MESSAGE);
 }
 else
      {
      JOptionPane.showMessageDialog(null,"Entered string is not a palindrome","Result",JOptionPane.PLAIN_MESSAGE);
 }   
   }
}

//End of program

Addition program in java :JAVA

Save below code as addition.java


CODE:

import javax.swing.JOptionPane;
public class addition
{
  public static void main(String args[])
  {
    String n1,n2;
int n3,n4,sum;
n1=JOptionPane.showInputDialog("Enter first Number");
n2=JOptionPane.showInputDialog("Enter second number");
n3=Integer.parseInt(n1);
n4=Integer.parseInt(n2);
sum=n3+n4;
JOptionPane.showMessageDialog(null,"The sum is "+sum,"Result",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
  }
}  

//End of program

Welcome program in java :JAVA


Save below code as welcome.java


CODE:
import javax.swing.JOptionPane;
public class welcome
{
  public static void main(String args[])
  {
    JOptionPane.showMessageDialog(null,"Welcome to java Programming");
System.exit(0);
  }
}
//End of program