TrapTest.java

/*
============================================================================
TrapTest.java
This class uses the user defined object below called Trap.
It will calculate the area of a trapezoid.
It will accept the bases and height as floats.
*/


< applet
code=TrapTest
width=195
height=275 >
< /applet >



===========================================================================
*/
import java.applet.*;
import java.awt.*;

public class TrapTest extends Applet
{
	Label Line1 = new Label("Find the Area of a trapezoid.");
	Label enBase1 = new Label("Enter base1: ");
	TextField Base1 = new TextField(5);
	Label enBase2 = new Label("Enter base2: ");
	TextField Base2 = new TextField(5);
	Label enHt = new Label("Enter height: ");
	TextField Ht = new TextField(5);
	Button Calculate = new Button("Calculate");
	TextArea Result = new TextArea(5,25);
	Trap Trapezoid = new Trap();

	public void init()
	{
		add(Line1);
		add(enBase1);
		add(Base1);
		add(enBase2);
		add(Base2);
		add(enHt);
		add(Ht);
		add(Calculate);
		add(Result);
	}

	public boolean action(Event thisEvent, Object thisObject)
	{
		//sets values on Trapezoid class
		Trapezoid.setBase1(Float.valueOf(Base1.getText()).floatValue());
		Trapezoid.setBase2(Float.valueOf(Base2.getText()).floatValue());
		Trapezoid.setHt(Float.valueOf(Ht.getText()).floatValue());
		Result.setText(""+Trapezoid+"");
		return true;
	}
}




/*
=======================================================================
Trap.java
This is the Trap object used by TrapTest.java
=======================================================================
import java.applet.*;

public class Trap extends Applet
{
	float ht, base1, base2, area;

	//constructor method
	Trap()
	{
		ht = 0;
		base1 = 0;
		base2 = 0;
		area = 0;
	}

	//methods section
	public void setHt(float iValue)
	{
		ht = iValue;
	}

	public void setBase1(float iValue)
	{
		base1 = iValue;
	}

	public void setBase2(float iValue)
	{
		base2 = iValue;
	}


	public String toString()
	{
		area = (float).5*ht*(base1 + base2); //calculates area
		return("The area of your trapezoid\n" +
			   "with bases "+base1+" and "+base2+"\n" + 
			   "and height "+ht+" is\n"+
			   ""+area+" units squared.");
	}
}




Output for TrapTest.java