Lab3: String Magic
In this assignment, you will create a class called "StringMagic":
Step1
Create a class called StringMagic:
Step2
Create a method called String removeSpace(String str)
, which is to remove the space of str and return a new String. Assume str contains only one space. For example, if you call the method in main(), the output will look like this:
Step3
Create a method called String replaceSpace(String str)
, which is to replace the space of str with three dots. Assume str contains only two spaces. For example:
Step4
You can think of String as a sequence of characters. The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c'. For example, "Hello" is a String, which consists of 5 characters: 'H', 'e', 'l', 'l', and 'o'.
We can use str.charAt(int index) to get the character at the specified index in a string. For example:
In Java, a character can be converted to an integer, which is known as ASCII code
, using type casing method. For example:
You can do some tricks to manipulate Strings and characters, like convert integer to character, or concatenate Strings with characters. For example:
Please copy and paste the program in Step4, run the program, and observe the results. It is an important pre-task to finish Step5. You don't have to submit anything in this step. You can remove the code after running it.
Step5
In cryptography, a Caesar cipher is one of the simplest and most widely known encryption techniques. For example, with a shift of 3, A would be replaced by D, E would become H, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
You will write a method String cipher(String str, int shift)
, which is to encrypt str to a new String. Assume str only contains 3 English letters, both uppercase and lowercase is allowed. For example:
An important note: if a char shift has exceeded 26 characters, then it will rotate back to the beginning of the alphabet. For example:
Submit:
Share your project link to 钉钉作业本 by Oct 8th, 22:00PM.
Last updated