Question

In: Computer Science

Java Program. Please read carefully. i need the exact same output as below. if you unable...

Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks

To the HighArray class in the highArray.java program (Listing 2.3), add the following methods:

1.      getMax() that returns the value of the highest key (value) in the array without removing it from the array, or 1 if the array is empty.

2.      removeMax() that removes the item with the highest key from the array.

3.      reverse() method for the HighArray class to reverse the order of elements of the array.

Sample output of HighArray class once you do the above methods:

current array items: 77 99 44 55 22 88 11 0 66 33

Can't find 35

array items after delete some values: 77 44 22 88 11 66 33

the Max is 88

the array after calling max method: 77 44 22 88 11 66 33

the array after calling remove max method: 77 44 22 11 66 33

the new max is 77

the array after calling reverse method   33 66 11 22 44 77

Solutions

Expert Solution

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 = 0; i < nElems; i++) {
                if (a[i] == result) {
                    for (int j = i; j < nElems - 1; j++) {
                        a[j] = a[j + 1];
                    }
                    nElems--;
                    i--;
                }
            }
            return result;
        }
    }

    public void reverse() {
        long temp;
        for (int i = 0; i < nElems; i++) {
            temp = a[i];
            a[i] = a[nElems - i - 1];
            a[nElems - i - 1] = temp;
        }
    }
//-----------------------------------------------------------
} // end class HighArray

Related Solutions

Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
It's Java; the code should be the same as the output sample below; please, thank you....
It's Java; the code should be the same as the output sample below; please, thank you. Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class. Create a class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Please, read the question carefully. I need correct answers and clear explanation. Thank you! On Sept...
Please, read the question carefully. I need correct answers and clear explanation. Thank you! On Sept 15, 2017, Julia Inc entered into a contract to provide 12 new deep fryers to a Montreal Poutinerie. Julia normally charges $1,500 for each deep fryer. As part of the agreement, Julia will provide on-site training at the Poutinerie’s 3 locations. Julia will host 3 separate training sessions at each location. For this training, Julia normally charges $700 / day. The Poutinerie needed Julia’s...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
Java Program Please Read all directions carefully Write a method named smallToLarge that asks the user...
Java Program Please Read all directions carefully Write a method named smallToLarge that asks the user to enter numbers, then prints the smallest and largest of all the numbers typed in by the user and the average (rounded to 2 decimal places). You may assume the user enters a valid integer number for the number of numbers to read. Here is an example dialogue: /* initialize smallest and largest variables with the 1st user input for Number */ How many...
JAVA PROGRAMMING ASSIGNMENT - MUST USE ARRAYLIST TO SOLVE. OUTPUT MUST BE EXACT SAME FORMAT AS...
JAVA PROGRAMMING ASSIGNMENT - MUST USE ARRAYLIST TO SOLVE. OUTPUT MUST BE EXACT SAME FORMAT AS SAMPLE OUTPUT. In this assignment, you will create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at...
Please read the following program requirements carefully:  You are working as an eBay Seller, where...
Please read the following program requirements carefully:  You are working as an eBay Seller, where your customers are only in New York (n), New Jersey (j) and Pennsylvania (p).  Your C++ program is supposed ask the name of the state where the purchase is made. (n or j or p)  Next your program asks the amount of purchase.  Then, you calculate the Total amount by adding the proper State sales tax calculation to the Purchase Amount...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT