Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public void setArrayList(ArrayList<Integer> newValues). a) ( already completed f

ID: 667564 • Letter: P

Question

public void setArrayList(ArrayList<Integer> newValues).

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).

My code so far

import java.util.Collections; //need this one for sorting ArrayList
import java.util.Random;
import java.util.ArrayList;

public class ArrayListMethods {
  
   //ArrayList to hold values to work on for lab
   private ArrayList<Integer> listValues;

   //ArrayList constructor
   public ArrayListMethods(ArrayList<Integer> initialValues) {
       //note this shows how to make a copy of an array list
       //simply pass the old list in as an argument to
       //the ArrayList constructor
       listValues = new ArrayList<Integer>(initialValues);
   }
  
   //set listValues to list passed as parm
   //use this method to "reset" our ArrayList data before each test
   public void setListValues(ArrayList<Integer> newValues) {
       listValues = new ArrayList<Integer>(newValues);
   }
  
   //Accessor method to get the list values
   public ArrayList<Integer> getListValues() {
       return listValues;
   }
  
   //method to print the current values in the ArrayList
   //or you can simply use println() with the array list as the
   //parameter
   public void printArrayList() {
       System.out.println(listValues);
   }
  
   //part a, done for you.
   public void swapFirstAndLast() {
       //save first element
       int temp = listValues.get(0);
       //move last element to first position (0)
       listValues.set(0, listValues.get(listValues.size()-1));
       //put temp value into last position (size() -1)
       listValues.set(listValues.size()-1, temp);
   }
  
  
   //part b, fill in this method
   public void shiftRight()
   {
       int temp = listValues.get(listValues.size()-1);
//shift all elements to the right by 1except last element
for(int i=listValues.size()-1;i>0;i--){
listValues.set(i, listValues.get(i-1));
}
listValues.set(0, temp);
}
//part c, set all even elements to 0.
public void setEvensToZero()
{
for(int i=1;i<listValues.size();i+=2)
{
listValues.set(i, 0);
}
}
//part d, replace each element except the first and last by larger of two
//around it
public void largerOfAdjacents()
{
int prev,i;
//initialize the previous element as 0th element
for(i=1,prev=listValues.get(0);i<listValues.size()-1;i++){
//if previous element is greater than following element
if(prev < listValues.get(i+1)){
//update the previous as current element
prev = listValues.get(i);
//set the current element as following element
listValues.set(i,listValues.get(i+1));
}  
else
{
////update the previous as current element
prev = listValues.get(i);
////set the current element as previous element
listValues.set(i,prev);
}
}
}
//part e, remove middle el if odd length, else remove middle two els.
public void removeMiddle()
{
if(listValues.size()%2 == 1)
{  
listValues.remove(listValues.size()/2);
}
else
{
listValues.remove(listValues.size()/2);
//Remove the only left middle element
listValues.remove(listValues.size()/2);
}
}
//part f - move all evens to front
public void moveEvensToFront()
{
      
}
  
   //part g - return second largest el in array
   public int ret2ndLargest() {
       return 0; //dummy value
   }
  
   //part H - returns true if array is sorted in increasing order
   public boolean isSorted() {
       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 boolean hasAdjDuplicates() {
       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 boolean hasDuplicates() {
       boolean hasDup = false; //assume no duplicates, then search array
       return hasDup;
   }
  
   public static void main(String[] args) {
       //In your main method you should test your array methods
       //First fill an array with random values
       Random myGen = new Random();
       final int TEST_SIZE = 10;
       //our initial random test data goes into the following array list
       //don't make changes to this array, it will hold your test data
       ArrayList<Integer> listTestValues = new ArrayList<Integer>();
       //fill in test arrayList
       for (int i=0; i<TEST_SIZE; i++) {
           //fills the array List with random ints between 1 and 50 inclusive
           listTestValues.add(1+myGen.nextInt(50));
       }
      
       //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(listTestValues);
       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.
       //create a new ArrayListMethods object.
       //note the following line will also initialize our arraylist data
       ArrayListMethods myMethods = new ArrayListMethods(listTestValues);
      
       //Test methods below this line.
       //Test of swapFirstAndLast()
       System.out.println("Before call to swapFirstAndLast():");
       System.out.println(myMethods.listValues);
       //swap first and last element
       myMethods.swapFirstAndLast();
       System.out.println("After call to swapFirstAndLast():");
       System.out.println(myMethods.listValues);
       System.out.println();
       //reset the Array List to the initial test values
       myMethods.setListValues(listTestValues);
      
       //***Begin second test below this line
   }
}

Explanation / Answer

import java.util.Collections; //need this one for sorting ArrayList

import java.util.Random;
import java.util.ArrayList;
public class ArrayListMethods {
  
//ArrayList to hold values to work on for lab
private ArrayList<Integer> listValues;
//ArrayList constructor
public ArrayListMethods(ArrayList<Integer> initialValues) {
//note this shows how to make a copy of an array list
//simply pass the old list in as an argument to
//the ArrayList constructor
listValues = new ArrayList<Integer>(initialValues);
}
  
//set listValues to list passed as parm
//use this method to "reset" our ArrayList data before each test
public void setListValues(ArrayList<Integer> newValues) {
listValues = new ArrayList<Integer>(newValues);
}
  
//Accessor method to get the list values
public ArrayList<Integer> getListValues() {
return listValues;
}
  
//method to print the current values in the ArrayList
//or you can simply use println() with the array list as the
//parameter
public void printArrayList() {
System.out.println(listValues);
}
  
//part a, done for you.
public void swapFirstAndLast() {
//save first element
int temp = listValues.get(0);
//move last element to first position (0)
listValues.set(0, listValues.get(listValues.size()-1));
//put temp value into last position (size() -1)
listValues.set(listValues.size()-1, temp);
}
  
  
//part b, fill in this method
public void shiftRight()
{
int temp = listValues.get(listValues.size()-1);
//shift all elements to the right by 1except last element
for(int i=listValues.size()-1;i>0;i--){
listValues.set(i, listValues.get(i-1));
}
listValues.set(0, temp);
}
//part c, set all even elements to 0.
public void setEvensToZero()
{
for(int i=1;i<listValues.size();i+=2)
{
listValues.set(i, 0);
}
}
//part d, replace each element except the first and last by larger of two
//around it
public void largerOfAdjacents()
{
int prev,i;
//initialize the previous element as 0th element
for(i=1,prev=listValues.get(0);i<listValues.size()-1;i++){
//if previous element is greater than following element
if(prev < listValues.get(i+1)){
//update the previous as current element
prev = listValues.get(i);
//set the current element as following element
listValues.set(i,listValues.get(i+1));
}
else
{
////update the previous as current element
prev = listValues.get(i);
////set the current element as previous element
listValues.set(i,prev);
}
}
}
//part e, remove middle el if odd length, else remove middle two els.
public void removeMiddle()
{
if(listValues.size()%2 == 1)
{
listValues.remove(listValues.size()/2);
}
else
{
listValues.remove(listValues.size()/2);
//Remove the only left middle element
listValues.remove(listValues.size()/2);
}
}
//part f - move all evens to front
public void moveEvensToFront()
{
int[] arr1=new int[10];
int[] arr2=new int[10];
int m=0,n=0,j=0,k=0;
for(int i=0;i<listValues.size();i++)
{
if((listValues.get(i))%2==0)
arr1[m++]=listValues.get(i);
else
arr2[n++]=listValues.get(i);
}
int i=0;
while(j<m)
{
listValues.set(i,arr1[j]);
i++;
j++;
  
}
while(k<n)
{
listValues.set(i,arr2[k]);
i++;
k++;
}
}
  
//part g - return second largest el in array
public int ret2ndLargest() {
Collections.sort(listValues);
return listValues.get(listValues.size()-2); //dummy value
}
  
//part H - returns true if array is sorted in increasing order
public boolean isSorted() {
boolean isSorted = false; //initially assume list is not sorted
for(int i=0;i<listValues.size()-1;i++)
{
if(listValues.get(i)>listValues.get(i+1))
return isSorted;
}
return true;
}
  
//PART I - return true if array contains 2 adjacent duplicate values
public boolean hasAdjDuplicates() {
boolean hasDup = false; //assume no duplicates, then search array
for(int i=1;i<listValues.size()-1;i++)
{
if(listValues.get(i)==listValues.get(i+1)||listValues.get(i)==listValues.get(i-1))
return true;
}
return hasDup;
}
  
  
//PART J - return true if array contains 2 duplicate values
//duplicates need not be adjacent to return true
public boolean hasDuplicates() {
boolean hasDup = false; //assume no duplicates, then search array
Collections.sort(listValues);
for(int i=1;i<listValues.size()-1;i++)
{
if(listValues.get(i)==listValues.get(i+1)||listValues.get(i)==listValues.get(i-1))
return true;
}
return hasDup;
}
  
public static void main(String[] args) {
//In your main method you should test your array methods
//First fill an array with random values
Random myGen = new Random();
final int TEST_SIZE = 10;
//our initial random test data goes into the following array list
//don't make changes to this array, it will hold your test data
ArrayList<Integer> listTestValues = new ArrayList<Integer>();
//fill in test arrayList
for (int i=0; i<TEST_SIZE; i++) {
//fills the array List with random ints between 1 and 50 inclusive
listTestValues.add(1+myGen.nextInt(50));
}
  
//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(listTestValues);
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.
//create a new ArrayListMethods object.
//note the following line will also initialize our arraylist data
ArrayListMethods myMethods = new ArrayListMethods(listTestValues);
  
//Test methods below this line.
//Test of swapFirstAndLast()
System.out.println("Before call to swapFirstAndLast():");
System.out.println(myMethods.listValues);
//swap first and last element
myMethods.swapFirstAndLast();
System.out.println("After call to swapFirstAndLast():");
System.out.println(myMethods.listValues);
System.out.println();
//method b testing
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println("Array List:");
System.out.println(myMethods.listValues);
System.out.println(" Array after shifting right:");
myMethods.shiftRight();
System.out.println(myMethods.listValues);
System.out.println();   
//method for testing c
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println("Array List:");
System.out.println(myMethods.listValues);
System.out.println(" Array with even elements set as 0:");
myMethods.setEvensToZero();
System.out.println(myMethods.listValues);
System.out.println();
  
//method for testing d
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println(" Array List:");
System.out.println(myMethods.listValues);   
myMethods.largerOfAdjacents();
System.out.println("After larger of the Adjacents is taken:");
System.out.println(myMethods.listValues);
  
  
//method for testing e
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println(" Array List:");
System.out.println(myMethods.listValues);
myMethods.removeMiddle();
System.out.println("After removing middle:");
System.out.println(myMethods.listValues);
System.out.println();
  
//method for testing f
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println(" Array List:");
System.out.println(myMethods.listValues);
myMethods.moveEvensToFront();
System.out.println("After removing middle:");
System.out.println(myMethods.listValues);

  
  
//method for testing g
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println(" Array List:");
System.out.println(myMethods.listValues);
  
System.out.println("Second largest value: "+myMethods.ret2ndLargest());
  
//method for testing h
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println(" Array List:");
System.out.println(myMethods.listValues);

System.out.println("Array is Sorted: "+myMethods.isSorted());
  
//method for testing i
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println(" Array List:");
System.out.println(myMethods.listValues);

System.out.println("Array has adjacent Duplicates: "+myMethods.hasAdjDuplicates());
  
//method for testing j
//reset the Array List to the initial test values
myMethods.setListValues(listTestValues);
System.out.println(" Array List:");
System.out.println(myMethods.listValues);
System.out.println("Array has Duplicates: "+myMethods.hasDuplicates());
//***Begin second test below this line
}
}