Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package ds.students; /** * @author simont * */ public class Node { public Node next; public Node prev; private Token t; public Node(Node next, Node

package ds.students;

/** * @author simont * */ public class Node {

public Node next; public Node prev; private Token t;

public Node(Node next, Node prev, Token token) { this.next = next; this.prev = prev; this.t = token; }

public Token getToken() { return t; }

@Override public boolean equals(Object other) { if (this == other) return true; if (other == null) return false; if (!(other instanceof Node)) return false;

return t.equals(((Node)other).getToken()); }

@Override public int hashCode() { if ( t == null ) return 0; return t.hashCode(); } }

package ds.students;

/** * @author simont * */ public class Token {

public enum Type { OPERATOR, OPERAND, PAREN }; public Type type; private String operator; private double operand;

public Token(double result) { this.operand = result; this.type = Type.OPERAND; } public Token(String op) { this.operator = op; this.type = Type.OPERATOR; if ( this.operator.equals("(") || this.operator.equals(")") ) { this.type = Type.PAREN; } } public Token(Token other) { this.operator = other.operator; this.operand = other.operand; this.type = other.type; } public String getOperator() { return operator; } public double getOperand() { return operand; } public int getPrecedence() { if ( type == Type.PAREN ) return -1; if ( type != Type.OPERATOR ) return 0; switch ( operator ) { case "+": case "-": return 0; case "*": case "/": return 2; } return 0; } @Override public boolean equals(Object obj) { if ( obj == null ) return false; if ( obj == this ) return false; if ( !obj.getClass().equals(Token.class)) return false; Token t = (Token)obj; if ( t.type == this.type ) { if ( this.type == Type.OPERATOR ) return operator.equals(t.operator); else return operand == t.operand; } return false; } @Override public int hashCode() { return 0; } public String toString() { return this.type == Type.OPERAND ? "" + this.operand : this.operator; } } package ds.interfaces;

import ds.students.Token;

/** * @author simont * */ public interface List { /** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices). * @param index Index at which to add * @param obj The object to add * @return True if insertion was successful * * @throws NullPointerException if the given object is null * @throws IndexOutOfBoundsException if the index is out of range */ public boolean add(int index, Token obj); /** * Returns true if the given object is contained in the list. * * @param obj The object whose presence is to be tested * @return True if the list contains the given object * * @throws NullPointerException if the specified element is null */ public boolean contains(Token obj); /** * Remove the first instance of the given object from the list, if it exists * @param obj The object to remove * @return True if the object was removed * * @throws NullPointerException if the specified object is null */ public boolean remove(Token obj); /** * Remove the object at the specified index from the list, if it exists. * @param index The index to remove * @return The object previously at the specified index * * @throws IndexOutOfBoundsException if the specified index is out of range */ public Token remove(int index); /** * Get the object at the specified index, if it exists. * @param index The index to retrieve * @return The object at the specified index, if it exists. * * @throws IndexOutOfBoundsException if the specified index is out of bounds */ public Token get(int index);

/** * Returns the first index of the specified object, or -1 if the object does not exist * in the list. * @param token * @return The index of the specified token, or -1 if it is not contained in the list. */ public int indexOf(Token token); /** * Appends the specified element to the end of this list. * @param obj The object to add. * @return True if the object has been added to the list. * * @throws NullPointerException if the specified object is null */ public boolean add(Token obj); /** * Returns true if this list contains no elements. * @return True if the list is empty. */ public boolean isEmpty(); /** * Returns the number of elements in this list. * @return The number of elements in this list. */ public int size(); /** * Returns a string containing the toString() * for each object in this list. * @return The concatenated toString() for each element in this list */ @Override public String toString(); /** * Compares this list with the specified object for equality. * The equality comparison must be value-based rather than the default * (reference based). * * @param obj The object to compare against. * @return True if the specified object is value-comparatively equal to this list */ @Override public boolean equals(Object obj); /** * Returns the hashCode for this list. * (This method must satisfy the constraint that if List l1.equals(List l2), * then l1.hashCode() == l2.hashCode() must also be true. * @return The hashCode of this list. */ @Override public int hashCode(); }

//I NEED TO IMPLEMENT THE ABOVE INTERFACE WITH THE FOLLOWING METHODS//

package ds.students; import ds.interfaces.List;

/** * @author simont* */ public class DSList implements List { public Node head; private int size;

public DSList() { head = null; size = 0; } public boolean isEmpty() { return head == null; }

public DSList(Node head_) { head = new Node(head_.next, head_.prev, head_.getToken()); }

public DSList(DSList other) { // Copy constructor.

}

public Token remove(int index) //PLEASE COMPLETE return null; }

public int indexOf(Token obj) { //PLEASE COMPLETE return -1; }

public Token get(int index) { //PLEASE COMPLETE return null; }

public int size() { //PLEASE COMPLETE return 0; }

@Override public String toString() { //PLEASE COMPLETE

return ""; }

public boolean add(Token obj) { //PLEASE COMPLETE return false;

}

public boolean add(int index, Token obj) { //PLEASE COMPLETE

return true;

}

public boolean contains(Token obj) { //PLEASE COMPLETE return false;

}

public boolean remove(Token obj) { //PLEASE COMPLETE return true; }

@Override public int hashCode() { //PLEASE COMPLETE return this.hashCode(); }

@Override public boolean equals(Object other) { //PLEASE COMPLETE return true; }

} I HAVE ADDED THE TOKEN CLASS ABOVE AND ADDED THE COMMENT "//PLEASE COMPLETE" TO EVERY METHOD THAT I NEED COMPLETED. Thanks in advance.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions

Question

Write an elaborate note on marketing environment.

Answered: 1 week ago