Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Java) convert the array in the class in the code given below into a Linked LIst.(use driver class provided below to test.) *details about adt

(Java)

convert the array in the class in the code given below into a Linked LIst.(use driver class provided below to test.)

*details about adt

image text in transcribed

----

class StringList{ private int size; private int count; private String List[]; public StringList(){ this.size=3; List=new String[size]; count=0; } public StringList(int size){ this.size=size; List=new String[size]; count=0; } public boolean isEmpty(){ return count==0?true:false; } public boolean isFull(){ return false; } public int size(){ return count; } public void clear(){ count=0; } public String StringRetrieve(int position){ if(positioncount){ throw new RuntimeException("Invalid Position"); } return List[position]; } public void StringReplace(String newStr,int position){ if(positioncount){ throw new RuntimeException("Invalid Position"); } List[position] = newStr; } public void StringRemove(int position){ if(positioncount){ throw new RuntimeException("Invalid Position"); } for(int i=position;i List[i]=List[i+1]; count--; } } public void insert(String newStr,int position){ if(positionsize){ throw new RuntimeException("Invalid Position"); } if(position==count){ List[position]=newStr; } else if(position for(int i=position;i List[i+1]=List[i]; } List[position]=newStr; } count++; } }

-----------------------------------------//driver to test

public class LinkedStringListTester { /** * main method: test the class ContiguousIntQueue */ public static void main(String[] args) { LinkedStringList lsl = new LinkedStringList(); // Empty list if (lsl.isEmpty()) { System.out.println("Currently, the list is empty."); } else { System.out.println("The method isEmpty() does not return correct result."); System.exit(0); } if (!lsl.isFull()) { System.out.println("The list is not full."); } else { System.out.println("The method isFull() does not return correct result."); System.exit(0); } if (lsl.size() == 0) { System.out.println("The length of the list is 0."); } else { System.out.println("The method size() does not return correct result."); System.exit(0); } System.out.println(); // inserting "aaa" at 0 System.out.println("Inserting string \"aaa\" at position 0 ..."); try { lsl.insert("aaa", 0); System.out.println("String \"aaa\" inserted at position 0."); } catch (RuntimeException e) { System.out.println(e); System.out.println("The method insert() does not work properly."); System.exit(0); } if (!lsl.isEmpty()) { System.out.println("Currently, the list is not empty."); } else { System.out.println("The method isEmpty() does not return correct result."); System.exit(0); } if (!lsl.isFull()) { System.out.println("The list is not full."); } else { System.out.println("The method isFull() does not return correct result."); System.exit(0); } if (lsl.size() == 1) { System.out.println("The length of the list is 1."); } else { System.out.println("The method size() does not return correct result."); System.exit(0); } System.out.println(); // inserting "ccc" at 1 System.out.println("Inserting string \"ccc\" at position 1 ..."); try { lsl.insert("ccc", 1); System.out.println("String \"ccc\" inserted at position 1."); } catch (RuntimeException e) { System.out.println(e); System.out.println("The method insert() does not work properly."); System.exit(0); } if (!lsl.isEmpty()) { System.out.println("Currently, the list is not empty."); } else { System.out.println("The method isEmpty() does not return correct result."); System.exit(0); } if (!lsl.isFull()) { System.out.println("The list is not full."); } else { System.out.println("The method isFull() does not return correct result."); System.exit(0); } if (lsl.size() == 2) { System.out.println("The length of the list is 2."); } else { System.out.println("The method size() does not return correct result."); System.exit(0); } System.out.println(); // inserting "ddd" at 10 System.out.println("Inserting string \"ddd\" at position 10 ..."); try { lsl.insert("ddd", 10); System.out.println("The method insert() does not work properly."); System.exit(0); } catch (RuntimeException e) { System.out.println(e); System.out.println("String \"ddd\" cannot be inserted at position 10."); } if (!lsl.isEmpty()) { System.out.println("Currently, the list is not empty."); } else { System.out.println("The method isEmpty() does not return correct result."); System.exit(0); } if (!lsl.isFull()) { System.out.println("The list is not full."); } else { System.out.println("The method isFull() does not return correct result."); System.exit(0); } if (lsl.size() == 2) { System.out.println("The length of the list is 2."); } else { System.out.println("The method size() does not return correct result."); System.exit(0); } System.out.println(); // inserting "bbb" at 1 System.out.println("Inserting string \"bbb\" at position 1 ..."); try { lsl.insert("bbb", 1); System.out.println("String \"bbb\" inserted at position 1."); } catch (RuntimeException e) { System.out.println(e); System.out.println("The method insert() does not work properly."); System.exit(0); } if (!lsl.isEmpty()) { System.out.println("Currently, the list is not empty."); } else { System.out.println("The method isEmpty() does not return correct result."); System.exit(0); } if (!lsl.isFull()) { System.out.println("The list is not full."); } else { System.out.println("The method isFull() does not return correct result."); System.exit(0); } if (lsl.size() == 3) { System.out.println("The length of the list is 3."); } else { System.out.println("The method size() does not return correct result."); System.exit(0); } System.out.println(); // retrieving "aaa" System.out.println("Retrieving the string at position 0 ..."); try { if (lsl.retrieve(0).equals("aaa")) { System.out.println("String \"aaa\" retrieved from position 0."); } } catch (RuntimeException e) { System.out.println(e); System.out.println("The method retrieve() does not return correct result."); System.exit(0); } System.out.println(); // retrieving "bbb" System.out.println("Retrieving the string at position 1 ..."); try { if (lsl.retrieve(1).equals("bbb")) { System.out.println("String \"bbb\" retrieved from position 1."); } } catch (RuntimeException e) { System.out.println(e); System.out.println("The method retrieve() does not return correct result."); System.exit(0); } System.out.println(); // retrieving "ccc" System.out.println("Retrieving the string at position 2 ..."); try { if (lsl.retrieve(2).equals("ccc")) { System.out.println("String \"ccc\" retrieved from position 2."); } } catch (RuntimeException e) { System.out.println(e); System.out.println("The method retrieve() does not return correct result."); System.exit(0); } System.out.println(); // retrieving from position 10 System.out.println("Retrieving the string at position 10 ..."); try { lsl.retrieve(10); System.out.println("The method retrieve() does not work properly."); System.exit(0); } catch (RuntimeException e) { System.out.println(e); System.out.println("We cannot retrieve from position 10 since the list size is 3."); } System.out.println(); // replacing "aaa" with "000" System.out.println("Replacing the string at position 0 with \"000\" ..."); try { lsl.replace("000", 0); if (lsl.retrieve(0).equals("000")) { System.out.println("String \"000\" placed into position 0."); } else { System.out.println("The method replace() does not work properly."); System.exit(0); } } catch (RuntimeException e) { System.out.println(e); System.out.println("The method replace() does not work properly."); System.exit(0); } System.out.println(); // replacing at position 10 System.out.println("Replacing the string at position 10 ..."); try { lsl.replace("111", 10); System.out.println("The method replace() does not work properly."); System.exit(0); } catch (RuntimeException e) { System.out.println(e); System.out.println("We cannot replace the string at position 10 since the list size is 3."); } System.out.println(); // removing "bbb" at position 1" System.out.println("Removing the string at position 1 ..."); try { lsl.remove(1); } catch (RuntimeException e) { System.out.println(e); System.out.println("The method remove() does not work properly."); System.exit(0); } if (!lsl.isEmpty()) { System.out.println("Currently, the list is not empty."); } else { System.out.println("The method isEmpty() does not return correct result."); System.exit(0); } if (!lsl.isFull()) { System.out.println("The list is not full."); } else { System.out.println("The method isFull() does not return correct result."); System.exit(0); } if (lsl.size() == 2) { System.out.println("The length of the list is 2."); } else { System.out.println("The method size() does not return correct result."); System.exit(0); } // retrieving "000" System.out.println("Retrieving the string at position 0 ..."); try { if (lsl.retrieve(0).equals("000")) { System.out.println("String \"000\" retrieved from position 0."); } } catch (RuntimeException e) { System.out.println(e); System.out.println("The method remove() does not work properly."); System.exit(0); } // retrieving "ccc" System.out.println("Retrieving the string at position 1 ..."); try { if (lsl.retrieve(1).equals("ccc")) { System.out.println("String \"ccc\" retrieved from position 1."); } } catch (RuntimeException e) { System.out.println(e); System.out.println("The method remove() does not work properly."); System.exit(0); } System.out.println(); // removing at position 10 System.out.println("Removing the string at position 10 ..."); try { lsl.remove(10); System.out.println("The method remove() does not work properly."); System.exit(0); } catch (RuntimeException e) { System.out.println(e); System.out.println("We cannot remove the string at position 10 since the list size is 2."); } System.out.println(); // clearing the list System.out.println("Clearing the whole list ..."); lsl.clear(); if (lsl.isEmpty()) { System.out.println("Currently, the list is empty."); } else { System.out.println("The method clear() does not work properly."); System.exit(0); } if (!lsl.isFull()) { System.out.println("The list is not full."); } else { System.out.println("The method clear() does not work properly."); System.exit(0); } if (lsl.size() == 0) { System.out.println("The length of the list is 0."); } else { System.out.println("The method clear() does not work properly."); System.exit(0); } } }
A boolean isEmptyO boolean isFullo , int size(),void clear() method , void insert(String newStr, int position) [If position equals the current number of strings in the list, then new Str is simply appended to the list. Otherwise, newStr is inserted at position in the list, and the original string at position and the subsequent strings in the list are moved to the next position in the list]. Note that position starts from 0. If, for example, the current number of strings in the list is 3, then a valid position value should be between 0 and 3, inclusively. In this example, if the position value is 3, then newStr will be appended to the end of the list. If the position value is, for example, 0, then newStr will be inserted at the beginning of the list while the original strings at positions 0, 1 and 2 are moved to positions 1, 2 and 3, respectively. In either case, the number of strings in the list becomes 4 after the insertion. A void remove(int position) method that throws a RuntimeException, if the position value is not valid in the list (i.e., beyond the last position of the list or being negative). It also throws a RuntimeException, if the list is empty. Otherwise, it removes the string at position in the list. After the removal, strings that follow the deleted string at position in the list, if any, are moved backward by one position. Note that position starts from 0. If, for example, the current number of strings in the list is 3, then a valid position value should be between 0 and 2, inclusively. In this example, if the position value is 2, then the last string in the list will be simply removed. If the position value is, for example, 0, then the string at position 0 will be removed and the original strings at positions 1 and 2 will be moved to positions 0 and 1, respectively. In either case, the number of strings in the list is reduced to 2 after the removal. A String retrieve(int position) method that throws a RuntimeException if the position value is not valid in the list (i.e., beyond the last position of the list or being negative). Otherwise, it returns the string at position in the list. A void replace(String newStr, int position) method that throws a RuntimeException, if the position value is not valid in the list (i.e., beyond the last position of the list or being negative). Otherwise, it replaces the string at position with newStr. (Ctrl)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions