Question

In Java, write an equals method suitable for the DoubleLinkedSequence class. The method takes a single...

In Java, write an equals method suitable for the DoubleLinkedSequence class.

The method takes a single argument, a DoubleLinkedSequence (or an Object) and returns true if both collections contain the same values in the same order (in sequences, order matters).  

Make sure your equals method is efficient and exits as soon as they are determined to be not equal.  

Note:  The data structure contains doubles, this is not about doubly?linked lists.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the equals() method in Java below:-

 /*
 * Returns true if linked lists a and b are identical, otherwise false
 */
public boolean equals(ValidateLinkedList listb) {
   Node a = this.head, b = listb.head;
   while (a != null && b != null) {
      if (a.getElement() != b.getElement())
         return false;

      /*
       * If we reach here, then a and b are not null and their data is
       * same, so move to next nodes in both lists
       */
      a = a.getNext();
      b = b.getNext();
   }

   // If linked lists are identical, then 'a' and 'b' must
   // be null at this point.
   return (a == null && b == null);
}

Please find the entire Java program where equals() is implemented:-

1. Node.java

public class Node {

private double element;

private Node next;

public Node(double element, Node next) {

super();

this.element = element;

this.next = next;

}

public Node(double element) {

super();

this.element = element;

}

public double getElement() {

return element;

}

public Node getNext() {

return next;

}

public void setNext(Node next) {

this.next = next;

}

}

2. DoubleLinkedSequence.java

public class DoubleLinkedSequence {

Node head; // head of list

/*

* Returns true if linked lists a and b are identical, otherwise false

*/

public boolean equals(DoubleLinkedSequence listb) {

Node a = this.head, b = listb.head;

while (a != null && b != null) {

if (a.getElement() != b.getElement())

return false;

/*

* If we reach here, then a and b are not null and their data is

* same, so move to next nodes in both lists

*/

a = a.getNext();

b = b.getNext();

}

// If linked lists are identical, then 'a' and 'b' must

// be null at this point.

return (a == null && b == null);

}

/* UTILITY FUNCTIONS TO TEST fun1() and fun2() */

/*

* Given a reference (pointer to pointer) to the head of a list and an int,

* push a new node on the front of the list.

*/

void push(double new_data) {

/*

* 1 & 2: Allocate the Node & Put in the data

*/

Node new_node = new Node(new_data);

/* 3. Make next of new Node as head */

new_node.setNext(head);

/* 4. Move the head to point to new Node */

head = new_node;

}

/* Drier program to test above functions */

public static void main(String args[]) {

DoubleLinkedSequence llist1 = new DoubleLinkedSequence();

DoubleLinkedSequence llist2 = new DoubleLinkedSequence();

DoubleLinkedSequence llist3 = new DoubleLinkedSequence();

/*

* The constructed linked lists are : llist1: 3->2->1 llist2: 3->2->1

*/

llist1.push(1.90);

llist1.push(2.89);

llist1.push(3.76);

llist2.push(1.90);

llist2.push(2.89);

llist2.push(3.76);

llist3.push(1.90);

llist3.push(3.76);

llist3.push(2.89);

if (llist1.equals(llist2))

System.out.println("LinkedList1 and LinkedList2 are Identical ");

else

System.out.println("LinkedList1 and LinkedList2 are Different ");

if (llist1.equals(llist3))

System.out.println("LinkedList1 and LinkedList3 are Identical ");

else

System.out.println("LinkedList1 and LinkedList3 are Different ");

}

}

The output is attached below:-

Please let me know in case of any clarifications required. Thanks!

Add a comment
Know the answer?
Add Answer to:
In Java, write an equals method suitable for the DoubleLinkedSequence class. The method takes a single...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Static methods can be called directly from the name of the class that contains the method....

    Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5);   System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...

  • Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static i...

    Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5);   System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...

  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • 1. Consider the class Circle below. Add an equals method to this class. The method should...

    1. Consider the class Circle below. Add an equals method to this class. The method should accept a Circle object as an argument. It should return true if the argument object contains the same data as the calling object, and false otherwise. public class Circle {     private double radius;     public Circle(double r)     {         radius = r;     }     public double getArea()     {         return Math.PI * radius * radius;     }     public double...

  • Please the answer have to be in JAVA programing language Problem 1: Write a class that...

    Please the answer have to be in JAVA programing language Problem 1: Write a class that implements a generic binary search tree including the methods add, contains and printInorder.   (We will do this as a group) Problem 2: Add a toString method to the class. This method returns a string of the form “[e1, e2, …, en]” containing all of the elements in the tree in order. Problem 3: Add a method to the class that returns the number of...

  • Write a JAVA contains method for a linked implementation of a sorted list. #This method is...

    Write a JAVA contains method for a linked implementation of a sorted list. #This method is written from the implementation perspective, meaning it would go inside of the LinkedSortedList class. This means we have access to firstNode and numberOfEntries. #write an efficient solution. This will involve directly accessing the linked structure rather than only invoking existing methods. You can assume T is Comparable. #The method header is: public boolean contains(T anEntry)

  • In Java create the equals() and toString() method for the Student Class Two students objects are...

    In Java create the equals() and toString() method for the Student Class Two students objects are considered equal if their id is the same. The toString() method should print out the name and id of the object.

  • Write two Java classes. A Numbers class has five fields -- maximum, minimum, sum, count, and...

    Write two Java classes. A Numbers class has five fields -- maximum, minimum, sum, count, and average. An input() method will prompt for and input a sequence of double inputs, negative to quit, calculating and updating the five fields as the doubles are input. A getMax() method returns the maximum of the doubles. A getMin() method returns the minimum of the doubles. A getSum() method returns the sum of the doubles. A getCount() method returns the number of doubles that...

  • Write a Java code to define an interface called FrugalList which contains a method named pushBack....

    Write a Java code to define an interface called FrugalList which contains a method named pushBack. The method pushBack takes an Object type argument and returns a boolean value.

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT