Functions

Functions in Shell Script


When program gets more complex we divide it into small modules or entities which is know as function. It is series of instruction or commands statements. Function performs some particular activities in shell programs i.e. it had specific task to do.

Syntax:

           function_name( )
           {
                command_statement1
                command_statement2
                .....
                ...
                command_statementN
                return
           }
Where function_name is the name of your function, that executes series of many commands and a return statement terminates the function.

Example:
$ name = "Sunni!"
Type SayHi() at $ prompt as follows
$ SayHello()
{
       echo "Hello $name, How are you?"
       return
}
To execute this Function SayHello() just type it's name as follows:

Output :



This way we can call function. Note that after restarting your terminal you will loss this function, because its created for current the session only.

To overcome this problem we can save this fucntion in a file for future use.

The given script will create the function today() and print the current date.

today()
{
        echo This is a `date +"%A %d in %B of %Y (%r)"`
        return
}

today


The read statement in shell

read statement


It is used to get the input from keyboard and store to variable.
Syntax is as follows:
read var1, var2,...varN

Above given script will ask user, to input from keyboard a number, character or string.

The script given below will ask user to input his name and then will wait to enter name via keyboard. After that user will enter his name from keyboard (after giving name he have to hit ENTER key on keyboard) and then the entered name through keyboard will be stored to variable name.

$ cat > SayHi
#
#Script to read your the name of user from key-board
#
echo -n "Enter your first name :"
read name
echo "Hello $name, How are you today?"

Output : 

Conditional Program or Command Execution

Conditional program or commands execution i.e. && and ||


The control operators in conditional execution are && and || .
The syntax for && is as follows :

           cmd1 && cmd2
           cmd2 will execute if only cmd1 returns an exit status of zero.

The syntax for OR list as follows
Syntax:
           cmd1 || cmd2
           cmd2 is executed if and only if cmd1 returns a non-zero exit status.

We can use both as follows syntax:



cmd1 && cmd2 if exist status is zero || cmd3 if exit status is non-zero
if cmd1 will get executed successfully then the shell will run cmd2 and if cmd1 is not successful then the cmd3 will be executed.

Example:


           $ rm myfile && echo "File removed successfully" || echo "File not removed"

If file "myfile" removed successfull exit status will be zero then "echo File removed successfully" statement will executed, otherwise "echo File not removed" statement will be executed since "exist status is non-zero"

Output : 


Arithmetic Calculations in Shell

It is used to perform arithmetic operations or Calculations .

Syntax:
expr operator1 mathmetic-operator operator2

Given below is examples:
$ expr 4 + 6
$ expr 6 - 2
$ expr 20 / 4
$ expr 10 % 4
$ expr 30 * 2
$ echo `expr 5 + 8`

Note:
expr 10 % 4 - Reads it as 10 mod 4 and will return the remainder 2.
expr 30 * 2 - Here multiplication uses * and not * since it is used as wild card in shell programming.

For given last statement above note the given following points :

(1) Before expr word we used ` (back quote) sign not the (single quote i.e. ') sign, in given statement it is also ending with ` i.e. back quote.

(2) Here if we use double quotes or single quotes, it will not be able to calculte example :

$ echo "expr 6 + 3" # It will print expr 6 + 3
$ echo 'expr 6 + 3' # It will print expr 6 + 3


Calculator Application Code in JAVA

Given below is code for Over All Project code Calculator Application written in JAVA Programming language.



import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.applet.*;
import java.math.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;

public class Calculator_app extends JApplet implements KeyListener, ActionListener
{
private File_class f=new File_class();// The File_class class is an inner class. The File_class class consists of methods used  to work with  files.
private ButtonGroup degree_cont;//To group the two checkbox options.
private JTextArea textarea1;

private JLabel label1,memoryTagLabel;

private Button_class[] button;// Declaring variable of Button_class.

private JTextField textf;// Declaring the text field to the input given.
private String str="0",str1="",str2="",op="", str3="";// Declaring the 5 string variable.
private double m=0,n=0,mem=0;
private JPanel pa1,pa2,pa3,pa4,pa5,pa6,pa7, pa8;// Declaring the eight variable.
// Declaring the label for the button.
private String[] Strbutton={"Power" , "Advanced" ,"Shift","C","CE", "BackSpace" ,"7", "8","9","4","5","6","1","2","3","0",".","Pie", "=","Ans","Sin","Cos","Sqrt","Sqr","X!","Pow" ,"%","1/X","+/-","StartStoring","EndStoring" ,"/", "Del","*","Me","-","M+","+","M-","MR","R","Preview","Close"};

private boolean cond=false,cond1=false, cond2=false,cond3=false,cond4=false,cond5=true,pressed=false;


public void init()// Init method is used to initialise the values.
{
 
Container contentPane = getContentPane();
  contentPane.setLayout(new BorderLayout(0,7));
// Setting the layout as borderlayout.
//   contentPane.setBackground(Color.yellow);
label1=new JLabel(" CALCULATOR ");
memoryTagLabel=new JLabel(" ");
memoryTagLabel.setEnabled(true);
label1.setFont(new Font("Times Roman",Font.BOLD,20));
memoryTagLabel.setFont(new Font("Times Roman",Font.BOLD,17));
memoryTagLabel.setForeground(Color.black);
label1.setForeground(Color.blue);
textarea1=new JTextArea("Developedn  Byn Sunni ",4,11);
textarea1.setFont(new Font("Times Roman",Font.BOLD,12));
textarea1.setEditable(false);

boolean wrap = true;
textarea1.setLineWrap(wrap);

degree_cont = new ButtonGroup();//The container of the checkboxes.
JRadioButton degrees = new JRadioButton("Deg",true);
degrees.setActionCommand("Deg");
//degrees.addItemListener(this);
JRadioButton radians = new JRadioButton("Rad",false);
radians.setActionCommand("Rad");
//radians.addItemListener(this);
degree_cont.add(degrees);
degree_cont.add(radians);
button =new Button_class[43];
for(int i=0;i<20;i++)//Creating the Advanced buttons such as, sin & cos & arcsin.
button[i]=new Button_class (Strbutton[i],Color.black);

// new Color(55,168,200)

for(int i=20;i<43;i++)//Creating the numbers.
button[i]=new Button_class (Strbutton[i],Color.black);
textf=new JTextField(22);
textf.setHorizontalAlignment(JTextField.RIGHT );
textf.setFont(new Font("Times Roman", Font.BOLD,12));
textf.addKeyListener(this);
textf.setText("0");
//Constructs  panels in the applet.
pa1=new JPanel();
pa2=new JPanel();
pa3=new JPanel();
pa4=new JPanel();
pa5=new JPanel();
pa6=new JPanel();
pa7=new JPanel();
pa8=new JPanel();
pa5.setBounds(20,1,275,30);
pa5.add(button[0]);
pa5.add(label1);
contentPane.add(pa5);
pa3.setLayout(new GridLayout(6,3,2,3));
pa3.setBounds(20,120,225,156);
for (int i=3;i<19;i++)
pa3.add(button[i]);
contentPane.add(pa3);
pa1.setLayout(new GridLayout(3,3,2,2));
pa1.setBounds(20,276,225,78);
for(int i=20;i<29;i++)
pa1.add(button[i]);
contentPane.add(pa1);
pa2.setBounds(20,32,300,90);
pa2.add(textf);
pa2.add(memoryTagLabel);
pa2.add(pa4);
pa4.setBounds(40,50,260,80);
pa4.add(button[1]);
pa4.add(button[2]);
pa4.add(degrees);
pa4.add(radians);

textf.setBackground(Color.black);
contentPane.add(pa2);
    pa7.setBounds(320,1,150,182);
pa7.add(textarea1);
pa7.add(button[41]);
pa7.add(button[42]);
contentPane.add(pa7);
pa6.setLayout(new GridLayout(6,2,2,2));
pa6.setBounds(280,196,200,156);
pa8.setBounds(0,0,400,40);
for(int i=29;i<41;i++)
pa6.add(button[i]);
contentPane.add(pa6);
contentPane.add(pa8);
//Before you can use the Calculator you have to press the power button on the calculator interface to enable  buttons on the calculator interface.
for(int i=1;i<43;i++)
button[i].setEnabled(false);
textf.setEditable(false);

//Adding the action listener.
for(int i=0;i<43;i++)
button[i].addActionListener(this);
//degrees.addItemListener(this);
//radians.addItemListener(this);
}//End of init method.


class Button_class extends JButton
{
public Button_class(String name, Color xyz)
{
this.setLabel(name);
this.setForeground(xyz);
}
}



 

  public void keyTyped(KeyEvent e)//Denotes a key press followed by a key release.
  {
char ch;
ch=e.getKeyChar();
if ((ch=='1')||(ch=='2') || (ch=='3') ||(ch=='4')||
(ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9')
||(ch=='0')) //To check the source label of the pressed key.
{
if(pressed)
{
str="0";
pressed=false;
}
if(str=="0")//Check whether string is empty or not.
str=""+ch;
else
str=str+ch;
}//End if.
else if(ch=='.'&& cond5)
{
if(pressed)
{
str="0";
pressed=false;
}
str=str+ch;
cond5=false;
}
else if((ch=='+')||(ch=='-')||(ch=='*') ||(ch=='/'))
{
op=""+ch;
str1=str;
str="0";
}
else if(ch=='=')
calculate();
textf.setText(str);
}//End of method declaration KeyTyped().




public void keyPressed(KeyEvent e)//Indicates  a key is pushed down.
{
double ac=e.getKeyCode();
if((ac==13)||(ac==10))
calculate();
else if(ac==8)
{
if(str.compareTo("")==0)
    textf.setText("Error");
else
{
str = str.substring(0,str.length()-1);
textf.setText(str);
}
}
}//End of method KeyPressed.

public void keyReleased(KeyEvent e)//Indicates  a key is Released.
{
}


public void actionPerformed(ActionEvent e)//  Action listener method for  buttons on the calculator.
{
if (e.getSource()==button[29])//For begin store in the file. // " StartStoring" button
{
cond4=true;
button[30].setEnabled(true);// for //button with label StartStoring "EndStoring " Button
button[29].setEnabled(false);
f.CreateFile();// Invokes the method for creating a new file.
}
  else if (e.getSource()==button[30])//End of the storing.
{
cond4=false;
button[29].setEnabled(true);
button[30].setEnabled(false);
f.endStore();// Call method  for stop storing the file.
}
else if (e.getSource() == button[41]) //Preview button.
{
button[41].setEnabled(false);
button[42].setEnabled(true);
f.WriteFileOnTextArea();
}
else if (e.getSource()==button[42]) // "Close" Button
{
button[41].setEnabled(true);
button[42].setEnabled(false);
textarea1.setText("Developedn byn Amann" );
}
else if (e.getSource()==button[32]) // "Del" Button
//Del button.
{
f.CreateFile();
}
else if (e.getSource()==button[1])  //For advanced buttons.
{
if(!cond)
{
cond=true;
pa1.setVisible(true);
button[1].setLabel("Simple");
for(int i=20;i<29;i++)
button[i].setEnabled(true);
}
else
{
button[1].setLabel("Advanced" );
pa1.setVisible(false);
cond=false;
}
}

else if(e.getSource()==button[20])
{
pressed=true;
double x=Double.valueOf(str).doubleValue();
String str4 = degree_cont.getSelection().getActionCommand();
if (!cond3)
{
if(str4.equals("Deg"))
{
while(x>=360)
x=x-360;
str=""+Math_class.sin_func(Math_class.convert_deg_rad(x));
textf.setText(""+str);
}
else if(str4.equals("Rad"))
{
while(x>=(2*Math.PI))
x=x-Math.PI;
str=""+Math_class.sin_func(x);
textf.setText(str);
}
f.CreateFile();
f.addTofile("Sin("+x+")="+str);
}
else
{
if(str4.equals("Deg"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.asin(x));
textf.setText(""+str);

}
else if(str4.equals("Rad"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_deg_rad(Math.acos(x));
textf.setText(str);

}
str3=str;
f.CreateFile();
f.addTofile("aSin("+x+")= "+str);
}
}
else if(e.getSource()==button[21])
{
pressed=true;
double x=Double.valueOf(str). doubleValue ();
String str4 = degree_cont.getSelection().getActionCommand();
if (!cond3)
{
if(str4.equals("Deg"))
{
while (x>=360)
x=x-360;
str=""+Math_class.cos_func(Math_class.convert_deg_rad(x));
textf.setText(str);
str3=str;

}
else if(str4.equals("Rad"))
{
while(x>=(2*Math.PI))
x=x-Math.PI;
str=""+Math_class.cos_func(x);
textf.setText(str);
str3=str;

}
f.CreateFile();
f.addTofile("Cos("+x+")="+str);
}
else
{
if(str4.equals("Deg"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.acos(x));
textf.setText(str);
str3=str;

}
else if(str4.equals("Rad"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.acos(x));
textf.setText(str);
str3=str;

}
f.CreateFile();
f.addTofile("aCos("+x+") ="+str);
}
}

else if (e.getSource()==button[0])//Power button.
{
if(!cond1)
{
cond5=true;
textf.setBackground (Color.white);
cond1=true;
button[0].setLabel("OFF");
for(int i=1;i<42;i++)
button[i].setEnabled (true);
button[30].setEnabled(false);
}
else
{
cond1=false;
cond5=true;
pressed=false;
textf.setBackground (Color.black);
button[0].setLabel("ON");
for(int i=1;i<43;i++)
button[i].setEnabled (false);
memoryTagLabel.setText(" ");
f.delFile();
str="0";str1="";str2="";
textf.setText(str);
mem=0;

}
}
else if(e.getSource()== button[16]) // "." Button
{
if(pressed)
{
str="0";
pressed=false;
}
if (cond5)
{
str=str+".";
cond5=false;
textf.setText(str);
}
}
//The implementation of the Number Buttons.
else if ((e.getSource()==button[6]) || (e.getSource()==button[7]) || (e.getSource()==button[8]) || (e.getSource()==button[9]) || (e.getSource()==button[10]) || (e.getSource()==button[11]) || (e.getSource()==button[12]) || (e.getSource()==button[13]) || (e.getSource()==button[14]) ||(e.getSource()==button[15]) )
{
if(pressed)
{
str="0";
pressed=false;
}
if(str.compareTo("0")==0||op!="")
str=e.getActionCommand();
else str=str+e.getActionCommand();
str3=str;
textf.setText(str);
showStatus(e.getActionCommand());
}
else if ((e.getSource()==button[37])|| (e.getSource()==button[35])||(e.getSource()==button[33])||(e.getSource()==button[31])||(e.getSource()==button[25]))
{ // "+" "+" "-" "*" "/" Buttons
cond5=true;
String tempStr="";
op=e.getActionCommand();
str1=str;
str3=str;
tempStr = str;
str="0";
textf.setText(tempStr);
}
else if ((e.getSource()==button[18])) // "=" Button
{ calculate();
//f.CreateFile();
}
else if (e.getSource()==button[5]) //  "BackSpace" Button
{
if(str.length() != 0)
{
str=str.substring(0,str.length()-1);
textf.setText(str);
}
else
JOptionPane.showMessageDialog(null, "Do not use backspace now.");
}
else if (e.getSource()==button[3]) // "C" Button
{
op="";
str=str3;
textf.setText(str);
}
else if (e.getSource()==button[4]) //"CE" Button
{
textf.setText("0");
cond5=true;
str="0";
str1="";
str2="";
}
else if (e.getSource()==button[27]) //  "1/X" button
{
pressed=true;
float g=0,p=0;
p=Float.valueOf(str).floatValue();
if(p==0)
{
str="0";
textf.setText("Infinite");
}
else
{
g=(1/p);
str=""+g;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("1/"+p+"="+str);
}
}
else if (e.getSource()==button[28]) // "+/-" Button
{
            if (!(str.compareTo("0")==0))
            {
                if(str.charAt(0)=='-')
str=str.substring(1,str.length());
            else
            str="-"+str;
textf.setText (str);
            }
        }
else if(e.getSource()==button[34]) // "Me" button
{
pressed=true;
memoryTagLabel.setText("M");
mem = Double.valueOf(str).doubleValue();
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[36]) //"M+" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem+(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[38]) //"M-" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem-(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);

}
else if(e.getSource()==button[39]) //"MR" Button
{
str=""+mem;
str3=str;
textf.setText(str3);
}

else if(e.getSource()==button[23])
{
pressed=true;
  double y=0;
double x=Double.valueOf(str).doubleValue();
y=Math_class.sqr_func(x);
textf.setText(""+y);
str=""+y;
str3=str;
f.CreateFile();
f.addTofile("sqr("+x+")="+str);
}
else if(e.getSource()==button[24])
    {
  pressed=true;
int x=Integer.valueOf (str).intValue();
if(x>39)
textf.setText("Infinite");
else
{
str=""+Math_class.fact_func(x);
textf.setText(""+str);
str3=str;
f.CreateFile();
f.addTofile(""+x+"!="+str);
   }

}
else if(e.getSource()==button[2]) //"Shift" Button
{
if(!cond3)
{
button[20].setLabel("arcSin");
button[21].setLabel("arcCos");
cond3=true;
}
else
{
cond3=false;
button[20].setLabel ("Sin");
button[21].setLabel ("Cos");
}
}
else if(e.getSource()==button[40]) //" R" Button
{
textf.setText("0");
mem=0;
memoryTagLabel.setText("");
}
else if(e.getSource()==button[26])
{
pressed=true;
float p,r;
p=Float.valueOf(str).floatValue();
r=p/100;
str=""+r;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile(""+p+"%="+str);
}
else if(e.getSource()==button[22])
{
pressed=true;
double l=0;
m=Double.valueOf(str).doubleValue();
l=Math.sqrt(m);
str=""+l;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("sqrt("+m+") ="+str);
}
else if (e.getSource()==button[19])
{
pressed=true;
  textf.setText(str3);
str=str3;
showStatus(str3);
}
else if (e.getSource()==button[17])
{
pressed=true;
textf.setText(""+3.1415926);
str=""+3.1415926;
}

}//Method ends action performed.

public void calculate()
{
double l=0;
int o=0;
String opp="";
str2=str;
str="0";
m=Double.valueOf(str1).doubleValue();
n=Double.valueOf(str2).doubleValue();
opp=op;
if (op.compareTo("+")==0)
{
l=m+n;
op="";
}
    else if (op.compareTo("-")==0)
    {
l=m-n;
op="";
}
    else if (op.compareTo("*")==0)
    {
        l=m*n;
op="";
    }
    else if (op.compareTo("/")==0)
    {
l=m/n;
op="";
}
    else if (op.compareTo("")==0)
    {
l=Double.valueOf(str2).doubleValue();
    }
    else if (op.compareTo("Pow")==0)
    {
o=Integer.valueOf(str2).intValue();
l=Math_class.pow_func(m,o);
op="";
    }
    cond5=true;
    pressed=true;//pressed=true specify the
//End user has pressed a symbol key such as, +, -, /.
    str=""+l;
    textf.setText(str);
    f.CreateFile();
    f.addTofile(str1+opp+str2+"="+l);
}//End of method declaration "calculate".



class File_class
{
DataOutputStream Of;

public void WriteFileOnTextArea()// Method to write in the file.
{
    String str;
  char st;
str="";
try
{
DataInputStream If=new DataInputStream(new FileInputStream("FileOut.txt"));
   while(If.readLine()!=null)
        {
      if ((st=If.readChar())!='E')
        {
                str=st+str;
        }
        if (st=='E')
        {
        textarea1.setText("");
    textarea1.append(str);
        textarea1.append("n");
        str="";
        }
      }//Ed while.
      If.close();
}//End try.
  catch(FileNotFoundException e){}   //Catches the error if the file is not found.
    catch(IOException e) {}
}// End of method WriteFileOnTextArea().

public void CreateFile()
{
try
  {
Of = new DataOutputStream(new  FileOutputStream("FileOut.txt"));
  Of.writeChars("Beginn");
    }
    catch(IOException e)
    {
System.err.println("File not opened properlyn"+e.toString());
   System.exit(1);
    }
}




public void endStore()
{
try
  {
Of.close();
    }
    catch(IOException e)
    {
System.err.println("File not closed properlyn"+e.toString());
   System.exit(1);
    }
}


public void addTofile(String str)
{
try
{
for(int i=str.length()-1;i>=0;i--)
{
Of.writeChar(str.charAt(i));
Of.writeChars("n");
}
Of.writeChar('E');
Of.writeChars("n");
}
catch (IOException io){}
}

public void delFile()
{
CreateFile();
try
{
Of.close();
}
catch (IOException io){}
}


} //End of File_class

}  // End of class Calculate_class




class Math_class  // MATH CLASS
{

public static double sin_func(double x)
{
if (x<=0.0005)
x=x+((x*x*x)/6);
else
x=2*sin_func(x/2)*cos_func(x/2);
return x;
}

public static double cos_func (double x)
{
if (x<=0.0005)
x=1-((x*x*x)/2);
else
x=(cos_func(x/2)*cos_func(x/2))-(sin_func(x/2)*sin_func(x/2));
return x;
}// End of cos_func() method.

public static double convert_rad_deg (double x)
{
//converting from radian to degree
double y;
y=(180*x)/Math.PI;
return y;
}

public static double convert_deg_rad (double x)
{
//Converting from degree to radian.
double y;
y=(Math.PI*x)/180;
return y;
}

public static double pow_func(double x,int y)
{
double s;
s=1;
for(int i=1;i<=y;i++)
s=s*x;
return s;
}

public static double sqr_func (double x)
{
double s=x*x;
return s;
}

public static long fact_func (int x)
//Factorial method.
{
long t=1;
for(int i=x;i>0;i--)
t=i*t;
return t;
}

} // END of class Math_class

Mathematical and Trigonometric Methods in Calculator Applicaiton

Implementing the Mathematical and Trigonometric Method



The Mathematical and trigonometric related methods are defined in the Math_class. The trigonometric related methods are sin and cos. The mathematical related methods that you can perform in the Calculator application are square, square root, factorial and pow.

The sin_func() method is defined to calculate the trigonometric sine value of an integer. The sin_func() accepts one argument of data type, double, and returns a value of data type, double.

Declaring sin_func() Method

public static double sin_func(double x)
{
if (x<=0.0005)
  x=x+((x*x*x)/6);
else
  x=2*sin_func(x/2)*cos_func(x/2);
return x;
}


The above listing shows the declaration of the sin_func() method that calculates the sine of a numeric value of double data type.


=============================================================

The cos_func() method id defined to calculate the cosine value of an integer. The cos_func() accepts one argument of double data type and returns a value of double data type.

Declaring cos_func()


public static double cos_func (double x)
{
if (x<=0.0005)
  x=1-((x*x*x)/2);
else
  x=(cos_func(x/2)*cos_func(x/2))-(sin_func(x/2)*sin_func(x/2));
return x;
}// End of cos_func() method.


The above listing shows the declaration of the cos_func() method that calculates the cosine of a numeric value of double data type.


=============================================================

The convert_rad_deg() is defined to convert the radian value into degree value. The convert_rad_deg() accepts one argument of double data type and returns a value of double data type.

Declaring convert_rad_deg() Method


public static double convert_rad_deg (double x)
{
//converting from radian to degree 
double y;
y=(180*x)/Math.PI;
return y;
}

The above listing shows the declaration of the convert_rad_deg() method that converts the radian value of to equivalent degree value of double data type.


=============================================================

The convert_deg_rad() is defined to convert the degree value into radian value. The convert_deg_rad() accepts one argument of double data type and returns a value of double data type.

Declaring the convert_deg_rad() Method

public static double convert_deg_rad (double x)
{
//Converting from degree to radian.
double y;
y=(Math.PI*x)/180;
return y;
}

The above listing shows the declaration of the convert_deg_rad() method that converts the degree value to equivalent radian value of double data type.


=============================================================

The pow_func() is defined to calculate the value of power method. The pow_func() accepts two arguments of double and int data types adn returns a value of double data type.

Declaring the pow_func() Method

public static double pow_func(double x,int y)
{
double s;
s=1;
for(int i=1;i<=y;i++)
  s=s*x;
return s;
}

The above listing shows the declaration of the pow_func() method.



=============================================================

The sqr_func() is defined to calculate the square of a number. The sqr_func() accepts one argument of double data type and value of daouble data type.

Declaring the sqr_func() Method


public static double sqr_func (double x)
{
            double s=x*x;
            return s;
}

The above listing shows the decarationf of the sqr_func() method. The sqr_func method calculates square of a variable of double data type.

The fact_func() method is defined to calculates the factorial of a number. The fact_func() method accepts one argument of int data types and returms a value of data type, long.


=============================================================

Declaring the fact_func() Method


public static long fact_func (int x)
//Factorial method.
{
long t=1;
for(int i=x;i>0;i--)
  t=i*t;
return t;
}

The above listing shows the declaration of the fact_func() method.

Implementation of CreateFile, addTofile and delFile Method for Calculator Application

Implementing the CreateFile() Method

The Method CreateFile() is defined in the class, File_class. This method is used to create a new file. The CreateFile() method creates file with the name FileOutput.txt. The method also catches the error, if the file is not opened.

public void CreateFile()
{
try
  {
Of = new DataOutputStream(new  FileOutputStream("FileOut.txt"));
   Of.writeChars("Beginn");
    }
    catch(IOException e)
    {
System.err.println("File not opened properlyn"+e.toString());
    System.exit(1);
    }
}

The above listing shows the declaration of the CreateFile() method. The CreateFile() method creates a filed named FileOuput.txt. The CreateFile() method also throws an IOException exception.


Implementing the addtofile() is defined in the class, File_class. This method is used to append the file. The method uses the string method, str.length() to calculate the lenght of the string.

Declaration of the method, addTofile()

public void addTofile(String str)
{
try
{
for(int i=str.length()-1;i>=0;i--)
{
Of.writeChar(str.charAt(i));
Of.writeChars("n");
}
Of.writeChar('E');
Of.writeChars("n");
}
catch (IOException io){}
}

The above listing shows the declaration of the addTofile() method. The addTofile() mehthod appends an existing file.

Implementating the delFile() Method

The method, delFile() is defined in the class, File_class. This method is used to delete the existing file formthe permanent storage.

public void delFile()
{
CreateFile();
try
{
Of.close();
}
catch (IOException io){}
}

The above listing shows the declration of the delFile() method. The delFile() method closes the existing opened file.

Implementation of the WriteFileOnTextArea() method for Calculator Application

Implementing the WriteFileOnTextArea() Method


The method WriteFileOnTextArea() is defined in the class, File_class. The method is used to write in the FileOutput.txt.

Declaration of the method, WriteFileOnTextArea()

public void WriteFileOnTextArea()// Method to write in the file.
{
         String str;
         char st;
str="";
try
{
  DataInputStream If=new DataInputStream(new FileInputStream("FileOut.txt"));
    while(If.readLine()!=null)
    {
        if ((st=If.readChar())!='E')
          {
                  str=st+str;
          }
          if (st=='E')
          {
          textarea1.setText("");
        textarea1.append(str);
          textarea1.append("n");
        str="";
          }
      }//Ed while.
      If.close();
}//End try.
  catch(FileNotFoundException e){}   //Catches the error if the file is not found.
    catch(IOException e) {}
}// End of method WriteFileOnTextArea().


The above listing shows the declaration of the WriteFileOnTextArea() method. The WriteFileOnTextArea() method reads a file named FileOutput.txt. The method also catches the FileNoteFoundException exception if the file FileOuput.txt does not exits.


Implementation of actionPerformed method for Calculator Application


Implementing the actionPerformed() Method


The actionPerformed() method is called. When the button on the calculator interface is pressed. The actionPressed() method is a built-in listener method of ActionListener class that is implemented by Calculator_app class for trapping the button event.

Declaration of the method actionPerformed()


public void actionPerformed(ActionEvent e)//  Action listener method for  buttons on the calculator.
{
if (e.getSource()==button[29])//For begin store in the file. // " StartStoring" button
{
cond4=true;
button[30].setEnabled(true);// for //button with label StartStoring "EndStoring " Button
button[29].setEnabled(false);
f.CreateFile();// Invokes the method for creating a new file.
}
else if (e.getSource()==button[30])//End of the storing.
{
cond4=false;
button[29].setEnabled(true);
button[30].setEnabled(false);
f.endStore();// Call method  for stop storing the file.
}
else if (e.getSource() == button[41]) //Preview button.
{
button[41].setEnabled(false);
button[42].setEnabled(true);
f.WriteFileOnTextArea();
}
else if (e.getSource()==button[42]) // "Close" Button
{
button[41].setEnabled(true);
button[42].setEnabled(false);
textarea1.setText("Developedn byn Amann" );
}
else if (e.getSource()==button[32]) // "Del" Button
//Del button.
{
f.CreateFile();
}
else if (e.getSource()==button[1])  //For advanced buttons.
{
if(!cond)
{
cond=true;
pa1.setVisible(true);
button[1].setLabel("Simple");
for(int i=20;i<29;i++)
button[i].setEnabled(true);
}
else
{
button[1].setLabel("Advanced" );
pa1.setVisible(false);
cond=false;
}
}

else if(e.getSource()==button[20])
{
pressed=true;
double x=Double.valueOf(str).doubleValue();
String str4 = degree_cont.getSelection().getActionCommand();
if (!cond3)
{
if(str4.equals("Deg"))
{
while(x>=360)
x=x-360;
str=""+Math_class.sin_func(Math_class.convert_deg_rad(x));
textf.setText(""+str);
}
else if(str4.equals("Rad"))
{
while(x>=(2*Math.PI))
x=x-Math.PI;
str=""+Math_class.sin_func(x);
textf.setText(str);
}
f.CreateFile();
f.addTofile("Sin("+x+")="+str);
}
else
{
if(str4.equals("Deg"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.asin(x));
textf.setText(""+str);

}
else if(str4.equals("Rad"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_deg_rad(Math.acos(x));
textf.setText(str);

}
str3=str;
f.CreateFile();
f.addTofile("aSin("+x+")= "+str);
}
}
else if(e.getSource()==button[21])
{
pressed=true;
double x=Double.valueOf(str). doubleValue ();
String str4 = degree_cont.getSelection().getActionCommand();
if (!cond3)
{
if(str4.equals("Deg"))
{
while (x>=360)
x=x-360;
str=""+Math_class.cos_func(Math_class.convert_deg_rad(x));
textf.setText(str);
str3=str;

}
else if(str4.equals("Rad"))
{
while(x>=(2*Math.PI))
x=x-Math.PI;
str=""+Math_class.cos_func(x);
textf.setText(str);
str3=str;

}
f.CreateFile();
f.addTofile("Cos("+x+")="+str);
}
else
{
if(str4.equals("Deg"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.acos(x));
textf.setText(str);
str3=str;

}
else if(str4.equals("Rad"))
{
x=Double.valueOf(str).doubleValue();
str=""+Math_class.convert_rad_deg(Math.acos(x));
textf.setText(str);
str3=str;

}
f.CreateFile();
f.addTofile("aCos("+x+") ="+str);
}
}

else if (e.getSource()==button[0])//Power button.
{
if(!cond1)
{
cond5=true;
textf.setBackground (Color.white);
cond1=true;
button[0].setLabel("OFF");
for(int i=1;i<42;i++)
button[i].setEnabled (true);
button[30].setEnabled(false);
}
else
{
cond1=false;
cond5=true;
pressed=false;
textf.setBackground (Color.black);
button[0].setLabel("ON");
for(int i=1;i<43;i++)
button[i].setEnabled (false);
memoryTagLabel.setText(" ");
f.delFile();
str="0";str1="";str2="";
textf.setText(str);
mem=0;

}
}
else if(e.getSource()== button[16]) // "." Button
{
if(pressed)
{
str="0";
pressed=false;
}
if (cond5)
{
str=str+".";
cond5=false;
textf.setText(str);
}
}
//The implementation of the Number Buttons.
else if ((e.getSource()==button[6]) || (e.getSource()==button[7]) || (e.getSource()==button[8]) || (e.getSource()==button[9]) || (e.getSource()==button[10]) || (e.getSource()==button[11]) || (e.getSource()==button[12]) || (e.getSource()==button[13]) || (e.getSource()==button[14]) ||(e.getSource()==button[15]) )
{
if(pressed)
{
str="0";
pressed=false;
}
if(str.compareTo("0")==0||op!="")
str=e.getActionCommand();
else str=str+e.getActionCommand();
str3=str;
textf.setText(str);
showStatus(e.getActionCommand());
}
else if ((e.getSource()==button[37])|| (e.getSource()==button[35])||(e.getSource()==button[33])||(e.getSource()==button[31])||(e.getSource()==button[25]))
{ // "+" "+" "-" "*" "/" Buttons
cond5=true;
String tempStr="";
op=e.getActionCommand();
str1=str;
str3=str;
tempStr = str;
str="0";
textf.setText(tempStr);
}
else if ((e.getSource()==button[18])) // "=" Button
{ calculate();
//f.CreateFile();
}
else if (e.getSource()==button[5]) //  "BackSpace" Button
{
if(str.length() != 0)
{
str=str.substring(0,str.length()-1);
textf.setText(str);
}
else
JOptionPane.showMessageDialog(null, "Do not use backspace now.");
}
else if (e.getSource()==button[3]) // "C" Button
{
op="";
str=str3;
textf.setText(str);
}
else if (e.getSource()==button[4]) //"CE" Button
{
textf.setText("0");
cond5=true;
str="0";
str1="";
str2="";
}
else if (e.getSource()==button[27]) //  "1/X" button
{
pressed=true;
float g=0,p=0;
p=Float.valueOf(str).floatValue();
if(p==0)
{
str="0";
textf.setText("Infinite");
}
else
{
g=(1/p);
str=""+g;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("1/"+p+"="+str);
}
}
else if (e.getSource()==button[28]) // "+/-" Button
{
if (!(str.compareTo("0")==0))
{
if(str.charAt(0)=='-')
str=str.substring(1,str.length());
else
str="-"+str;
textf.setText (str);
}
}
else if(e.getSource()==button[34]) // "Me" button
{
pressed=true;
memoryTagLabel.setText("M");
mem = Double.valueOf(str).doubleValue();
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[36]) //"M+" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem+(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);
}
else if(e.getSource()==button[38]) //"M-" Button
{
pressed=true;
memoryTagLabel.setText("M");
mem=mem-(Double.valueOf(str).doubleValue());
if(mem==0)
memoryTagLabel.setText(" ");
showStatus("Memory="+mem);

}
else if(e.getSource()==button[39]) //"MR" Button
{
str=""+mem;
str3=str;
textf.setText(str3);
}

else if(e.getSource()==button[23])
{
pressed=true;
double y=0;
double x=Double.valueOf(str).doubleValue();
y=Math_class.sqr_func(x);
textf.setText(""+y);
str=""+y;
str3=str;
f.CreateFile();
f.addTofile("sqr("+x+")="+str);
}
else if(e.getSource()==button[24])
{
pressed=true;
int x=Integer.valueOf (str).intValue();
if(x>39)
textf.setText("Infinite");
else
{
str=""+Math_class.fact_func(x);
textf.setText(""+str);
str3=str;
f.CreateFile();
f.addTofile(""+x+"!="+str);
}

}
else if(e.getSource()==button[2]) //"Shift" Button
{
if(!cond3)
{
button[20].setLabel("arcSin");
button[21].setLabel("arcCos");
cond3=true;
}
else
{
cond3=false;
button[20].setLabel ("Sin");
button[21].setLabel ("Cos");
}
}
else if(e.getSource()==button[40]) //" R" Button
{
textf.setText("0");
mem=0;
memoryTagLabel.setText("");
}
else if(e.getSource()==button[26])
{
pressed=true;
float p,r;
p=Float.valueOf(str).floatValue();
r=p/100;
str=""+r;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile(""+p+"%="+str);
}
else if(e.getSource()==button[22])
{
pressed=true;
double l=0;
m=Double.valueOf(str).doubleValue();
l=Math.sqrt(m);
str=""+l;
str3=str;
textf.setText(str);
f.CreateFile();
f.addTofile("sqrt("+m+") ="+str);
}
else if (e.getSource()==button[19])
{
pressed=true;
textf.setText(str3);
str=str3;
showStatus(str3);
}
else if (e.getSource()==button[17])
{
pressed=true;
textf.setText(""+3.1415926);
str=""+3.1415926;
}

}//Method ends action performed.

The above listing shows the declaration of the actionPerformed() method. The actionPerformed() method is invoked if a button is clicked on the calculator interface. The CreateFile() method is invoked if the player clicks on the StartStoring button. The WriteFileOnTextArea() method is invoked if the player clicks on the preview button.

Implementation of keyTyped and keyPressed method for Calculator Application

Implementing the keyTyped() method

The keyTyped() method executes. When a key on the keyboard interface is pressed and is followed by key release event. The keyTyped() method calls the calculate() method, if the key typed label is +,-, and pow. The keyTyped() method also appends the key typed label in the text field of the calculator. The label of the label need to be between 0 - 9. The keyTyped method is a keyListener method that is implemented by the calculator_app for trapping keyboard events.

Declaration of the method keyTyped()



  public void keyTyped(KeyEvent e)//Denotes a key press followed by a key release.
  {
char ch;
ch=e.getKeyChar();
if ((ch=='1')||(ch=='2') || (ch=='3') ||(ch=='4')|| (ch=='5')||(ch=='6')||(ch=='7')||(ch=='8')||(ch=='9') ||(ch=='0')) //To check the source label of the pressed key.
{
  if(pressed)
  {
    str="0";
    pressed=false;
  }
  if(str=="0")//Check whether string is empty or not.
    str=""+ch;
  else
    str=str+ch;
}//End if.
else if(ch=='.'&& cond5)
{
  if(pressed)
  {
    str="0";
    pressed=false;
  }
  str=str+ch;
  cond5=false;
}
else if((ch=='+')||(ch=='-')||(ch=='*') ||(ch=='/'))
{
  op=""+ch;
  str1=str;
  str="0";
}
else if(ch=='=')
  calculate();
textf.setText(str);
}//End of method declaration KeyTyped().


The above listing shows the declaration of the keyTyped() method. The keyTyped() method is executed when a key is presed and is followed by key release event on the calculator interface.



Implementing the keyPressed() method



The keyPressed() method is called, when the key on the keyboard is pressed. Teh keyPressed() method is a built-in listener method of keyListener class that is implemented by Calculator_app class for ttrapping the keyboard event. The method checks the American Starndard Code for Information Interchange(ASCII) value of the key pressed. The Java built-in event handling method, getKeyCode() gives the ASCII value of the key, which is perssd.

public void keyPressed(KeyEvent e)//Indicates  a key is pushed down.
{
double ac=e.getKeyCode();
if((ac==13)||(ac==10))
  calculate();
else if(ac==8)
{
  if(str.compareTo("")==0)
       textf.setText("Error");
  else
{
  str = str.substring(0,str.length()-1);
    textf.setText(str);
  }
}
}//End of method KeyPressed.

The above listing shows the declaration of the keyPressed() method. The keyPressed() method invokes a built-in method named getCodeKey(). The calculate() method is invoked if the ASCII value of the key pressed is 13 or 10.

More To Be Added ...

Implementing the claculate() method in Calculator Application

The calculate() method is called, when any of the arithmetical methods is executed, such as addition, subtraction, division multiplication and pow from the calculator application interface The calculate() method is defined in the Calculator_app class.

Declaration of the methods calculate()



public void calculate()
{
double l=0;
int o=0;
String opp="";
str2=str;
str="0";
m=Double.valueOf(str1).doubleValue();
n=Double.valueOf(str2).doubleValue();
opp=op;
if (op.compareTo("+")==0)
{
l=m+n;
  op="";
}
  else if (op.compareTo("-")==0)
 {
  l=m-n;
  op="";
}
 else if (op.compareTo("*")==0)
 {
        l=m*n;
  op="";
 }
 else if (op.compareTo("/")==0)
 {
    l=m/n;
    op="";
  }
  else if (op.compareTo("")==0)
 {
    l=Double.valueOf(str2).doubleValue();
  }
  else if (op.compareTo("Pow")==0)
 {
    o=Integer.valueOf(str2).intValue();
    l=Math_class.pow_func(m,o);
    op="";
 }
 cond5=true;
 pressed=true;//pressed=true specify the
 //End user has pressed a symbol key such as, +, -, /.
 str=""+l;
 textf.setText(str);
 f.CreateFile();
 f.addTofile(str1+opp+str2+"="+l);
}//End of method declaration "calculate".

The abouve listing shows the declartion of the Calculate() method. The calculate() methods is invoked by keyPressed(), keyTyped() and actionPerfomed methods. The calculator() method is invoked the end user presses addition, subtraction, division, multiplication and power button on the calculator interface.