Question

Java code: Use at least one array defined in your code and one array or array...

Java code:

Use at least one array defined in your code and one array or array list defined by user input.

   Examples of arrays you can define in code are: A deck of cards, the days of the week, the months of a year. User input can be things like grades, shopping list items, wish lists, deposits an withdrawals, etc. Be able to add, remove, print, sort as necessary to complete the program's goal.

Thanks

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

class Main

{   

    // Inserts a key in arr[] of given

    // capacity. n is current size of arr[].

    // This function returns n+1 if insertion

    // is successful, else n.

static int binarySearch(int arr[], int low, int high, int key)

    {

        if (high < low)

            return -1;

        int mid = (low + high)/2;

        if (key == arr[mid])

            return mid;

        if (key > arr[mid])

            return binarySearch(arr, (mid + 1), high, key);

        return binarySearch(arr, low, (mid -1), key);

    }

      

    /* Function to delete an element */

    static int deleteElement(int arr[], int n, int key)

    {

        // Find position of element to be deleted

        int pos = binarySearch(arr, 0, n-1, key);

       

        if (pos==-1)

        {

            System.out.println("Element not found");

            return n;

        }

       

        // Deleting element

        int i;

        for (i=pos; i<n-1; i++)

            arr[i] = arr[i+1];

       

        return n-1;

    }

    static int insertSorted(int arr[], int n, int key, int capacity)

    {

        // Cannot insert more elements if n is already

        // more than or equal to capcity

        if (n >= capacity)

           return n;

       

        int i;

        for (i=n-1; (i >= 0 && arr[i] > key); i--)

           arr[i+1] = arr[i];

       

        arr[i+1] = key;

       

        return (n+1);

    }

   

    /* Driver program to test above function */

    public static void main (String[] args)

    {

        int arr[] = new int[20];

        arr[0] = 12;

        arr[1] = 16;

        arr[2] = 20;

        arr[3] = 40;

        arr[4] = 50;

        arr[5] = 70;

        int capacity = arr.length;

        int n = 6;

        int key = 26;

   

System.out.print("Array before deletion:\n");

        for (i=0; i<n; i++)

          System.out.print(arr[i] + " ");

       

        n = deleteElement(arr, n, key);

       

        System.out.print("\n\nArray after deletion:\n");

        for (i=0; i<n; i++)

          System.out.print(arr[i] + " ");

    System.out.print("\nBefore Insertion: ");

    for (int i=0; i<n; i++)

        System.out.print(arr[i] + " ");

   

    // Inserting key

    n = insertSorted(arr, n, key, capacity);

   

    System.out.print("\nAfter Insertion: ");

    for (int i=0; i<n; i++)

        System.out.print(arr[i] + " ");

    }    

}

Add a comment
Know the answer?
Add Answer to:
Java code: Use at least one array defined in your code and one array or array...
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
  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! The Learning Goal for this exercise is to use and understand and know the difference between arrays and array lists. !!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in...

  • C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists...

    C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values. Paste your code for that here (this should only be one line) Based on your code or the lab from 4 or your doubly linked list from...

  • can i get an example of a java code using a class and user defined menthod...

    can i get an example of a java code using a class and user defined menthod with everything else listed in the picture 20% Control structures decision: if. else if.... Else switch ... case (optional) repetition (at least 2 iterations) while, do...while for 20% Classes (at least 2 classes) 10% Methods (at least 2 methods) user-defined and/or built-in methods 10% Arrays (at least 1 set of array) 20% User Interface Menu like starting point User friendliness Error free

  • *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card...

    *in Java 1. Create a card class with two attributes, suit and rank. 2.In the card class, create several methods: a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards. b.shuffleDeck – input an unshuffled deck array and return a shuffled one. + Create a swap method to help shuffle the deck c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of...

  • #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a...

    #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a list of up to 10 players and their high scores in the computer's memory. Use two arrays to manage the list. One array should store the players' names, and the other array should store the players' high scores. Use the index of the arrays to correlate the names with the scores. Your program should support the following features: a. Add a new player and...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Use python to code! Q1 - You need to keep track of book titles. Write code...

    Use python to code! Q1 - You need to keep track of book titles. Write code using function(s) that will allow the user to do the following. 1. Add book titles to the list. 2. Delete book titles anywhere in the list by value. 3. Delete a book by position. Note: if you wish to display the booklist as a vertical number booklist , that is ok (that is also a hint) 4. Insert book titles anywhere in the list....

  • Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm...

    Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm Write a static function called RemoveNullElements() without creating another array. that remove Null elements and shift all the rest of the elements to the left/front (small indexes) of the array without changing the size or length of the original array( this meant the front of the array will have all the not Null elements, and the other haft will be empty/Null. Please provide test...

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