Java string after class job

Source: Internet
Author: User

" Experimental Task "

1. Experiment Title: string encryption

2. experimental Content: the ancient Roman Emperor Caesar used the following methods to encrypt military information during the War:

3. Design Ideas: This program is divided into parts: 1, the use of input statements to enter the user to encrypt the string, there are two ways: (1) the use of scanner statement input, and then use The. nextline () converted to String type Strings. (2) using the window input statement, joptionpane.showinputdialog ("..."), The input is a string type. 2, define a flag to determine whether the string contains non-conforming characters, if there is output when the user entered the wrong character. 3. Define two string strings, one to store the correct characters, and the other to store non-conforming Characters. Using the For loop and if statements, the input characters are divided into two parts, a-w and a-w, and the conversion method is +3. The second part is x-z and x-z, the conversion method is-23, get Abc. 4, the output of the encrypted string or output input error characters, as well as the input as there are two methods, the second method used in this Program.

4. program Flowchart:

5. Source Code: Kaisa.java

String Encryption.

Zhaoxuan Li,october 27th,2016.

Import javax.swing.JOptionPane;

public class Kaisa

{

public static void main (string[] Args)

{

String A=joptionpane.showinputdialog ("please Enter plaintext to encrypt:");

Boolean flag=true;

int S=a.length (), i;

String output= "", output1= "";

For (i=0;i<s;i++)

{

If ((a.charat (i) <= ' W ' &&a.charat (i) >= ' a ') | | (a.charat (i) <= ' W ' &&a.charat (i) >= ' a '))

{

output+= (char) (a.charat (i) +3);

}

else if ((a.charat (i) >= ' x ' &&a.charat (i) <= ' Z ') | | (a.charat (i) >= ' X ' &&a.charat (i) <= ' Z '))

{

output+= (char) (a.charat (i)-23);

}

Else

{

Output1+=a.charat (i);

output1+= "";

flag=false;

}

}

If (flag) joptionpane.showmessagedialog (null, "the plaintext you enter is:" +a+ "\ n:" +output, "encryption", joptionpane.plain_message );

else Joptionpane.showmessagedialog (null, "+output1+" you entered does not meet the requirements (not within the uppercase letter Range!) ) "," Errow ", joptionpane.plain_message);

}

}

6 , Results:

Homework after class

After class work, the string encryption, hands-on brain string.equals () method, Sorting String class length (), charAt (), getChars (), replace (), touppercase (), tolowercase (), Trim (), ToCharArray () usage notes, reading notes

Length (): Gets the length of the string, which is an object that contains the length property, and the length () method in the string class to get the string lengths.

CharAt (): Gets the character at the specified Position.

GetChars (): gets the substring from the specified position copied into the character array (it has four parameters, 1. The copied character starts in the string 2. The last character that is copied is added 1 3 in the String. the target character array 4. The copied character is placed in the character array starting subscript).

Replace (): string substitution, using the Replace () method of the string class, you can replace a character in the original string with the specified character and get a new string that is specifically defined as Follows: public string replace (char Oldchar,char newchar).

toUpperCase (): Uppercase conversion, which provides a method for implementing the uppercase conversion in the string class, the returned value is the converted string, and the method toUpperCase () is used to change all lowercase letters in the string to uppercase Letters.

toLowerCase (), Lowercase conversion, provides a method in the string class to implement the lowercase conversion of the letter, the returned value is the converted string, and the method toLowerCase () is used to change all uppercase letters in the string to lowercase letters.

Trim (): removes the leading and trailing spaces, through the trim () method of the string class, you can get a new string by removing the string's leading and trailing spaces, the method is defined as Follows: public string trim ().

ToCharArray (): Converts a string object to a character array.

Hands on One's head

    • ? Run the following sample code Stringpool.java to see its output. How do I interpret this output? What can you sum up from it?

Experimental results:

Summary: in java, a string constant with the same content ("Hello") holds only one copy to conserve memory, so s0,s1,s2 actually refers to the same object.

When the compiler compiles a S2 sentence, it strips out the "+" number and connects the two strings directly to a string ("Hello"). This optimization work is done automatically by the Java Compiler.

When you create a string object directly using the new keyword, the value is the same (all "Hello"), but it is still two separate objects.

Hands on the brain two

Look again ...

Why does the above output result? What can you sum up from it?

Summary: Assigning a value to a string variable means: two variables (s1,s2) now refer to the same string object "a"!

The contents of the string object are read-only, and the value of the S1 variable is modified with "+", in effect, a new string object is obtained with the content "ab", which is independent of the object "a" referenced by the original s1, so S1==s2 returns false;

The "ab" string in the code is a constant that refers to a string that is not related to the "ab" object referenced by s1.

The String.Equals () method can compare the contents of a two String.

Hands on the brain three

Please review the implementation code of the String.Equals () method and learn how to implement it.

public class Stringequals {

/**

* @param args the command line arguments

*/

public static void main (string[] Args) {

String S1=new string ("Hello");

String S2=new string ("Hello");

System.out.println (s1==s2);

System.out.println (s1.equals (s2));

String s3= "Hello";

String s4= "Hello";

System.out.println (s3==s4);

System.out.println (s3.equals (s4));

}

}

Experimental results:

Four hands on the brain

    • ? String the methods of the class can be called Consecutively:

String str= "abc";

String Result=str.trim (). touppercase (). concat ("defg");

    • ? Please read the source code of the above methods of the string class in the jdk, imitate its programming method, write a MyCounter class, its method also supports the above "cascade" invocation attribute, the invocation example Is:

MyCounter counter1=new MyCounter (1);

MyCounter counter2=counter1.increase, Decrease (2). Increase (3);

... .. .

Experiment Code : Mycounter.java

By imitating its programming, a mycounter class is written, and its methods support the "cascading" invocation features described Above.

Zhaoxuan Li, October 27th,2016.

public class MyCounter

{

public Static void main (string[] Args)

{

String s= "aqz";

String Result=s.trim (). touppercase (). concat ("qwe");

System. Out. println (result);

}

}

Experimental results :

Java string after class job

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.