import java.awt.*;

class NumField extends TextField
// A NumField stores (and displays) the text representation of a number
{
    boolean hasDecimalPoint = false;
    
    // -------Constructors------------------------
    
    public NumField()
    // Default constuctor -- build a NumField eight columns wide, 
    // initially empty
    {
	this(8);    // Call the other constructor
    }
    
    public NumField(int cols)
    {
	super(cols);	// Call the superclass constructor
	this.setText("");
	this.setEditable(false);    // Don't let the user in
	hasDecimalPoint = false;
	this.setBackground(Color.white);
    }
    
    // -------Accessors------------------------ 
    
    public double getValue()
    // Returns the value represented by this field's text, 
    // or zero if empty
    {
	if (this.getText().equals(""))
	    return 0.0;
	else
	{
	    Double d = new Double(this.getText());
	    return d.doubleValue();
	}
    }
    
    // -------Mutators------------------------
    
    public void clear()
    // Set the text to be the empty String
    {
	super.setText("");
	hasDecimalPoint = false;
    }
    
    public void setValue(double d)
    // Set the text to represent the double value of the argument
    {
	super.setText(String.valueOf(d));
    }
    
    public void append(char c)
    // Append the character c to the end of the text.  
    // Checks that this method will result in a legal number 
    // representation
    {
	if (isLegal(c))
	{
	    if (c == '.')
		hasDecimalPoint = true;
	    super.setText(this.getText() + c);
	}
    }
    
    public void append(String s)
    // Append the first character of s to the end of the text
    {
	char c = s.charAt(0);
	append(c);
    }
    
    public void setText(String s)
    // Override the TextComponent method so the the user can not change 
    // the text arbitrarily.
    {
    }
    
    // -------Utility------------------------
    
    private boolean isLegal(char c)
    // Return true if and only if c could legally be appended to 
    // the end of the text.  In other words, return true if 
    // c is a digit or if it would be the only decimal point 
    // in the text representation.
    {
	if (Character.isDigit(c))
	    return true;
	else if ((c == '.') && !hasDecimalPoint)
	    return true;
	else
	    return false;
    }
}