Implentaiont of init() method for Calculator Application

The init method is used to initialise variables in the Calculator application. The init() method initialises the layout and background colour of the Calculation applicatin The method also initialises 8 new panel data types and the palcement of each panel on the calculator interface.

Declaration of the method init()
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.

The above listing shows how to initialise the layout of the calculator interface. The listing also shows how to add buttons onthe panel. The init() initialises the button enabled property to false. The init() method also initialises the default property for buttons and textboxes.

Implementing Button, File and Math Class for Calculator Application

Implementing Button_class
The class Button_class is an inner class defined in the class, Calculator_app. The Button_class class extends the built-in Button class of java. The Button_class class is used to create and initialise the properties fo the buttons, such as foreground colour, background colour, size and font. The Button_class also declares a constructor for overriding the main constructor of built-in Button class. The constructor declared in the Button_class class is:

public Button_class(String name, Color xyz)

Implementing File_class
The class File_class is an inner class defined in the class Calculator_app. The File_class class consits of methods to create a file or to append information to an existing file, or delete a file. It also consits of methods to append a string in the text area. The methods declared in the File_class class are:

public void WriteFileOnTextArea()
public void CreateFile()
public void addTofile(String str)
public void delFile()
public void endStore()

Implementing Math_class
The Math_class class is a separate class defined outside the scope of Calculator_app class in the Calculator applicaiton. The Math_class declares methods for the trigonometric calculations, such as sin and cos values evaluation. It also defines the methods for converting the radian into degree and degree into radian, such as convert_deg_rad() and convert_rad_deg() function. The Math_class class declared method for mathematical calculations, such as factorial of a number, square of a number and finding the power method. The methods declared in the Math_class class are:

public static double sin_func(double x)
public static double cos_func(double x)
public static double convert_rad_deg(double x)
public static double convert_deg_rad(double x)
public static double pow(double x, int y)
public static double sqr_func(double x)
public static long fact_func(int x)
Methods used in Calculator Application
The Calculator application uses different method for different operation. The Calculator applicaiton usese various user difined methods, such as calculator(). The Calculator application also uses built-in methods which are overloaded, such as keyPressed(), keyTyped and actinPerformed() methods.

Creating a Calculator Application

The calculator application enables the end users to perform arithmetic and trigonometric calculations. This application usese java built-in classes and packages to provide various graphic outputs and mathematical results. The calculator application uses the object-oriented concepts and is developed in Java Programming language. This application also uses java file handling to store information about the steps you perform while performing arithmetic calculations. The calculator application makes used of mouse and, keyboard related
methods available in java. The calculator application makes uses several built-in methods of the java string class.

Architecture of the Calculator Application


The Calculator applicaiton uses different classes and packages for different operations. Packages are groups of related classes and interfaces. The eight different java built-in
packages are calculator application are:

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

The Calculator application extends the class JApplet and implements three different interfaces. The class JApplet is present inside the package java.applet. The three different interfaces are:
KeyListener
ActionListener

The Calculator application also uses four different user-defined classes for providing arithmatical and trigonometric calculation functionality. The six different calsses are:
Calculator_app
Button_class
Fil_class
Math_class

Packages used in Calculator Application


The java packages used by calculator application have different methods. You need first import the packages in the source code before you can used these packages. You need to used the keyword, import,
to import the package. The import keyword is used to import a particular class or all the classes of the particular package. The different packages used are:

  • java.lang package: Provides classes and interfaces that are used in java program, such as     Object, Class, System, Wrapper, Math and String.

  • java.util package: Provides classes interfaces that support collection of Application Programming Interfaces(API) such as the DAte and Calendar calss in java.

  • java.applet package: Provides functionality for the creation of aplets, such as starting of an applet, stopping of an applet and destroying of an applet.

  • java.awt. package: Provides classes and interfaces that are used for developing a standard GUI progra. The claculator application uses the awt package for creating push buttons, labesl, chaeck boxes and text boxes. The awt package also provides the cotainers panel and frame that are used bye the calculator applicaion.

  • java.io. package: Provides functionality for working with streams. A stream is a sequence of bytes that flows from a source to destination.


Classes used in Calculator Applicaiton
Claculator application uses four different classes. The main() method of Calculator applicatiion is defined in Calculator_app class. The calculator applicaiton also usese the classes, such as Button_class, File_class, Math_class.

Implementing Calculator_app
The class, Calculator_app starts the caculator application. The calculator_app class also declares variables for button, check boxex, text boxex, labesl, panels, checkbox group and strings for the buttons label. The methods declared and used by Calculator_app class are:
public void init()
public void calculate()
public void keyPressed(KeyEvent e)()
public void keyTyped(KeyEvent e)()
public void keyReleased(KeyEvent e)()
public void actionPerformed(AcitonEvent e)

The two inner classes defined by the Calculator class are:
class Button_class extends Button
class File_class

private File_class f = new File_class();//The File_class class in an inner class. The File_class class consists of methods used to work with files.
private ButtonGroup degree_cont;//To group two checkbox options.
private JTextArea textarea1;
private JLabel label1, memoryTagLabel;
private String str="0", str1="", 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" ,"2" ,"0" ,"." ,"Pie" ,"=" ,"Ans" ,"Sin" ,"Cos" ,"Sqrt" ,"Sqr" ,"X!" ,"Pow" ,"%" ,"1/X" ,"StartStoring" ,"EndStoring" ,"EndStoring" ,"/" ,"Del" ,"Me" ,"-" ,"M+" ,"+" ,"M-" ,"MR" ,"R" ,"Preview" ,"Close"};
private boolean cond = false, cond1=false, cond2=false, cond3=false, cond4=false, cond5=false, pressed=false;

The above listing shows, the declartion and initialisation of different variables used in the calculator application.

 





While Loop


while loop in Shell Script

Syntax:

           while [ condition ]
           do
                 command1
                 command2
                 ...................
                 ...................
            done

The while will execute as long as the given condition specified is true. For example : The for loop in last posted program we can be writte that using while loop as follows:

#vi naturalnumber.sh

i=0
while [ $i -le 9 ]
do
  echo $i
  i=`expr $i + 1`
done

Save it and try as
$ sh naturalnumber.sh


For Loop

Simple For Loop


for (( i = 1; i <= 9; i++ ))
do
echo $i
done



Nested For Loop


for (( i = 1; i <= 9; i++ ))
do
for (( j = i; j <= 9; j++))
do
echo -n $i
done
echo ""
done





ArrayList Example in Collection Framework Java


Let there be an employee class that contains main job and salary. This class also contains a parameterised constructor and a display method.

import java.util.*;
class Emp
{
String name, job;
int salary;
Emp(String n, String j, int s)
{
  name = n;
  job = j;
  salary = s;
}
public void display()
{
  System.out.println(name + "t"+job+"t" + salary);
}
public boolean equals(Object o)
{
  Emp e = (Emp)o;
  return this.name.equals(e.name) && this.job.equals(e.job) && this.salary == e.salary;
}
}

class TestEmp
{
public static void main(String args[])
{
  ArrayList list = new ArrayList();
  list.add(new Emp("Amit","Manager",25000));
  list.add(new Emp("Sumit","Trainer",35000));
  list.add(new Emp("Vinit","Accountant",20000));
  list.add(new Emp("Rohit","Trainee",10000));
  System.out.println("The number of records in list : "+list.size());
  Iterator itr = list.iterator();
  while(itr.hasNext())
   {
    Emp e = (Emp)itr.next();
    e.display();
  }
  Emp e1 = new Emp("Amit", "Manager",25000);
  System.out.println("Removing following record from the list");
   e1.display();
  list.remove(e1);
  System.out.println("Total number of record after removal in list : "+list.size());
  Emp e2 = new Emp("Rohit", "Trainee", 20000);
  System.out.println("Searching following record in list.");
  e2.display();
  System.out.println("Result of search is : "+list.contains(e2));
}
}






Whenever an element is searched in a list, an exastive search is intiated by the contains method using equals() method taht element to be searched is compared for equality with each list element these comparision are made until element is found or all the elements are compared. Success or failure of a search operation in a list in dynamic depends on the implements of equals method.


Note : Defualt implementation of equals method in object class compares the value of reference not the content of an object.


In order to successfully search object of user defined class in a list, equals method must be overridden in the lcass to facilitate content wise comparision.