Question

In: Computer Science

2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns...

2.1) To the HighArray class in the highArray.java program, add a method called getMax() that returns the value of the highest key in the array, or a -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers.

2.2) Modify the method in Programming project 2.1 so that the item with the highest key is not only returned by the method but also removed from the array. Call the method removeMax().

// 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 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("");

}

//-----------------------------------------------------------

} // end class HighArray

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

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

Solutions

Expert Solution

// 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 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 long getMax() {
        if (nElems == 0) {
            return -1;
        } else {
            long max = a[0];
            for (int i = 0; i < nElems; i++) {
                if (a[i] > max) {
                    max = a[i];
                }
            }
            return max;
        }
    }
//-----------------------------------------------------------
} // end class HighArray

////////////////////////////////////////////////////////////////
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

        System.out.println("Max in array is " + arr.getMax());
    } // end main()
} // end class HighArrayApp

// 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 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 long getMax() {
        if (nElems == 0) {
            return -1;
        } else {
            long max = a[0];
            for (int i = 0; i < nElems; i++) {
                if (a[i] > max) {
                    max = a[i];
                }
            }
            return max;
        }
    }

    public long removeMax() {
        if (nElems == 0) {
            return -1;
        } else {
            int maxIndex = 0;
            for (int i = 0; i < nElems; i++) {
                if (a[i] > a[maxIndex]) {
                    maxIndex = i;
                }
            }
            long result = a[maxIndex];
            for (int i = maxIndex; i < nElems - 1; i++) {
                a[i] = a[i + 1];
            }
            nElems--;
            return result;
        }
    }
//-----------------------------------------------------------
} // end class HighArray

////////////////////////////////////////////////////////////////
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

        System.out.println("Max in array is " + arr.removeMax());
        System.out.println("Next Max in array is " + arr.removeMax());
    } // end main()
} // end class HighArrayApp

Related Solutions

Given the LinkedStack Class as covered in class add a method to the LinkedStack class called...
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
Add a clone method to the BST program. This method returns a deep copy of a...
Add a clone method to the BST program. This method returns a deep copy of a tree. Coding Language C++ Use this main method to test your code: int main() { Tree T; T.insert(50); T.insert(20); T.insert(80); T.insert(60); T.insert(90); T.display(); Tree T1 = T; Tree T2 = T.clone(); cout << "\n\nT1 and T2 before insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); T.insert(999); cout << "\n\nT1 and T2 after insert(999)\n=====================\n\n"; T1.inOrder(); T2.inOrder(); system("pause"); // may need to remove this on some systems return 0; } #include...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns...
Add a method to OurQueue class that dequeues the Nth item on a queue and returns it. It must remove the Nth item from the Queue but leave the rest of the queue. Test it with the following code: Console.WriteLine("Testing DequeueNth"); OurQueue<string> ourQ = new OurQueue<string>(12); // Empty Q try { ourQ.DequeueNth(0); Console.WriteLine("\a Error on empty list"); } catch { Console.WriteLine("Empty Queue worked"); } for (int i = 0; i < 9; ++i) ourQ.Enqueue("a" + i); for (int i =...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount for the purchase of items. DiscountPolicy knows the name of the item and its cost as well as the number of items being purchased. - Class BulkDiscount, derived from DiscountPolicy, has two fields, minimum and percentage. computeDiscount method will return the discount based on the percentage applied, if the quantity of items purchased is more than minimum. For example: if minimum is 3 and...
Create a project called rise. Add a class called GCD. Complete a program that reads in...
Create a project called rise. Add a class called GCD. Complete a program that reads in a numerator (as an int) and a denominator (again, as an int), computes and outputs the GCD, and then outputs the fraction in lowest terms. Write your program so that your output looks as close to the following sample output as possible. In this sample, the user entered 42 and 56, shown in green. Enter the numerator: 42 Enter the denominator: 56 The GCD...
To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D...
To the TwoDArray class, add a method called transpose() that generates the transpose of a 2D array with the same number of rows and columns (i.e., a square matrix). Add appropriate code in main() of the TwoDArrayApp class to execute the transpose() method and use the display() method to print the transposed matrix. /** * TwoDArray.java */ public class TwoDArray {    private int a[][];    private int nRows; public TwoDArray(int maxRows, int maxCols)        //constructor {    a...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a sort instance method to the IntList class, so that x.sort() returns an IntList that...
Add a sort instance method to the IntList class, so that x.sort() returns an IntList that is a version of the IntList x, sorted in non-decreasing order. You may use any sorting algorithm you like. There should be no side effect on x. //IntList Class public class IntList { private ConsCell start; public IntList(ConsCell s) { start = s; } public IntList cons (int h) { return new IntList(new ConsCell(h,start)); } public int length() { int len = 0; ConsCell...
Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and...
Add a public method called CountChildren to the OurPriorityQueue class that receives a priority value and returns the number of children nodes below the node with the priority value. It must call a private recursive method that does the actual counting of children nodes. The recursive private method must count and return the number of nodes within a subtree where the subtree’s “root” node has a Value that equals the value passed into the method. Do not include the parent...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT