Question

Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

  1. Copy the following java codes and compile

//// HighArray.java

//// HighArrayApp.java

Study carefully the design and implementation HighArray class and note the attributes and its methods.

  

  1. Create findAll method which uses linear search algorithm to return all number of occurrences of specified element.

/**
* find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit.
*
* @param foundElement   Element to be found
*/
int findAll(int foundElement)

  1. Modify the HighArray class to create a deleteAll method, which deletes a specified element from the array (if it exists, else return 0). Moving subsequent elements down one place in the array to fill the space vacated by the deleted element. The method should delete all the element occurrences.

    /**
    * Delete all element occurrences from array if found otherwise do nothing.
    *
    * @param deletedElement   Element to delete
    */
    int deleteAll(int deletedElement)
  1. Modify HighArray class to create the following methods

long max()

Returns the maximum value in a

int maxIndex()

Returns the index of the maximum value

long min()

Returns the minimum value in a

int minIndex()

Returns the index of the minimum value

long range()

Calculates the range (max-min)

long sum()

Calculates the sum of elements of a

double avg()

Calculates the average of a

double std()

Calculates the standard deviation of a

long rank(int i)

Return the ith largest element of a

boolean checkOrdered()

Returns true if a is ordered otherwise returns false

boolean checkUnique()

Returns true if a has unique elements

void removeDuplicates()

Removes duplicates from a

void fillArrayRandom()

Fills a with Random numbers

  

  1. Modify the binary search algorithm implemented in the search method of the HighArray class, to print a trace showing for each iteration of the search, the upper and lower limits of the search and the middle item selected in the binary split.

    The output might look something like this:

    Pass 1 left=0 right=9 pivot=4
    Pass 2 left=0 right =3 pivot =1
    Pass 3 left=2 right =3 pivot =2
  1. Modify the HighArray class to implement the method specified below, which reads integer data from a file to fill the array. The first integer read from the file should indicate the number of integers contained in the file which can be read into the array.



/**
* Read integers into array from named file. First element in
* file is a count of number of integers contained in the file.
*
* @param fileName   name of file containing integer data
*/
void readIntFile(String fileName)

Note: Use the void insert(int value) method defined in the class to add each integer to the array as it is loaded from the file.

Here is a sample code to read data from file

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class FileReaderSample {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner in = new Scanner(System.in);

        System.out.println("Please enter file name");

      String s = in.nextLine();

        Scanner input = new Scanner(new File(s));

        int[] arr = new int[100];

        int i = 0;

        while (input.hasNextInt()) {

            int next = input.nextInt();

            arr[i] = next;

            i++;

        }// end while

        int nElems = i;

// Print the array contents

       System.out.println("Array Contents");

        for (i = 0; i < nElems; i++) {

            System.out.println(i + "\t" + arr[i]);

        }// end for

    }// end main

}// end class

  1. Compare the performance between linearSearch, BinarySearch, InsertOrdered and InsertNotOrdered using System.nanoTime();

Fill the following table

         

Method

N= 100

N = 1000

N = 100000

N=1000000

InsertOrdered

Insert

LinearSearch(find method in HighArray)

BinarySearch

  1. Create HiArray<T> class to store generic values,
  2. Modify HighArray class and implement bubble sort, insertion sort and selection sort
  3. Modify HighArray<T> class to implement bubble sort, insertion sort and selection sort

-------------------------------------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// highArray.java
// demonstrates array class with high-level interface
// to run this program: C>java HighArrayApp
////////////////////////////////////////////////////////////////
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<nElems; j++)            // for each element,
         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 void insertOrdered(long value)    // put element into array
      {
      int j;
      for(j=0; j<nElems; j++)        // find where it goes
         if(a[j] > value)            // (linear search)
            break;
      for(int k=nElems; k>j; k--)    // move bigger ones up
         a[k] = a[k-1];
      a[j] = value;                  // insert it
      nElems++;                      // increment size
      }  // end insert()
   //-----------------------------------------------------------
   public boolean delete(long value)
      {
      int j;
      for(j=0; j<nElems; j++)        // look for it
         if( value == a[j] )
            break;
      if(j==nElems)                  // can't find it
         return false;
      else                           // found it
         {
         for(int k=j; k<nElems; k++) // move higher ones down
            a[k] = a[k+1];
         nElems--;                   // decrement size
         return true;
         }
      }  // end delete()
   //-----------------------------------------------------------
   public void display()             // displays array contents
      {
      for(int j=0; j<nElems; j++)       // for each element,
         System.out.print(a[j] + " ");  // display it
      System.out.println("");
      }
   //-----------------------------------------------------------
public int binarySearch(int searchKey) {
  int left = 0, right = nElems-1;
   
  while (left <= right) {
      int pivot = (left + right) / 2;
      if (a[pivot] == searchKey) {
          return pivot;       // found
      } else {
          if (a[pivot] < searchKey)
              left = pivot + 1;  // search right
          else 
        right = pivot - 1;  // search left
      }
   }
   return -1;
} 
   
   public int size() {
     return nElems;
 }

}  // end class HighArray


////////////////////////////////////////////////////////////////

--------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class HighArrayApp
   {
   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.display();                // display items

      int searchKey = 35;           // search for item
      if( arr.find(searchKey) )
         System.out.println("Found " + searchKey);
      else
         System.out.println("Can't find " + searchKey);

      arr.delete(00);               // delete 3 items
      arr.delete(55);
      arr.delete(99);

      arr.display();                // display items again
      }  // end main()
   }  // end class HighArrayApp

------------------------------------------------------------------

I need the answer of 9 and 10

0 0
Add a comment Improve this question Transcribed image text
Answer #1

output:

below number of occurances of 55 is 3 and results also displaying after delteing all the occurances of 55

77 99 44 55 22 88 55 11 0 66 55 33
Can't find 35
number of occurances of 55 is 3
77 44 22 88 11 66 33

HighArray.java

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<nElems; j++) // for each element,
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 int findAll(long searchKey)
{ // find specified value
   int n=0;
int j;
for(j=0; j<nElems; j++) // for each element,
if(a[j] == searchKey) // found item?
n++; // exit loop before end
return n; // no, found it
} // end find()
//-----------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//-----------------------------------------------------------
public void insertOrdered(long value) // put element into array
{
int j;
for(j=0; j<nElems; j++) // find where it goes
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//-----------------------------------------------------------
public boolean delete(long value)
{
int j;
for(j=0; j<nElems; j++) // look for it
if( value == a[j] )
break;
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//-----------------------------------------------------------
public boolean deleteAll(long value)
{
   int i;
   for(i=0; i<nElems; i++){
   int j;
   for(j=0; j<nElems; j++) // look for it
   if( value == a[j] )
   break;
   if(j==nElems) // can't find it
   return false;
   else // found it
   {
   for(int k=j; k<nElems; k++) // move higher ones down
   a[k] = a[k+1];
   nElems--; // decrement size
   }
       }
   return true;
} // end delete()
//-----------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//-----------------------------------------------------------
public int binarySearch(int searchKey) {
int left = 0, right = nElems-1;

while (left <= right) {
int pivot = (left + right) / 2;
if (a[pivot] == searchKey) {
return pivot; // found
} else {
if (a[pivot] < searchKey)
left = pivot + 1; // search right
else
right = pivot - 1; // search left
}
}
return -1;
}

public int size() {
return nElems;
}

} // end class HighArray

HighArrayApp.java

class HighArrayApp
{
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(55);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(55);
arr.insert(33);

arr.display(); // display items

int searchKey = 35; // search for item
if( arr.find(searchKey) )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
  
searchKey = 55; // search for item
System.out.println("number of occurances of " + searchKey + " is "+arr.findAll(searchKey));

arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.deleteAll(55);

arr.display(); // display items again
} // end main()
} // end class HighArrayApp

Add a comment
Know the answer?
Add Answer to:
Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • To the HighArray class in the highArray.java, add a method called getMax() that returns the value...

    To the HighArray class in the highArray.java, add a method called getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. // highArray.java class HighArray { private long[] a; private int nElems;    public HighArray(int max)    { a = new long[max];    nElems = 0; } public boolean find(long searchKey) { int...

  • Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This...

    Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; //...

    // ArrayIns.java // demonstrates insertion sort 11--- class ArrayIns private long[] a; private int nElems; // ref to array a // number of data items public ArrayIns(int max) // constructor a = new long[max]; nElems - © // create the array // no items yet --- public void insert(long value) // put element into array a[nElems] = value; nElems++; // insert it // increment size public void display() // displays array contents for(int j=0; j<ntlems; 1++) 1/ for each element,...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • pls help, idk whats wrong with this Add the reverse() method which reverses the content of...

    pls help, idk whats wrong with this Add the reverse() method which reverses the content of array without using additional array and the rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT