//This is the source code for a typical application applet written
for the project
//"Software Supplement to Abstract Algebra " by Arvind Rajagopal,
Univ. of Minn. Duluth.
import java.applet.Applet;
import java.awt.*;
import java.lang.*;
import algebra.*; //algebra package has
the most important classes
import misc.*; //misc package has other miscellaneous
classes
public class chap2q1Applet extends Applet {
- textfield tf;
- Button clearTextArea;
- ScrollableCanvas can;
- GridBagLayout gridbag = new GridBagLayout();
- public void init() {
- //The colors are accepted as applet parameters so that they can be
changed
- //as we please.
- Color comp_background = getColorParameter(getParameter("cbg"));
- Color applet_background = getColorParameter(getParameter("abg"));
- Color comp_foreground = getColorParameter(getParameter("cfg"));
- String f = "fname";
- int st = Font.BOLD + Font.ITALIC;
- int it = Font.ITALIC+Font.BOLD;
- //The Font size is also accepted as an applet parameter.
- int siz = Integer.parseInt( getParameter( "font_size" ) );
- Font fon = new Font(f,st,siz);//This is the font we use for the text
in the canvas.
- Font italic = new Font("italic", it,siz);
- this.setBackground(applet_background);
- //The clear button.
- clearTextArea = new Button("clear");
- clearTextArea.setForeground(comp_foreground);
- clearTextArea.setBackground(comp_background);
- clearTextArea.setFont(fon);
- //The textfield.
- tf = new textfield(5);
- tf.setForeground(comp_foreground);
- tf.setBackground(comp_background);
- tf.setFont(fon);
- //Add Components to the Applet.
- Label L = new Label("Enter n here:");
- L.setForeground(comp_foreground);
- L.setFont(italic); can = new ScrollableCanvas(375,150);
- can.setForeground(comp_foreground);
- can.setBackground(comp_background);
- can.setFont(fon); this.setLayout(gridbag);
- this.setFont(fon);
- //The class misc.layout is used.
- layout.constrain(this, L, 0,0,1,1);
- layout.constrain(this, tf, GridBagConstraints.RELATIVE,
- GridBagConstraints.RELATIVE,1,1, GridBagConstraints.HORIZONTAL,
- GridBagConstraints.WEST, 0.0,0.0,0,0,0,0);
- layout.constrain(this, can, 0,1,2,1, GridBagConstraints.HORIZONTAL,
- GridBagConstraints.CENTER, 0.0,0.0,0,0,0,0);
- layout.constrain(this, clearTextArea, 0,2, 2,1, GridBagConstraints.NONE,
- GridBagConstraints.CENTER, 0.0,0.0,0,0,0,0 );
- validate(); }
- //*********end of init
- //This method extracts the Colors which are passed as applet parameters.
- //This method was taken from the book Java in a Nut Shell, 1st edn.
O'Reilly Assoc.
- protected Color getColorParameter(String name) {
- String value = name;
- int initvalue;
- try {initvalue = Integer.parseInt(value,16);}
- catch (NumberFormatException e) {return null;}
- return new Color(initvalue);
- }
- public boolean action(Event event, Object obj) {
- //Here we instantiate an object of type utilc2q1, which is an utility
class specific
- //for this application applet.
- utilc2q1 utils = new utilc2q1();
- switch(event.id) {
- case Event.ACTION_EVENT:
- if(event.target == tf) {
- try{
- String text = tf.getText();
- int value = Integer.parseInt(text);
- utils.ugroup(this.getGraphics(), value,can);
- can.newanswer();
- }
- catch( NumberFormatException e ){
- //This checks the user's entry and prints an error message if it is
wrong.
- sentence err = new sentence();
- err.add( new specialString( "Error: Please check if you have "+
- "entered an integer or given any " +
- "space while entering. ", this.getFont()));
- can.paint( err ); can.newanswer();
- } tf.selectAll();
- }//end of if
- else if(event.target == clearTextArea) {
- //If the clear button is pressed, the clear() method in the
- //misc.ScrollableCanvas is called.
- can.clear();
- }
- break;
- default: break;
- } //end of switch statement.
- return true; }//************end of action
}//*************end of applet
//This class has all the utility functions for this application program.
class utilc2q1 {
- public utilc2q1() {}
- public void ugroup( Graphics g,int n, ScrollableCanvas canvas) {
- int i,j;
- //This sentence is used again and again.
- sentence curr_sent = new sentence();
- if(n <= 1) {
- //Typically, this is how a sentence is sent for printing.
- //First the individual strings are added to the sentence.
- //then the paint(entity ent) of the misc.ScrollableCanvas is invoked.
- ///The current sentence is cleared for reuse
- curr_sent.add( new specialString( String.valueOf(n) + " is not
a valid number ",
- canvas.paint(curr_sent);
- curr_sent.erase();
- return;
- }//end of if
- //Here we do the computations.
- String heading = " a. The elements of U(" + String.valueOf(n)
+ ") are:" ;
- curr_sent.add( new specialString( heading, g.getFont() ) );
- canvas.paint(curr_sent);
- curr_sent.erase();
- String elements = " { 1 ";
- for(i = 2; i < n; i++) {
- if( Utilities.gcd(n,i) == 1 ) elements += String.valueOf( i ) + "
";
- } elements += "}.";
- curr_sent.add( new specialString( elements, g.getFont() ) );
- canvas.paint(curr_sent);
- curr_sent.erase();
- curr_sent.add( new specialString( " b. The inverses of each member:"
, g.getFont() ) ) ;
- canvas.paint( curr_sent );
- curr_sent.erase();
- curr_sent.add( new specialString( " inverse of 1 is: 1 ",
g.getFont() ) );
- canvas.paint( curr_sent );
- curr_sent.erase();
- for(i = 2 ; i < n ; i++) {
- if ( Utilities.gcd(n,i) == 1 ) {
- for(j = 2 ; j < n ; j++) {
- if( ( (i*j) %n) == 1 ) {
- String temp = " inverse of " + String.valueOf(i) + "
is: " +
- curr_sent.add( new specialString( temp, g.getFont() ) );
- canvas.paint(curr_sent);
- curr_sent.erase();
- } //end of if
- } //end of for( j = 2... )
- } //end of if
- } //end of for(i = 2 ...... )
- } //end of the method ugroup
}//**************end of utility************
back.