import java.applet.*; import java.awt.*; public class FractionCalculator extends Applet { TextField fractionField, densityAField, densityBField; Button calculate; Label outputLabel, outputExplLabel, fractionLabel; double densityA, densityB, fraction; int calcWhich = 0; CheckboxGroup which; Checkbox[] choice; public void init() { Font font = new Font("TimesRoman",Font.PLAIN,14); setLayout(new GridLayout(0,2)); setBackground(Color.white); which = new CheckboxGroup(); choice = new Checkbox[2]; choice[0] = new Checkbox("Volume Fraction", which, true); choice[1] = new Checkbox("Mass Fraction", which, false); choice[0].setFont(font); choice[1].setFont(font); add(choice[0]); add(choice[1]); Label densityALabel = new Label("Mass Density A (g/cm^3)"); densityALabel.setFont(font); densityAField = new TextField(20); densityAField.setFont(font); add(densityALabel); add(densityAField); Label densityBLabel = new Label("Mass Density B (g/cm^3)"); densityBLabel.setFont(font); densityBField = new TextField(20); densityBField.setFont(font); add(densityBLabel); add(densityBField); fractionLabel = new Label("Mass Fraction ([A]/[A+B])"); fractionLabel.setFont(font); fractionField = new TextField(20); fractionField.setFont(font); add(fractionLabel); add(fractionField); Label spacer = new Label(" "); spacer.setFont(font); calculate = new Button("Calculate"); calculate.setFont(font); add(spacer); add(calculate); outputExplLabel = new Label("Volume Fraction ([A]/[A+B])"); outputExplLabel.setFont(font); add(outputExplLabel); outputLabel = new Label("0.0"); outputLabel.setFont(font); add(outputLabel); validate(); } public void updateFractionLabels() { String whichFraction = which.getCurrent().getLabel(); String volFractionText = "Volume Fraction ([A]/[A+B])"; String weightFractionText = "Mass Fraction ([A]/[A+B])"; if( whichFraction.equalsIgnoreCase("Volume Fraction") ) { calcWhich = 0; fractionLabel.setText(weightFractionText); outputExplLabel.setText(volFractionText); } else { calcWhich = 1; fractionLabel.setText(volFractionText); outputExplLabel.setText(weightFractionText); } validate(); } public boolean action(Event e, Object a) { if (e.target instanceof Button) { //parse densityA field densityA=-1.; String errorText = "Bad Value for Density of A"; try { densityA=Double.valueOf(densityAField.getText()).doubleValue(); } catch (NumberFormatException exc) { outputLabel.setText(errorText); return true; } if((densityA < 0.0) || Double.isNaN(densityA)){ outputLabel.setText(errorText); return true; } //parse densityB field densityB=-1.; errorText = "Bad Value for Density of B"; try { densityB=Double.valueOf(densityBField.getText()).doubleValue(); } catch (NumberFormatException exc) { outputLabel.setText(errorText); return true; } if((densityB < 0.0) || Double.isNaN(densityB)){ outputLabel.setText(errorText); return true; } //parse fraction field fraction=0.0; try { fraction=Double.valueOf(fractionField.getText()).doubleValue(); } catch (NumberFormatException exc) { fractionField.setText(String.valueOf(0.0)); } if (fraction < 0.0) { fraction = 0.0; fractionField.setText(String.valueOf(0.0)); } if (fraction > 1.0) { fraction = 1.0; fractionField.setText(String.valueOf(1.0)); } String outputText = ""; double output; if (calcWhich == 0) { output = NCNRUtils.getVolumeFraction(densityA, densityB, fraction); } else { output = NCNRUtils.getWeightFraction(densityA, densityB, fraction); } //Arrange fraction label if(Double.isNaN(output)) { outputText = "Error in calculation."; outputLabel.setText(outputText); } else { outputText = String.valueOf(NCNRUtils.sigfigs(output,4)); outputLabel.setText(outputText); } } if (e.target instanceof Checkbox) { updateFractionLabels(); } return true; } }