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

A. ArrayListTest Topics: ArrayLists This lab will help you practice using ArrayL

ID: 3683547 • Letter: A

Question

A. ArrayListTest Topics: ArrayLists This lab will help you practice using ArrayList methods (such as get, set, add, and remove) using an Integer ArrayList named iArray. Each time we test a method you have defined, we will empty (clear) the previous values from the ArrayList and repopulate the ArrayList with random Integer values. Open the ArrayListTest file and examine the following inside

: • A constant defined named TEST_SIZE: each time we repopulate the ArrayList, we will populate it with 12 values, therefore this constant was created.

• A static ArrayList named iArray: because we are in an application class, no objects will be created from this class. Therefore any data fields created are linked to the class itself, which is why this defined as static. This ArrayList will be used throughout the program.

• printArray method: this method accepts a String parameter. In the main method, each time we repopulate the ArrayList, we will call this method to print a message indicating what method we are about to test and to print all of the values in the ArrayList. Because this method is not modifying the ArrayList, an enhanced for loop (for each loop) has been used for printing.

• populateArray method: this method takes 2 integer parameters. Inside of the method the ArrayList is cleared so that all previous Integers inside of iArray are deleted. A for loop executes TEST_SIZE amount of times, populating the ArrayList with new random numbers starting at offset (inclusive) and going through offset + range (exclusive). Because the ArrayList is being modified (values are being added), a traditional for loop must be used.

• Next there are several methods that you must define. Each method is doing something with the contents of iArray. Use the comments above each method to complete the method definition. Sample values as well as hints will be shown above each method.

• The main method is completed for you. Inside of the main method, each time a method is tested, the populateArray method is called to put random numbers into iArray, the printArray method is called to print the contents of iArray, the method being tested is called, and then printArray is called again to show the contents of the iArray after the tested method has executed.

import java.util.ArrayList;

/*
Sample output:
new(0-19): : 14, 12, 4, 18, 3, 2, 0, 19, 3, 14, 14, 7,
cnt odds :8
new(1-15): : 11, 7, 14, 10, 8, 8, 10, 9, 8, 2, 13, 10,
rmv odds : : 14, 10, 8, 8, 10, 8, 2, 10,
new(1-5) : : 1, 1, 3, 4, 3, 5, 4, 2, 5, 4, 2, 2,
rmv consec: : 1, 1, 3, 5, 4, 2, 5, 4, 2, 2,
new(1-2): : 2, 1, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1,
no dups : : 2, 1, 2, 1, 0, 2, 1, 0, 2, 1, 0, 1,
new(1-1): : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
no dups : : 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
new(1-2): : 2, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1,
make123 : : 2, 1, 1, 2, 3, 2, 1, 1, 1, 2, 3, 1, 1, 1,
*/
public class ArrayListTest
{
final static int TEST_SIZE = 12;
static ArrayList<Integer> iArray = new ArrayList<Integer>();


/*
* printArray - display the contents of the ArrayList<Integer> iArray
*/
public static void printArray (String msg)
{
System.out.print(msg + ": ");
for(Integer k : iArray)
System.out.print(k + ", ");
System.out.println(" ");
}

/*
* populateArray
* Clear any values already inside of iArray.
* Then repopulate iArray with random numbers within the specified offset and range.
*/
public static void populateArray(int range, int offset)
{

iArray.clear();
for (int k=0; k<TEST_SIZE; k++)
{
int temp = (int)(Math.random()*range + offset);
iArray.add(temp);
}

}

/*
* countEvens
* Count all elements in iArray with even values.
*
* countEvens({1, 6, 2, 3, 5, 4, 2}) => 4
* - use get(index)
*/
public static int countEvens()
{
for(int x=0 ; x<=TEST_SIZE; x++)
{
   int count = x+2;
}
  
return count;
}


/*
* removeOdds
* Remove all elements in iArray with odd values.
* Note: when removing elements within a for loop, be careful with the index.
* hint: traverse the array backwards.
*
* removeOdds({1, 6, 2, 3, 5, 4, 2}) => {6, 2, 4, 2}
* - use remove(index)
*/
public static void removeOdds()
{
  
}


/*
* noDuplicateValues
* Look for elements in iArray with duplicate values (2 of the same value in a row).
* If found, modify the 2nd element by subtracting 5 if it's equal to 2
* or adding 5 if it's equal to 1.
*
* noDuplicateValues({2, 1, 2, 1, 1, 1, 2, 1, 2, 2, 2, 2}) => {2, 1, 2, 1, 6, 1, 2, 1, 2, -3, 2, -3}
* - use set(index, element)
*/
public static void noDuplicateValues()
{
  
  
}


/*
* make123
* Look for a pattern of 1,2 in iArray.
* If found, insert the number 3 into the next element
*
* make123({1, 2, 1, 1, 2, 2}) => {1, 2, 3, 1, 1, 2, 3, 2}
* - use add(index, element)
*/
public static void make123()
{


}


/*
* removeConsecutiveValue
* Look for elements with consecutive values in iArray.
* If found, remove both elements
*
* removeConsecutiveValue({1, 2, 8, 5, 6, 9}) => {8, 9}
* - use remove(index)
*/
public static void removeConsecutiveValue()
{

  
}


//Main method to test above methods
public static void main(String[] args)
{
  
//Count Evens
populateArray(20, 0);
printArray("new(0-19): ");
int cntEvens = countEvens();
System.out.println("cnt evens :" + cntEvens);
System.out.println();
//Remove Odds
populateArray(15, 1);
printArray("new(1-15): ");
removeOdds();
printArray("rmv odds : ");
System.out.println();
//No duplicate values with number range 1-2
populateArray(2, 1);
printArray("new(1-2): ");
noDuplicateValues();
printArray("no dups : ");
System.out.println();
//No duplicate values again with number 1 only
populateArray(1, 1);
printArray("new(1-1): ");
noDuplicateValues();
printArray("no dups : ");
System.out.println();
//Make 123
populateArray(2, 1);
printArray("new(1-2): ");
make123();
printArray("make123 : ");
System.out.println();
//Remove consecutive values
populateArray(5, 1);
printArray("new(1-5) : ");
removeConsecutiveValue();
printArray("rmv consec: ");

}
}

Explanation / Answer

Hello there ,

I have modified the program as per your demand. Kindly find the code below.


import java.util.ArrayList;


/*
Sample output:
new(0-19): : 14, 12, 4, 18, 3, 2, 0, 19, 3, 14, 14, 7,
cnt odds :8
new(1-15): : 11, 7, 14, 10, 8, 8, 10, 9, 8, 2, 13, 10,
rmv odds : : 14, 10, 8, 8, 10, 8, 2, 10,
new(1-5) : : 1, 1, 3, 4, 3, 5, 4, 2, 5, 4, 2, 2,
rmv consec: : 1, 1, 3, 5, 4, 2, 5, 4, 2, 2,
new(1-2): : 2, 1, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1,
no dups : : 2, 1, 2, 1, 0, 2, 1, 0, 2, 1, 0, 1,
new(1-1): : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
no dups : : 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
new(1-2): : 2, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 1,
make123 : : 2, 1, 1, 2, 3, 2, 1, 1, 1, 2, 3, 1, 1, 1,
*/
public class ArrayListTest {

final static int TEST_SIZE = 12;
static ArrayList<Integer> iArray = new ArrayList<Integer>();

/*
* printArray - display the contents of the ArrayList<Integer> iArray
*/
public static void printArray(String msg) {
System.out.print(msg + ": ");
for (Integer k : iArray) {
System.out.print(k + ", ");
}
System.out.println(" ");
}

/*
* populateArray Clear any values already inside of iArray. Then repopulate
* iArray with random numbers within the specified offset and range.
*/
public static void populateArray(int range, int offset) {

iArray.clear();
for (int k = 0; k < TEST_SIZE; k++) {
int temp = (int) (Math.random() * range + offset);
iArray.add(temp);
}

}

/*
* countEvens Count all elements in iArray with even values.
*
* countEvens({1, 6, 2, 3, 5, 4, 2}) => 4 - use get(index)
*/
public static int countEvens() {
int count = 0;
for (int i = 0; i < iArray.size(); i++) {
if (iArray.get(i) % 2 == 0) {
count++;
}
}

return count;
}

/*
* removeOdds Remove all elements in iArray with odd values. Note: when
* removing elements within a for loop, be careful with the index. hint:
* traverse the array backwards.
*
* removeOdds({1, 6, 2, 3, 5, 4, 2}) => {6, 2, 4, 2} - use remove(index)
*/
public static void removeOdds() {
for (int i = iArray.size() - 1; i >= 0; i--) {
if (iArray.get(i) % 2 != 0) {
iArray.remove(i);
}
}
}

/*
* noDuplicateValues Look for elements in iArray with duplicate values (2 of
* the same value in a row). If found, modify the 2nd element by subtracting
* 5 if it's equal to 2 or adding 5 if it's equal to 1.
*
* noDuplicateValues({2, 1, 2, 1, 1, 1, 2, 1, 2, 2, 2, 2}) => {2, 1, 2, 1,
* 6, 1, 2, 1, 2, -3, 2, -3} - use set(index, element)
*/
public static void noDuplicateValues() {

// Record encountered Strings in HashSet.
ArrayList<Integer> dupList = new ArrayList<Integer>();
boolean got1Again = false;
boolean got2Again = false;
for (int index = 0; index < iArray.size(); index++) {
if (((got1Again && iArray.get(index) == 1) || (got2Again && iArray.get(index) == 2))
&& iArray.get(index - 1) == iArray.get(index)) {
dupList.add(index);
//Set flags again
if (iArray.get(index) == 1) {
got1Again = false;
} else if (iArray.get(index) == 2) {
got2Again = false;
}
} else if (iArray.get(index) == 1) {
got1Again = true;
} else if (iArray.get(index) == 2) {
got2Again = true;
}

}
for (Integer index : dupList) {
if (index != 0 && iArray.get(index) == 1) {
iArray.set(index, (iArray.get(index).intValue() + 5));
} else if (index != 0 && iArray.get(index) == 2) {
iArray.set(index, iArray.get(index).intValue() - 5);
}
}
}

/*
* make123 Look for a pattern of 1,2 in iArray. If found, insert the number
* 3 into the next element
*
* make123({1, 2, 1, 1, 2, 2}) => {1, 2, 3, 1, 1, 2, 3, 2} - use add(index,
* element)
*/
public static void make123() {
for (int index = 1; index < iArray.size(); index++) {
if (iArray.get(index - 1) == 1 && iArray.get(index) == 2) {
iArray.add(index + 1, 3);
}

}
}

/*
* removeConsecutiveValue Look for elements with consecutive values in
* iArray. If found, remove both elements
*
* removeConsecutiveValue({1, 2, 8, 5, 6, 9}) => {8, 9} - use remove(index)
*/
public static void removeConsecutiveValue() {
for (int index = 1; index < iArray.size(); index++) {
if (iArray.get(index - 1) == (iArray.get(index) - 1)) {
iArray.remove(index);
iArray.remove(index - 1);
}

}
}


// Main method to test above methods
public static void main(String[] args) {

// Count Evens
populateArray(20, 0);
printArray("new(0-19): ");
int cntEvens = countEvens();
System.out.println("cnt evens :" + cntEvens);
System.out.println();
// Remove Odds
populateArray(15, 1);
printArray("new(1-15): ");
removeOdds();
printArray("rmv odds : ");
System.out.println();
// No duplicate values with number range 1-2
populateArray(2, 1);
printArray("new(1-2): ");
noDuplicateValues();
printArray("no dups : ");
System.out.println();
// No duplicate values again with number 1 only
populateArray(1, 1);
printArray("new(1-1): ");
noDuplicateValues();
printArray("no dups : ");
System.out.println();
// Make 123
populateArray(2, 1);
printArray("new(1-2): ");
make123();
printArray("make123 : ");
System.out.println();
// Remove consecutive values
populateArray(5, 1);
printArray("new(1-5) : ");
removeConsecutiveValue();
printArray("rmv consec: ");

}
}

===O/P==

run:
new(0-19): : 6, 19, 9, 18, 16, 7, 17, 16, 14, 3, 7, 16,
cnt evens :6

new(1-15): : 4, 3, 4, 9, 4, 1, 15, 3, 15, 8, 1, 15,
rmv odds : : 4, 4, 4, 8,

new(1-2): : 2, 1, 1, 2, 1, 1, 1, 2, 2, 2, 1, 1,
no dups : : 2, 1, 6, 2, 1, 6, 1, 2, -3, 2, 1, 6,

new(1-1): : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
no dups : : 1, 6, 1, 6, 1, 6, 1, 6, 1, 6, 1, 6,

new(1-2): : 1, 1, 1, 2, 1, 1, 1, 1, 2, 2, 2, 1,
make123 : : 1, 1, 1, 2, 3, 1, 1, 1, 1, 2, 3, 2, 2, 1,

new(1-5) : : 2, 1, 4, 5, 1, 5, 5, 1, 5, 5, 4, 1,
rmv consec: : 2, 1, 1, 5, 5, 1, 5, 5, 4, 1,
BUILD SUCCESSFUL (total time: 1 second)

Let me know if you still have questions .

Thanks.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote