Answered step by step
Verified Expert Solution
Question
1 Approved Answer
setup the code so it compiles which is followed by the directions. Create a Java file called > FunWithStrings.java. My java file would be named
setup the code so it compiles which is followed by the directions.
Create a Java file called > FunWithStrings.java. My java file would be named ConwayFunWithStrings.java and my class would be called ConwayFunWithStrings. Failure to do this will result in points taken off. Write the code as directed without deleting any code. You will upload the .java file when you have completed all activities in this document. a. Type the following to create a String variable: String school = "Hudson Valley Community College"; Print the length of this string: System.out.println (school. length()); Compile and run the program. It should display 31. We could also store that value in a variable. Declare an int variable and make it equal to the result of school. length (). Print this variable. Compile and run the program. b. To further experiment with String methods, declare a char variable and set it equal to the result of the charat () method (refer to page 74 in your book for reference). There is one argument to the charAt () method, an integer that indicates the index of the desired char. The argument sent to the method should return the capital letter V. Print the char variable. Next, use the method System.out.println () and the String method toLowerCase () to print the lower case version of the String variable school. Next, use the method System.out.println () and the String method toUpperCase ( ) to print the upper case version of the String variable school. Compile and run the program. c. The string method substring () is important because it allows us to break apart a string into pieces. For example, a string might be comprised of a department code, space, and course number: "CISS 110 " Declare a String variable called course and set it equal to the string literal "CISS 110". It might be useful to separate the department code (CISS) and course number (110) and store them in separate strings. We can do this using the substring method. (We will call it twice - once to get the department code, and a second time to get the course number.) The substring method takes two arguments: begin Index and endindex. The string returned by this method begins at the specified beginindex and extends to the character at index endindex - 1. To get the department code from this string, we need to know the beginning and ending indices to send to the substring method. The beginning index is easy: it is 0 . The endindex is not as easy because we might not know how long the department code is. What we do know is that the department code and course number are separated by a space. We can get the index of that space with another method called indexOf. The method indexof returns the first index of the given character: The character we are sending to the method indexOf is a space because we want to know at what place (or index) this space lies in the string. After typing the above line, make sure the program compiles and runs. If you get syntax errors, delete and replace the single quotes. The variable indexof Space should contain the value 4 because that is where the space is. This variable can be used for the endindex argument for the substring method. Although the value of indexOfspace ( 4 in this case) seems like it is off by one (CISS begins with index 0 and ends with index 3 ), the substring method will use endindex-1. The number or variable that is used for the endindex argument to the substring method has to be one more than the index of the last character in the desired string. Declare a String variable called deptcode and set it equal to the result of the substring method with the appropriate arguments like this String deptcode = course. substring (0, indexofspace ); We will use the substring method to extract the course number. The beginning index will have to be the index of the first 1 in 110 . We might not know that value, but we do know the value of the index of the space. It is stored in indexofspace. All we have to do is add 1 to indexofspace. Type the following: intbIndex2=indexOfSpace+1; We also need the endindex. This can be conveniently obtained using the method length () . This method will return 8 . The index of the last character in our string is 7 , so 8 is the right number to use. intendindex2=course.length(); Declare a String variable called courseNumber and set it equal to the course number using the substring method. Print out deptcode and courseNumber. Make sure deptCode contains the string CISS and courseNumber contains 110Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started