Search code examples
javatostringpriority-queue

How do I print an implemented PriorityQueue as a String?


I am making a PriorityQueue without using the PQ class by Java, and the get and put methods work as intended, unfortunately the toString() method is not passing my test cases and I cannot seem to get anything printed onto the console. I'm quite lost as to what the issue is, as the get() and put() methods work fine, but multiple test cases that are given are not being passed. My code:


private int priority;
private String data;

Element(int priority, String data) { 
    // Ihr Code
    this.priority = priority;
    this.data = data;

 }

public String getData() { 
    // Ihr Code
    return data;
}


public int getPriority() { 
    // Ihr Code
    return priority;
 }

/**
 * Return data and priority as string 
 * Format: Data (Priority)
 * e.g: abc (7)
 */
public String toString()        {
    String str = data + " " + Integer.toString(priority) + ")";  
    return str;
}
}


public class PriorityQueue {


static final int SIZE = 32;


private Element[] data = null;

// actual number of entries
private int len = 0;


/**
 * Creates a new PriorityQueue
 */
public PriorityQueue() {
     data = new Element[SIZE];     
}

/** 
 * Adds a new element into the queue as long as there is space
 * Returns true if element could be added, otherwise false 
 */
boolean put(Element element) {
    // Ihr Code
    if (len == SIZE) {
        return false;
        }else if (len > 0 && len < SIZE){
            int i = len;
            while (i > 0 && element.getPriority() > data[i-1].getPriority()){
                data[i] = data[i-1];
                i--;
            }
            data[i] = element;
            len++;

            return true;
        }else{
            return false;
        }

}

/**
 * Returns element with the highest priority 
 * and removes it from the queue. Otherwise returns null
 * 
 */
Element get() {
    // Ihr Code
         if(len==0){
            return null;
            }else if (len > 0){ 
              Element x = data[0];
              for(int i = 1; i < len; i++){
                 data[i-1] = data[i];
            }
            --len;
            return x;
            }else{
                 return null;
            }
    }

/**
 * Number of entries 
 */
int length() {
    // Ihr Code
    return len;
 }

/**
 * Returns contents of the queue as a String
 * Format: data1 (priority1), data2 (priority2)
 * e.g: abc (7), cde (8)
 * Attention: There should be no comma at the end of the String 
 */
public String toString() {
    //  Code
    StringBuilder sb = new StringBuilder();
         for (int i = 0; i < data.length; i++){
             sb.append(data[i]).append(",");
         }
         if(sb.length() > 0){
             sb.deleteCharAt(sb.length()-1);
         }
        String res = sb.toString();

        return res; 
 }

I can compile the program just fine, but nothing is printed as intended. My test cases such as

        pq.put(new Element(3, "hello"));
        pq.put(new Element(7, "world"));
        String pqAsString = pq.toString().replace(" ", "");
        assertTrue(pqAsString.contains("hello"));

The judge says that assertTrueis failing. Furthermore, another testcase assertEquals(gt.getPriority(), e.getPriority()); is also failing, and I do not get at all. gtand eare elements defined in the method, with no further declaration.

Any help in making my PQ better or helping me with the toString method would be greatly appreciated! Thanks.


Solution

  • in your method put() you need one more else if block:

    } else if(len==0) {
        data[0] = element;
        len++;
    }
    

    I will reorganize this method as below. Is more readable.

    public boolean put(Element element) {
        // Ihr Code
        if(len >= SIZE) {
            return false;
        }else {
            data[len]=element;
            Element temp;
            for(int i=len; i>0; i--) {
                if(data[i].getPriority()>data[i-1].getPriority()) {
                    temp=data[i];
                    data[i]=data[i-1];
                    data[i-1]=temp;
                    i++;
                }
            }
            len++;
            return true;
        }
    }
    

    You can also add if block to toString() - print out will be prettier

    for (int i = 0; i < data.length; i++){
         if(data[i]!=null) {
            sb.append(data[i]).append(",");
        }
     }