Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write a Java program that calls a method called reverse3 that accepts an ArrayList of integer values as a parameter and reverses each successive

1. 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, 92) 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(Stringl] algs) {

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)); 1/ Madison

dates.add(new Date(10, 30, 1735)); // Adams

dates.add(new Date(4, 28, 1758)); 1/ Monroe

System.outprintIn("birthdays = " + dates);

Collections.sort(dates);

System.outprintlncbirthdays = " + 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]

Step by Step Solution

3.42 Rating (158 Votes )

There are 3 Steps involved in it

Step: 1

PART 1 package reverse import javautilArrayList public class Reverse private ArrayList reverse3Array... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Structures and Algorithms in Java

Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser

6th edition

1118771334, 1118771338, 978-1118771334

More Books

Students also viewed these Programming questions

Question

Determine miller indices of plane A Z a/2 X a/2 a/2 Y

Answered: 1 week ago