Question
3.1: Write a noDups() method for the HighArray class. This method should remove all duplicates from the array. That is, if three items with the
3.1: Write a noDups() method for the HighArray class. This method should remove all duplicates from the array. That is, if three items with the key 17 appear in the array, noDups() should remove two of them. Dont worry about maintaining the order of the items. Of course, the array size will be reduced. Finally, in the answer section of your lab report, please analyze the time complexity of your method. 3.2: Write a isSorted() method for the HighArray class. This method should check if the array is sorted or not. if the array is sorted in ascending order, return 1; If the array is sorted in descending order, return -1; For all other situations (such as array is not sorted, array is empty, array has only 1 item, or all items in the array are equal), return 0. Finally, in the answer section of your lab report, please analyze the time complexity of your method. All questions can be solved in one project within one java file. Please add some code in main() function to exercise all those methods you added or revised. This is the code provided for it
import java.util.*;
public class HighArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
private int sizeofarray;
//-----------------------------------------------------------
public HighArray(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
sizeofarray=1;
}
//-----------------------------------------------------------
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 { if(nElems==sizeofarray) growSize(); a[nElems] = value; // insert it nElems++; // increment size } //----------------------------------------------------------- // growSize() method to increase the size of Array public void growSize() { //declares a temp[] array long temp[] = null; if (nElems == sizeofarray) { //initialize a double size array of array temp = new long[sizeofarray * 2]; { for (int i = 0; i < sizeofarray; i++) { //copies all the elements of the old array temp[i] = a[i]; } } } a = temp; sizeofarray= sizeofarray * 2; } //end growSize() //----------------------------------------------------------- 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 if(find(value)==true) { for(j=0;j { if(value==a[j]) break; } for(int k=j; k a[k] = a[k+1]; nElems--; // decrement size } return true; } // end delete() //----------------------------------------------------------- public void display() // displays array contents { for(int j=0; j System.out.print(a[j] + " "); // display it System.out.println(""); } //------------------------------------------------------------ // Method to find minimum from array public long min() { if(nElems==0) return -1; int b; long minm=a[0]; for(int i=0;i { if(a[i] minm=a[i]; } return minm; } //end min() //----------------------------------------------------------- //searchKey() method public int searchKey(long searchKey) { // find specified value int j; for(j=0; j if(a[j] == searchKey) // found item? break; // exit loop before end return j; // no, found it } // end searchKey() //----------------------------------------------------------- } // end class HighArray //////////////////////////////////////////////////////////////// class HighArrayApp //change the name { static Scanner sc =new Scanner(System.in); public static void main(String[] args) { int maxSize = 100; // array size HighArray arr; // reference to array arr = new HighArray(maxSize); // create the array arr.insert(77); // insert 10 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33); arr.insert(-23); arr.display(); // display items int searchKey = 25; // search for item if( arr.find(searchKey) ) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); System.out.println("Min value " + arr.min()); arr.display(); // display items again System.out.println("Enter number to search"); long number =sc.nextLong(); if(arr.find(number)==true) System.out.println("It is found at " +(arr.searchKey(number)+1) +" Position"); else System.out.println(number +" is not in array"); } // end main() }
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