Write a java program that calls a method called reverse3 that accepts an ArrayLi
ID: 3671489 • 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. 2.
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. (Note: you can use Collections.sort() to sort your ArrayList after you implement the compareTo() method).
import java.util.*;
public class DateTest {
public static void main(String[] args) {
ArrayList dates = new ArrayList();
dates.add(new Date(4, 13, 1743)); // Jefferson
dates.add(new Date(2, 22, 1732)); // Washington
dates.add(new Date(3, 16, 1751)); // Madison
dates.add(new Date(10, 30, 1735)); // Adams
dates.add(new Date(4, 28, 1758)); // Monroe
System.out.println("birthdays = " + dates);
Collections.sort(dates);
System.out.println("birthdays = " + dates);
}
}
When you execute the following code it should print:
birthdays = [4/13/1743, 2/22/1732, 3/16/1751, 10/30/1735, 4/28/1758]
birthdays = [2/22/1732, 10/30/1735, 4/13/1743, 3/16/1751, 4/28/1758]
Explanation / Answer
Code for both problem is given with comments as guidelines
Problem 1:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
//list created
ArrayList<Integer> a=new ArrayList<Integer>();
a.add(10);a.add(13);a.add(2);a.add(8);a.add(7);a.add(90);a.add(-1);a.add(2);
System.out.println(" Before call:");
for (Integer integer : a) {
System.out.print(integer+" ");
}
reverse3(a);
System.out.println(" After call:");
for (Integer integer : a) {
System.out.print(integer+" ");
}
}
private static void reverse3(ArrayList<Integer> a) {
for(int i=0;i<a.size();i=i+3)
{
int j=i;
int t1=a.get(j++);
if(j==a.size()) break;
int t2=a.get(j++);
if(j==a.size()) break;
int t3=a.get(j++);
a.set(--j, t1);
a.set(--j, t2);
a.set(--j, t3);
}
}
}
Problem 2:
import java.util.ArrayList;
import java.util.Collections;
public class Date implements Comparable<Date>{
int year,month,day;
//constructor
public Date(int m,int d,int y)
{day=d;month=m;year=y;}
/*this 'toString' method is overrided to print output neatly!*/
@Override
public String toString() {
return (month+"/"+day+"/"+year).toString();
}
//mutator method
public void setDate(int m,int d,int y)
{day=d;month=m;year=y;}
//accessor methods
public int getDay(){return day;}
public int getMonth(){return month;}
public int getYear(){return year;}
public static void main(String[] args) {
ArrayList<Date> dates=new ArrayList<Date>();
dates.add(new Date(4,13,1743));
dates.add(new Date(2,22,1732));
dates.add(new Date(3,16,1751));
dates.add(new Date(10,30,1735));
dates.add(new Date(4,28,1758));
System.out.println("birthdays= "+dates);/*here 'toString' is used to print*/
/*Automatic comparisons are made as 'comapreTo' method is implemented*/
Collections.sort(dates);
System.out.println("birthdays= "+dates);
}
@Override
public int compareTo(Date o) {
/*as per preference given, method is implemented
* */
int dd=this.day-o.day;
int md=this.month-o.month;
int yd=this.year-o.year;
if(yd!=0) return yd;
else if(md!=0) return md;
else if(dd!=0) return dd;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.