|
/*
* Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
* Copyright (c) 1998-1999 ChemicaLogic. All Rights Reserved.
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
* without fee is hereby granted.
* Please refer to the file http://java.sun.com/copy_trademarks.html
* for further important copyright and trademark information and to
* http://java.sun.com/licensing.html for further important licensing
* information for the Java (tm) Technology.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*
* THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
* CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
* PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
* NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
* SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
* SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
* PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SUN
* SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
* HIGH RISK ACTIVITIES.
*/
// Molecular Weight Calculator - version 1.05 (experimental)
// max formula length = 1000
// max single level parentheses = 100
// 1.Control of elements orthography (identification of error condition)
// 2.Control of special characters (identification of error condition)
// 3.Control of existence of multiple parentheses (identification of error condition)
// 4.Control of element's coefficients (integers <=99)(identification of error not available)
import java.awt.*;
import java.applet.*;
import java.net.*;
// for file input/output
import java.io.*;
import java.lang.*;
import java.util.*;
public class mweight extends Applet
{
String text, temp1;
Font f1;
String msg1, msg2, msg3, msg, msg4, msg5;
TextField tfa;
TextField controla;
Label lbl1, lbl2, vuoto1;
public void init()
{
Color cverdea = new Color(0, 128, 128);
Color crosso = new Color(255, 0, 0);
setBackground(cverdea);
setForeground(Color.white);
setLayout (new FlowLayout (FlowLayout.LEFT));
tfa=new TextField("(NH4)4Ce(SO4)4*2H2O",38);
controla=new TextField("",26);
Button but1;
but1=new Button("Calculation");
Button but2;
but2=new Button("Clear Values");
lbl2 = new Label("Molecular formula : ");
add(lbl2);
add(tfa);
add(controla);
//setForeground(crosso);
vuoto1 = new Label("Copyright ChemicaLogic, 1998-99. ");
add(vuoto1);
add(but1);
add(but2);
repaint();
}
public void start()
{
msg1 = "";
msg2 = "";
f1 = new Font("TimesRoman", Font.BOLD, 12);
setFont(f1);
repaint();
}
public boolean action(Event ev, Object arg)
{
if (ev.target instanceof Button)
{
double mw[] = new double[104];
String elem[] =new String[104];
String special[]=new String[29];
String car[] = new String[1001];
String pip[] = new String[101];
int aprip[] = new int[101];
int chiudip[] = new int[101];
int coef[] = new int[101];
String name, formula;
String txt, temp1;
String crystal, numcryst;
crystal = "";
numcryst = "";
temp1 = "";
String flag1 = "";
String partea = "";
String parteb = "";
String carr = "";
String s1 = "";
String ErrorCondition = "False";
String Controlstring = "";
String teststr1 = "";
String teststr2 = "";
String digit[] = {"False", "False"};
int numelem=103;
int j, l, ll, m, coe, k, n;
int countera = 0;
int counterb = 0;
double sumcryst = 0.0;
double valnumcryst = 0.0;
double sum = 0.0;
double sum1 = 0.0;
// Elements
elem[1] = "H";
elem[2] = "He";
elem[3] = "Li";
elem[4] = "Be";
elem[5] = "B";
elem[6] = "C";
elem[7] = "N";
elem[8] = "O";
elem[9] = "F";
elem[10] = "Ne";
elem[11] = "Na";
elem[12] = "Mg";
elem[13] = "Al";
elem[14] = "Si";
elem[15] = "P";
elem[16] = "S";
elem[17] = "Cl";
elem[18] = "Ar";
elem[19] = "K";
elem[20] = "Ca";
elem[21] = "Sc";
elem[22] = "Ti";
elem[23] = "V";
elem[24] = "Cr";
elem[25] = "Mn";
elem[26] = "Fe";
elem[27] = "Co";
elem[28] = "Ni";
elem[29] = "Cu";
elem[30] = "Zn";
elem[31] = "Ga";
elem[32] = "Ge";
elem[33] = "As";
elem[34] = "Se";
elem[35] = "Br";
elem[36] = "Kr";
elem[37] = "Rb";
elem[38] = "Sr";
elem[39] = "Y";
elem[40] = "Zr";
elem[41] = "Nb";
elem[42] = "Mo";
elem[43] = "Tc";
elem[44] = "Ru";
elem[45] = "Rh";
elem[46] = "Pd";
elem[47] = "Ag";
elem[48] = "Cd";
elem[49] = "In";
elem[50] = "Sn";
elem[51] = "Sb";
elem[52] = "Te";
elem[53] = "I";
elem[54] = "Xe";
elem[55] = "Cs";
elem[56] = "Ba";
elem[57] = "La";
elem[58] = "Ce";
elem[59] = "Pr";
elem[60] = "Nd";
elem[61] = "Pm";
elem[62] = "Sm";
elem[63] = "Eu";
elem[64] = "Gd";
elem[65] = "Tb";
elem[66] = "Dy";
elem[67] = "Ho";
elem[68] = "Er";
elem[69] = "Tm";
elem[70] = "Yb";
elem[71] = "Lu";
elem[72] = "Hf";
elem[73] = "Ta";
elem[74] = "W";
elem[75] = "Re";
elem[76] = "Os";
elem[77] = "Ir";
elem[78] = "Pt";
elem[79] = "Au";
elem[80] = "Hg";
elem[81] = "Tl";
elem[82] = "Pb";
elem[83] = "Bi";
elem[84] = "Po";
elem[85] = "At";
elem[86] = "Rn";
elem[87] = "Fr";
elem[88] = "Ra";
elem[89] = "Ac";
elem[90] = "Th";
elem[91] = "Pa";
elem[92] = "U";
elem[93] = "Np";
elem[94] = "Pu";
elem[95] = "Am";
elem[96] = "Cm";
elem[97] = "Bk";
elem[98] = "Cf";
elem[99] = "Es";
elem[100] = "Fm";
elem[101] = "Md";
elem[102] = "No";
elem[103] = "Lw";
// Molecular w.
mw[1] = 1.00797;
mw[2] = 4.0026;
mw[3] = 6.939;
mw[4] = 9.0122;
mw[5] = 10.811;
mw[6] = 12.01115;
mw[7] = 14.0067;
mw[8] = 15.9994;
mw[9] = 18.9984;
mw[10] = 20.183;
mw[11] = 22.9898;
mw[12] = 24.312;
mw[13] = 26.9815;
mw[14] = 28.086;
mw[15] = 30.9738;
mw[16] = 32.064;
mw[17] = 35.453;
mw[18] = 39.948;
mw[19] = 39.102;
mw[20] = 40.08;
mw[21] = 44.956;
mw[22] = 47.90;
mw[23] = 50.942;
mw[24] = 51.996;
mw[25] = 54.9381;
mw[26] = 55.847;
mw[27] = 58.9332;
mw[28] = 58.71;
mw[29] = 63.54;
mw[30] = 65.37;
mw[31] = 69.72;
mw[32] = 72.59;
mw[33] = 74.9216;
mw[34] = 78.96;
mw[35] = 79.904;
mw[36] = 83.80;
mw[37] = 85.47;
mw[38] = 87.62;
mw[39] = 88.905;
mw[40] = 91.22;
mw[41] = 92.906;
mw[42] = 95.94;
mw[43] = 99.0;
mw[44] = 101.07;
mw[45] = 102.905;
mw[46] = 106.4;
mw[47] = 107.868;
mw[48] = 112.40;
mw[49] = 114.82;
mw[50] = 118.69;
mw[51] = 121.75;
mw[52] = 127.60;
mw[53] = 126.9044;
mw[54] = 131.30;
mw[55] = 132.905;
mw[56] = 137.34;
mw[57] = 138.91;
mw[58] = 140.12;
mw[59] = 140.907;
mw[60] = 144.24;
mw[61] = 145.0;
mw[62] = 150.35;
mw[63] = 151.96;
mw[64] = 157.25;
mw[65] = 158.924;
mw[66] = 162.50;
mw[67] = 164.930;
mw[68] = 167.26;
mw[69] = 168.934;
mw[70] = 173.04;
mw[71] = 174.97;
mw[72] = 178.49;
mw[73] = 180.948;
mw[74] = 183.85;
mw[75] = 186.2;
mw[76] = 190.2;
mw[77] = 192.2;
mw[78] = 195.09;
mw[79] = 196.967;
mw[80] = 200.59;
mw[81] = 204.37;
mw[82] = 207.19;
mw[83] = 208.980;
mw[84] = 210.0;
mw[85] = 210.0;
mw[86] = 222.0;
mw[87] = 223.0;
mw[88] = 226.0;
mw[89] = 227.0;
mw[90] = 232.038;
mw[91] = 231.0;
mw[92] = 238.03;
mw[93] = 237.0;
mw[94] = 242.0;
mw[95] = 243.0;
mw[96] = 247.0;
mw[97] = 249.0;
mw[98] = 251.0;
mw[99] = 254.0;
mw[100] = 253.0;
mw[101] = 256.0;
mw[102] = 254.0;
mw[103] = 257.0;
// special characters
special[0] = "~";
special[1] = "!";
special[2] = "@";
special[3] = "#";
special[4] = "$";
special[5] = "%";
special[6] = "^";
special[7] = "&";
special[8] = "_";
special[9] = "+";
special[10] = "|";
special[11] = "\\";
special[12] = "[";
special[13] = "]";
special[14] = "{";
special[15] = "}";
special[16] = "<";
special[17] = ">";
special[18] = "?";
special[19] = "/";
special[20] = ",";
special[21] = ".";
special[22] = ":";
special[23] = ";";
special[24] = "\'";
special[25] = "\"";
special[26] = "-";
special[27] = "=";
special[28] = "`";
if ( arg.equals("Calculation") ) {
controla.setText("");
formula = tfa.getText();
formula = formula.trim();
if ( formula.length() > 1000 ) {
ErrorCondition = "True";
}
j = 1;
while ( j <= formula.length() ) {
car[j] = formula.substring(j-1, j);
//control for space
if ( " ".equals (car[j]) ) {
ErrorCondition = "True";
}
// Se la molecola e' idratata, anche valori reali
// di molecole d'acqua (1.5, 2, 52, 123, ecc)
if ( "*".equals (car[j]) ) {
crystal = formula.substring(j, formula.length());
if ( crystal.equals ("H2O") ) {
numcryst = "1";
} else {
numcryst = crystal.substring(0, crystal.length()-3);
}
valnumcryst= Double.valueOf(numcryst).doubleValue();
sumcryst = valnumcryst * (mw[1] * 2 + mw[8]);
temp1 = String.valueOf(sumcryst).toString();
formula = formula.substring(0,formula.length()-crystal.length()-1);
}
j++;
}
j=100;
while (j <= 999 ) {
temp1 = String.valueOf(j).toString();
if ( formula.indexOf(temp1) != -1 ) {
ErrorCondition = "True";
break;
}
j++;
}
formula = formula + " ";
// analisi parentesi *************************************************
j = 1;
while (j <= formula.length() ) {
car[j] = formula.substring(j-1, j);
n = 0;
while (n <= 28) {
if ( special[n].equals (car[j]) ) {
ErrorCondition = "True";
break;
}
n++;
}
if ( "(".equals (car[j]) ) {
countera = countera + 1;
aprip[countera] = j;
Controlstring=Controlstring+"ap";
}
if ( ")".equals (car[j]) ) {
counterb = counterb + 1;
chiudip[counterb] = j;
Controlstring=Controlstring+"ch";
}
if ( countera >100 || counterb >100 ) {
ErrorCondition = "True";
}
j++;
}
l = 1;
while ( l <= countera ) {
if ( ErrorCondition.equals ("True") )
break;
n = 1;
while (n <= Controlstring.length() ) {
if ( Controlstring.substring(n-1, n+3).equals ("apch") ) {
} else {
ErrorCondition = "True";
}
n = n+4;
}
pip[l] = formula.substring(aprip[l] , aprip[l] + (chiudip[l]-aprip[l]) -1 );
digit[0]="False";
digit[1]="False";
k=0;
while (k<=9) {
temp1 = String.valueOf(k).toString();
temp1 = temp1.trim();
if (temp1.equals (formula.substring(chiudip[l],chiudip[l] + 1 ))) {
digit[0]="True";
break;
}
k++;
}
k=0;
while (k<=9) {
temp1 = String.valueOf(k).toString();
temp1 = temp1.trim();
if (temp1.equals (formula.substring(chiudip[l]+1,chiudip[l] + 2 ))) {
digit[1]="True";
break;
}
k++;
}
if (digit[0].equals ("True") && digit[1].equals ("True") ) {
temp1 = formula.substring(chiudip[l],chiudip[l] + 2 );
coef[l] = Integer.parseInt(temp1);
}
if (digit[0].equals ("True") && digit[1].equals ("False") ) {
temp1 = formula.substring(chiudip[l],chiudip[l] + 1 );
coef[l] = Integer.parseInt(temp1);
}
if (digit[0].equals ("False") && digit[1].equals ("False")) {
coef[l] = -1;
}
l++;
}
// fine analisi parentesi *************************************************
// parte senza parentesi *************************************************
j = 1;
while (j <= formula.length()) {
if ( ErrorCondition.equals ("True") )
break;
l = 1;
while (l <= countera) {
temp1 = String.valueOf(coef[l]).toString();
temp1 = temp1.trim();
if (coef[l] == -1 ) {
temp1="";
}
if ( j >= aprip[l] && j <= (chiudip[l] + temp1.length()) ) {
car[j] = "";
}
l++;
}
j++;
}
j = 1;
while (j <= formula.length() ) {
if ( ErrorCondition.equals ("True") )
break;
partea = partea + car[j];
j++;
}
pip[0] = partea;
coef[0] = 1;
l=1;
while (l <= countera) {
if ( ErrorCondition.equals ("True") )
break;
if (coef[l] == -1 ) {
coef[l] = 1;
}
l++;
}
m = 0;
while (m <= countera) {
if ( ErrorCondition.equals ("True") )
break;
formula = pip[m] + " ";
//CALCOLO DEL PESO MOLECOLARE
sum = 0;
flag1 = "";
j = 1;
while ( j <= formula.length() ) {
car[j] = formula.substring(j-1, j);
j++;
}
j = 1;
while ( j <= formula.length() ) {
flag1 = "";
//Controllo se si tratta di elemento con due lettere
l = 1;
while ( l <= numelem ) {
temp1 = car[j] + car[j + 1];
if ( temp1.equals (elem[l]) ) {
teststr1 = "S";
flag1 = "OK";
digit[0]="False";
digit[1]="False";
coe = 0;
k=0;
while (k<=9) {
temp1 = String.valueOf(k).toString();
temp1 = temp1.trim();
if (temp1.equals (car[j + 2]) ) {
digit[0]="True";
break;
}
k++;
}
k=0;
while (k<=9) {
temp1 = String.valueOf(k).toString();
temp1 = temp1.trim();
if (temp1.equals (car[j + 3])) {
digit[1]="True";
break;
}
k++;
}
if (digit[0].equals ("True") && digit[1].equals ("True") ) {
temp1 = (car[j + 2] + car[j + 3]);
coe = Integer.parseInt(temp1);
}
if (digit[0].equals ("True") && digit[1].equals ("False") ) {
temp1 = car[j + 2];
coe = Integer.parseInt(temp1);
}
if ( coe == 0 ) {
coe = 1;
}
sum = sum + coe * mw[l];
j = j + 1;
break;
} else {
teststr1 = "N";
}
l++;
}
// Allora si tratta di elemento con una lettera solo
if ( flag1 != "OK" ) {
l = 1;
while ( l <= numelem ) {
temp1 = car[j];
if ( temp1.equals (elem[l]) ) {
teststr2 = "S";
flag1 = "XX";
digit[0]="False";
digit[1]="False";
coe = 0;
k=0;
while (k<=9) {
temp1 = String.valueOf(k).toString();
temp1 = temp1.trim();
if (temp1.equals (car[j + 1]) ) {
digit[0]="True";
break;
}
k++;
}
k=0;
while (k<=9) {
temp1 = String.valueOf(k).toString();
temp1 = temp1.trim();
if (temp1.equals (car[j + 2])) {
digit[1]="True";
break;
}
k++;
}
if (digit[0].equals ("True") && digit[1].equals ("True") ) {
temp1 = (car[j + 1] + car[j + 2]);
coe = Integer.parseInt(temp1);
}
if (digit[0].equals ("True") && digit[1].equals ("False") ) {
temp1 = car[j + 1];
coe = Integer.parseInt(temp1);
}
if ( coe == 0 ) {
coe = 1;
}
sum = sum + coe * mw[l];
break;
} else {
teststr2 = "N";
}
l++;
}
}
if ( car[j].equals (" ") || car[j].equals ("0") || car[j].equals ("1") || car[j].equals ("2") || car[j].equals ("3") || car[j].equals ("4") || car[j].equals ("5") || car[j].equals ("6") || car[j].equals ("7") || car[j].equals ("8") || car[j].equals ("9") ) {
teststr1 = "N";
teststr2 = "S";
} else {
if ( (teststr1+teststr2).equals ("NN") ) {
ErrorCondition = "True";
}
}
j++;
}
sum1 = sum1 + sum * coef[m];
m++;
}
if ( ErrorCondition.equals ("False") ) {
temp1 = String.valueOf(sum1 + sumcryst).toString();
controla.setText("Molecular Weight : "+temp1+" g/mol ");
} else {
controla.setText("An error occured...");
}
//---------------------------------------------------------------------
return true;
} else if ( arg.equals("Clear Values") ) {
tfa.setText("");
controla.setText("");
return true;
}
return true;
}
return false;
}
public void paint(Graphics g) {
Color cblue = new Color(0, 0, 255);
Color cverdea = new Color(0, 128, 128);
Color cverdes = new Color(21, 101, 0);
Color cverdem = new Color(32, 127, 0);
Color crosso = new Color(255, 0, 0);
Color cgiallo = new Color(255, 255, 0);
Color cmagenda = new Color(255, 0, 255);
Color cwhite = new Color(255, 255, 255);
//g.setColor(cgiallo);
}
}
|