Javascript  
 
  ScriptJava  
 
  Perl  
 
  PHP  
 
  ROR  
 
  ask us  
 

 

Applications
  1. Getting started
  2. Definitions
  3. First program
  4. Characters and Strings
  5. Object and methods
  6. Arrays and Circles
  7. Exceptions
  8. The main method
  9. Reading
  10. Writing file
  11. Vectors
  12. Stacks
  13. Map The Dictionary
  14. Lists
  15. Linked lists
  16. Collection
  17. Interfaces
  18. Scanner
  19. StringTokenizer
  20. Generics
  21. JDBC
  22. DataBase Queries
  23. JSP, The main step

Graphics

Applets
  1. Regards
  2. One Picture
  3. Calculator
  4. Random pictures
  5. Bouncing picture

Swings
  1. Buttons listeners
  2. TextFields
  3. Swing Example

JavaBeans
  1. The first step
  2. Example

Search a word:
   



© The scientific sentence. 2010

Scientific calculator
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; i<keyNames.length; i++) 
	        { 
	keys[i] = new Button(keyNames[i]); 
	keys[i].addActionListener(this); 
	keys[i].addKeyListener(this); 
	keypad.add(keys[i]); 
	        } 
	add("Center", keypad); 


	// 7. Add colors:
	// ---------------
	for(int k =15; k<32; k+=5) 
	{keys[k].setBackground(Color.white);
	keys[k].setForeground(Color.green);} 
	keys[2].setBackground(Color.green); 
	keys[3].setBackground(Color.green); 
	keys[4].setBackground(Color.green); 
	keys[34].setForeground(Color.blue); 
	keys[34].setBackground(new Color(150,150,150)); 
	keys[34].setFont(sbf); 
	keys[35].setFont(sbf); 
	keys[35].setForeground(Color.red); 
	keypad.setFont(bf); 
	add("West", west);                   
	add("East", east); 
	add("South", south); 
	display.setAlignment(Label.RIGHT);        
	display.setBackground(lightRed); 
	keys[0].requestFocus(); 
}//end of init() 

// 8. We name the key bottuns:
// ---------------------------
public void keyPressed(KeyEvent e) {   } 
public void keyReleased(KeyEvent e) {   } 
public void keyTyped(KeyEvent e) 
{ 
	char keyT=e.getKeyChar(); 
	String name=""; 
	switch(keyT) { 
	case '0': name="0"; break; 
	case '1': name="1"; break; 
	case '2': name="2"; break; 
	case '3': name="3"; break; 
	case '4': name="4"; break; 
	case '5': name="5"; break; 
	case '6': name="6"; break; 
	case '7': name="7"; break; 
	case '8': name="8"; break; 
	case '9': name="9"; break; 
	case '+': name="+"; break; 
	case '-': name="-"; break; 
	case '*': name="*"; break; 
	case '/': name="/"; break; 
	case '%': name="%"; break; 
	case '=': name="="; break; 
	case '\b': name="Back"; break; 
	case '\33': name="Clear"; break; 
	default: break; 
				} 
	runCalculations(name); 
	  
} 
 
 //9. We perform the calculations:
 // ------------------------------
  
public void actionPerformed(ActionEvent ev) 
{ 
	Button keyClicked= (Button)(ev.getSource()); 
	String name = keyClicked.getLabel(); 
	runCalculations(name); 
} 
  

  // 10. The related function : runcalculations
// -----------------------------------------------
  
public void runCalculations(String name) 
{   
if(name =="1"||name=="2"||name=="3"||name=="4"||name=="5"
||name=="6" ||name=="7"||name=="8"||name=="9"||name=="0"
||name==".") 
	{ 
	StrBuff.append(name); // StrBuff declared in 3.
	String s = StrBuff.toString(); 
	display.setText(s); 
		} 
	 /*The following functions are defined below */ 
	else if(name=="Back") backUp(); 
	if(name=="+")  first=addNumbers(); 
	else if(name=="-")   first=subNumbers(); 
	else if(name=="*")  first=multNumbers(); 
	else if(name=="/")   first=divideNumbers(); 
	else if(name=="=")  result=calcResult(first); 
	else if(name=="%") result=calcPercent(); 
	else if(name=="+/-") result=invert(); 
	else if(name=="1/x") result=calcRec(); 
	else if(name=="Sqrt") result=calcSqrt(); 
	else if(name=="x^2") result=calcSqr(); 
	else if(name=="x^y") first=calcPower(); 
	else if(name=="Log") result=calcLog(); 
	else if(name=="sin") result=getSin(); 
	else if(name=="cos") result =getCos(); 
	else if(name=="tan") result=getTan(); 
	else if(name=="M+") addMemory(); 
	else if(name=="M-") subMemory(); 
	else if(name=="MC") clearMemory(); 
	else if(name=="MR") returnMemory(); 
	else if(name=="CE")  clearEntry(); 
	else if(name=="Clear")  clearAll(); 
	else if(name=="OFF") quit();
} //end of function runCalculations 
  
// 11. The related operations:
// ---------------------------

// 11.1. The addition:
//---------------------
	public double addNumbers() 
	{ 
		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.2. The subtraction:
//---------------------------

public double subNumbers() 
{ 
	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.3. The multiplication
//-------------------------
public double multNumbers() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 
	oper='*'; 
	StrBuff.setLength(0); 
	result=value; 
	} 
	catch(NumberFormatException ex) 
		   { display.setText("Enter a number"); } 

	return result; 
} 


// 11.4. division:
//---------------------

public double divideNumbers() 
	{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 
	oper='/'; 
	StrBuff.setLength(0); 
	result=value; 
	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number"); } 
	
	return result; 
} 
  
// 11.5. The purcentage:
//-----------------------
public double calcPercent() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 
	result=value/100; 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 

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

// 11.6. The square root:
//------------------------

public double calcSqrt() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

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

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

// 11.7. The square :
//--------------------


public double calcSqr() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

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

	} 
	catch(NumberFormatException ex) 
	{ display.setText("Enter a number "); } 
	
	return result; 
} 
		   
		   
// 11.8. The reverse:
//--------------------	   
public double calcRec() 
{ 
	try 
	{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

	result=1/value; 
	display.setText(result+""); 
	StrBuff.setLength(0); 
	value=0; 

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

	return result; 
} 




// 11.9. The opposite:
//----------------------	   
public double invert() 
{ 
try 
{ 
	Double X = Double.valueOf(display.getText()); 
	double value = X.doubleValue(); 

	if(value>0) 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


  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2009. All rights reserved.