JAVA APPLET SPECIFICATION

Document Sample
JAVA APPLET SPECIFICATION Powered By Docstoc
					JAVA APPLET SPECIFICATION


Module 1: KWIC

/*
 * KWIC.java
 * Created on February 3, 2005, 7:42 PM
 */

/**
 * @author Ameya Velankar & Shakesh Jain
 */

//package KWICPackage;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/* This is the main class of the application which also has the main
function.
 * The instance of this class acts as the Controlling agent for rest of
the application.
 * Application uses three other classes :
 * 1. LineStorage
 * 2. CircularShift
 * 3. SortLine
 */

public class KWIC extends Applet implements ActionListener{
    //Declaration of the Components of GUI
    TextArea inputText;
    TextArea outputText;
    Button submitText;
    Label UpperString, LowerString;
    //Classes used for string operations
    LineStorage line;
    CircularShift shiftLine;
    SortLine sortedLine;
    ArrayList outputStrings;

    /** Creates a new instance of Main */
    public void init() {
        inputText = new TextArea(); //Textbox where user enters the
input
        UpperString = new Label("Enter the Input String here(line
separator is $)");//Input Box Label
        LowerString = new Label("Cicularly Shifted and Sorted output
lines");//Output Box Label
        outputText = new TextArea(); //This is where output will go
        submitText = new Button("Submit"); //Invoke the cicular shifter
and sorter
           setSize(600,600);

           //setting the size and position of the components of GUI
           inputText.setEditable(true);
           outputText.setEditable(false);
           setLayout(null); //No formal layout used
           UpperString.setBounds(5,1,500,15);
           inputText.setBounds(5,25,500,220 );
           submitText.setBounds(240,260,50,20);
           LowerString.setBounds(5,280,500,15);
           outputText.setBounds(5,300,500,220);


           //Adding components to the Panel
           add(UpperString);
           add(LowerString);
           add(inputText);
           add(outputText);
           add(submitText);

           //Adding an Event Listener for Submit button to process the
request.
           submitText.addActionListener( this);
           validate();
    }

    public void actionPerformed(ActionEvent e) {
        int index;
        String tempStr;

        //creating an instance of LineStorage class to store the
initial input
        line = new LineStorage();
        //Clearing initially the output area each time Submit is
pressed
        outputText.replaceRange("", 0, outputText.getText().length());
        //Making sure that we do processing only if some input is
entered
        if( inputText.getText().length() != 0 ) {
            //storing input string in the LineStorage object
            line.StoreLines(inputText.getText().concat("$"));
            //creating permutation of the input lines
            shiftLine = new CircularShift(line.returnLines());
            //sorting the permutated lines
            sortedLine = new SortLine(shiftLine.returnLines());
            //outputting on the output window
            outputStrings = new ArrayList(sortedLine.retSortLines());
            for( index = 0; index< outputStrings.size(); index++)
                outputText.append((String)outputStrings.get(index) +
"\n");
        }
    }
}
Module 2: LineStorage

/*
 * LineStorage.java
 * Created on February 12, 2005, 12:47 AM
 */

/**
 *@author Ameya Velankar & Shakesh Jain
 */

//package KWICPackage;
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.lang.*;

/*
 *This is the class which is responsible for storing the lines
initially which are inout by the user
 *Lines are stored in an ArrayList.
 *These are passed on to the CircularShift object to make circular
shifted permutations
 */

public class LineStorage {

    /** Creates a new instance of LineStorage */
    ArrayList wordStrings;

    public LineStorage() {
         wordStrings = new ArrayList();
    }
    /*
      *This function is called by main module (KWIC) to store input
      *in the form of lines (in the original input lines are separated
by '$' character).
      *Here we store words of individual lines as consecutive members of
an ArrayList of
      *String objects with each line separated from each other by "$"
string in the ArrayList
      */
    protected void StoreLines(String input) {
         int i, lastIndex;
         char lastchar;
         String s;

        /*As per the functional requirements number of consecutive
        * white spaces are considered as single white space.
        */
        s = new String(input);
        lastIndex = 0;
        lastchar = s.charAt(0);
         //This for loop is responsible for creatng the word strings and
storing them in an ArrayList
         for(i=0; i < s.length(); i++){
             if( s.length() == 0 ) {
                  System.out.println("Empty String");
             }
             else if(s.charAt(i) == '$' || s.charAt(i) == ' ' ||
s.charAt(i) == '\n' || s.charAt(i) == '\r' || s.charAt(i) == '\t' ||
s.length()-1 == i ) {
                if(lastchar != '$' && lastchar != ' ' && lastchar !=
'\n' && lastchar != '\r' && lastchar != '\t' ){
                  if(s.length()-1 == i && s.charAt(i) != '$')
                       wordStrings.add(s.substring(lastIndex, i+1));
                  else
                       wordStrings.add(s.substring(lastIndex, i));
                  lastIndex = i+1;
                }
                else {
                     lastIndex++;
                }
             }
             if(s.charAt(i) == '$' || (s.charAt(i) != '$' && s.length()-
1 == i))
                  wordStrings.add("$");
             lastchar = s.charAt(i);
         }
    }

    //this function returns Strings which is called by CircularShift
object
    protected ArrayList returnLines() {
        return wordStrings;
    }
}




Module 3: CircularShift


/*
 * CircularShift.java
 * Created on February 12, 2005, 4:15 AM
 */

/**
 *@author Ameya Velankar & Shakesh Jain
 */

//package KWICPackage;
import java.applet.Applet;
import java.awt.*;
import java.util.*;

/*
 *Class is responsible for producing the Circular shifted permutations
of the strings stored in LineStorage class
 *The output is passed to SortLine object to sort the lines
 */

public class CircularShift {

    /** Creates a new instance of CircularShift */
    ArrayList circShiftStr;

    /*This is the class constructor which calls ShiftWordsCirc
iteratively to produce
     *circularly shifted permutations of the lines
     */

    public CircularShift(ArrayList StoreLine) {
       int i;
       int sizeLst;
       ArrayList tempStrLst;

       sizeLst = StoreLine.size();
       tempStrLst = new ArrayList();
       circShiftStr = new ArrayList();

       for(i=0; i<sizeLst; i++) {
           if( StoreLine.get(i) != "$") {
                tempStrLst.add(StoreLine.get(i));
           }
           else {
             ShiftWordsCirc(circShiftStr,tempStrLst);
             tempStrLst = new ArrayList();
           }
       }
    }
    /*this function called by the constructor is used to create
permutations of
      *a single input line (passed as an ArrayList tempStrLst) and
appending the permutations
      *to the ArrayList passed from the constructor(circShiftStr)
      */
    public void ShiftWordsCirc(ArrayList circShiftStr, ArrayList
tempStrLst){
         int index, i, j;
         String tempStr;

        i=0;
        for( index = 0; index < tempStrLst.size(); index++) {
             tempStr = new String("");

            for( j = index; j < tempStrLst.size(); j++) {
                tempStr += tempStrLst.get(j);
                tempStr += " ";
            }

            for( j = 0; j < index; j++) {
                tempStr += tempStrLst.get(j);
                tempStr += " ";
            }
            circShiftStr.add(tempStr);
        }
    }

    // This function returns the circularly shifted lines stored in the
CicularShift object
    public ArrayList returnLines() {
        return circShiftStr;
    }
}




Module 4: SortLine


/*
 * SortLine.java
 * Created on February 13, 2005, 1:40 AM
 */

/**
 *@author Ameya Velankar & Shakesh Jain
 */

//package KWICPackage;
import java.awt.*;
import java.util.*;

/*
 *This class object is responsible for sorting the cicular permutaions
of the lines.
 */
public class SortLine {

    /** Creates a new instance of SortLine */
    ArrayList SortList;

    public SortLine(ArrayList CircShiftLines ) {
        //Storing the ArrayList in a collection and calling the
Collections class sort function
        SortList = new ArrayList(CircShiftLines);
        Collections.sort(SortList);
    }

    //Returns sorted permutations of the input string to the KWIC class
instance to display on output area
    public ArrayList retSortLines() {
        return SortList;
    }
}