Fourth chapter hands-on brain and after-school homework

Source: Internet
Author: User

The String.Equals () method in the String class in Java:

Source:

public boolean equals (object AnObject) {       //if is the same object if        (this = = AnObject)        {            return true;        }        If the passed in argument is an instance of the String class        if (AnObject instanceof String)        {            String anotherstring = (string) anobject;            int n = count;//string length if            (n = = anotherstring.count)//If equal length is compared            {                char v1[] = value;//takes each position character                Char v2[] = Anotherstring.value;                int i = offset;                int j = Anotherstring.offset;                while (n--! = 0)//compare                {                    if (v1[i++]! = v2[j++]) return to false for each position                        ;                }                return true;            }        }        return false;}

For the Equals () method of the object class, the object that calls the method is not the same object as the object to be compared (the address is judged), and the Equals () method in the String class determines whether the current string is consistent with the contents of the passed-in string.

  

The Equals () method in the object class:

public boolean equals (Object obj) {  //calls the address of the equal object and the address of the parameter object is equal to        return (this = = obj);}

Also, for other classes in Java, if you do not override the Equals () method in the object class, we can assume that the Equals () method of invoking the class is to determine whether the two classes are the same class. As long as you use the new class name (), this form of the generated class (except for the single exception), we can think of a different class (address is different).

the ancient Roman Emperor Caesar used the following methods to encrypt military information during the war:                Write a program that uses the above algorithm to encrypt or decrypt the user-entered English string.
package string encryption; import Javax.swing.JOptionPane;
public class Password {public static void main (string args[]) {string Password; Password = joptionpane.showinputdialog ("Please enter a string to encrypt or to crack:"); String output; Output = "string:" +password; Char[] C = new char[password.length ()]; Password.getchars (0, Password.length (), c,0); Encrypt for (int i=0;i<password.length (); i++) {if (c[i]== ' x ') c[i]= ' a '; else if (c[i]== ' y ') c[i]= ' B '; else if (c[i]== ' z ') c[i]= ' C '; else if (c[i] = = ") c[i]=c[i]; else c[i]+=3; } output=new String (c); Decrypt char[] D = new char[password.length ()]; Password.getchars (0, Password.length (), d,0); for (int i=0;i<password.length (); i++) {if (d[i]== ' C ') d[i]= ' z '; else if (d[i]== ' B ') d[i]= ' y '; else if(d[i]== ' a ') d[i]= ' x '; else if (d[i] = = ") d[i]=d[i]; else d[i]-=3; } string O=new string (d); Output + = "The decrypted string is:" +o;//defines the output format joptionpane.showmessagedialog (NULL, "The encrypted string is:" +output, " String "+password, joptionpane.plain_message); System.exit (0); }}






















Fourth chapter hands-on brain and after-school homework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.