Please write in JAVA ONLY 1 Write an object class with just
Please write in JAVA ONLY!
1. Write an object class with just one instance variable, an integer array. Your constructor should take an integer array as its only parameter.
2. Write all the accessors, mutators, toString and equals methods.
3. Write an additional RECURSIVE method that returns the sum of all the elements in the array (your method should have an array as it\'s argument and should have the recursive call in its return for the general case).
4. Write a client class to test all your methods and especially your recursive method.
Solution
ArrayElements.java
import java.util.Arrays;
public class ArrayElements {
//Declaring private variables
private Integer arr[];
//Parameterized constructor
public ArrayElements(Integer[] arr) {
super();
this.arr = arr;
}
//Setters and getters
public Integer[] getArr() {
return arr;
}
public void setArr(Integer[] arr) {
this.arr = arr;
}
@Override
public String toString() {
return \"Array Elements are :\ \" + Arrays.toString(arr);
}
//This equals() method compare two arrays
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ArrayElements other = (ArrayElements) obj;
if (!Arrays.equals(arr, other.arr))
return false;
return true;
}
//This method will add the elements in the array recursively
int addElements(Integer arr[], int size) {
if (size < 0) {
return 0;
} else {
return (arr[size] + addElements(arr, size - 1));
}
}
}
______________________________________
TestClass.java
public class TestClass {
public static void main(String[] args) {
//Creating 3 arrays
Integer arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16, 17, 18, 19, 20 };
Integer arr2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16, 17, 18, 19, 20 };
Integer arr3[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 100 };
//Creating ArrayElements class Object by passing the array as parameter
ArrayElements ae1 = new ArrayElements(arr1);
System.out.println(\"________Displaying array1 Elements________\");
System.out.println(ae1.toString());
/* Calling the method addElements()
* by passing the array and its size as parameters
*/
int sum = ae1.addElements(arr1, arr1.length - 1);
System.out.println(\"Sum of the array1 Elements Using recursion is :\"+ sum);
//Creating ArrayElements class Object by passing the array as parameter
ArrayElements ae2 = new ArrayElements(arr2);
System.out.println(\"\ ________Comparing array1 and array2________\");
if (ae1.equals(ae2))
System.out.println(\":: Two arrays are equal ::\");
else
System.out.println(\":: Two arrays are not equal ::\");
//Creating ArrayElements class Object by passing the array as parameter
ArrayElements ae3 = new ArrayElements(arr3);
System.out.println(\"\ ________Displaying array3 Elements________\");
System.out.println(ae1.toString());
/* Calling the method addElements()
* by passing the array and its size as parameters
*/
int sum1 = ae3.addElements(arr3, arr3.length - 1);
System.out.println(\"Sum of the array3 Elements Using recursion is :\"+ sum1);
System.out.println(\"\ ________Comparing array1 and array3________\");
if (ae1.equals(ae3))
System.out.println(\":: Two arrays are equal ::\");
else
System.out.println(\":: Two arrays are not equal ::\");
}
}
_________________________________
output:
________Displaying array1 Elements________
Array Elements are :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Sum of the array1 Elements Using recursion is :210
________Comparing array1 and array2________
:: Two arrays are equal ::
________Displaying array3 Elements________
Array Elements are :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Sum of the array3 Elements Using recursion is :595
________Comparing array1 and array3________
:: Two arrays are not equal ::
_______________________Thank You


