An example of encryption and decryption principles
Package lockunlock;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Applet .*;
Import javax. Swing .*;
Import java. util .*;
Public class lockunlock extends japplet {
Private Boolean isstandalone = false;
// Get a parameter value
Public String getparameter (string key, string DEF ){
Return isstandalone? System. getproperty (Key, DEF ):
(Getparameter (key )! = NULL? Getparameter (key): DEF );
}
// Construct the Applet
Public lockunlock (){
}
// Initialize the Applet
Public void Init (){
Try {
Jbinit ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
// Component Initialization
Private void jbinit () throws exception {
Contentpane = (jpanel) This. getcontentpane ();
Jlabel1.settext ("string ");
Jlabel1.setbounds (New rectangle (35, 36, 57, 21 ));
Contentpane. setlayout (null );
This. setsize (new dimension (400,300 ));
Jlabel2.settext ("String Length ");
Jlabel2.setbounds (New rectangle (29, 73, 69, 22 ));
Jtextfield1.settext ("");
Jtextfield1.setbounds (New rectangle (108, 40,166, 17 ));
Jtextfield2.settext ("");
Jtextfield2.setbounds (New rectangle (107, 72, 56, 21 ));
Jbutton1.setbounds (New rectangle (30,236,137, 27 ));
Jbutton1.settext ("Exercise 3 ");
Jbutton1.addactionlistener (New lockunlock_jbutton?actionadapter (this ));
Jbutton2.setbounds (New rectangle (218,237,131, 27 ));
Jbutton2.settext ("Exercise 4 ");
Jbutton2.addactionlistener (New lockunlock_jbutton2_actionadapter (this ));
Jtextfield3.settext ("");
Jtextfield3.setbounds (New rectangle (106,105, 58, 21 ));
Jlabel3.settext ("moshu ");
Jlabel3.setbounds (New rectangle (36,106, 86, 18 ));
Contentpane. Add (jlabel1, null );
Contentpane. Add (jbutton2, null );
Contentpane. Add (jbutton1, null );
Contentpane. Add (jlabel3, null );
Contentpane. Add (jtextfield2, null );
Contentpane. Add (jlabel2, null );
Contentpane. Add (jtextfield3, null );
Contentpane. Add (jtextfield1, null );
}< br> // get applet Information
Public String getappletinfo () {
return "applet information ";
}< br> // get parameter info
Public String [] [] getparameterinfo () {
return NULL;
}< br> // main method
Public static void main (string [] ARGs) {
lockunlock applet = new lockunlock ();
applet. isstandalone = true;
jframe = new jframe ();
// exit_on_close = 3
frame. setdefaclocloseoperation (3);
frame. settitle ("applet frame");
frame. getcontentpane (). add (applet, borderlayout. center);
applet. init ();
applet. start ();
frame. setsize (400,320);
dimension D = toolkit. getdefatooltoolkit (). getscreensize ();
frame. setlocation (D. width-frame. getsize (). width)/2, (D. height-frame. getsize (). height)/2);
frame. setvisible (true);
}
// Static initializer for setting look & feel
Static {
Try {
// Uimanager. setlookandfeel (uimanager. getsystemlookandfeelclassname ());
// Uimanager. setlookandfeel (uimanager. getcrossplatformlookandfeelclassname ());
}
Catch (exception e ){
}
}
// Declare datamember
Int index;
//-----------------------------------------------------
Jpanel contentpane;
Jlabel jlabel1 = new jlabel ();
Jlabel jlabel2 = new jlabel ();
Jtextfield jtextfield2 = new jtextfield ();
Jtextfield jtextfield1 = new jtextfield ();
Jbutton jbutton1 = new jbutton ();
Jbutton jbutton2 = new jbutton ();
Jtextfield jtextfield3 = new jtextfield ();
Jlabel jlabel3 = new jlabel ();
// ---------------------- N! ------------------------------
Public int function (int n ){
If (n = 1)
Return 1;
Else {
Return N * function (N-1 );
/* Not return function (N-1 );
But N * function (N-1 );*/
}
}
// ----------- Use recursion to find the full arrangement of a string -----------------------
Public void arrange (string prefix, string suffix, int [] array ){
String newprefix, newsuffix;
Int numofchars = suffix. Length ();
If (numofchars = 1 ){
Array [Index] = integer. parseint (prefix + suffix );
Index ++;
}
Else {
For (INT I = 1; I <= numofchars; I ++ ){
Newsuffix = suffix. substring (1, numofchars );
Newprefix = prefix + suffix. charat (0 );
Arrange (newprefix, newsuffix, array );
Suffix = newsuffix + suffix. charat (0 );
}
}
}
// ---------- Arrange from the min to the max ------------------
/* Public void rankforarrange (INT [] array ){
Int Bottom = array. Length-1;
Int temp;
For (INT I = bottom; I> 0; I --){
For (Int J = 0; j <I; j ++ ){
If (array [J]> array [J + 1]) {
Temp = array [J];
Array [J] = array [J + 1];
Array [J + 1] = temp;
}
}
}
}
*/
// ------------------- Find the aim number ----------------------
Public int findaim (INT aim, int [] array ){
Boolean isfound = false;
Int location = 0;
Int length = array. length;
For (INT I = 0; I <length; I ++ ){
If (array [I] = aim ){
Location = I;
Isfound = true;
}
}
If (isfound)
Return location;
Else
System. Out. println ("not found ");
Return location;
/* Can't return in if? */
}
// ------------------ Creat string -------------------------------
Public String creatstring (INT length ){
Stringbuffer bufstring = new stringbuffer ();
For (INT I = 1; I <= length; I ++ ){
Bufstring. append (I );
}
Return bufstring. tostring ();
}
// ----------- Output result --------------------
Public void outputresult1 (){
Index = 0; // clear to 0
String aimstring, prefix;
Aimstring = jtextfield1.gettext ();
Int length = aimstring. Length ();
String strlength = string. valueof (length );
Int aim = integer. parseint (aimstring );
/* The method. parseint is converted to the int type.
Instead of. getinteger */
Int [] eacharrange = new int [This. function (length)];
Jtextfield2.settext (strlength );
Prefix = ""; // make an empty string
If (aimstring. Length ()> 2 &&
Aimstring. Length () <9 & aimstring! = ""){
Arrange (prefix, aimstring, eacharrange );
// Rankforarrange (eacharrange );
Arrays. Sort (eacharrange );
String result = string. valueof (findaim (aim, eacharrange ));
Jtextfield3.settext (result );
}
Else {
System. Out. println ("your string is too short ");
}
}
// ---------- Out put result 2 ---------------------
Public void outputrestlt2 (){
Index = 0; // Let index come back to 0
String strlength, strmoshu,
Aimstring, prefix, suffix;
Int length, moshu, limit;
Strlength = jtextfield2.gettext ();
Strmoshu = jtextfield3.gettext ();
Length = integer. parseint (strlength );
Moshu = integer. parseint (strmoshu );
Limit = function (length );
Int [] eacharrange = new int [This. function (length)];
If (length> 2 & length <9 &&
Strlength! = "" & Strmoshu! = ""
& Moshu <limit ){
Prefix = "";
Suffix = creatstring (length );
Arrange (prefix, suffix, eacharrange );
Arrays. Sort (eacharrange );
String strresult = string. valueof (eacharrange [moshu]);
Jtextfield1.settext (strresult );
}
Else
System. Out. println ("input ouf moshu, try again ");
}
Void jbutton?action=med (actionevent e ){
This. outputresult1 ();
}
Void jbutton2_actionreceivmed (actionevent e ){
This. outputrestlt2 ();
}
//-----------------------------------------------------------
}
Class lockunlock_jbutton?actionadapter implements java. AWT. event. actionlistener {
Lockunlock adaptee;
Lockunlock_jbutton?actionadapter (lockunlock adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (actionevent e ){
Adaptee. jbutton?action=med (E );
}
}
Class lockunlock_jbutton2_actionadapter implements java. AWT. event. actionlistener {
Lockunlock adaptee;
Lockunlock_jbutton2_actionadapter (lockunlock adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (actionevent e ){
Adaptee. jbutton2_actionreceivmed (E );
}
}