Question
JAVA- Edit the following code to implement the following- 1. Add a private attribute size for the capacity of the array class nElems is for
JAVA- Edit the following code to implement the following-
1. Add a private attribute size for the capacity of the array class
nElems is for the number of elements in the current array
size is for the capacity of the array class
2. Change the insert class so that it can check for the capacity before an element is inserted. If the array class is full, print the "Array is full" message
3. Change the name of the display() method to toString(), and in the driver class, the print method to print an array object, rather than calling the display() method
4. The output is MyArray Found 44 success success Can't find 44 MyArray
5. You want to display the array elements rather than "MyArray"
The output should be Array Capacity: 10 Number of Elements: 6 Array Values: [77, 99, 44, 55, 22, 88]
-----------------------------------------------------------
class HighArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
public HighArray(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
public boolean find(long searchKey) // find specified value
{
int j;
for (j=0; j if (a[j] == searchKey) // found item? break; // exit loop before end if (j == nElems) // gone to end? return false; // yes, can't find it else return true; // no, found it } // end find() public void insert(long value) // put element into array { a[nElems] = value; // insert it nElems++; // increment size } public boolean delete(long value) { int j; for (j=0; j if (value == a[j]) break; if (j == nElems) // can't find it return false; else { // found it for (int k=j; k a[k] = a[k+1]; nElems--; // decrement size return true; } } public String toString() // displays array contents { return "MyArray"; } } // end class HighArray // Driver class public class Lab1 { public static void main(String[] args) { int maxSize = 10; // array size HighArray arr; // reference to array arr = new HighArray(maxSize); // create the array arr.insert(77); // insert 6 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); System.out.println(arr); int searchKey = 44; if (arr.find(searchKey)) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); if (arr.delete(55)) System.out.println("success"); if (arr.delete(44)) System.out.println("success"); if (arr.delete(98)) System.out.println("success"); if (arr.find(searchKey)) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); System.out.println(arr); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started