Homework after class

Source: Internet
Author: User

Usage of string.equals ()

The Equals method verifies that two objects are equal, and using the Equals method is equivalent to using the = = operator, which must overwrite the Equals method if the objects in the subclass are compared for equality. In the string class, the Equals method has been used to compare the contents of two strings for equality, because the method has been modified in the string class

The use of length () in the String class

It is a property that evaluates the length of a string in a string, and the length property sets or returns the number of elements in the array

Usage of charAt ()

The CharAt () function is similar to an array, where a string can be thought of as an array of type char, which is a string split to get one of the characters, returning the character at the specified position

Usage of GetChars ()

GetChar has a return value of type int, when the program calls GetChar, the program waits for the user to press the key, and the characters entered by the user are stored in the keyboard buffer until the user presses ENTER.

Use of replace ()

It is the return of a new string, which is generated by replacing all the oldchar that appear in this string with Newchar.

Usage of toUpperCase ()

It returns a string that all letters in the string are converted to uppercase

Usage of toLowerCase ()

It returns a string that all letters in the string are converted to lowercase letters

Use of trim ()

Remove whitespace from the beginning and end of the string (either with the head or tail or the first and the tails, depending on the control parameters), but leave the substring inside as the space separating the word from the word.

Usage of ToCharArray ()

Converts a character in a string object to an array of characters

The ancient Roman Emperor Caesar used the following methods to encrypt military information during the war:

Please write a program, using the above algorithm to encrypt or decrypt the user input of the English string requirements design ideas, program flowchart, source code, results.

Design ideas:

First get the encrypted content and the key, the key of Caesar's password is the number of bits that the character moves,

(1) Read the string to be encrypted

(2) Remove each digit in the string

(3) to shift each character

Source:

import Java.util.Scanner;

Public class Jiami {

Public Static void Main (string[] args)

{

int I=1;

Scanner Reader=new Scanner (System. in);

while (i==1)

{

int j=0; int n=0; int key=3;

String m= "";

System. out. println ("Enter 0 if you want to encrypt characters, enter 1 if you want to decrypt the string.");

N=reader.nextint ();

if (n==0)

{

String str=new string ();

System. out. println ("Please enter a string to encrypt");

Str=reader.next ();

for (J=0;j<str.length (); j + +)

{

Char B=str.charat (j);

if (b>= ' A ' &&b<= ' z ')

{

if (b>= ' x ' &&b<= ' z ')

{

b-=26;

B+=key;

}

Else

{

B+=key;

}

}

M+=b;

}

System. out. println ("Encrypted string is:");

System. out. println (m);

System. out. println ("If continuing please select 0; End Select 1:");

I=reader.nextint ();

}

if (n==1)

{

String str1=new string ();

System. out. println ("Please enter a string to decrypt");

Str1=reader.next ();

for (J=0;j<str1.length (); j + +)

{

Char D=str1.charat (j);

if (d>= ' A ' &&d< ' z ')

{

if (d>= ' x ' &&d< ' d ')

{

d+=26;

D-=key;

}

Else

{

D-=key;

}

}

M+=d;

}

System. out. println ("Decrypted string is:");

System. out. println (m);

}

}

System. out. println ("If continuing please select 0; End Select 1:");

I=reader.nextint ();

}

}

Results:

Homework after class

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.