📄 bodmass.java
字号:
// Applet source code copyright 1996 Richard E. Wendt III, rwendt@acm.org
//
// This applet calculates the "Body Mass Index" according to the algorithm
// given in US News & World Report, 8 January 1996, p. 60
//
import java.applet.Applet;
import java.util.Observable;
import java.util.Observer;
import java.awt.TextField;
import java.awt.Event;
import java.awt.Label;
import java.awt.Panel;
import java.awt.GridLayout;
import java.awt.CheckboxGroup;
import java.awt.Checkbox;
class UnitCheckbox extends Checkbox{
Units un;
public UnitCheckbox(String s, CheckboxGroup g, boolean b, Units u){
super(s,g,b);
un=u;
}
public boolean action(Event e, Object o){
if(((java.awt.Checkbox)(e.target)).getLabel().compareTo("Metric")==0)
un.setMetric();
else un.setUS();
return true;
}
}
class Units extends Observable{
private boolean metric=false;
CheckboxGroup cbg=new CheckboxGroup();
UnitCheckbox mcb=new UnitCheckbox("Metric",cbg,false,this);
UnitCheckbox ucb=new UnitCheckbox("US",cbg,true,this);
// Start out with US units (lbs and inches)
public void setMetric(){
metric=true;
setChanged();
notifyObservers(this);
clearChanged();
}
public void setUS(){
metric=false;
setChanged();
notifyObservers(this);
clearChanged();
}
public boolean areMetric(){
return metric;
}
public boolean areUS(){
return !metric;
}
public String USToCurrLength(String s){ // assumes s is in US
String value;
double dv;
if(metric){
dv=(new Double(s)).doubleValue();
dv*=2.54; // 2.54 cm/inch
value=(new Double(dv)).toString();
}
else {
value=new String(s);
}
return value;
}
public String CurrToUSLength(String s){
String value;
double dv;
if(metric){
dv=(new Double(s)).doubleValue();
dv/=2.54;
value=(new Double(dv)).toString();
}
else {
value=new String(s);
}
return value;
}
public String USToCurrWeight(String s){
String value;
double dv;
if(metric){
dv=(new Double(s)).doubleValue();
dv*=0.4536;
value=(new Double(dv)).toString();
}
else {
value=new String(s);
}
return value;
}
public String CurrToUSWeight(String s){
String value;
double dv;
if(metric){
dv=(new Double(s)).doubleValue();
dv/=0.4536;
value=(new Double(dv)).toString();
}
else {
value=new String(s);
}
return value;
}
public String getUnitsLength(){
if(metric) return (new String("cm"));
else return (new String("in"));
}
public String getUnitsWeight(){
if(metric) return (new String("kg"));
else return (new String("lb"));
}
}
class DataEntry extends Observable{
String data="1";
public void setData(String s){
data=new String(s);
setChanged();
notifyObservers(data);
clearChanged();
}
public String getData(){
return data;
}
public double getDataAsDouble(){
return (new Double(data)).doubleValue();
}
}
class DataField extends TextField implements Observer{
DataEntry de;
Label label;
public DataField(String s, int n, DataEntry d){
super(s,n);
de=d;
}
public boolean action(Event e, Object o){
String value;
value=o.toString();
de.setData(USValueOfCurrent(value));
this.setText(value);
return true;
}
public void update(Observable o, Object obj){
String value;
value=currValueOfUS(de.getData());
this.setText(value);
}
public String currValueOfUS(String s){
return (new String(s));
}
public String USValueOfCurrent(String s){
return (new String(s));
}
public Label getLabel(){
return label;
}
public String getLabelText(){
return "";
}
}
class WeightField extends DataField{
Units un;
public WeightField(String s, int n, DataEntry d, Units u){
super(s,n,d);
un=u;
}
public String currValueOfUS(String s){
return un.USToCurrWeight(s);
}
public String USValueOfCurrent(String s){
return un.CurrToUSWeight(s);
}
public String getLabelText(){
return new String("Weight ("+un.getUnitsWeight().trim()+"):");
}
public Label getLabel(){
return new Label(getLabelText());
}
}
class HeightField extends DataField{
Units un;
public HeightField(String s, int n, DataEntry d, Units u){
super(s,n,d);
un=u;
}
public String currValueOfUS(String s){
return un.USToCurrLength(s);
}
public String USValueOfCurrent(String s){
return un.CurrToUSLength(s);
}
public String getLabelText(){
return new String("Height ("+un.getUnitsLength().trim()+"):");
}
public Label getLabel(){
return new Label(getLabelText());
}
}
class BMIField extends TextField implements Observer{
DataEntry weight;
DataEntry height;
DataEntry bmi;
double wt=0.0,ht=0.0,bmit=0.0;
public BMIField(String s, int n, DataEntry w, DataEntry h,
DataEntry b){
super(s,n);
weight=w;
height=h;
bmi=b;
this.setEditable(false);
}
public void update(Observable o, Object obj){
String value;
wt=weight.getDataAsDouble();
ht=height.getDataAsDouble();
bmit=(0.45*wt)/((0.025*ht)*(0.025*ht));
value=(new Double(bmit).toString());
bmi.setData(value);
this.setText(value);
}
}
public class BodMass extends Applet implements Observer{
Units units=new Units();
Panel row1=new Panel();
Panel row2=new Panel();
Panel row3=new Panel();
DataEntry weight=new DataEntry();
WeightField weightField=new WeightField("",8,weight,units);
Label weightLabel=weightField.getLabel();
DataEntry height=new DataEntry();
HeightField heightField=new HeightField("",8,height,units);
Label heightLabel=heightField.getLabel();
DataEntry bmi=new DataEntry();
BMIField bmiField=new BMIField("",7,weight,height,bmi);
Label bmiLabel=new Label("Body Mass Index =");
public void init(){
setLayout(new GridLayout(0,1));
row1.add(units.mcb);
row1.add(units.ucb);
units.addObserver(this);
units.addObserver(weightField);
units.addObserver(heightField);
row2.add(weightLabel);
row2.add(weightField);
row2.add(heightLabel);
row2.add(heightField);
weight.addObserver(bmiField);
height.addObserver(bmiField);
row3.add(bmiLabel);
row3.add(bmiField);
add(row1);
add(row2);
add(row3);
show();
}
public void update(Observable o, Object obj){
weightLabel.setText(weightField.getLabelText());
heightLabel.setText(heightField.getLabelText());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -