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

Design and code a new method to be exported from arraystringlog called smallest

ID: 3786622 • Letter: D

Question

Design and code a new method to be exported from arraystringlog called smallest with the following signature:

public String smallest()

The method returns the smallest string in the StringLog. By "smallest" we mean in lexicographical order supported by the String class's compareTo method. As a precondition, you should assume that the StringLog is not empty.

The code:

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;
}
}

Explanation / Answer

class StringBufferDemo
{
   public static void main(String[] args)
   {
       //1. append

/*
       public StringBuffer append(xxx value)
       Where xxx is data type
      
       Ex:
       public StringBuffer append(boolean value)
       public StringBuffer append(int value)
       public StringBuffer append(String value)
           .....
           .....
           .....

       StringBuffer sb1 = new StringBuffer("abc");
       StringBuffer sb2 = sb1.append("d");

       System.out.println(sb1);//abcd
       System.out.println(sb2);//abcd
       System.out.println(sb1 == sb2);//true
*/
       //insert
       //public StringBuffer insert(int offset, xxx value)
       //Where xxx is data type
/*
       StringBuffer sb = new StringBuffer("2942011");

       System.out.println(sb);// 2942011
       sb.insert(2, "/");
       System.out.println(sb);// 29/42011

       sb.insert(4, "/");
       System.out.println(sb);// 29/4/2011
*/
       //delete
//       public StringBuffer delete(int start,int end)
//       public StringBuffer deleteCharAt(int index)
/*      
       StringBuffer sb = new StringBuffer("Hari xyz Krishna");

       System.out.println(sb);//Hari xyz Krishna

       sb.delete(sb.indexOf("xyz") , sb.indexOf("xyz") +"xyz".length() );
       //sb.delete(5, 5 +3 );
       //sb.delete(5, 8 );
      
       System.out.println(sb); //Hari Krishna

       sb.deleteCharAt(4);
       sb.deleteCharAt(4);
       System.out.println(sb); //HariKrishna
*/
       //reverse
       //public StringBuffer reverse()
/*
       StringBuffer sb = new StringBuffer("abc");
       System.out.println(sb); //abc

       sb.reverse();
       System.out.println(sb); //cba
*/
       //capacity() and length()
       //public int capacity()
       //public int length()

       StringBuffer sb1 = new StringBuffer();
       System.out.println(sb1);//
       System.out.println(sb1.capacity());//16
       System.out.println(sb1.length());//0

       sb1.insert(0,"abc");
       System.out.println(sb1);//abc
       System.out.println(sb1.capacity());//16
       System.out.println(sb1.length());//3


       StringBuffer sb2 = new StringBuffer("abc");
       System.out.println(sb2);//abc
       System.out.println(sb2.capacity());//19
       System.out.println(sb2.length());//3


   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote