I'm trying to type a remove method that takes removes a random class from an array

171 Views Asked by At

I'm trying to get my program to work so that whenever a class is chosen, it removes it, it can be random or in order (I'm just trying to get it to work at this point). However when I run my current program, it just constantly loops without removing a class. Any help is appreciated. I can't seem to get the program to remove any of the classes and output them no matter what I do. I can't figure out why the classes aren't being taken out of the list.

package labs.lab2;

public class TestDriver {
public static void main(String[] args)
{ 
    ArrayStringBag bag = new ArrayStringBag("My bag of courses", 6);
    bag.insert("CIS110 Object-Oriented Programming");
    bag.insert("CS120 Data Structures and Algorithms");
    bag.insert("CIS210 Database Design and Implementation");
    bag.insert("CS340 Advanced Techniques in Application Development");
    bag.insert("CIS345 Introduction to XML");
    bag.insert("CIS345/WDMD345 Hot Topics in .NET Programming");

    System.out.println(bag);

    while(!bag.isEmpty())
    {
        System.out.println("Removing course: "+bag.remove());
        if(bag.isEmpty())
            System.out.println("My bag of courses is empty!");
        else
            System.out.println(bag);
    }
}


package labs.lab2;

public interface StringBagInterface
{
    public void insert(String element);
    // Precondition:   This StringBag is not full.
    // 
    // Places element into this StringBag.

    public boolean isFull();
    // Returns true if this StringBag is full, otherwise returns false.

    public boolean isEmpty();
    // Returns true if this StringBag is empty, otherwise returns false.

    public int size();
    // Returns the number of Strings in this StringBag.

    public String remove();
    // Precondition: The StringBag is not empty.
    //
    // Removes a random string from this StringBag; then fills in the hole
    // with the last element in the bag.

    public void clear();
    // Makes this StringBag empty.

    public String getName();
    // Returns the name of this StringBag.

    public String toString();
    // Returns a nicely formatted string representing this StringBag.
}


package labs.lab2;

public class ArrayStringBag implements StringBagInterface 
{
    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 ArrayStringBag(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 ArrayStringBag(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 + "\n\n";

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

        return logString;
    }


    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    public String remove() {
        // TODO Auto-generated method stub
        return null;
    }
}
1

There are 1 best solutions below

0
On

you need to implement the methods.

  1. You need to figure out how to determine if the bag is empty or it's going to loop forever.
  2. How to remove a random element. (hint since you are using arrays it will be more work. You can swap a random element with the last element in the array and reduce the lastIndex.)

public boolean isEmpty() {
    // TODO Auto-generated method stub
    return false;
}

public String remove() {
    // TODO Auto-generated method stub
    return null;
}