ΑΡΙΘΜΟΜΗΧΑΝΗ

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;
 
/********************************************* 
Save this file as MyCalculator.java 
to compile it use 
    javac MyCalculator.java 
to use the calcuator do this 
    java MyCalculator 
 
**********************************************/  
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.JFrame;
/*********************************************/  
  
public class MyCalculator extends JFrame  
{  
  
public boolean setClear=true;  
double number, memValue;  
char op;  
    private String str;
 
String digitButtonText[] = {"7", "8", "9", 
                            "4", "5", "6", 
                            "1", "2", "3", 
                            ".", "0", "+/-",
                            "0/0","PI","e" };  
 
String operatorButtonText[] = {"+",  "sin","cos",  "tan",
                               "-",  "asin","acos","atan",
                               "*", "quad",  "e^x",  "10^x",  
                               "/",  "sqrt", "ln",   "%",  
                               "=",  "1/X",  "qbrt",    "Φ",};  
String memoryButtonText[] = {"MC", "MR", "MS", "M+","M-" };  
String specialButtonText[] = {"Backspc", "C", "CE" };  
 
MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length];  
MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length];  
MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length];  
MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length];  
  
Label displayLabel=new Label("0",Label.RIGHT);  
Label memLabel=new Label(" ",Label.CENTER);  
 
final int FRAME_WIDTH=720,FRAME_HEIGHT=700;  
final int HEIGHT=60, WIDTH=60, H_SPACE=20,V_SPACE=20;  
final int TOPX=30, TOPY=30;   
//final int FRAME_WIDTH=325,FRAME_HEIGHT=325;  
//final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10;  
//final int TOPX=30, TOPY=50;  
///////////////////////////  
MyCalculator(String frameText)//constructor  
{  
super(frameText);  
  
int tempX=TOPX, y=TOPY;  
displayLabel.setBounds(tempX,y,640,HEIGHT);  
displayLabel.setBackground(Color.BLUE);  
displayLabel.setForeground(Color.WHITE); 
displayLabel.setFont(new java.awt.Font("Tahoma", 1, 50));
add(displayLabel);  
  
memLabel.setBounds(TOPX,  TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT);  
memLabel.setBackground(Color.red);  
memLabel.setForeground(Color.WHITE); 
memLabel.setFont(new java.awt.Font("Tahoma", 1, 30));
add(memLabel);  
  
// set Co-ordinates for Memory Buttons  
tempX=TOPX;   
y=TOPY+2*(HEIGHT+V_SPACE);  
for(int i=0; i<memoryButton.length; i++)  
{  
memoryButton[i]=new MyMemoryButton(tempX,y,WIDTH,HEIGHT,memoryButtonText[i], this);  
memoryButton[i].setForeground(Color.RED);
memoryButton[i].setFont(new java.awt.Font("Tahoma", 1, 18));
y+=HEIGHT+V_SPACE;  
}  
  
//set Co-ordinates for Special Buttons  
tempX=TOPX+1*(WIDTH+H_SPACE); y=TOPY+1*(HEIGHT+V_SPACE);  
for(int i=0;i<specialButton.length;i++)  
{  
specialButton[i]=new MySpecialButton(tempX,y,WIDTH*2,HEIGHT,specialButtonText[i], this);  
specialButton[i].setForeground(Color.RED); 
specialButton[i].setFont(new java.awt.Font("Tahoma", 1, 18));
tempX=tempX+2*WIDTH+H_SPACE;  
}  
  
//set Co-ordinates for Digit Buttons  
int digitX=TOPX+WIDTH+H_SPACE;  
int digitY=TOPY+2*(HEIGHT+V_SPACE);  
tempX=digitX;  y=digitY;  
for(int i=0;i<digitButton.length;i++)  
{  
digitButton[i]=new MyDigitButton(tempX,y,WIDTH,HEIGHT,digitButtonText[i], this);  
digitButton[i].setForeground(Color.BLUE);  
digitButton[i].setFont(new java.awt.Font("Tahoma", 1, 18));
tempX+=WIDTH+H_SPACE;  
if((i+1)%3==0){tempX=digitX; y+=HEIGHT+V_SPACE;}  
}  
  
//set Co-ordinates for Operator Buttons  
int opsX=digitX+2*(WIDTH+H_SPACE)+H_SPACE;  
int opsY=digitY;  
tempX=opsX;  y=opsY;  
for(int i=0;i<operatorButton.length;i++)  
{  
tempX+=WIDTH+H_SPACE;  
operatorButton[i]=new MyOperatorButton(tempX,y,WIDTH,HEIGHT,operatorButtonText[i], this);  
operatorButton[i].setForeground(Color.RED); 
operatorButton[i].setFont(new java.awt.Font("Tahoma", 1, 18));
if((i+1)%4==0){tempX=opsX; y+=HEIGHT+V_SPACE;}  
}  
  
addWindowListener(new WindowAdapter()  
{  
public void windowClosing(WindowEvent ev)  
{System.exit(0);}  
});  
  
setLayout(null);  
setSize(FRAME_WIDTH,FRAME_HEIGHT);  
setVisible(true);  
}  
//////////////////////////////////  
static String getFormattedText(double temp)  
{  
String resText=""+temp;  
if(resText.lastIndexOf(".0")>0)  
    resText=resText.substring(0,resText.length()-2);  
return resText;  
}  
////////////////////////////////////////  
public static void main(String []args)  
{  
new MyCalculator("ΑΡΙΘΜΟΜΗΧΑΝΗ ΠΑΝΤΑΖΗ");  
}  
}  
  
/*******************************************/  
  
class MyDigitButton extends Button implements ActionListener  
{  
MyCalculator cl;  
  
//////////////////////////////////////////  
MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
{  
super(cap);  
setBounds(x,y,width,height);  
this.cl=clc;  
this.cl.add(this);  
addActionListener(this);  
}  
////////////////////////////////////////////////  
 
static boolean isInString(String s, char ch)  
{  
for(int i=0; i<s.length();i++) if(s.charAt(i)==ch) return true;  
return false;  
 
public void actionPerformed(ActionEvent ev)  
{  
String tempText=((MyDigitButton)ev.getSource()).getLabel();  
  
if(tempText.equals("."))  
{  
 if(cl.setClear)   
    {cl.displayLabel.setText("0.");cl.setClear=false;}  
 else if(!isInString(cl.displayLabel.getText(),'.'))  
    cl.displayLabel.setText(cl.displayLabel.getText()+".");  
 return;  
}  
// 
{  
double temp=Double.parseDouble(cl.displayLabel.getText()); 
if(tempText.equals("0/0"))  
    {  
     try  
       {double tempd= 100*temp*Math.pow(100, -1)/100;  
    cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
   catch(ArithmeticException excp)  
                       {cl.displayLabel.setText("Divide by 0.");}  
  return;  
  } 
if(tempText.equals("+/-"))  
    {  
    try  
        {double tempd=-temp;  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
if(tempText.equals("PI"))  
    {  
    try  
        {double tempd=Math.PI;  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
if(tempText.equals("e"))  
    {  
    try  
        {double tempd=Math.E;  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
 
}
 
int index=0;  
try{  
        index=Integer.parseInt(tempText);  
    }catch(NumberFormatException e){return;}  
  
if (index==0 && cl.displayLabel.getText().equals("0")) return;  
  
if(cl.setClear)  
            {cl.displayLabel.setText(""+index);cl.setClear=false;}  
else  
    cl.displayLabel.setText(cl.displayLabel.getText()+index);  
}//actionPerformed  
}//class defination  
  
/********************************************/  
  
class MyOperatorButton extends Button implements ActionListener  
{  
MyCalculator cl;  
  
MyOperatorButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
{  
super(cap);  
setBounds(x,y,width,height);  
this.cl=clc;  
this.cl.add(this);  
addActionListener(this);  
}  
///////////////////////  
public void actionPerformed(ActionEvent ev)  
{  
String opText=((MyOperatorButton)ev.getSource()).getLabel();  
  
cl.setClear=true;  
double temp=Double.parseDouble(cl.displayLabel.getText());  
 if(opText.equals("sqrt"))  
    {  
    try  
        {double tempd=Math.sqrt(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }   
if(opText.equals("quad"))  
    {  
    try  
        {double tempd=Math.pow(temp, 2);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }   
 
 
 if(opText.equals("log"))  
    {  
    try  
        {double tempd=Math.log(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }   
 if(opText.equals("sin"))  
    {  
    try  
        {double tempd=Math.sin(temp); 
        if(Math.abs(tempd)>=Math.pow(10, -12))
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));  
          
           }
      catch(ArithmeticException excp)  
                   {cl.displayLabel.setText("Divide by 0.");}  
     
    } 
 if(opText.equals("sin"))  
    {  
    try  
        {double tempd=Math.sin(temp); 
        if(Math.abs(tempd)<Math.pow(10, -12))
        cl.displayLabel.setText(MyCalculator.getFormattedText(0));  
          
           }
      catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    } 
 
if(opText.equals("asin"))  
    {  
    try  
        {double tempd=Math.asin(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }   
if(opText.equals("cos"))  
    {  
    try  
        {double tempd=Math.cos(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
if(opText.equals("acos"))  
    {  
    try  
        {double tempd=Math.acos(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
if(opText.equals("tan"))  
    {  
   try  
       {double tempd=Math.tan(temp);  
       cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
          catch(ArithmeticException excp)  
                  {cl.displayLabel.setText("Divide by 0.");}  
   return;  
   }  
 
 
 
if(opText.equals("atan"))  
    {  
    try  
        {double tempd=Math.atan(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
if(opText.equals("Φ"))  
    {  
    try  
        {double tempd=Math.toRadians(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
if(opText.equals("qbrt"))  
    {  
    try  
        {double tempd=Math.cbrt(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }    
if(opText.equals("e^x"))  
    {  
    try  
        {double tempd=Math.exp(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    } 
if(opText.equals("ln"))  
    {  
    try  
        {double tempd=Math.log(temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }
if(opText.equals("10^x"))  
    {  
    try  
        {double tempd=Math.pow(10, temp);  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
            catch(ArithmeticException excp)  
                    {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
 
if(opText.equals("1/X"))  
    {  
    try  
        {double tempd= 1/temp;  
        cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
    catch(ArithmeticException excp)  
                        {cl.displayLabel.setText("Divide by 0.");}  
    return;  
    }  
   if(opText.equals("0/0"))  
    {  
     try  
       {double tempd= temp*Math.pow(100, -1);  
    cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
   catch(ArithmeticException excp)  
                       {cl.displayLabel.setText("Divide by 0.");}  
  return;  
  }  
      
if(!opText.equals("="))  
    {  
    cl.number=temp;  
    cl.op=opText.charAt(0);  
    return;  
    }  
// process = button pressed  
switch(cl.op)  
{  
case '+':  
    temp+=cl.number;
    break;  
case '-':  
    temp=cl.number-temp;break;  
case '*':  
    temp*=cl.number;break;  
 
case '%':  
    try{temp=cl.number%temp;}  
    catch(ArithmeticException excp)  
        {cl.displayLabel.setText("Divide by 0."); return;}  
    break;  
case '/':  
    try{temp=cl.number/temp;}  
        catch(ArithmeticException excp)  
                {cl.displayLabel.setText("Divide by 0."); return;}  
    break;  
 
 
}
//switch  
  
cl.displayLabel.setText(MyCalculator.getFormattedText(temp));  
//cl.number=temp;  
}//actionPerformed  
}//class  
  
/****************************************/  
  
class MyMemoryButton extends Button implements ActionListener  
{  
MyCalculator cl;  
  
/////////////////////////////////  
MyMemoryButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
{  
super(cap);  
setBounds(x,y,width,height);  
this.cl=clc;  
this.cl.add(this);  
addActionListener(this);  
}  
////////////////////////////////////////////////  
public void actionPerformed(ActionEvent ev)  
{  
 
    
  char memop=((MyMemoryButton)ev.getSource()).getLabel().charAt(1);  
  
cl.setClear=true;  
double temp=Double.parseDouble(cl.displayLabel.getText());  
  
switch(memop)  
{  
case 'C':   
    cl.memLabel.setText(" ");cl.memValue=0.0;break;  
case 'R':   
    cl.displayLabel.setText(MyCalculator.getFormattedText(cl.memValue));break;  
case 'S':  
    cl.memValue=0.0;  
case '+':   
    cl.memValue+=Double.parseDouble(cl.displayLabel.getText());  
  
    if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0")  )  
        cl.memLabel.setText(" ");  
    else   
        cl.memLabel.setText("M");     
    break; 
    
 case '-':   
    cl.memValue-=Double.parseDouble(cl.displayLabel.getText());  
  
    if(cl.displayLabel.getText().equals("0") || cl.displayLabel.getText().equals("0.0")  )  
        cl.memLabel.setText(" ");  
    else   
        cl.memLabel.setText("M");     
    break;   
}//switch  
}//actionPerformed  
}//class  
  
/*****************************************/  
  
class MySpecialButton extends Button implements ActionListener  
{  
MyCalculator cl;  
  
MySpecialButton(int x,int y, int width,int height,String cap, MyCalculator clc)  
{  
super(cap);  
setBounds(x,y,width,height);  
this.cl=clc;  
this.cl.add(this);  
addActionListener(this);  
}  
//////////////////////  
static String backSpace(String s)  
{  
String Res="";  
for(int i=0; i<s.length()-1; i++) Res+=s.charAt(i);  
return Res;  
}  
  
//////////////////////////////////////////////////////////  
public void actionPerformed(ActionEvent ev)  
{  
String opText=((MySpecialButton)ev.getSource()).getLabel();  
//check for backspace button 
double a=0;
if(opText.equals("Backspc"))  
{  
String tempText=backSpace(cl.displayLabel.getText());  
if(tempText.equals(""))   
 
  cl.displayLabel.setText("0"); 
else   
    cl.displayLabel.setText(tempText);  
return;  
}  
//check for "C" button i.e. Reset  
if(opText.equals("C"))   
{  
cl.number=0.0; cl.op=' '; cl.memValue=0.0;  
cl.memLabel.setText(" ");  
}  
  
//it must be CE button pressed  
cl.displayLabel.setText("0");cl.setClear=true;  
}//actionPerformed  
}//class  
  
/********************************************* 
Features not implemented and few bugs 
i) Menubar is not included. 
ii)Some of the computation may lead to unexpected result 
   due to the representation of Floating point numbers in computer 
   is an approximation to the given value that can be stored 
   physically in memory. 
***********************************************/