In: Computer Science
Hi,
Hope yoou are doing fiine. I have coded the above question in java keepping all the given conditions in mind. The program has been clearly explained using comments that have been highlighted in bold. I have additionallly written tthe main method for checking the method and displayed some sample results.
Program:
public class myArray {
   //arr is attribute of class myArray which is
an array of type float
   float [] arr;
  
   //constructor of myArray
   public myArray(float[] arr) {
       this.arr = arr;
   }
  
   //copy constructor
   public myArray(myArray b)
   {
       System.out.println("Copy
constructor is called");
       //copying contents of
current object to the passed object
       arr=b.arr;
   }
  
   //operator takes in an integer and returns
array element at that position if valid
   public float operator(int index)
   {
       //if index is
valid
       if(index>=0 &&
index<arr.length)
           return
arr[index];
       return -1;
   }
  
   //method to check if two objects are
equal
   public boolean notEqual(myArray b)
   {
       if(b.arr==this.arr)
           return
false;
       return true;
   }
  
   //another operator method thaat takes in a
float value and subtracts it from each element of
arr
   public void operator(float num)
   {
       for(int
i=0;i<this.arr.length;i++)
       {
          
this.arr[i]-=num;
       }
   }
  
   //Main method to check all the above
methods
   public static void main(String[] args) {
       // TODO Auto-generated method
stub
       //declaring a float type
array arr
       float [] arr= {1,2,8,9,6};
       //creating object a by
passing arr as argument
       myArray a=new myArray(arr);
       //checking operator
method
       float index=a.operator(3);
       if(index==-1)
          
System.out.println("Not valid index");
       else
          
System.out.println("Element at given index is: "+index);
       //declaring another array
arr2
       float [] arr2= {100,101,193};
       //creating object b by
passing arr2 as argument
       myArray b=new myArray(arr2);
       //checking notEqual
method
       boolean res=a.notEqual(b);
       if(res)
          
System.out.println("The objects are not equal");
       else
          
System.out.println("The objects are equal");
       //Printing the array of
object a before performing operator() method
       System.out.println("Contents of
array before operator():");
       for(int
i=0;i<a.arr.length;i++)
          
System.out.println(a.arr[i]);
       System.out.println();
       //declaring f as a float
number that is to be subtracted
       float f=1;
       //checking operator method.
Here overloading occurs as there are two operator
methods
       a.operator(f);
       System.out.println("Contents of
array after operator():");
       for(int
i=0;i<a.arr.length;i++)
          
System.out.println(a.arr[i]);
       //declaring new object c
and calling the copy constructor to construct it
       myArray c=new myArray(a);
       System.out.println("Contents of the
copied object c:");
       //Printing contents of
c
       for(int
i=0;i<c.arr.length;i++)
          
System.out.println(c.arr[i]);
}
}
Executable code snippet:


Output:
