Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

String Lab OBJECTIVES - Be able to declare, initialize, and update strings - Be

ID: 3602073 • Letter: S

Question

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 incoolWord

SuperDocious

Combine two subparts of coolWord together

-1  

Find result of trying to find the letter w incoolWord

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 toReverse’s 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 toReverse’s 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

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());

0 1 2 3 4 5 6 789 10 11 12 13 14

Explanation / Answer

Task 1 (A)

Method call and argument(s)

niceDay.charAt(5);

A

niceDay.toLowerCase();

niceDay.indexOf("H");

niceDay.indexOf("A", 2);

niceDay.charAt(niceDay.length());

niceDay.substring(8, 9);

niceDay.substring(5);

niceDay.substring(0);

HAVE A NICE DAY

Below code contains answers to the rest of the assignment (with necessary comments)

package stringlab3;

public class StringLab3 {

public static void main(String[] args) {

funWithStrings();

}

public static void funWithStrings() {

// *************************** TASK 1(B) *******************************

String coolWords = "SuperCaliFragilisticExpialiDocious";

// Get "a" from coolWords and capitalize it

int indexA = coolWords.indexOf("a");

System.out.println(coolWords.substring(indexA, indexA + 1).toUpperCase());

// Find number of occurrences of the letter 'a' in coolWord

int countA = 0;

for (int i = 0; i < coolWords.length(); i++) {

// if character at current index is equal to 'a' then add 1 to

// countA

if (coolWords.charAt(i) == 'a') {

countA++;

}

}

System.out.println("Count of 'a': " + countA);

// Combine two subparts of coolWord together

String subPart1 = coolWords.substring(0, coolWords.indexOf("C"));

String subPart2 = coolWords.substring(coolWords.indexOf("D"));

System.out.println("Combined subparts: " + subPart1 + subPart2);

// Find result of trying to find the letter w in coolWord

System.out.println("Index of 'w': " + coolWords.indexOf('w'));

// Make coolWord be all lowercase, and concatenate 3 at the end of it

String lower = coolWords.toLowerCase();

System.out.println(lower + 3);

// *************************** TASK 2(C) *******************************

String test1 = "cyber security";

String test2 = "internet of things";

String test3 = "cloud computing";

// write code below, should occupy at most six lines

System.out.println(reverse(test1));

System.out.println(reverse(test2));

System.out.println(reverse(test3));

System.out.println(test1);

System.out.println(test2);

System.out.println(test3);

// *************************** TASK 2(D) *******************************

System.out.println(reverse("Banana"));

System.out.println(reverse("Solstice"));

System.out.println(reverse("Top Secret"));

System.out.println(reverse("caaaaaab"));

System.out.println(reverse("cab"));

}

// *************************** TASK 2(B) *******************************

public static String reverse(String toReverse) {

String reversed = ""; // reversed must be built from scratch

// Use toReverse’s characters to build reversed

for (int i = toReverse.length() - 1; i > -1; i--) {

reversed = reversed + toReverse.charAt(i);

}

return reversed; // reversed should be built at this point

}

}

Method call Output Explaination

niceDay.charAt(5);

A

This method gets character present at specified index.

niceDay.toLowerCase();

have a nice day This method changes a string's all characters to their lowercase representations (e.g. A -> a, B->b)

niceDay.indexOf("H");

0 This method gets the index of first occurrence of specified character or substring in the main string. Here, H is at first (0) index.

niceDay.indexOf("A", 2);

5 This method also gets the index of first occurence of the specified character or substring in the main string but starts its search from the provided index (as second argument).

niceDay.charAt(niceDay.length());

RUNTIME ERROR This call will throw an IndexOutOfBoundException because charAt() expects a valid index but length() returns total length of the string, which is greater than last index.

niceDay.substring(8, 9);

I This method returns substring starting at argument 1 and ending before argument 2. Here, argument 1 and argument 2 are specified index(s).

niceDay.substring(5);

A NICE DAY This method call returns substring starting at the given index and ending at the end of the main string.

niceDay.substring(0);

HAVE A NICE DAY

Here as the given index is 0 (first index) therefore it will return complete main string. niceDay.substring(6, 4); RUNTIME ERROR This call throws an IndexOutOfBoundException as the second specified index cannot be less than the first specified index. niceDay.toUpperCase(niceDay.toLowerCase()); HAVE A NICE DAY This call contains two calls first is uppercase and second is lowercase. Because computer first computes the inner call therefore lowercase will be called first and will change the string to all lower characters. After that upper case is called on the previous result and it will change it back to the call capital characters.