/* Project for CS 131 Michael Hecht Note: the Integer class built-in to Java does not have any "set...()" methods, making aliases useless. That's where Int comes in. */ import java.applet.*; import java.awt.*; import java.awt.event.*; //---Convert class--- public class Convert extends Applet implements TextListener { private int numUnits=3; private String[] unitNames={"Fahrenheit","Celcius","Kelvin"}; private double[] unitScale={9.0/5.0,1,1}; private double[] unitShift={32,0,-273}; private Int sourcePos=new Int(0); //index of source unit private Int targetPos=new Int(0); //index of target unit private Label conversionSourceUnitLabel=new Label(); private Label conversionTargetUnitLabel=new Label(); private Label conversionTargetUnitLabelSf=new Label(); private TextField result=new TextField(); private TextField resultSf=new TextField(); private TextField sourceField=new TextField("0"); private Panel topPanel=new Panel(); private Panel bottomPanel=new Panel(); private Panel sourcePanel=new Panel(); private Panel destPanel=new Panel(); private Panel unitPanel=new Panel(); private Panel valuePanel=new Panel(); //---applet's init() function--- public void init() { result.setEditable(false); resultSf.setEditable(false); setBackground(new Color(.85f,.85f,.85f)); setLayout(new BorderLayout()); add(topPanel,BorderLayout.NORTH); add(bottomPanel,BorderLayout.CENTER); topPanel.add(new Label("Unit Converter for Temperature")); bottomPanel.setLayout(new BorderLayout()); bottomPanel.add(unitPanel,BorderLayout.NORTH); bottomPanel.add(valuePanel,BorderLayout.SOUTH); unitPanel.setLayout(new BorderLayout()); unitPanel.add(sourcePanel,BorderLayout.WEST); unitPanel.add(destPanel,BorderLayout.EAST); valuePanel.setLayout(new GridLayout(0,3)); valuePanel.add(new Label("Original Temperature: ",Label.RIGHT)); valuePanel.add(sourceField); sourceField.addTextListener(this); valuePanel.add(conversionSourceUnitLabel); valuePanel.add(new Label("Converted Temperature (sf): ",Label.RIGHT)); valuePanel.add(resultSf); valuePanel.add(conversionTargetUnitLabelSf); valuePanel.add(new Label("Converted Temperature: ",Label.RIGHT)); valuePanel.add(result); valuePanel.add(conversionTargetUnitLabel); sourcePanel.setLayout(new BorderLayout()); sourcePanel.add(new UnitBox("Source Units",sourcePos),BorderLayout.WEST); destPanel.setLayout(new BorderLayout()); destPanel.add(new UnitBox("Target Units",targetPos),BorderLayout.EAST); doConversion(); } //end init() function //---function doConversion(): performs conversion and updates all fields--- public void doConversion() { try { double s=(Double.valueOf(sourceField.getText()).doubleValue()); double t=(s - unitShift[sourcePos.value]) / unitScale[sourcePos.value] * unitScale[targetPos.value] + unitShift[targetPos.value]; result.setText(String.valueOf(t)); int sf=countSigFigs(sourceField.getText()); int tBaseSf=countSigFigs(String.valueOf(Math.round(Math.abs(t)))); t=Math.round(t*Math.pow(10,sf-tBaseSf))/Math.pow(10,sf-tBaseSf); resultSf.setText(String.valueOf(t)); conversionSourceUnitLabel.setText("degrees " + unitNames[sourcePos.value].toLowerCase()); conversionTargetUnitLabelSf.setText("degrees " + unitNames[targetPos.value].toLowerCase() + " (sf)"); conversionTargetUnitLabel.setText("degrees " + unitNames[targetPos.value].toLowerCase()); } catch(Exception e) { result.setText("n/a"); conversionTargetUnitLabel.setText(""); resultSf.setText("n/a"); conversionTargetUnitLabelSf.setText(""); } } //end doConversion() public void textValueChanged(TextEvent t) { doConversion(); } public int countSigFigs(String s) { int i=0,n=0; while(i0)?n:1; } //---class UnitBox--- private class UnitBox extends Panel implements ItemListener { private CheckboxGroup unitGroup=new CheckboxGroup(); private Checkbox[] unitType=new Checkbox[numUnits]; private Int myIndex; //---UnitBox constructor--- public UnitBox(String label,Int myIndex) { this.myIndex=myIndex; //REFERENCE to index in outer class setLayout(new GridLayout(0,1)); add(new Label(label)); for(int i=0;i