Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

E7.2 - Write array list methods that carry out the following tasks for an array list of integers by completing the ArrayListMethods class posted on

E7.2 - Write array list methods that carry out the following tasks for an array list of integers by completing the ArrayListMethods class posted on Google Drive. For each method, provide a test (or tests) within the main method in the tester class. (Follow the procedure outlined in the starter code, part a has been done for you). Do not use the starter code in the book for E7.2, copy it from Google Drive. I have made a few changes to the code.

a) (already completed for you) Swap the first and last elements in the array list. b) Shift all elements by one to the right and move the last element into the first position. For example, 1 4 9 16 25 would become 25 1 4 9 16. c) Replace all even elements with 0. d) Replace each element except the first and last by the larger of its two neighbors. For the example in part (b), your output would be 1 9 16 25 25. e) Remove the middle element if the array list size is odd, or the middle two elements if the length is even. (You will need to test two different array lists here to make sure your method works on both even and odd length array lists). f) Move all even elements to the front, otherwise preserving the order of the elements. For the example in part (b) your output should be 4 16 1 9 25 g) Return the second-largest element in the array list. (Your method should return this value, and your main method should print the value out). If your input is 3 25 50 50 22 note that 50 should be returned. 50 is both the largest and second largest value in this array list because there are 2 elements with value 50. h) Return true if the array list is currently sorted in increasing order. (You can create a sorted ArrayList to use as input by starting with an empty Array List. Then use the add() method to add values in order one at a time). (Alternately you can use Collections.sort(); to sort your list. Use your ArrayList as a parameter to the sort() method). Make sure to test both a sorted array list and an unsorted array list as input. Your main method should print whether the array was in order or not. i) Return true if the array list contains two adjacent duplicate elements. j) Return true if the array list contains duplicate elements (which need not be adjacent).

SAMPLE CODE:

import java.util.Collections; //need this one for sorting ArrayList import java.util.Random; import java.util.ArrayList; public class ArrayListMethods { //part a, done for you. public static void swapFirstAndLast(ArrayList a) { //save first element int temp = a.get(0); //move last element to first position (0) a.set(0, a.get(a.size()-1)); //put temp value into last position (size() -1) a.set(a.size()-1, temp); } //part b, fill in this method public static void shiftRight(ArrayList a) { } //part c, set all even elements to 0. public static void setEvensToZero(ArrayList a) { } //part d, replace each element except the first and last by larger of two //around it public static void largerOfAdjacents(ArrayList a) { } //part e, remove middle el if odd length, else remove middle two els. public static void removeMiddle(ArrayList a) { } //part f - move all evens to front public static void moveEvensToFront(ArrayList a) { } //part g - return second largest el in array public static int ret2ndLargest(ArrayList a) { return 0; //dummy value } //part H - returns true if array is sorted in increasing order public static boolean isSorted(ArrayList a) { boolean isSorted = false; //initially assume list is not sorted return isSorted; //dummy value } //PART I - return true if array contains 2 adjacent duplicate values public static boolean hasAdjDuplicates(ArrayList a) { boolean hasDup = false; //assume no duplicates, then search array return hasDup; } //PART J - return true if array contains 2 duplicate values //duplicates need not be adjacent to return true public static boolean hasDuplicates(ArrayList a) { boolean hasDup = false; //assume no duplicates, then search array return hasDup; } }

SAMPLE CODE FOR TESTER:

import java.util.ArrayList; import java.util.Random; public class ArrayListMethodsTester { public static void main(String[] args) { //In your main method you should test your array methods //First fill an array list with random values - change the code below ArrayList a = new ArrayList<>(); a.add(0); //adds a 0 at location [0] //Now print the array list to show initial values System.out.println("Initial Array List:"); //you can use the following line to print the list System.out.println(a); System.out.println(); //from here you should fill in code that tests parts a-j //for problem E7.2 from the textbook //part a is done for you below. You should follow this format //to test each of your methods. //Test methods below this line. //Test of swapFirstAndLast() System.out.println("Before call to swapFirstAndLast():"); System.out.println(a); //swap first and last element ArrayListMethods.swapFirstAndLast(a); System.out.println("After call to swapFirstAndLast():"); System.out.println(a); System.out.println(); //***Begin second test below this line } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions