In: Computer Science
JAVA Practice Exam 2
The project practice_exam_2 contains three (3) classes:
Testing – This is the file you will use to execute your program and test your cases. Each section refers to
one or more specific array(s). From here, you can run the whole program or one section at a time.
TestCases – This file contains all test cases. NO NEED FOR YOU TO PAY ATTENTION TO IT.
YourCode – This is where you will be writing your code.
Implement the body of the seven (7) methods that are declared in the class YourCode as follows:
Method section_1
Given an array of integers, print all the elements in the array in reverse order.
Consider the case when the array is empty and output the message, “The array is empty.”
Method section_2
Given an array of integers, overwrite all the odd numbers in the array with 0.
Do NOT print the array; the testing section will print the array.
Method section_3
Given an array of integers, find the index of the smallest element in the array and print it out on
the screen.
Do NOT print the array; the testing section will print the array.
Method section_4
Given an array of integers, count all elements in the array that are multiples of 3, but do NOT
count number 3 itself, and print out the total count.
Do NOT print the array; the testing section will print the array.
Method section_5
Given an array of integers, sum up every other element in the array starting at index 0, and print
out the result.
Use a while loop.
Do NOT print the array; the testing section will print the array.
Method section_6
Given a two arrays (a and b) with equal length and that have at least one element, for every
element in array a, consider the corresponding element in array b (at the same index) and add
the element from array b to the element in array a (modifying the elements in array a).
Example:
a is: 4 2 6 5
b is: 2 3 7 4
Resulting state of a is: 6 5 13 9 (b remains the same)
Do NOT print the array; the testing section will print the array.
Method section_7
Create a new array of integers with length equal to the length of the given array, and copy every
other element of the given array into the new array, starting at index 0.
Use two (2) for loops: The first will copy the array and the second will print it.
Consider the case when the given array is empty and output the message, “The array to copy
from is empty.”
Print out the new array.
package april_2;
public class YourCode {
//method to print array in reverse
public static void section_1(int arr[])
{
if(arr.length == 0)
{
System.out.println("The array is empty.");
return;
}
System.out.println("Array is
reverse order: ");
for(int i = arr.length-1 ; i >=
0 ; i--)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
//method to replace odd elements with 0.
public static void section_2(int arr[])
{
for(int i = 0 ; i < arr.length ;
i++)
{
if(arr[i] % 2 !=
0)
{
arr[i] = 0;
}
}
}
//method to findthe index of the smallest element in
the array and print it out on
public static void section_3(int arr[])
{
int ind = 0;
int min = arr[0];
for(int i = 1 ; i < arr.length ;
i ++)
{
if(arr[i] <
min)
{
min = arr[i];
ind = i;
}
}
System.out.println("Minimum is: " +
min + " and is found at index: " + ind);
}
//count all elements in the array that are multiples
of 3, but do NOT count number 3
public static void section_4(int arr[])
{
int count = 0;
for(int i = 0 ; i < arr.length ;
i++)
{
if(arr[i] % 3 ==
0 && arr[i] != 3)
count++;
}
System.out.println("There are "+
count +" multiples of 3.");
}
//sum up every other element in the array starting at
index 0, and print
public static void section_5(int arr[])
{
int sum = 0;
int i = 0;
while(i < arr.length)
{
sum +=
arr[i];
i++;
}
System.out.println("Sum of every
other element of the array is: " + sum);
}
//add the element from array b to the element in array
a
public static void section_6(int a[], int b[])
{
for(int i = 0 ; i < a.length ;
i++)
{
a[i] = a[i] +
b[i];
}
}
//copy every other element of the given array into the
new array, starting at index 0.
public static void section_7(int a[])
{
if(a.length == 0)
{
System.out.println("The array to copy from is empty.");
return;
}
int[] b = new int[a.length];
for(int i = 0 ; i < a.length ;
i++)
{
b[i] =
a[i];
}
System.out.println("Printing the
new array.");
section_1(b);
}
}