Я пытаюсь ввести метод удаления, который принимает удаляет случайный класс из массива - PullRequest
0 голосов
/ 07 октября 2018

Я пытаюсь заставить мою программу работать так, чтобы всякий раз, когда выбран класс, он удалял его, он мог быть случайным или по порядку (я просто пытаюсь заставить его работать на этом этапе).Однако, когда я запускаю мою текущую программу, она просто постоянно зацикливается, не удаляя класс.Любая помощь приветствуется.Я не могу заставить программу удалить любой из классов и вывести их независимо от того, что я делаю.Я не могу понять, почему классы не исключаются из списка.

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 Ответ

0 голосов
/ 07 октября 2018

вам нужно реализовать методы.

  1. Вам нужно выяснить, как определить, пуст ли мешок или он будет зацикливаться вечно.
  2. Как удалить случайныйэлемент.(подсказка, так как вы используете массивы, это будет больше работы. Вы можете поменять случайный элемент с последним элементом в массиве и уменьшить lastIndex.)

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

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