Question

In: Computer Science

implement please... ........................................... public class TernarySearch {     /** Task: Searches for a value in an array....

implement please...

...........................................

public class TernarySearch

{

    /** Task: Searches for a value in an array.

     * @param a an array of Comparable objects

     * @param desiredItem an item to search for   

     * @param n an integer > 0

     */

    public static <T extends Comparable<? super T>>

    boolean ternarySearch(T[] a, T desiredItem, int n)

    {

        // it is assumed that the data is already sorted

        return ternarySearch(a, 0, n-1, desiredItem);

    } // end ternarySearch

    /** Task: recursive ternarySearch search through an array of objects.

     * @param a an array of Comparable objects

     * @param desiredItem an item to search for   

     * @param left an integer >= 0

     * @param right an integer > left and < a.length

     */

    public static <T extends Comparable<? super T>>

    boolean ternarySearch(T[] a, int left, int right, T desiredItem )

    {

        // TODO Project 1



        return false;

    } // end ternarySearch

    public static void main(String[] args)

    {

        ArrayList<Integer> accounts = new ArrayList<Integer>();

        try

        {

            Scanner file = new Scanner(new File("accounts.txt"));

            while (file.hasNext()) // test for the end of the file

            {

                // read a line

                try

                {

                    accounts.add(file.nextInt());

                } catch (NumberFormatException nfe)

                {

                    System.out.println("Error in input file ignoring");

                }

            }

            // release resources associated with flights.txt

            file.close();


            // print the accounts read

            System.out.println("Accounts are:");

            for (int index = 0; index < accounts.size(); index++)

            {

                System.out.println("[" + index + "] " + accounts.get(index));

            }

            Integer[] accountsU = accounts.toArray(new Integer[accounts.size()]);

            Integer[] accountsS = Arrays.copyOf(accountsU, accountsU.length);

            Arrays.sort(accountsS);

            System.out.println("Sorted accounts are:");

            for (int index = 0; index < accountsS.length; index++)

            {

                System.out.println("[" + index + "] " + accountsS[index]);

            }

            boolean foundT = ternarySearch(accountsS, 7881200, accountsS.length);

            int foundB = Arrays.binarySearch(accountsS, 7881200);

            System.out.println("\nternarySearch: element 7881200 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

            else

                System.out.println("\u001B[35m\u001B[1m   FAIL\u001B[0m");

            foundT = ternarySearch(accountsS, 7881199, accountsS.length);

            foundB = Arrays.binarySearch(accountsS, 7881199);

            System.out.println("\nternarySearch: element 7881199 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

            else

                System.out.println("\u001B[35m\u001B[1m   FAIL\u001B[0m");

            foundT = ternarySearch(accountsS, 7881201, accountsS.length);

            foundB = Arrays.binarySearch(accountsS, 7881201);

            System.out.println("\nternarySearch: element 7881201 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

            else

                System.out.println("\u001B[35m\u001B[1m   FAIL\u001B[0m");

            foundT = ternarySearch(accountsS, 2222222, accountsS.length);

            foundB = Arrays.binarySearch(accountsS, 2222222);

            System.out.println("\nternarySearch: element 2222222 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

            else

                System.out.println("\u001B[35m\u001B[1m   FAIL\u001B[0m");

            foundT = ternarySearch(accountsS, 9999999, accountsS.length);

            foundB = Arrays.binarySearch(accountsS, 9999999);

            System.out.println("\nternarySearch: element 9999999 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

            else

                System.out.println("\u001B[35m\u001B[1m   FAIL\u001B[0m");

            foundT = ternarySearch(accountsS, 0000000, accountsS.length);

            foundB = Arrays.binarySearch(accountsS, 0000000);

            System.out.println("\nternarySearch: element 0000000 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

            else

                System.out.println("\u001B[35m\u001B[1m   FAIL\u001B[0m");

            foundT = ternarySearch(accountsS, 1111111, accountsS.length);

            foundB = Arrays.binarySearch(accountsS, 1111111);

            System.out.println("\nternarySearch: element 1111111 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

            else

                System.out.println("\u001B[35m\u001B[1m   FAIL\u001B[0m");

            foundT = ternarySearch(accountsS, 1005231, accountsS.length);

            foundB = Arrays.binarySearch(accountsS, 1005231);

            System.out.println("\nternarySearch: element 1005231 is found " + foundT);

            if (foundT && foundB >= 0 || !foundT && foundB < 0)

                System.out.println("   PASS");

Solutions

Expert Solution

JAVA program screenshots for the provided problem statement

Input file screenshot

JAVA program output screenshots


Related Solutions

Trace the sample run provided for Search2D class .................................................................. public class Search2D {     /**      * Searches...
Trace the sample run provided for Search2D class .................................................................. public class Search2D {     /**      * Searches for the desiredItem in a rectangular matrix[][] where      * elements are sorted within each row and within each column      * If the element is found, prints its position,      * otherwise prints "not found"      *      * @author  YOUR NAME      * @version 10/20/2020      *      */     private void search(int[][] matrix, int desiredItem)     {         // TODO Project 4         // TODO must implement with one loop only         System.out.println("Searching for "...
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT