Question

1. Create a new class called ReversibleArray. 2. In the class header, add the code <T>...

1. Create a new class called ReversibleArray.

2. In the class header, add the code <T> immediately to the right of the class name.

3. Give this class two private instance variables: T[] array and int count

4. Create a constructor which takes in one parameter of type T[] and assign that parameter's value into this.array. Set count as the length of the array.

5. Add a toString() method that outputs the array values in the format: elem0, elem1, elem2, etc. (hint: add the comma and space only if you are not on the final iteration of the loop).

6. Create the most important function in this class: public void reverse () { } a. This method must swap elements within the array, without making a new array or other collection. b. The swaps should be made symmetrically about the middle of the array. This means array[0] swaps with array[n-1], array[1] swaps with array[n-2], etc. c. Use a temp variable to help with these swaps. What variable type should this temp variable be? d. How many swaps must be made to correctly reverse the array?

7. Open TestReverse.java and read over the code in its main() function.

8. Run TestReverse to see the output. If your output is incorrect, then debug the program until it works properly. It should print out the following text: atom, breeze, clock, daydream, energy energy, daydream, clock, breeze, atom 11, 22, 33, 44, 55, 66, 77 77, 66, 55, 44, 33, 22, 11 Jerry Seinfeld, Salma Hayek, Reese Witherspoon, Vince Vaughn Vince Vaughn, Reese Witherspoon, Salma Hayek, Jerry Seinfeld

0 0
Add a comment Improve this question Transcribed image text
Answer #1

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//NOTE: YOU CAN COMMENT THIS LINE OR REMOVE THIS FROM THE CODE IF YOU DONT WANT TO SEE THE NUMEBR OF SWAPS

       System.out.println("Total swaps: " + j);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CODE TO COPY


public class ReversibleArray<T> {
   T[] array;
   int count;

   public ReversibleArray(T[] array) {
       this.array = array;
       count = array.length;
   }

   @Override
   public String toString() {
       String s = "";
       for (int i = 0; i < array.length; i++) {
           if (i != array.length - 1) {
               s += array[i] + ", ";
           } else {
               s += array[i];
           }
       }
       return s;
   }

   public void reverse() {
       int j = 0;
       int x = array.length;
       for (int i = 0; i < x; i++) {
           T temp = array[i];
           array[i] = array[x - 1];
           array[x - 1] = temp;
           x--;
           j++;
       }
       System.out.println("Total swaps: " + j);
   }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public class TestReverse {

   public static void main(String[] args) {

       String[] arr = new String[] { "atom", "breeze", "clock", "daydream", "energy" };
       ReversibleArray<String> a = new ReversibleArray<String>(arr);
       System.out.println(a.toString());
       a.reverse();
       System.out.println(a.toString());

       System.out.println("============================");

       Integer[] arr1 = new Integer[] { 11, 22, 33, 44, 55, 66, 77 };
       ReversibleArray<Integer> a1 = new ReversibleArray<Integer>(arr1);
       System.out.println(a1.toString());
       a1.reverse();
       System.out.println(a1.toString());

       System.out.println("============================");

       String[] arr2 = new String[] { "Jerry Seinfeld", "Saalma Hayek", "Reese", "Witherspoon", "Vince" };
       ReversibleArray<String> a2 = new ReversibleArray<String>(arr2);
       System.out.println(a2.toString());
       a2.reverse();
       System.out.println(a2.toString());
   }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

EPXLANATION


public class ReversibleArray<T> { // Creating a new class called ReversibleArray.

//creating two private instance variables: T[] array and int count
   T[] array;
   int count;

   public ReversibleArray(T[] array) { // constructor which takes in one parameter of type T[]
       this.array = array; // assigning the parameter's value into this.array
       count = array.length; //Set count as the length of the array
   }

//Add a toString() method
   @Override
   public String toString() {
       String s = ""; //to store the string

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

           if (i != array.length - 1) { // if you are not on the final iteration of the loop
               s += array[i] + ", "; //add the comma and space
           } else {
               s += array[i]; // dont add space or comma at the end
           }
       }
       return s; //return the ouput
   }

   public void reverse() {
       int j = 0; // to keep count of number of swaps
       int x = array.length; // to store last element number
       for (int i = 0; i < x; i++) { //iterate through all elements
           T temp = array[i]; // store array[i] in temp
           array[i] = array[x - 1]; // swap with last element
           array[x - 1] = temp;// swap with element stored in temp
           x--; // shift from last element to last second and so on...
           j++; // increment number of swaps
       }
       //NOTE: YOU CAN COMMENT THIS LINE OR REMOVE THIS IF YOU DONT WANT TO SEE THE NUMEBR OF SWAPS
       System.out.println("Total swaps: " + j); //print the swaps count
   }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public class TestReverse { // CREATING A CLASS TestReverse

   public static void main(String[] args) { //CREATING A main METHOD

//CREATING A ARRAY arr to store words
       String[] arr = new String[] { "atom", "breeze", "clock", "daydream", "energy" };
//creating a object a with and passing arr
       ReversibleArray<String> a = new ReversibleArray<String>(arr);
//printing all the elements in a
       System.out.println(a.toString());
//reversing array
       a.reverse();
//printing the reversed array
       System.out.println(a.toString());

//printing line
       System.out.println("============================");

//CREATING A ARRAY arr1 to store words
       Integer[] arr1 = new Integer[] { 11, 22, 33, 44, 55, 66, 77 };
//creating a object a1 with and passing arr
       ReversibleArray<Integer> a1 = new ReversibleArray<Integer>(arr1);
//printing array a1
       System.out.println(a1.toString());
//reversing array
       a1.reverse();
//printing array a1 after reversing
       System.out.println(a1.toString());

//printing line
       System.out.println("============================");

//CREATING A ARRAY arr2 to store words
       String[] arr2 = new String[] { "Jerry Seinfeld", "Saalma Hayek", "Reese", "Witherspoon", "Vince" };
//creating a object a2 with and passing arr
       ReversibleArray<String> a2 = new ReversibleArray<String>(arr2);
//printing array a2 elements
       System.out.println(a2.toString());
//reversing array
       a2.reverse();
//printing array a2 after reversing
       System.out.println(a2.toString());
   }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

3 46 un 6 7 8 9 ReversibleArray.java TestReverse.java X 1 2 public class TestReverse el public static void main(String[] argsReversibleArray.java X TestReverse.java 1 2 public class ReversibleArray<t> ] T[] array; int count; public ReversibleArray (Console X <terminated> TestReverse [Java Application] C:\Program Files\Java\jdk-12.0.1\bin\javaw.exe btom, breeze, clock, day

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IF YOU HAVE ANY DOUBT REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU

Add a comment
Know the answer?
Add Answer to:
1. Create a new class called ReversibleArray. 2. In the class header, add the code <T>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This...

    Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT