Question: implement the behaviors of the List ADT using a java array. The behaviors of the List ADT are listed in slide 7 of our deck

implement the behaviors of the List ADT using a java array. The behaviors of the List ADT are listed in slide 7 of our deck on Canvas ADTsToListsToArrays.pdf. These 5 behaviors are: size(), get(), set(), add(), remove(). We will use a fixed size array - and keep a list inside that array containing the first names of people and implement commands:ADD index nameImplements the add() list behavior REMOVE indexImplements the remove() list behavior SET index nameImplements the set() list behavior GET indexImplements the get() list behavior PRINTPrints out the current list and current size of the listsize() implemented Generally, each command and its arguments are typed at the CLI all in one line. So, the input lines are blank-space-separated Strings.More on this program .I have only a few friends but occasionally, I will add a new one or even lose one! I will create a java array to keep track of the first names of my friends. I want the array to list the friends I have now, but also have room in case I add any new friends. There is also the possibility of losing a friend sometime!So, I want an array that lists all my friends in the order I want them in and I want to implement the ADT List operations in the table above for my list. Assume I will never have more than 20 friends and I create an array with the names of my current friends and extra slots for possible additional friends later with a grand total of twenty slots. This is the initialized list/array your program will start with!String [] friend ={"Annie", "Elbert", "Andrew", "Harry", "Charles", "Mary", "Frank", "Sandy", "Elvis", null, null, null, null, null, null, null, null, null, null, null};Note that this String array has 20 slots to hold up to 20 friends but the actual List we are implementing is not the whole array but rather the contiguous set of non-null elements at the start of the array. And we keep things that way - so that there are no null gaps between non-null entries. Note that the size of the list (its number of elements) is a varying number that grows when ADDs are done and lessens when REMOVEs are done. Your code will have to always keep track of the size variable.Write a program that will use a Scanner to repeatedly input a one-line request presenting any of the commands in the table above - and then responds to that command then goes back and reads in another command until an EOF condition is hit. With commands being typed in at the CLI, an EOF condition is created by control-z followed by enter key.Before starting the loop to input commands the program should print out the initial list and the initial size of the list (which is 9). In this weeks program you will implement two of the 5 commands: ADD and PRINT. Next week we will extend the program to do the rest!ADD COMMANDIn the list operation add(i, f)- we add friend f into the list at the i-th position and move everyone in the i-th or further position out one position (that is: for each position j i, the item in position j moves to position j+1). For our syntax in this program, the command is:ADD index name (blank separated!)Here index represents a legitimate index of a current list element (so 0 index < size) the legitimate indexes being 0,1,, size-1. Here name can be any String but lets play this game and make it a persons first name.And when it gets this command, your program adds the given name into the index position index and moves all the rest of the friends (whose index is index) out one position.So, for example if the Scanned command is:ADD 3 EggbertThen the list becomes:{ "Annie", "Elbert", "Andrew", "Eggbert", "Harry", "Charles", "Mary", "Frank", "Sandy", "Elvis", null, null, null, null, null, null, null, null, null, null, };Note AGAIN that the List is NOT the whole array the list is actually the non-null elements of the array! So, the size() function for the list does not return 20(the array length) but instead, the number of actual friends in the list!Note that after you process a command like that above, the size of the list has increased by 1. So, you need an int variable size to track the changing size of the Friends list.Input the command with a Scanner from the CLI and split out the blank-space-separated items check the parts for legitimacy. You must see the String ADD then an integer digit (which has to be non-negative and less than size). The third component of the comma separated line is the String which is the first name of a friend to be added into the given position in the list. If they give you and index out of range print out an error and go back to reading in another command,Once you have all three pieces validated for correctness, now place the third item the name to be added into the array friend in the position indexed by the second value. The item that was formerly in that position needs to move to the right 1 slot as do all subsequent items. PRINT COMMANDWhen your program receives this command it prints out the current list in order on one line (space or tab separated) and then on the next line, prints out the current size of the list. FORM OF PROGRAMWrite the program in a loop that repeatedly comes back to wait for another ADD or PRINT command until sc.hasNextLine() returns a false. Each time sc.hasNextLine() returns true, use sc.nextLine() to bring in a new command line either an ADD or a PRINT. You will have to plsit the inputted line with String split() base on one blank space. Once you split the input line the first part (index 0) is the String which is the actual command say, you store it in a variable called cmd. Then you can use a switch/case statement to branch off as to whether it is a ADD or a PRINT.So, you might use:switch(cmd){ case "ADD": // your code here to process ADD break; case "PRINT": // your code here to process PRINT break;}Note that the ADD command produces no output. The print command prints the whole current list on one line use System.out.print() not println() and also the current size of the list.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!