Question

When I run the main method with the test case, it returns the list backwards. How...

When I run the main method with the test case, it returns the list backwards. How can I fix it so that it gives me all 6 correct actions? My guess is that the problem is in the getAction() function.



public class StringDoublyLinkedList {

    /**
     * A private class to represent a link in the linked list.
     */
    private class Node {
        String value;
        Node next;
        Node prev;

        Node(String value) {
            this.value = value;
            this.next = null;
            this.prev = null;
        }
    }

    private int size = 0;
    private Node head = null;
    private Node tail = null;
    private Node head_2 = null;

    /**
     * Add a String to the end of the list.
     *
     * @param value The String to add.
     */
    public void add(String value) {
        Node newNode = new Node(value);
        if (this.size == 0) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.tail.next = newNode;
            newNode.prev = this.tail;
            this.tail = newNode;
        }
        this.size += 1;
    }

    /**
     * Get the number of elements in the list.
     *
     * @return The number of elements in the list.
     */
    public int size() {
        return this.size;
    }

    public String get(int index) {
        return this.getNode(index).value;
    }

    public void remove(int index) {
        Node curr = this.head;
        if (index == 0) {
            this.head = curr.next;
        } else {
            Node node = this.getNode(index - 1);
            node.next = node.next.next;
            if (node.next == tail) {
                this.tail = node;
            }
        }
        this.size -= 1;
    }

    private Node getNode(int index) {
        Node curr = head;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        return curr;
    }

    public boolean undo() {
        Node curr = this.head;
        if (this.size > 0) {
            curr.next.prev = null;
            this.head = curr.next;
            this.head_2 = curr;
        } else if (this.size == 0) {
            return false;
        }
        this.size -= 1;
        return true;
    }

    public boolean redo() {
        Node curr = this.head;
        Node curr_2 = this.head_2;
        if (this.size > 0) {
            curr_2.next.prev = null;
            this.head_2 = curr_2.next;
            curr.next = this.head;
            this.head.prev = curr;
            this.head = curr;
            } if (this.size == 0) {
            return false;
        }
        this.size += 1;
        return false;
    }

    /**
     * Record an action.
     *
     * @param action The action to record.
     */
    public void doAction(String action) {
        Node newNode = new Node(action);
        if (this.size == 0) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.head.prev = newNode;
            newNode.next = this.head;
            this.head = newNode;
        }
        this.size += 1;
    }

    /**
     * Get the number of actions recorded. Does *not* include actions that were undone.
     *
     * @return The number of actions recorded.
     */
    public int getNumActions() {
        int count = 1;
        int i;
        for (i = this.size() - 1; i > 0; i--) {
            count++;
        }
        return count;
    }

    /**
     * Get the action at the specified index.
     *
     * Assumes the index < this.getNumActions().
     *
     * @param index The index of the desired action.
     * @return The action (String).
     */

    public String getAction(int index) {
        int i;
        for (i = 0; i < this.size(); i++) {
            index = this.size() - index - 1;
        }
        String Actions = this.get(index);
        return Actions;
    }

    public void print() {
        Node curr = this.head;
        while (curr != null) {
            System.out.print(curr.value);
            System.out.print(", ");
            curr = curr.next;
        }
        System.out.println();
    }
public static void main(String[] args) {
    StringDoublyLinkedList actions = new StringDoublyLinkedList();

    actions.doAction("create outline");
    actions.doAction("write introduction paragraph");
    actions.doAction("write paragraph 1a");
    actions.undo();
    actions.doAction("write paragraph 1b");
    actions.doAction("write paragraph 2a");
    actions.undo();
    actions.undo();
    actions.redo();
    actions.doAction("write paragraph 2b");
    actions.doAction("write paragraph 3");
    actions.doAction("write paragraph 4");
    actions.undo();
    actions.doAction("write conclusion paragraph");
    actions.doAction("add expletive about how long this assignment took");
    actions.undo();


    String[] correctActions = {
            "create outline",
            "write introduction paragraph",
            "write paragraph 1b",
            "write paragraph 2b",
            "write paragraph 3",
            "write conclusion paragraph"
    };

    // create a variable for overall correctness
    boolean allCorrect;
    // check the number of actions
    System.out.println(
            "Expected " + Integer.toString(correctActions.length) + " actions " +
                    "and found " + Integer.toString(actions.getNumActions())
    );
    allCorrect = (actions.getNumActions() == correctActions.length);
    // if the number of actions is correct, check each action
    if (allCorrect) {
        for (int i = 0; i < correctActions.length; i++) {
            // get the expected and action actions
            String expectedAction = correctActions[i];
            String actualAction = actions.getAction(i);
            // compare them
            boolean correct = (expectedAction == actualAction);
            // print them out
            System.out.println(
                    "(" + (correct ? "correct" : "incorrect") + ") " +
                            "Action " + Integer.toString(i) + " should be \"" + correctActions[i] + "\" " +
                            "and got \"" + actions.getAction(i) + "\"."
            );
            // update the overall correctness
            allCorrect = (allCorrect && correct);
        }
    }
    // give a summary correct/incorrect judgment
    if (allCorrect) {
        System.out.println("CORRECT!");
    } else {
        System.out.println("INCORRECT!");
    }
}
}

Homework Answers

Answer #1

The problem is not with getAction().

The problems are with the following methods:

  • doAction() -> in this method, you are adding a new element to the start of the list, but the expectation is to append the new action at the end of the list
  • undo() -> in this method, you are removing the element from the head of the list, but the expectation is to remove from the end of the list
  • redo() -> again you are operating at the beginning of the list, rather you should redo at the end of the list.

Once you do the above changes, you should get all correct.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Using the given SList as a template to implement a doubly linked list with the following...
Using the given SList as a template to implement a doubly linked list with the following functionalities Add first: add an element to the head of the list Add last: add an element to the end of the list Print: print the content of the list starting from the head Find: a private method that will return a reference to a node if it contains the same value of a given argument Insert after: a method that takes a key...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
Q: Implement an equals method for the ADT list that returns true when the entries in...
Q: Implement an equals method for the ADT list that returns true when the entries in one list equal the entries in a second list. In particular, add this method to the class AList. The following is the method header: public boolean equals (Object other) public class AList<T>{ private T list[]; private int capacity = 100; private int numOfEnteries =0; public AList(){ list = (T[])new Object[capacity + 1]; } public void add(T element){ numOfEnteries++; if (numOfEnteries >capacity) System.out.println ("Exceed limit");...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
How do I get this portion of my List Iterator code to work? This is a...
How do I get this portion of my List Iterator code to work? This is a portion of the code in the AddressBookApp that I need to work. Currently it stops at Person not found and if it makes it to the else it gives me this Exception in thread "main" java.lang.NullPointerException    at AddressBookApp.main(AddressBookApp.java:36) iter.reset(); Person findPerson = iter.findLastname("Duck"); if (findPerson == null) System.out.println("Person not found."); else findPerson.displayEntry(); findPerson = iter.findLastname("Duck"); if (findPerson == null) { System.out.println("Person not found.");...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
I am trying to check for duplicates in a private method I created. I don't think...
I am trying to check for duplicates in a private method I created. I don't think I am implementing it correctly. I was hoping to get some help. Below are the methods I need to check for duplicates and also the private Boolean method that checks for duplicates. public LinkedList2<T> union( LinkedList2<T> other )    {        LinkedList2 unionSet = new LinkedList2() ;        LinkedList2 unionSet2 = other;        Node curr;        for(curr = head; curr...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT