// ArrayOneDimension3.java // Using an array to computer sales commissions // APPLET CODE="ArrayOneDimension3" HEIGHT=400 WIDTH=100 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ArrayOneDimension3 extends JApplet implements ActionListener { Container c; JLabel lJan, lFeb, lMar, lApr, lMay, lJun, lComm; JTextField sJan, sFeb, sMar, sApr, sMay, sJun, outputComm; JButton compute; int sales[ ]; double rate1, rate2; int comm; public void init( ) { sales = new int[ 6 ]; rate1 = .1; rate2 = .2; comm = 0; // GUI components // get content pane and set layout c = getContentPane( ); c.setLayout( new FlowLayout( ) ); // labels lJan = new JLabel( "Jan" ); lFeb = new JLabel( "Feb" ); lMar = new JLabel( "Mar" ); lApr = new JLabel( "Apr" ); lMay = new JLabel( "May" ); lJun = new JLabel( "Jun" ); lComm = new JLabel( "Commission" ); // text fields sJan = new JTextField( 6 ); sFeb = new JTextField( 6 ); sMar = new JTextField( 6 ); sApr = new JTextField( 6 ); sMay = new JTextField( 6 ); sJun = new JTextField( 6 ); // button outputComm = new JTextField( 8 ); outputComm.setEditable( false ); compute = new JButton( "COMPUTE" ); // make compute button the only component that can trigger an event compute.addActionListener( this ); c.add( lJan ); c.add( sJan ); c.add( lFeb ); c.add( sFeb ); c.add( lMar ); c.add( sMar ); c.add( lApr ); c.add( sApr ); c.add( lMay ); c.add( sMay ); c.add( lJun ); c.add( sJun ); c.add( lComm ); c.add( outputComm ); c.add( compute ); } public void actionPerformed( ActionEvent e ) { // reset comm to 0 comm = 0; // convert text entered in text fields to integers and fill sales array sales[ 0 ] = Integer.parseInt( sJan.getText( ) ); sales[ 1 ] = Integer.parseInt( sFeb.getText( ) ); sales[ 2 ] = Integer.parseInt( sMar.getText( ) ); sales[ 3 ] = Integer.parseInt( sApr.getText( ) ); sales[ 4 ] = Integer.parseInt( sMay.getText( ) ); sales[ 5 ] = Integer.parseInt( sJun.getText( ) ); // compute commission // 10% of first 100 in sales and 20% of any sales over 100 for ( int i = 0; i < sales.length; i++ ) comm += ( rate1 * Math.min( sales[ i ], 100 ) + rate2 * Math.max( 0, sales[ i ] - 100 ) ); outputComm.setText( String.valueOf( comm ) ); } }