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:

StringMagic obj = new StringMagic();

System.out.println(obj.removeSpace("Hello World"));//will print "HelloWorld"

System.out.println(obj. removeSpace("Java Rocks"));//will print "JavaRocks"

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:

StringMagic obj = new StringMagic();

System.out.println(obj.replaceSpace("Welcome to SCLS"));//will print "Welcome...to...SCLS"

System.out.println(obj.replaceSpace("I am fine"));//will print "I...am...fine"

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:

String x = "xyz";
char ch = x.charAt(0);
System.out.println(ch); //will print 'x'

In Java, a character can be converted to an integer, which is known as ASCII code, using type casing method. For example:

char character = 'a';    
int ascii = (int) character; //convert char to int
System.out.println(ascii);   //will print 97

You can do some tricks to manipulate Strings and characters, like convert integer to character, or concatenate Strings with characters. For example:

public class Main
{
    public static void main(String[] args) {
        String s = "xyz";
        char ch = s.charAt(0);
        int ascii = (int)ch+1;
        ch = (char)ascii;    //convert integer to character
        System.out.println(ch);
        s = s + ch;          //concatenate strings with char
        System.out.println(s);
    }
}

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:

StringMagic obj = new StringMagic();

System.out.println(obj.cipher("red",1));//will print "sfe"

An important note: if a char shift has exceeded 26 characters, then it will rotate back to the beginning of the alphabet. For example:

StringMagic obj = new StringMagic();

System.out.println(obj.cipher("xyz",2));//will print "zab"

Submit:

Share your project link to 钉钉作业本 by Oct 8th, 22:00PM.

Last updated