Part 1

  • Write a method String reverse1(String s) that calculates and returns the String that is the reverse of s. For example reverse1("1 2xy3")=="3yx2 1". This method must work as follows. Convert s to a StringBuilder sb, use the reverse() method in StringBuilder to reverse sb, and finally convert the result back to a String that is the final result returned. This what the book did in section 9.4.3. Feel free to use or adapt the book's code.
  • Write a method String reverse2(String s) that calculates and returns the same result as reverse1() but does not use any StringBuilders(). That is reverse2() does the job by hand. You may (and probably will want to) use methods from the String class. This is what I did in my solution, a clickable link in section 9.4.3. Feel free to use or adapt my code.
  • Write a main() method that
    • Reads a String using Scanner.nextLine().
    • Calculates the reverse using reverse1() and prints the result.
    • Calculates the reverse using reverse2() and prints the result.

Part 2

  • Reread Lab 2 Part 1 (Copied to bottom of document, along with the java code)
  • Write a method String mixReverse1(String str1, String str2) that
    • Checks that the two parameters have the same length. If they do not, the method prints an error message and returns a copy of str1.
    • Returns a new String of length twice that of str1. This String consists of the String arr1 interleaved with the reverse of String arr2. The reversal must be calculated by reverse1() from part 1. The mixing can be performed any way you like.
  • Similarly write String mixReverse2(String str1, String str2) that uses reverse2().
  • Write a main() method that
    • Reads two Strings str1 and str2 using Scanner.nextLine().
    • Calls mixReverse1(str1,str2) and prints the resulting string.
    • Calls mixReverse2(str1,str2) and prints the resulting string.

Part 3

Write a class Lab4Part3 with the following components.

  • Two instance data fields String s1,s2.
  • A no-arg constructor that initializes each data field to the empty string.
  • A 1-argument constructor Lab4Part3(String s) that initializes each data field to a separate copy of s. (A 1-argument String constructor is good for this.)
  • A 2-argument constructor Lab4Par3(String s1, String s2) that initializes each data field to a copy of the corresponding argument (use the Java keyword this so that you can name the parameters the same as the data fields.
  • A public boolean instance method BothFieldsEqual that returns true if and only if the two data fields are equal. Test for equality using the equals() method in the String class.
  • A public String instance method mixReverse() that does the same thing as one of your routine from part2. This should not be much work. Take either mixReverse1() or mixReverse2() (your choice) and remove the arguments. In place of the arguments you use the data fields of the object.

Write a main() method that uses and checks all the features of the class Lab4Part3.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.