In: Computer Science
Array of Hope! Write code for a Java class ArrayPair with the following fields and methods: ArrayPair - arr1 [] : int - arr2 [] : int Fields - length1 : int - length2 : int + ArrayPair (int arraySize) Methods + add (int arrNumber, int value) : void + remove (int arrNumber) : void + equal () : boolean + greater () : boolean + print () : void Thus, there are two arrays of integers in the class with associated lengths (number of elements in each array).
Methods: ArrayPair: Constructor, instantiates the two arrays with the given size as a parameter. Sets the private lengths equal to zero since the arrays are initially empty. The constructor should not print or read anything.
add: Adds the given value to the right end of the array specified by the arrNumber (if equal to 1, adds to the arr1, if 2 adds to arr2, otherwise prints an error message and does not add the value). add should not print anything other than the error message when appropriate.
remove: Removes the rightmost element of the array specified by arrNumber. If the array is empty, an error message should be printed. remove should not print anything other than an error message when appropriate.
equal: Returns true if the two arrays have the same number of values and the exact same values in the same order. Otherwise, equal should return false. equal should not print anything to output.
greater: greater returns true if arr1 has more elements than arr2. greater returns false if arr2 has more elements than arr1. If the two arrays have the same number of elements, compare corresponding elements in the two arrays. For the first non-equal pair of values: if the arr1 value is greater than the arr2 value, true is returned; if the arr2 value is greater than the arr1 value, false is returned. If all the values are equal, false is returned.
print: Prints the values in both of the arrays with labels.
Main Program (Java class PairApp) Program first creates an ArrayPair object. The program then repeatedly reads commands of add, remove, equal, greater, print, or quit, until the user enters quit. If the user enters: add: Prompts the user for array number and value and calls add method to add that value to the given array. remove: Prompts the user for array number and calls the remove method to remove that element from the given array. equal: Calls equal method and prints whether or not the two arrays are equal. greater: Calls greater method prints whether or the not the first array is greater than the second. print: Calls print to print the values in the arrays. Note these further requirements: • User options should be accepted as uppercase or lowercase. • An error message is printed for an illegal choice. • The program should print an ending message when stopping. There is a sample run on the next page. Your output should follow the given text and spacing. Include a header comment describing the program and comment all variables, control structures and methods in the program.
ArrayPair.java
class ArrayPair{
//private variables
private int arr1[];
private int arr2[];
private int length1;
private int length2;
//Constructor
public ArrayPair(int arraySize){
arr1=new int[arraySize]; //instanstiate array
arr2=new int[arraySize];
length1=0;
length2=0;
}
//methods
public void add(int arrNumber, int value){
if(arrNumber==1){
arr1[length1++]=value;
}
else if(arrNumber==2){
arr2[length2++]=value;
}
else{
System.out.println("Error");
}
}
public void remove(int arrNumber){
if(arrNumber==1){
if(length1==0){
System.out.println("Error, array1 is empty");
}
else{
--length1;
}
}
else if(arrNumber==2){
if(length2==0){
System.out.println("Error, array2 is empty");
}
else{
--length2;
}
}
else{
System.out.println("Error");
}
}
public boolean equal(){
if(length1!=length2){
return false;
}
else{
for(int i=0;i<length1;i++){
if(arr1[i]!=arr2[i]){
return false;
}
}
return true;
}
}
public boolean graeter(){
if(length1>length2){
return true;
}
else if(length1<length2){
return false;
}
else{
for(int i=0;i<length1;i++){
if(arr1[i]!=arr2[i]){
if(arr1[i]>arr2[i]){
return true;
}
else if(arr2[i]>arr1[i]){
return false;
}
}
}
}
return false;
}
public void print(){
for(int i=0;i<length1;i++){
System.out.print(arr1[i]+" ");
}
System.out.println();
for(int i=0;i<length2;i++){
System.out.print(arr2[i]+" ");
}
}
}
Screenshot of code
For main program sample output is required