Write a java program that calls a method called reverse3 that accepts an ArrayLi
ID: 3672048 • Letter: W
Question
Write a java program that calls a method called reverse3 that accepts an ArrayList of integer values as a parameter and reverses each successive sequence of three values in the list. If the list has extra values that are not part of a sequence of three, those values are unchanged. For example if a list stores values [10, 13,2. 8, 7,90, -1,2], after the call the list should store the values [2, 13, 10,90, 7, 8.-1,2]. The first sequence of three (10. 13.2) has been reversed to (2, 13, 10). The second sequence (8,7,90) has been reversed to (90. 7. 8) and so on. Notice that - I and 2 are unchanged because they were not part of a sequence of three values. Print the array before the call and after the call. Write a class called Date that includes three fields year, month and day. This class stores information about a single date (year, month and day). Your class should have constructors), accessors and mutators and it should implement the Comparable interface. Years take precedence over months, which take precedence over days. For example. Feb 19,2016 comes after Nov 20, 2015. The following class DateTest can be used to test the Date class that you wrote. It creates a list of the birthdays of the first $ U S. presidents in random order and puts them into sorted order.Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Reverse
{
//reverse3 function
public static void reverse3(ArrayList<Integer> elements)
{
int count = elements.size();
for(int counter: elements)
System.out.print(counter+ " ");
System.out.println(" ");
for(int i = 2; i<count;i = i+3)
Collections.swap(elements, i, i-2);
for(int counter: elements)
System.out.print(counter+ " ");
}
public static void main (String[] args) throws java.lang.Exception
{
// Create new ArrayList.
ArrayList<Integer> elements = new ArrayList<>();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number of elements in Array");
int n = keyboard.nextInt();
int m;
System.out.println("Enter the array elements");
for(int i=0;i<n;i++){
m = keyboard.nextInt();
elements.add(m);
}
reverse3(elements);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.