3 Replies - 1377 Views - Last Post: 29 January 2011 - 07:13 PM Rate Topic: -----

#1 mdh007  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 09-November 10

Array String Help in Java !

Posted 29 January 2011 - 06:35 PM

Can somebody please help me with my problem. Its at the bottom of the code called public string smallest

//----------------------------------------------------------------------
// ArrayStringLog.java        
// 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 StringLog 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 StringLog 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.
  {
    boolean moreToSearch;
    int location = 0;
    boolean found = false;
    moreToSearch = (location <= lastIndex);

    while (moreToSearch && !found)
    {
      if (element.equalsIgnoreCase(log[location]))  // if they match
        found = true;
      else
      {
        location++;
        moreToSearch = (location <= lastIndex);
      }
    }

   return found;
  }

  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 + "\n\n";

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

    return logString;
  }





  public boolean isEmpty()
  {
  	if  (log.length == -1)
  	return true;
  		else
  		return false;
  }

  public String smallest()
  {


  }
}





Ive got this much of the code written can anybody help me finish !!

Is This A Good Question/Topic? 0
  • +

Replies To: Array String Help in Java !

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Array String Help in Java !

Posted 29 January 2011 - 06:43 PM

What exactly is the problem?
What does that method should do?
Was This Post Helpful? 0
  • +
  • -

#3 mdh007  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 34
  • Joined: 09-November 10

Re: Array String Help in Java !

Posted 29 January 2011 - 07:09 PM

Oh yeh lol im sorry

It needs to return the smallest string in the stringlog file. By smallest i mean in terms of the lexicographic ordering supported by the string class's compareTo method. As a precondition you should assume that the StringLog is not empty.

Thanks for you help !!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Array String Help in Java !

Posted 29 January 2011 - 07:13 PM

Which method? And what about your efforts isn't working?

As for a find min, just create a String variable to hold the first element. Then iterate through the array, and if you find an element < min lexicographically, set min = that element.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1