Question
String Lab OBJECTIVES - Be able to declare, initialize, and update strings - Be able to use basic methods of the String class Task #1
String Lab
OBJECTIVES
- Be able to declare, initialize, and update strings - Be able to use basic methods of the String class
Task #1 In Lab
Assume that a string variable niceDay points to the string literal "HAVE A NICE DAY".
A.) Without writing a Java program, write the output from the following method calls in the table below. If the statement violates Java syntax rules, write SYNTAX ERROR. If the statement follows Java syntax rules but crashes a running program when executed, write RUNTIME ERROR.
NOTE: Write one output per line.
NOTE: Assume niceDay contains HAVE A NICE DAY immediately before each method call.
Hint: If these calls were parameters of System.out.println(), what would be printed?
Method call and argument(s)
niceDay.charAt(5); |
|
niceDay.toLowerCase(); |
|
niceDay.indexOf("H"); |
|
niceDay.indexOf("A", 2); |
|
niceDay.charAt(niceDay.length()); |
|
niceDay.substring(8, 9); |
|
niceDay.substring(5); |
|
niceDay.substring(0); |
|
niceDay.substring(6, 4); |
|
niceDay.toUpperCase(niceDay.toLowerCase()); |
|
Task #1 In Lab
B.) Modify the funWithStrings method to create a new string variable called coolWord that points to the string literal "SuperCaliFragilisticExpialiDocious".
Call the appropriate String methods necessary to print out the following outputs.
Output | Strategy |
A | Get this letter from coolWord, and capitalize it |
3 | Find number of occurrences of the letter a in coolWord |
SuperDocious | Combine two subparts of coolWord together |
-1 | Find result of trying to find the letter w in coolWord |
supercalifragilisticexpialidocious3 | Make coolWord be all lowercase, and concatenate 3 at the end of it |
Task #2 For H.W. (due together with Task #1)
Complete the reverse method. A skeleton is provided in the Java driver class. Hint: Create a for loop that uses concatenation and charAt.
public static String reverse (String toReverse) {
String reversed = ; //reversed must be built from scratch
//Use toReverses characters to build reversed
//Hint: Create a for-loop that uses concatenation and charAt!
return reversed; //reversed should be built at this point
}
B.) Modify the funWithStrings method to call the completed reverse method to print the original and the reversed versions of the following three string literals:
String test1 = "cyber security";
String test2 = "internet of things";
String test3 = "cloud computing";
//print reversed versions...
//print non-reversed versions
Test the reverse method by calling it again in the funWithStrings method, using the following input as arguments, writing the calculated output in the table below and checking that the calculated output matches the expected output. If they are different, then update your method until they match.
Input | Calculated output | Correct output |
"Banana" |
| "ananaB" |
"Solstice" |
| "ecitsloS" |
"Top Secret" |
| "terceS poT" |
"caaaaaab" |
| "baaaaaac" |
"cab" |
| "bac" |
package stringlab3;
public class StringLab3 {
public static void main(String[] args) { funWithStrings(); } public static void funWithStrings() { /* ***************Task 1B**************** In this method, create a new string variable called coolWord that points to the string literal "SuperCaliFragilisticExpialiDocious". Then print out the following output using System.out.println(). A (get this letter from coolWord, and capitalize it) 3 (number of occurrences of the letter a in coolWord) SuperDocious (print 2 subparts of coolWord together) -1 (as a result of trying to find the letter w in coolWord) supercalifragilisticexpialidocious3 (make coolWord be all lowercase, and concatenate 3 at the end of it)
Use ONLY the string class methods. Do not simply use "a", "-1", etc. as the argument for System.out.println(). Use concatenation to print out the last two strings. Use indexOf to print out the numbers. */ /* ***************Task 2C**************** In this method, use the completed reverse method to print the reversed versions, followed by the non-reversed versions, of the three string literals pointed by test1, test2, and test3. */ String test1 = "cyber security"; String test2 = "internet of things"; String test3 = "cloud computing"; //write code below, should occupy at most six lines //print reversed versions... //print non-reversed versions... /* ******************Task 2D ******************** Further test the reverse method by calling it again from the playWithStrings() method, providing the following input as arguments, writing the calculated output in the word document table and checking that the calculated output matches the expected output. If they are different, then update your method until they match. Input Correct output "Banana" "ananaB" "Solstice" "ecitsloS" "Top Secret" "terceS poT" "caaaaaab" "baaaaaac" "cab" "bac" Fill out the final table, which is provided in the String Lab document. */
} /* ***************Task 2B**************** A skeleton for the reverse method is written below. Complete the skeleton. Hint: write a for-loop that uses concatenation, and charAt. */ /** * Reverses a string. * * @param toReverse The string to reverse * @return The reversed string */ public static String reverse(String toReverse) { String reversed = ""; //reversed must be built from scratch //Use toReverses characters to build reversed //Hint: Write a for-loop that uses concatenation and charAt! return reversed; } }
Need help completing this. I'm using Netbeans. Java
0 1 2 3 4 5 6 789 10 11 12 13 14Step 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