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

CS Lab project. Learning Objectives Declare references to objects from the Strin

ID: 3688407 • Letter: C

Question

CS Lab project.

Learning Objectives

Declare references to objects from the String and StringBuilder classes. (5 points)

Construct objects from String and StringBuilder classes. (5 points)

Access properties of the constructed objects using accessor methods. (20 points)

Mutate properties of the constructed objects using mutator methods or identify immutable objects. (20 points)

Demonstrate pass by sharing for objects. (20 points)

Return newly constructed objects from methods. (20 points)

10 points will be awarded for the proper documentation of the program, including meaningful comments, proper indentation, meaningful use of whitespace, and meaningful variable names.

Description

This laboratory will give you experience working with objects, references, constructors, accessors and mutators in one familiar class (String) and another class we haven’t used (StringBuilder). It will also give you an opportunity to read the API on your own and start to use methods in the API to perform tasks strategically.

There is a file called Lab 11.java available on Janux (and in the shared dropbox). This file is a template for practice using objects from the String and StringBuilder classes. Some of the tasks that are assigned are impossible to do in Java. When you find those tasks, you need to explain why the task is impossible.

Warning about the StringBuilder API. Some of the signature choices in the StringBuilder class are very odd. Specifically, many of the methods return a StringBuilder object unnecessarily. As an example, all of the append methods in the StringBuilder class return a StringBuilder object. They should not since append is a mutator method (it changes the implicit argument, and therefore does not need to return the reference to the implicit argument).   The way this is expressed in the API is by saying “Returns: A reference to this object”. The choice of the word “this” is what indicates that the reference to the implicit object is returned. When I talk about reading the API between the lines, this is what I mean. It is very subtle.

// Some of the parts below are impossible to do.
// When you encounter them, say they are impossible and explain why.

public class Lab11
{

   public static void main(String[] args)
   {
       experimentWithString();
      
       experimentWithStringBuilder();
   }
  
   public static void experimentWithString()
   {
       // Declare a reference to a String object. Do not construct the String here (2 points)
      
       // Construct a String object that contains "ABCDE" using the new operator (1 point)
      
       // Construct a String object that contains "abcde" without using the new operator (1 point)
      
       // Print out the characters of a String object, one to a line, in reverse order   (5 points)
      
       // Append "ABC" to a String object containing "abc" without using the + operator (5 points)
      
       // Remove the vowels from a String object containing "hello" without constructing a new object
       // (5 points)
      
       // Change a String object containing "hello" so that it contains "hi world!" without constructing
       // a new object.
       // (5 points)
      
       // Write a static method that randomly scrambles the letters in a String using pass by sharing
       // The signature should be: public static void scrambleShare(String word)
       // Use this method to scramble a String object that contains "ZYWXV" (10 points)
      
       // Write a static method that randomly scrambles the letters in a String using a returned value
       // The signature should be: public static String scrambleReturn(String word)
       // Use this method to scramble a String object that contains "LMNOP" (10 points)

   }
  
   public static void scrambleShare(String word)
   {
      
   }
  
   public static String scrambleReturn(String word)
   {
       return null; // this keep the compiler quiet--you will need to edit it
   }
  
  
   public static void experimentWithStringBuilder()
   {
       // Declare a reference to a StringBuilder object. Do not construct the String here (3 points)
      
       // Construct a StringBuilder object that contains "ABCDE" using the new operator (1 point)
      
       // Construct a StringBuilder object that contains "abcde" without using the new operator (2 points)
      
       // Print out the characters of a StringBuilder object containing "ABCDE", one to a line, in reverse order  
       // (5 points)
      
       // Append "ABC" to a StringBuilder object containing "abc" without using the + operator
       // (5 points)
      
       // Remove the vowels from a StringBuilder object containing "hello" without constructing a new object
       // (5 points)
      
       // Change a StringBuilder object containing "hello" so that it contains "hi world!" without constructing
       // a new object.
       // (5 points)
      
       // Write a static method that randomly scrambles the letters in a StringBuilder using pass by sharing
       // The signature should be: public static void scrambleShare(StringBuilder word)
       // Use this method to scramble a StringBuilder object that contains "ZYWXV" (10 points)
      
       // Write a static method that randomly scrambles the letters in a String using a returned value
       // The signature should be: public static StringBuilder scrambleReturnBuilder(String word)
       // Use this method to scramble a StringBuilder object that contains "LMNOP" (10 points)
   }
  
   public static void scrambleShare(StringBuilder word)
   {
      
   }
  
   public static StringBuilder scrambleReturnBuilder(String word)
   {
       return null; // this keep the compiler quiet--you will need to edit it
   }
}

Explanation / Answer

Hello there ,

Please find below code and its o/p.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Lab11 {
   private static Random random = new Random(); // the shared random.

   public static void main(String[] args) {
       experimentWithString();

       experimentWithStringBuilder();
   }

   public static void experimentWithString() {
       // Declare a reference to a String object. Do not construct the String
       // here (2 points)
       String stringObj;
       // Construct a String object that contains "ABCDE" using the new
       // operator (1 point)
       String stringObj1 = new String("ABCDE");
       System.out.println("Construct a String object that contains "ABCDE" using the new operator: " + stringObj1);
       // Construct a String object that contains "abcde" without using the new
       // operator (1 point)
       String stringObj2 = "abcde";
       System.out.println("Construct a String object that contains "abcde" without using the new operator: " + stringObj2);
       // Print out the characters of a String object, one to a line, in
       // reverse order (5 points)
       System.out.println("Print out the characters of a String object, one to a line, in reverse order: ");
       for (int i = stringObj1.length() - 1; i >= 0; i--) {
           System.out.println(stringObj1.charAt(i));
       }
       // Append "ABC" to a String object containing "abc" without using the +
       // operator (5 points)
       String newObj = "abc".concat("ABC");
       System.out.println("Append "ABC" to a String object containing "abc" without using the +: " + newObj);
       // Remove the vowels from a String object containing "hello" without
       // constructing a new object
       // (5 points)
       String stringWithoutVowel = "hello".replaceAll("[aeiouAEIOU]", "");
       System.out.println("Remove the vowels from a String object containing "hello" without constructing a new object: "
               + stringWithoutVowel);
       // Change a String object containing "hello" so that it contains
       // "hi world!" without constructing
       // a new object.
       // (5 points)
       String helloString = "hello";
       helloString = "hi world!";
       System.out.println("Change a String object containing "hello" so that it contains "hi world!" without constructing: "
               + helloString);
       // Write a static method that randomly scrambles the letters in a String
       // using pass by sharing
       // The signature should be: public static void scrambleShare(String
       // word)
       // Use this method to scramble a String object that contains "ZYWXV" (10
       // points)

       scrambleShare("ZYWXV");
       // Write a static method that randomly scrambles the letters in a String
       // using a returned value
       // The signature should be: public static String scrambleReturn(String
       // word)
       // Use this method to scramble a String object that contains "LMNOP" (10
       // points)
       System.out.println(scrambleReturn("LMNOP"));
   }

   public static void scrambleShare(String word) {
       char[] a = word.toCharArray();
       int n = a.length; // the length of the array.
       for (int i = 0; i < n; i++) {
           int t = random.nextInt(n); // pick a random number 0 - the length.
           if (t == i) { // if the random number is the loop counter
               if (i > 0) { // check if we're at the first element.
                   t = random.nextInt(i); // pick another number between 0 -
                                           // and the loop counter.
               } else {
                   t = a.length - 1; // the end of the loop.
               }
           }
           a[i] ^= a[t]; // swap a[i] and a[t]
           a[t] ^= a[i];
           a[i] ^= a[t];
       }
       System.out.println(new String(a));
   }

   public static String scrambleReturn(String word) {
       List<String> letters = Arrays.asList(word.split(""));
       Collections.shuffle(letters);
       String shuffled = "";
       for (String letter : letters) {
           shuffled += letter;
       }
       return shuffled;
   }

   public static void experimentWithStringBuilder() {
       // Declare a reference to a StringBuilder object. Do not construct the
       // String here (3 points)
       StringBuilder builderObj;
       // Construct a StringBuilder object that contains "ABCDE" using the new
       // operator (1 point)
       StringBuilder bulderObj1 = new StringBuilder("ABCDE");
       System.out.println("Construct a StringBuilder object that contains "ABCDE" using the new operator: " + bulderObj1.toString());
       // Construct a StringBuilder object that contains "abcde" without using
       // the new operator (2 points)
       StringBuilder builderObj2 = new StringBuilder();
       builderObj2.insert(0, "abcde");
       System.out.println("Construct a StringBuilder object that contains "abcde" without using the new operator: "
               + builderObj2.toString());
       // Print out the characters of a StringBuilder object containing
       // "ABCDE", one to a line, in reverse order
       // (5 points)
       System.out.println("Print out the characters of a StringBuilder object containing "ABCDE", one to a line, in reverse order: ");
       for (int i = bulderObj1.length() - 1; i >= 0; i--) {
           System.out.println(bulderObj1.charAt(i));
       }
       // Append "ABC" to a StringBuilder object containing "abc" without using
       // the + operator
       // (5 points)
       StringBuilder builder = new StringBuilder("abc");
       builder.append("ABC");
       System.out.println("Append "ABC" to a StringBuilder object containing "abc" without using the + operator :"
               + builder.toString());
       // Remove the vowels from a StringBuilder object containing "hello"
       // without constructing a new obaject
       // (5 points)
       StringBuilder builderWihoutVowels = new StringBuilder("hello");
       char[] vowels = "AaEeIiOoUu".toCharArray();
       for (int i = 0; i < vowels.length; i++) {
           if (builderWihoutVowels.toString().contains(String.valueOf(vowels[i]))) {
               int indexOfVowel = builderWihoutVowels.indexOf(String.valueOf(vowels[i]));
               builderWihoutVowels.deleteCharAt(indexOfVowel);

           }
       }
       System.out.println("Remove the vowels from a StringBuilder object containing "hello" without constructing a new obaject: "
               + builderWihoutVowels);
       // Change a StringBuilder object containing "hello" so that it contains
       // "hi world!" without constructing
       // a new object.
       // (5 points)
       StringBuilder builder1 = new StringBuilder("hello");
       builder1.replace(0, builder1.length(), "hi world!");
       System.out.println("Change a StringBuilder object containing "hello" so that it contains "hi world!" without constructing: "
               + builder1.toString());
       // Write a static method that randomly scrambles the letters in a
       // StringBuilder using pass by sharing
       // The signature should be: public static void
       // scrambleShare(StringBuilder word)
       // Use this method to scramble a StringBuilder object that contains
       // "ZYWXV" (10 points)
       StringBuilder shuffel = new StringBuilder("ZYWXV");
       scrambleShare(shuffel);
       // Write a static method that randomly scrambles the letters in a String
       // using a returned value
       // The signature should be: public static StringBuilder
       // scrambleReturnBuilder(String word)
       // Use this method to scramble a StringBuilder object that contains
       // "LMNOP" (10 points)
       System.out.println(scrambleReturnBuilder("LMNOP"));
   }

   public static void scrambleShare(StringBuilder word) {
       Collections.shuffle(Arrays.asList(word.toString().toCharArray()));
       System.out.println(word);
   }

   public static StringBuilder scrambleReturnBuilder(String word) {
       return new StringBuilder(scrambleReturn(word));
   }
}

===O/P===

Construct a String object that contains "ABCDE" using the new operator: ABCDE
Construct a String object that contains "abcde" without using the new operator: abcde
Print out the characters of a String object, one to a line, in reverse order:
E
D
C
B
A
Append "ABC" to a String object containing "abc" without using the +: abcABC
Remove the vowels from a String object containing "hello" without constructing a new object: hll
Change a String object containing "hello" so that it contains "hi world!" without constructing: hi world!
ZWXVY
MONLP
Construct a StringBuilder object that contains "ABCDE" using the new operator: ABCDE
Construct a StringBuilder object that contains "abcde" without using the new operator: abcde
Print out the characters of a StringBuilder object containing "ABCDE", one to a line, in reverse order:
E
D
C
B
A
Append "ABC" to a StringBuilder object containing "abc" without using the + operator :abcABC
Remove the vowels from a StringBuilder object containing "hello" without constructing a new obaject: hll
Change a StringBuilder object containing "hello" so that it contains "hi world!" without constructing: hi world!
ZYWXV
OPNML