Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//---------------------------------------------------------------------- // Arra

ID: 3792571 • Letter: #

Question

//---------------------------------------------------------------------- // ArrayStringLog.java by Dale/Joyce/Weems Chapter 2 // // Implements StringLogInterface using an array to hold the strings. //----------------------------------------------------------------------
package ch02.stringLogs;
public class ArrayStringLog implements StringLogInterface { protected String name; // name of this StringLog protected String[] log; // array that holds strings protected int lastIndex = -1; // index of last string in array
public ArrayStringLog(String name, int maxSize) // Precondition: maxSize > 0 // // Instantiates and returns a reference to an empty ArrayStringLog // object with name "name" and room for maxSize strings. { log = new String[maxSize]; this.name = name; }
public ArrayStringLog(String name) // Instantiates and returns a reference to an empty ArrayStringLog // object with name "name" and room for 100 strings. { log = new String[100]; this.name = name; }
public void insert(String element) // Precondition: This StringLog is not full. // // Places element into this StringLog. { lastIndex++; log[lastIndex] = element; }
public boolean isFull() // Returns true if this StringLog is full, otherwise returns false. { if (lastIndex == (log.length - 1)) return true; else return false; }
public int size() // Returns the number of Strings in this StringLog. { return (lastIndex + 1); }
public boolean contains(String element) // Returns true if element is in this StringLog, // otherwise returns false. // Ignores case differences when doing string comparison. {    int location = 0; while (location <= lastIndex) { if (element.equalsIgnoreCase(log[location])) // if they match return true; else location++; } return false; }
public void clear() // Makes this StringLog empty. { for (int i = 0; i <= lastIndex; i++) log[i] = null; lastIndex = -1; }
public String getName() // Returns the name of this StringLog. { return name; }
public String toString() // Returns a nicely formatted string representing this StringLog. { String logString = "Log: " + name + " ";
for (int i = 0; i <= lastIndex; i++) logString = logString + (i+1) + ". " + log[i] + " ";
return logString; } } //---------------------------------------------------------------------- // ArrayStringLog.java by Dale/Joyce/Weems Chapter 2 // // Implements StringLogInterface using an array to hold the strings. //----------------------------------------------------------------------
package ch02.stringLogs;
public class ArrayStringLog implements StringLogInterface { protected String name; // name of this StringLog protected String[] log; // array that holds strings protected int lastIndex = -1; // index of last string in array
public ArrayStringLog(String name, int maxSize) // Precondition: maxSize > 0 // // Instantiates and returns a reference to an empty ArrayStringLog // object with name "name" and room for maxSize strings. { log = new String[maxSize]; this.name = name; }
public ArrayStringLog(String name) // Instantiates and returns a reference to an empty ArrayStringLog // object with name "name" and room for 100 strings. { log = new String[100]; this.name = name; }
public void insert(String element) // Precondition: This StringLog is not full. // // Places element into this StringLog. { lastIndex++; log[lastIndex] = element; }
public boolean isFull() // Returns true if this StringLog is full, otherwise returns false. { if (lastIndex == (log.length - 1)) return true; else return false; }
public int size() // Returns the number of Strings in this StringLog. { return (lastIndex + 1); }
public boolean contains(String element) // Returns true if element is in this StringLog, // otherwise returns false. // Ignores case differences when doing string comparison. {    int location = 0; while (location <= lastIndex) { if (element.equalsIgnoreCase(log[location])) // if they match return true; else location++; } return false; }
public void clear() // Makes this StringLog empty. { for (int i = 0; i <= lastIndex; i++) log[i] = null; lastIndex = -1; }
public String getName() // Returns the name of this StringLog. { return name; }
public String toString() // Returns a nicely formatted string representing this StringLog. { String logString = "Log: " + name + " ";
for (int i = 0; i <= lastIndex; i++) logString = logString + (i+1) + ". " + log[i] + " ";
return logString; } }

Take screenshots please when you run the program. 24. Design and code a new method to be exported from ArrayStringLog cald delete, with the following signature: public boolean delete (String element) possible The method deletes one occurrence of element from the S if returns true if a deletion is made and false otherwise.

Explanation / Answer

/**
   * The method delete that takes string element
   * and returns true if the element is removed from
   * the array otherwise returns false.
   * */
   public boolean delete(String element)
   {
       boolean result=false;
      
       //cehcking if element is not existed
       if(!contains(element))
           return result;
       else
       {
           result=true;
           boolean found=false;
           int elementPos = -1;
           int index=0;
           //find index of the element
           while (index <= lastIndex && !found)
           {
               if (element.equalsIgnoreCase(log[index])) // if they match
               {
                       elementPos=index;
                       found=true;
               }
           }
          
           //replace element from the remaining elements
           for (int i = elementPos; i < size(); i++)
           {
               log[i]=log[i+1];
           }
           //decrement the size by 1
           lastIndex=lastIndex-1;          
       }
      
       //return result
       return result;
   }//end of delete method

---------------------------------------------------------------------------------------------------------------------------------

//ArrayStringLog.java
public class ArrayStringLog
{
   protected String name;              // name of this StringLog
   protected String[] log;             // array that holds strings
   protected int lastIndex = -1;       // index of last string in array

   public ArrayStringLog(String name, int maxSize)
   // Precondition:   maxSize > 0
   //
   // Instantiates and returns a reference to an empty ArrayStringLog
   // object with name "name" and room for maxSize strings.
   {
       log = new String[maxSize];
       this.name = name;
   }

   public ArrayStringLog(String name)
   // Instantiates and returns a reference to an empty ArrayStringLog
   // object with name "name" and room for 100 strings.
   {
       log = new String[100];
       this.name = name;
   }

   public void insert(String element)
   // Precondition:   This StringLog is not full.
   //
   // Places element into this StringLog.
   {    
       lastIndex++;
       log[lastIndex] = element;
   }

   public boolean isFull()
   // Returns true if this StringLog is full, otherwise returns false.
   {            
       if (lastIndex == (log.length - 1))
           return true;
       else
           return false;
   }

   public int size()
   // Returns the number of Strings in this StringLog.
   {
       return (lastIndex + 1);
   }

   public boolean contains(String element)
   // Returns true if element is in this StringLog,
   // otherwise returns false.
   // Ignores case differences when doing string comparison.
   {               
       int location = 0;
       while (location <= lastIndex)
       {
           if (element.equalsIgnoreCase(log[location])) // if they match
               return true;
           else
               location++;
       }
       return false;
   }

   public void clear()
   // Makes this StringLog empty.
   {                
       for (int i = 0; i <= lastIndex; i++)
           log[i] = null;
       lastIndex = -1;
   }

   public String getName()
   // Returns the name of this StringLog.
   {
       return name;
   }

   public String toString()
   // Returns a nicely formatted string representing this StringLog.
   {
       String logString = "Log: " + name + " ";

       for (int i = 0; i <= lastIndex; i++)
           logString = logString + (i+1) + ". " + log[i] + " ";

       return logString;
   }
  
  
   /**
   * The method delete that takes string element
   * and returns true if the element is removed from
   * the array otherwise returns false.
   * */
   public boolean delete(String element)
   {
       boolean result=false;
      
       //cehcking if element is not existed
       if(!contains(element))
           return result;
       else
       {
           result=true;
           boolean found=false;
           int elementPos = -1;
           int index=0;
           //find index of the element
           while (index <= lastIndex && !found)
           {
               if (element.equalsIgnoreCase(log[index])) // if they match
               {
                       elementPos=index;
                       found=true;
               }
           }
          
           //replace element from the remaining elements
           for (int i = elementPos; i < size(); i++)
           {
               log[i]=log[i+1];
           }
           //decrement the size by 1
           lastIndex=lastIndex-1;          
       }
      
       //return result
       return result;
   }//end of delete method
  
  
  
}

---------------------------------------------------------------------------------------------------------------------------------

//ArrayStringLogTester.java
public class ArrayStringLogTester {
  
   public static void main(String[] args) {
      
       //Create an instance of ArrayStringLog class with name and size
       ArrayStringLog obj=new ArrayStringLog("Numbers",5);
      
       //insert data into obj
       obj.insert("One");
       obj.insert("Two");
       obj.insert("Three");
       obj.insert("Four");
       //demonstrates delete method
       System.out.println("Before deletion");
       System.out.println(obj);
       System.out.println("Deleting One ");
       boolean result=obj.delete("One");
       System.out.println(result);
      
       System.out.println("After deletion");
       System.out.println(obj);
       System.out.println("Deleting Two ");
       result=obj.delete("Two");
       System.out.println(result);
      
       System.out.println("After deletion");
       System.out.println(obj);
       System.out.println("Deleting Three ");
       result=obj.delete("Three");
       System.out.println(result);
      
      
       System.out.println("After deletion");
       System.out.println(obj);
       System.out.println("Deleting Four ");
       result=obj.delete("Four");
       System.out.println(result);
      
   }

}

---------------------------------------------------------------------------------------------------------------------------------

sample output:

Before deletion
Log: Numbers

1. One
2. Two
3. Three
4. Four

Deleting One
true
After deletion
Log: Numbers

1. Two
2. Three
3. Four

Deleting Two
true
After deletion
Log: Numbers

1. Three
2. Four

Deleting Three
true
After deletion
Log: Numbers

1. Four

Deleting Four
true