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 of our deck on Canvas ADTsToListsToArrays.pdf These 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 blankspaceseparated 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 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 listarray 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 slots to hold up to friends but the actual List we are implementing is not the whole array but rather the contiguous set of nonnull elements at the start of the array. And we keep things that way so that there are no null gaps between nonnull 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 oneline 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 controlz 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 In this weeks program you will implement two of the commands: ADD and PRINT. Next week we will extend the program to do the rest!ADD COMMANDIn the list operation addi f we add friend f into the list at the ith position and move everyone in the ith or further position out one position that is: for each position j i the item in position j moves to position j 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 index size the legitimate indexes being size 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 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 nonnull elements of the array! So the size function for the list does not return 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 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 blankspaceseparated items check the parts for legitimacy. You must see the String ADD then an integer digit which has to be nonnegative 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 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 schasNextLine returns a false. Each time schasNextLine returns true, use scnextLine 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 is the String which is the actual command say, you store it in a variable called cmd Then you can use a switchcase statement to branch off as to whether it is a ADD or a PRINT.So you might use:switchcmd 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
