
Write a static method called "flip4" that takes an ArrayList of integer values as a parameter and that rewrites successive sequences of four values in the list. For each sequence of four values, the method will swap the first element and the fourth element and then swap the second element with the third element. For example, suppose that a variable called list stores the following sequence of values:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4]
and we make the following call: flip4(list);
Afterwards the list should store the following sequence of values:
[42, 19, 8, 3, -8, 19, 26, 7, -4, 6, 204, 193]
Each sequence of three elements will be rotated so that the 3rd value becomes the 1st value and the other two are moved to the next higher index; ie. the 1st becomes the 2nd and the 2nd becomes the 3rd. If the list has extra values that are not part of a sequence of three, those values are unchanged. For example, if the list had instead stored:
[3, 8, 19, 42, 7, 26, 19, -8, 193, 204, 6, -4, 99, 2, 1]
The result would have been:
[42, 19, 8, 3, -8, 19, 26, 7, -4, 6, 204, 193, 99, 2, 1]
Notice that the values (99, 2, 1) are unchanged in position because they were not part of a sequence of four values.
You may use the following code to test your program:
Expected output is listed to the right of the print statements.
import java.util.*;
public class Flipper {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(8);
list.add(19);
list.add(42);
list.add(7);
list.add(26);
list.add(19);
list.add(-8);
list.add(193);
list.add(99);
list.add(2);
System.out.println(list); // [3, 8, 19, 42, 7, 26, 19, -8, 193, 99, 2]
flip4(list);
System.out.println(list); // [42, 19, 8, 3, -8, 19, 26, 7, 193, 99, 2]
}
// *** Your method code goes here ***
} // End of Flipper class

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images

- java Create a static method that: is called repeatAll returns ArrayList of Booleans takes in a single parameter - an ArrayList of Booleans This method should modify its ArrayList parameter by repeating its ArrayList values. For example, if the parameter is (true, false, false) The modified ArrayList should be (true, false, false, true, false, false) public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Boolean> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextBoolean()); } System.out.println(repeatAll(list)); } }arrow_forwardjava Create a method that: is called timesTwo returns an ArrayList of Integers takes in a single parameter - an ArrayList of Integers called nums This method should take the ArrayList parameter and multiply every value by two. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(timesTwo(list)); } }arrow_forwardQuestion in java Arraylist Please help fastarrow_forward
- Javaarrow_forwardJavaarrow_forwardjava Create a static method that: is called appendPosSum returns an ArrayList of Integers takes one parameter: an ArrayList of Integers This method should: Create a new ArrayList of Integers Add only the positive Integers to the new ArrayList Sum the positive Integers in the new ArrayList and add the Sum as the last element For example, if the incoming ArrayList contains the Integers (4,-6,3,-8,0,4,3), the ArrayList that gets returned should be (4,3,4,3,14), with 14 being the sum of (4,3,4,3). The original ArrayList should remain unchanged. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(appendPosSum(list));arrow_forward
- Write a static method named "bigBeforeSmall" that accepts an array of Strings as a parameter and rearranges its elements so that longer Strings appear before shorter Strings. For example, if the following array is passed to your method: String[] numbers = { "one", "twelve", "three", "four", "fourteen", "six" }; Then after the method has been called, one acceptable ordering of the elements would be: ["fourteen", "twelve", "three", "four", "one", "six" ] The exact order of the elements does not matter, so long as all longer Strings appear before all shorter Strings. Another acceptable order would be: ["fourteen", "twelve", "three", "four", "six", "one" ] Do not make any assumptions about the length of the array or the range of values it might contain. For example, the array might contain empty Strings (length zero), or all Strings could have the same length. You may assume that the array is not null (empty). Hint: This is actually a sorting problem. In BubbleSort you tested pairs…arrow_forwardWrite a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4. new code can only be added between existing code. as seen in image.arrow_forwardWrite a static method that displays all combinations of picking two numbers from the array list: public static void combinations(ArrayList<Integer> list) Include the driver program that prompts the user to enter 10 integers and displays all combinations of picking two numbers from the array list.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





