CS120 Lab 2 (Chongqing) - Learning String object

Step 1: for today’s lab you will create your own Java class called UsingString, in this UsingString Class, you will learn how to construct several String objects and be able to use toUpperCase(), toLowerCase(), length(), replace(), trim(), and substring() methods which were predefined for String class in Java library.

Step 2: launching the Eclipse program from your desktop, click on “File” button on the toolbar, select “New Java Project”, and give Lab2 as the project name.

Step 3: Create a new Java class called UsingString: right click on Lab2 folder on the left column, select “New” and then “class”. Name the class as “UsingString”. Check the box before “public static void main (String[]args)”. Then click on “Finish.

Step 4: please complete the seven tasks are listed below:

Task 1: add comments for UsingString class like the below:

//Project name: Lab2

//Programmer: Your First and Last Name

//Date:

public class UsingString { //Start class

public static void main(String[] args) { //Start main method

} //close main method

}//Close class


Task 2: constructs a String object (str1) with the value "BALL STATE UNIVERSITY" and then creates a new String object (str2) with the same message as the original string, but with every character converted from str2 to lowercase. Then, print THIS new string like (ball state university). We will show you how to write the 2rd task. However, you need to complete the rest of tasks by yourself. Please compile and run each task and make sure this or that task works, then move on to the next task.
public static void main(String[] args){ //Start main method

//Task 3: write you task 3 code here by using the below Task 3 instruction

}//ending a main method

}//Close class


//Task 3: Convert a string to all uppercase:

construct a String object (str3) with the value "ball state university" and then creates a new String object (str4) with the same message as the original string, but with every character converted to uppercase by using toUpperCase()method. Then, print THIS new string (str4) like (BALL STATE UNIVERSITY).
//Task 4: Find out a string’s length:
construct a String object (str5) with the value "I love Ball State." Then print out the length of this string by calling the length() method in the String class. (hint: System.out.println(str5.length()); )
//Task 5: Letter replacement:
construct a String object (str6) with the value “Mississippi”. Then replace “issipp” with “our” by calling the replace() method in the String class. The “Mississippi” will be replaced by new word “Missouri”. Print out the new string called str7 like Missouri.

//Task 6: Trim your string:
construct a String object (str8) with the value “ Uni ”. Then declare another String object (str9) with the value “versity ”. Then remove the white spaces in the strings and concatenate the two strings by calling the trim() method from the String class (you can create a string called str10 to hold str8 and str9’s trim result. Please print the str10 result like: University
//Task 7: Using substring method:
construct a String object (str11) with the value “Muncie, Indiana.”. Then print out only “Indiana” (Do not print “.”) from this string by using the subString() method in the String class. Please print the result like: Indiana

Step 5: please show your Lab2 result to your lab instructor to get your Lab2 credit.

Thanks!