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

Java Write a method, makeUserName, that is passed two Strings and an int: the first is...
Java Write a method, makeUserName, that is passed two Strings and an int: the first is a first name, the second is a last name, and the last is a random number. The method returns a user name that contains the last character of the first name, the first five characters of the last name (assume there are five or more characters in last name), and the random number. An example: Assume that “John”, “Smith”, 45, are passed when the...
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:
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if...
Write a RECURSIVE method that receives 2 strings as parameters. The method will return true if the 2nd string is a subsequence of the 1st string. If not, the method will return false. An empty string is a subsequence of every string. This is because all zero characters of the empty string will appear in the same relative order in any string This method must not contain any loops. In java
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings will each be ONE character and will represent a nucleotide. The list will be a nested int list representing a 4x4 score matrix. This function will return the value (int) from the nested int list at the location of the two referenced nucleotides. a. An example call to calculate_score would be calculate_score(“A”, “T”, score_matrix). If we look at the alignment score table in the...
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) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT