Java
this is my first Java course and I have an assignment
before I took C programming, so I feel confused a little bit
the assignment asked me to fix the method in part 1 ( after the compile and run the file)
I compiled and run the A1Tester file
but I don't know how to fix the method
I only need to learn how to fix the method in part 1
all the files are in google docs and I have posted the link (also, there is an explanation for the assignment in pdf)
link: https://drive.google.com/drive/folders/1GgRWZ-lL9xkuGfW2Sq9BaTZxDz9Zy-rM?usp=sharing
thank you
Here is the completed code for this problem (completed ArrayOperations.java only (part 1)). Now if you run A1Tester program, the tests for all ArrayOperations class methods will pass without any errors. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// ArrayOperations.java
/*
* ArrayOperations
* DO NOT use builtin java Arrays mehthods
* A class with basic array methods to
* - print the values in an array
* - calculate the product of the values in an array
* - calculate the minimum of the values in an array
* - calculate the maximum of the values in an array
* - determine the equality to 2 arrays
* -
*
*/
public class ArrayOperations {
/*
* printArray
*
* Purpose: prints all the values in the array to the console example
* format: {1,2,3,4}
*
* Parameters: an array of integers
*
* Returns: void
*/
public static void printArray(int[] array) {
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1)
System.out.print(",");
}
System.out.println("}");
}
/*
* arrayProduct
*
* Purpose: computes the product of all values in the input array NOTE:
* product of 3 numbers n1, n2 and n3 = n1*n2*n3 NOTE: product of no numbers
* = 1
*
* Parameters: an array of integers
*
* Returns: product of all values in the array
*/
public static int arrayProduct(int[] array) {
// initializing product to 1
int product = 1;
// looping through all elements
for (int i = 0; i < array.length; i++) {
// multiplying current element with product and storing in product
product = product * array[i];
}
return product;
}
/*
* arrayMax
*
* Purpose: finds the maximum value in the input array
*
* Parameters: an array of integers
*
* Preconditions: array contains at least one element
*
* Returns: maximum value in the array
*/
public static int arrayMax(int[] a) {
int max = 0;
// looping through each element
for (int i = 0; i < a.length; i++) {
// if this is first element or if current element is bigger than
// max, updating max
if (i == 0 || a[i] > max) {
max = a[i];
}
}
return max;
}
/*
* arrayMin
*
* Purpose: finds the minimum value in the input array
*
* Parameters: an array of integers
*
* Preconditions: array contains at least one element
*
* Returns: minimum value in the array
*/
public static int arrayMin(int[] a) {
int min = 0;
for (int i = 0; i < a.length; i++) {
// if this is first element or if current element is smaller than
// min, updating max
if (i == 0 || a[i] < min) {
min = a[i];
}
}
return min;
}
/*
* arraysEqual
*
* Purpose: determines whether the two arrays are equal where equal means
* array1 and array2 are the same length and the contain the same values in
* the same order
*
* Parameters: two arrays of integers
*
* Returns: true if the are equal, false otherwise
*/
public static boolean arraysEqual(int[] a, int[] b) {
// NOTE:
// Because there are only two possible return values
// from this function, this stub actually passes some
// of the tests.
//
// However, you will not receive those marks unless you
// change this code to meet the specifications above.
//
if (a.length == b.length) {
// length is same, comparing each element
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
// mismatch, not equal
return false;
}
}
// all checks are passed, returning true
return true;
}
return false; // unequal lengths
}
/*
* shiftLeft
*
* Purpose: copies every element in the array into a new array of the same
* length with every element shifted by the left by the specified amount
*
* Parameters: an input array of integers, and the number of positions to
* shift left by
*
* Returns: int[] - the new array
*/
public static int[] shiftLeft(int[] a, int pos) {
// creating a new array and copying all elements from a
int copy[] = new int[a.length];
for (int i = 0; i < a.length; i++) {
copy[i] = a[i];
}
// if array is empty, returning it
if (copy.length == 0) {
return copy;
}
int counter = 0;
// looping for pos number of times
while (counter < pos) {
// copying first element
int first = copy[0];
// shifting all elements to the left for one time
for (int i = 0; i < copy.length - 1; i++) {
copy[i] = copy[i + 1];
}
// storing old front value to last position of the array
copy[copy.length - 1] = first;
counter++;
}
//returning copy array
return copy;
}
}
Get Answers For Free
Most questions answered within 1 hours.