Question

In: Computer Science

Create a class and name it MyArray. This class must have an internal array of integers...

Create a class and name it MyArray. This class must have an internal array of integers and the consumer should specify the maximum capacity when instantiating. MyArray class must provide following functions:

1- insert: This method receives and integer and inserts into the array. For simplicity you can assume the array is large enough and never overflows.

2- display: This method displays all integers stored in the array in the same order as they are inserted (first in first out).

3- reverseDisplay: This method displays all integers stored in the array in the reverse order as they are inserted (first in last out).

4- getMax: This method returns the maximum value stored in the array.

Solutions

Expert Solution

Explanation:

Here is the class MyArray in Bold, which has the array a, elements to store the current number of elements in the array.

Then insert, display, reverseDisplay and getMax methods are defined.

In the Main function, an object of the class MyArray is created and all the functions are checked.

Code:


class MyArray
{
int a[];
int elements;
  
MyArray(int max_capacity)
{
a = new int[max_capacity];
elements = 0;
}
  
void insert(int n)
{
a[elements] = n;
elements++;
}
  
void display()
{
for(int i=0; i<elements; i++)
{
System.out.print(a[i]+" ");
}
  
System.out.println();
}
  
void reverseDisplay()
{
for(int i=elements-1; i>=0; i--)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
  
int getMax()
{
int max = a[0];
  
for(int i=0; i<elements; i++)
{
if(a[i] > max)
max = a[i];
}
  
return max;
}
  
  
}

public class Main
{
   public static void main(String[] args) {
      
       MyArray m = new MyArray(10);
      
       m.insert(3);
       m.insert(5);
       m.insert(7);
      
       m.display();
       m.reverseDisplay();
      
       System.out.println("Max: "+m.getMax());
   }
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


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...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
Array indices must be positive integers or logical values?
How can I fix this MATLAB error Index in position 2 is invalid. Array indices must be positive integers or logical values?
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT