Question

In: Computer Science

Write a method that concatenates two strings passed as parameters.    Version #1 - assign to...

Write a method that concatenates two strings passed as parameters.
   Version #1 - assign to the 1st parameter the concatenation of both parameters.
Perform the concatenation using the + operator.
We are assuming reference parameter work that way (they don't).
   Version #2 - fine. return the value of the 1st param after concatenating
to it the second parameter then.
   Version #3 - Let's do it the proper way now, I say.
Create a temporary String reference variable in your method.
Assign to it the result of concatenating both parameters.
Return the temporary variable.
   Please note, this exercise is only useful if you take the time to trace or draw
   a stack diagram to make sure you exactly understand why each version works or
   does not work (and how they do so). Office hours are a good place to get help
   if you are rusty with tracing or stack diagrams.
*/
public class TinkeringWithStringsLive {
   public static void main(String[] args){
String s1 = "hello";
String s2 = "world";

version1(s1,s2);
System.out.println(s1);
System.out.println(version2(s1,s2));
System.out.println(version3(s1,s2));
   } // end main
  
   public static void version1(String s1, String s2){
// please note that the return type is void
// we modify s1 instead of returning anything
// worth a try, right?
   }// end method version1
  
   public static String version2(String s1, String s2){

return "";
   }// end method version2
  
   public static String version3(String s1, String s2){

return "";
   }// end method version3
} // end class

Solutions

Expert Solution

public class TinkeringWithStringsLive {
        public static void main(String[] args) {
                String s1 = "hello";
                String s2 = "world";

                version1(s1, s2);
                System.out.println(s1);
                System.out.println(version2(s1, s2));
                System.out.println(version3(s1, s2));
        } // end main

        public static void version1(String s1, String s2) {
                // Return is void, hence we are not returning anything
                s1 = s1 + s2;
                
                // When values are passed to the method, they are given as a copy of original value
                // Hence s1 and s2 are copies inside method.
                // If we change s1 in the method, it is not going to affect anything outside the method
        }// end method version1

        public static String version2(String s1, String s2) {
                s1 = s1 + s2;
                
                // Here, we change the string s1 inside the method and then return the modified string.
                // the returned string is captured in main method and shown there, hence we see the change.
                return s1;
        }// end method version2

        public static String version3(String s1, String s2) {
                String temp = s1 + s2;
                
                // We just created another string inside the method which is concatenated, and we are returning
                // that. main method captures and shows it.
                return temp;
        }// end method version3
} // end class
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Write a progrm that accepts two strings as input, then indicates if the two strings are...
Write a progrm that accepts two strings as input, then indicates if the two strings are equal when the case of individual letters is ignored. Sample runs are shown below. Use C++ Enter two strings. String 1: PROgraM String 2: proGram PROgraM and proGram are equal. Enter two strings. String 1: litter String 2: LittLe litter and LittLe are not equal.
Write in main • setArray() – This method has two integer parameters, one for the rows...
Write in main • setArray() – This method has two integer parameters, one for the rows of a two-dimensional array and the other for the columns of a two-dimensional array. The method will return a two-dimensional array of integers with rows and columns given by the parameters. In addition, the method will use the Random class, along with the nextInt() method, to give each element of the two-dimensional array a value between 1 and 20. • display() – This is...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT