The java code:
// CoinsApp.java program
// This program will guide you through the process of
determine
// Which coin in your basket is the heaviest one.
// the program guide the user step by step how to scale the
coins
// untill he finds the heaviest coin in his
basket.
// te program also counts the number of scales until the coin
is
// found.
// writen by Naftali Feldman for Programming Languages class.
Fall 97.
import java.awt.*;
import java.applet.Applet;
public class CoinsApp extends Applet
{
Label prompt,observation,directions;
TextField input,observation_input;
int num_of_coins, counter, temp, option;
public void init()
{
prompt= new Label ("First Enter the number of coins that you
have and press ENTER: ");
observation= new Label("Your observation is: ");
directions= new Label("Now read the directions
below");
input= new TextField( 5 );
observation_input= new TextField (2);
add (prompt);
add (input);
add (directions);
add (observation);
add (observation_input);
num_of_coins=1;
option=0;
counter=0;
}
public void paint ( Graphics g )
{
if (num_of_coins>1)
{
temp=num_of_coins;
num_of_coins /= 2;
if ((num_of_coins*2)!=temp)
g.drawString("Leaving one coin in the side
",20,150);
g.drawString("Please put on scale: " +num_of_coins+ " (on left
side). " +num_of_coins+ " (on right side).",20,160);
g.drawString("and enter your observation choice (1-3) in the
appropriate box above.",20,170);
g.drawString("1= the LEFT side of the scale is heavier.",20,
190);
g.drawString("2= the RIGHT side of the scale is
heavier.",20,200);
g.drawString("3= BOTH sides of the scale are
equal.",20,210);
} else if ((num_of_coins==1) && (option!=0))
option=3;
switch(option) {
case 1:
g.drawString("using only the coins from left
side...",20,140);
break;
case 2:
g.drawString("using only the coins from right
side...",20,140);
break;
case 3:
g.drawString("The individual coin is the heaviest one. YOU
HAVE FOUND IT !!! " +counter+ " steps",5,120);
break;
}
}
public boolean action (Event event, Object o )
{
if (event.target == input) {
num_of_coins=Integer.parseInt( input.getText()
);
repaint();
}
if (event.target == observation_input) {
option=Integer.parseInt( observation_input.getText()
);
if ((option<=3) && (option>=1)) {
counter++;
repaint();
}
}
return true;
}
}