// The "Powers" class. // Two buttons allow users to select either to get a cube or a square. import java.applet.*; import java.awt.*; public class Powers extends Applet { Label enter; //on the input text field TextField input; // place to put input int number; // variable to receive the input Label result; // on the output text field TextField output; // plce to show answers Button square; // button to press to get the square of a number Button cube; // button to press to get the cube of a number public void init () { //prepare the display by initializing the display items.\: enter = new Label("Enter an integer"); input = new TextField(20); result = new Label("Result"); output = new TextField(20); square = new Button("Square"); cube = new Button("Cube"); //put the items on the applet: add(enter); add(input); add(result); add(output); add(square); add(cube); } // init method public boolean action(Event e, Object o) { number = Integer.parseInt(input.getText()); //convert the input string to an integer if (e.target instanceof Button) //check to see if a button was pushed //see Chapter 7 for getting doubles. { if(e.target == cube)// check to see if the button pushed is the cube button output.setText("Cube is " + Integer.toString(cube (number)));//call the cube method else if (e.target == square)//check to see if the button pushed is the square button output.setText("Square is " + Integer.toString(square(number)));//call the square method return true; } return true; }//action method public int square(int number)//method to find the square of an integer { return number*number; } public int cube(int number)//method to find the cube of a number { return number*number*number; } public void paint (Graphics g) { // Place the body of the drawing method here } // paint method } // Powers class