Question

In: Computer Science

Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...

  1. Implement the following methods. Assume that these will be methods within your myArray class.
    1. operator[]
      1. Should take in an int and return arr at that index if it is a valid index
    2. notEqual
      1. The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal.
    3. operator-(float)
      1. Will subtract the float value that is passed in from each of the values in arr
    4. Copy constructor
      1. Will take in another myArray object and construct an object that is a copy of it.

Solutions

Expert Solution

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:


Related Solutions

Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Language: Python Implement the Book class as demonstrated below. You should not change the Book methods...
Language: Python Implement the Book class as demonstrated below. You should not change the Book methods and implementation as provided. The docstrings in the template contain descriptions of each method. >>> b=Book ( ' Code' , 'Charles Ptzold' 'Computing' , 2001) >>> b Book ( ' Code' , 'Charles Ptzold' 'Computing' , 2001) >>> str (b) ' Code : Charles Ptzold: Computing: 2001 ' >>> b. getTitle ( ) ' Code ' >>> b. getAuthor() ' Charles Ptzold' >>> b....
For this Lab you have to implement a class Builder. Your Builder class should have instance...
For this Lab you have to implement a class Builder. Your Builder class should have instance variable name. , Supply a constructor method for your Builder class and the following methods: getName(), makeRow(int n, String s), printPyramid(int n, String s). Examining the problem, we need to create a Builder class, declare Builder class as follows public class Builder { } Inside the Builder class, declare a String variable called name. Step 3: Defining the constructors: Remember that the purpose of...
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT