Write a java program that calls a method called reverse3 that accepts an ArrayLi
ID: 3672915 • 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 -1 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 constructor(s), 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 5 U.S. presidents in random order and puts them into sorted order. When you execute the following code it should print:Explanation / Answer
PART 1
package reverse;
import java.util.ArrayList;
public class Reverse {
private ArrayList<Integer> reverse3(ArrayList<Integer> arrayList) {
int length = arrayList.size();
int i = 0;
Integer temp;
while (i+2 < length) {
temp = arrayList.get(i);
arrayList.set(i, arrayList.get(i+2));
arrayList.set(i+2, temp);
i += 3;
}
return arrayList;
}
public static void main(String[] args) {
Reverse reverse = new Reverse();
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(13);
list.add(2);
list.add(8);
list.add(7);
list.add(90);
list.add(-1);
list.add(-2);
list = reverse.reverse3(list);
System.out.println(list);
}
}
PART 2
package reverse;
public class Date implements Comparable<Date>{
private int year;
private int month;
private int day;
public Date(int month, int day, int year) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public int compareTo(Date anotherDate) {
if (this.year > anotherDate.getYear())
return 1;
else if (this.year < anotherDate.getYear())
return -1;
else if(this.month > anotherDate.getMonth())
return 1;
else if(this.month < anotherDate.getMonth())
return -1;
else if(this.day > anotherDate.getDay())
return 1;
else if(this.day < anotherDate.getDay())
return -1;
return 0;
}
@Override
public String toString() {
return month + "/" + day + "/" + year;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.