Question

A bounded string list has an upper limit on its size, which is set when the...

  1. A bounded string list has an upper limit on its size, which is set when the list is created. The size limit specifies the maximum number of strings that can be stored in a bounded string list. String lists are mutable and allow random accesses to their elements using indexes. The list operations include

BoundedStringList(int sizeLimit);

void insert(String newString, int index);

void delete(int index);

String get(int index);

The constructor creates an empty list with a maximum size of sizeLimit. The method insert inserts a new element newString at the position index in the list and shifts the original string at index (and all the subsequent strings) forward by one position. The method delete removes the string at the position index in the list and shifts all the subsequent strings backward by one position. The method get returns the string at the position index in the list.

Provide a specification of BoundedStringList, including additional operations as needed for adequacy. Implement your specification. Give the rep invariant and abstraction function of your implementation and implement repOk and toString accordingly.

Hint: In your specification, consider edge cases such as a full list, index out of range, etc.

You may need to define new exception types for the specification and the implementation.

2. Discuss how the implementation of the bounded string list described in Question 1 should deal with the methods equals and clone. Implement these operations based on your discussion.

Note: You are required to add the implementations of clone and equals in your source code solution for Question 1.

  1. Explain the concept of benevolent side effects. Give an example that illustrates the concept. To receive credits for this question, do not use the rat/rational example given in the textbook.

Note: You are not required to provide a comprehensive example. It is good to use one that can just illustrate the concept clear enough.

  1. We propose a new data type Project with operations such as changing the project’s name and returning the project’s budget. Should Project be a mutable or immutable type? Justify your decision.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implementation in Java:

class LengthException extends Throwable //custom exception to handle deletion/insertion errors and edge cases
{
   LengthException(String Message)
   {
       super(Message);
   }
}

class BoundedStringList implements Cloneable
{
   String[] list;
   int sizeLimit;
   int flag=0, count=0; //flag used to mark deletions, count for length check
  
   BoundedStringList(int sizeLimit) //constructor
   {
       this.sizeLimit = sizeLimit;
       list= new String [sizeLimit];
   }
  
   void insert(String newString, int index) throws LengthException
   {
       checkLength(-10); //-10 is code for insertion, in case error is required for insertion
       for(int i=list.length-1; i > index; i--)
       {
           list[i] = list[i-1];
       }
       list[index] = newString;
       count = count+1; //to check number of insertions done
   }
  
   void delete(int index) throws LengthException
   {
       flag=1; //marking flag as active since deletion is required.
       checkLength(index);
       for(int i = index; i < list.length -1; i++)
       {
           list[i] = list[i + 1];
       }
       flag=0; //setting flag back to 0 since deletion is complete
   }
  
   String get(int index)
   {
       return list[index];
   }
  
   void show()
   {
       System.out.println(" " + Arrays.toString(list)); //toString method usage
   }
  
   void checkLength(int index) throws LengthException
   {
       if(count == sizeLimit && index==-10)
       {
           throw new LengthException("Cannot insert more elements in list.");
       }
      
       if(flag==1)
       {
           int check = 0;
           for (int i = 0; i < list.length; i ++)
           if (list[i] != null)
               check ++;
           if(check==0) //check if list is empty
               throw new LengthException("Cannot delete from list, no elements present");
       }
      
       if(flag==1 && list[index]==null) //check if list is not empty but has null at the index mentioned for deletion
       {
           throw new LengthException("Cannot delete from list, no element at index");
       }
   }
  
   public Object clone() throws CloneNotSupportedException //clone override
   {
       return super.clone();
   }
  
}


public class HelloWorld
{
   public static void main(String [] args) throws LengthException, CloneNotSupportedException
   {
       BoundedStringList ls = new BoundedStringList(5);
       ls.insert("Hello", 1);
       ls.insert("World", 2);
       ls.insert("How", 2);
       ls.insert("Are", 2);
       ls.insert("You", 2);
       //ls.insert("Doing", 2);//throws custom exception, since list is full now.
       ls.show();
       ls.delete(2);
       ls.show();
       System.out.println(ls.get(1));
       ls.delete(1);
       ls.delete(1); //throws custom exception : Cannot delete from list, no elements present
       BoundedStringList cloned_ls = (BoundedStringList) ls.clone();
       cloned_ls.show(); //using clone
   }
}

Equals and Clone: Equals method cannot be overriden for arrays in a direct manner, since arrays are rimitive datatypes, hence we will have to use the pre built Arrays.equals(arr1[], arr2[]), when it comes to comparing two arrays. Hence implemtation will not be possible.

Clone can be used s expected, and impleemtd in the code above, to clone the object that we desire and make a similar replica of the same.

Benevolent side effects : The user of an abstraction will be unable to see a side effect if that particular side effect doesnt affect the abstract value of a mutable object. Since they are more private/internal, they do not affect the abstraction or destroy it in any manner.

Example: reduction is a benevolent side effect. The same can be exemplified with the help of rational numbers/division/GCD and so forth, or any other reduction process.

Add a comment
Know the answer?
Add Answer to:
A bounded string list has an upper limit on its size, which is set when the...
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
  • Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due...

    Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due Friday, February 9th, 2017 @ 11:59PM EST Directions Create a List object. Using the following definition (List.h file is also in the repository for your convenience) for a list, implement the member functions (methods) for the List class and store the implementation in a file called List.cpp. Use an array to implement the list. Write the client code (the main method and other non-class...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

  • Please write in Java Recall that the ADT list class methods are;  void List() ...

    Please write in Java Recall that the ADT list class methods are;  void List()  bool isEmpty()  int size()  void add(int item, int pos)//inserts item at specified position (first postion is 1)  void remove(int index)//removes item from specified position  void removeAll()  int indexOf(int item)//returns the index of item  int itemAt(int index)//returns the item in position specified by index Implementation of LIST ADT operations and details are hidden. Only the methods listed above are...

  • 10.3 Example. When you first declare a new list, it is empty and its length is...

    10.3 Example. When you first declare a new list, it is empty and its length is zero. If you add three objects—a, b, and c—one at a time and in the order given, to the end of the list, the list will appear as a b c The object a is first, at position 1, b is at position 2, and c is last at position 3.1 To save space here, we will sometimes write a list’s contents on one...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...

    Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() {     arr = new String[10];     size = 0; } public int size() {     return size; } public void add(String value) {    ...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • Create a complete LinkedList class which implements all of the methods listed below using dynamic memory...

    Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...

  • LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as...

    LinkedListBox Patterned after the ArrayBox assignment, please create your own implementation of LinkedList called LinkedListBox as explained in class. Refer to the class slides on linked lists. Specifically, implement your generic classes as: public class LinkedListNode { public E element; public LinkedListNode next; } public class LinkedListBox { public LinkedListNode head; public LinkedListNode tail; public int count = 0; } Within the LinkedListBox class, implement the following methods: public boolean add(E e). Returns true after adding an element to the...

  • Use the Concept of vector and string to do the following question 1) Write a program...

    Use the Concept of vector and string to do the following question 1) Write a program that creates a vector of string called V. Vector V grows and shrinks as the user processes the transactions from a data file called “data.txt”. The transaction file can only include three commands: Insert, Delete and Print. With Insert command, you get two more information, the information you need to insert and the position it should be inserted. With Delete, you get one more...

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