Calculator Applet. Version 1.00              The related applet

  
import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 

// 1. First, we define the class ScientificCalculator:
// ---------------------------------------------------
public class ScientificCalculator extends Applet implements ActionListener, KeyListener { //closed at the bottom

// 2. We make labels and buttons:
// ------------------------------
Label display = new Label("  "); //Class Label is predefined in Java.
private Button[] keys = new Button[36]; //We define 36 buttons in an array os string keyNames
String [] keyNames = {"  ","  ","Back", "CE","Clear","Log","x^y", "sin","cos","tan", "%","1/x", "x^2", "Sqrt","*" ,"M+", "7","8","9","/",  "M-","4","5","6","-", "MR","1","2","3","+","MC","+/-",".","0","=","OFF"}; 

// 3. We declare variables.
// -------------------------
	StringBuffer StrBuff = new StringBuffer(); 
	Panel keypad = new Panel(); 
	Font bf = new Font("Arial", Font.BOLD, 13); 
	Font sbf = new Font("Arial", Font.BOLD, 15); 
	Label east = new Label("   "); 
	Label west = new Label("   "); 
	Label south = new Label("  "); 
	double result=0; 
	char oper; 
	double first=0; 
	double x = 0; 
	Panel displayPanel = new Panel(); 

// 4. We Define colors:
// --------------------
	Label infoName = new Label(" Scientific Calculator. Version 1.00"); 
	Color lightBlue = new Color(185,185,255); 
	Color lightGray = new Color(200,200,200); 
	Color lightRed = new Color(248,200,200); 
	double memory = 0; 
  
  
// 5. Initialisation: 
// ------------------
public void init() 
{ 
	this.setBackground(lightGray ); 
	this.setLayout(new BorderLayout()); 
	/* displayPanel is the object of Panel declared in 3.*/
	displayPanel.setLayout(new GridLayout(2,1,3,3));  
	displayPanel.add(display); 
	displayPanel.add(infoName); 
	add("North", displayPanel); 
	display.setFont(sbf); 
	infoName.setFont(bf); 
	infoName.setBackground(lightBlue); 
	keypad.setLayout(new GridLayout(7,5));

	// 6. We add listner methods
	// -------------------------
	for(int i=0; i0) result=-value; 
	if(value<0) result=+value; 
	display.setText(result+"" ); 
	StrBuff.setLength(0); 
	value=0; 

	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number "); } 

	return result; 
} 

// 11.10. The logarithm:
//------------------------   
public double calcLog() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

	result=Math.log(value); 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 

	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number "); } 

	return result; 
} 
  
// 11.11. The power:
//----------------------  
public double calcPower() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 
	oper='^'; 
	result=value; 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 

	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number "); } 

	return result; 
} 
  
// 11.12. The sine
//-----------------  
public double getSin() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

	result=Math.sin(value); 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 

	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number "); } 

	return result; 
} 

// 11.13. The cosine:
//----------------------  
public double getCos() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

	result=Math.cos(value); 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 

	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number "); } 

	return result; 
} 

// 11.14. The tangent:
//----------------------  
public double getTan() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

	result=Math.tan(value); 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 

	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number"); } 

	return result; 
} 

// 12. The Result function:
//---------------------------
public double calcResult(double first) 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	  double value = X.doubleValue(); 
	switch(oper) 
	{ 
	case '*': 
		 result=first*value; 
		 break; 
	case '/': 
		  result=first/value; 
		  break; 
	case  '+': 
		  result=first+value; 
		  break; 
	case '-': 
		  result=first-value; 
		  break; 
	case '^': 
		  result=Math.pow(first, value); 
		  break; 
	default: 
		  break; 
	} 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 
	first=0; 
	oper=' '; 
	result=0; 
	} 

	catch(NumberFormatException ex) 
	{ display.setText("Entrez a number "); } 

	return result; 
} 
  
// 13. The entry function:
//---------------------------
public void clearEntry() 
{ 
	try 
	{ 
	StrBuff.setLength(0); 
	String s=StrBuff.toString(); 
	display.setText(s); 
	} 
	catch(IndexOutOfBoundsException ex) 
	{ display.setText("There is no number before");} 
} 



// 14. The empty function:
//---------------------------

public void clearAll() 
{ 
	StrBuff.setLength(0); 
	String s=StrBuff.toString(); 
	display.setText(s); 
	result=0; 
	first=0; 
	x=0; 
} 


	
// 15. The backup function:
//---------------------------

public void backUp() 
{ 
	try 
	{ 
	StrBuff.setLength(StrBuff.length()-1); 
	String s=StrBuff.toString(); 
	display.setText(s); 
	} 
	catch(IndexOutOfBoundsException ex) 
	{ display.setText("There is no number before"); } 
} 

// 16. The + memory function:
//---------------------------
public void addMemory() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double x = X.doubleValue(); 
	memory+=x; 
	StrBuff.setLength(0); 
	display.setText(""); 
	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number "); } 
} 

// 17. The + memory function:
//---------------------------

public void subMemory() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double x = X.doubleValue(); 
	memory-=x; 
	StrBuff.setLength(0); 
	display.setText(""); 
	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number"); } 
} 


// 18. The empty memory function:
//--------------------------------

public void clearMemory() 
{ 
	memory=0; 
} 


// 19. The return function:
//---------------------------

public void returnMemory() 
{ 
	display.setText(memory+""); 
} 

	
// 20. The function OFF to quit:
//------------------------------

public void quit()
{
	System.exit(0); 
}
  
 } //end of applet