Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C+t: operator overloading and friend functions Operator functions for the Data c

ID: 3591751 • Letter: C

Question

C+t: operator overloading and friend functions Operator functions for the Data class provided +Prefix increment operator that increments the object's day member - - Postfix decrement operator that decrements the object's day member cin's stream extraction operator that prompts the user for a date to be stored in a Date object >Greater than relational operator that returns true if a Date object d1 is greater than another object d2 and false otherwise - Subtraction operator. If one Date object is subtracted from another Date object, this operator should return the number of days between the two classes. For example, if April 10, 2014 is subtracted from April 18, 2014, the result will be 8 Functions should detect the following conditions and handle them accordingly: When a date is set to the last day of the month and incremented, it should become the first day of the following month When a date is set to December 31 and incremented, it should become January 1 of the following year When a day is set to the first day of the month and decremented, it should become the last day of the previous month When a date is set to January 1 and decremented, it should become December 31 of the previous year Complete the following program based on the given comments (SHOWN IN RED) Some of the functions are already implemented #include #include using namespace std; class Date; // Forward declaration

Explanation / Answer


· Fields: as usual, but with the generic type parameter

· Constructor: takes two parameters to initialize the fields

Instance methods

· addNodeAfter() as in the book, but not void, returns the link it creates.

public addNodeAfter(int element){

             link= new Node (element, link);

            

             return link;

       }

· removeNodeAfter() as in the book, but returns the link it creates (that is, the node after the removed element) :

public void removeNodeAfter(){

             link= link.link;

            

             return link;

       }

· toString() creates and returns a string message which reveals the stored value (data) but not the link (this method is not recursive)

· toString(int dum) creates and returns a string message which reveals the stored value (data) and the links (recursive). This method is applicable for shorter lists only.

Static methods

· listCopy() as in the book, but, since our addNodeAfter( ) returns the link, you should assign the return value directly to copyTail . That is, combine the two copyTail statements of the while loop in one statement, see the slide from the lecture.

public static Node listCopy(Node source){

   

        Node copyHead;

        Node copyTail;

        if ( source == null )

           return null;        

        copyHead = new IntNode( source.data, null );

        copyTail = copyHead;

        while ( source.link != null ) {

           source = source.link;

           copyTail = copyTail.addNodeAfter( source.data );

        }

        return copyHead;

      

    }

· listPosition() as in the book

public static Node listPosition(Node head, int position){

      

       Node cursor;

        int i;

        if ( position <= 0 )

             throw new IllegalArgumentException( "position is not positive");

        cursor = head;

        for ( i = 1; ( i < position ) && ( cursor != null ); i++ )

           cursor = cursor.link;

        return cursor;

    }

· listLength()as in the book

public static int listLength(Node head){

      

       Node cursor;

       int answer;

      

       answer= 0;

       for(cursor= head; cursor != null; cursor = cursor.link){

             answer++;

            

             return answer;

       }

    }

· getTail() This is a new method! Starting at a node given as the parameter, the method iterates through the list until arrives at the tail, then it returns the tail. The iteration is the same as in the listCopy method. This method helps to maintain the tail reference, and combined with the listCopy method, the listCopyWithTail becomes superfluous

· listCopyWithTail() omitted

· listPart() omitted

· listSearch()omitted

3. LinkedSequence

This class is also implemented as a generic class. The specification of a nongeneric DoubleLinkedSeq class is fairly well detailed in your reading, pp 232 – 238, it helps to design the generic class. It is a marked difference that the book does not apply a dummy node. Also, the LinkedBag implementation in the book can be helpful, but the analogy is rather weak.

Fields

· The field manyNodes is optional. It can be convenient to have direct access to the size of the sequence, but this variable needs careful update in each method that manipulates the size, and this can be a source of errors. For instance, the removeNodeAfter() method changes the size most of the time, but there is no change when tail is the reference. The listLength() method of the Node class makes the field dispensable, but the length algorithm is O(n).

There are five private variables of type Node:

· head

· tail

· cursor

· precursor

· dummy

The LinkedSequence invariant

For your convenience the invariant as part of the class documentation is supplied. When you design and implement the methods, it is important that you pay attention to the invariant.  

1. manyNodes is the number of nodes in the sequence; in particular the value is 0 for an empty sequence

2. For a non-empty sequence head is the reference to the first, and tail is a reference to the last node of the sequence; for an empty sequence head and tail are null

3. For a non-empty sequence cursor can be a reference to any of the nodes of the sequence, it cannot reference dummy. The cursor can be null; for an empty sequence cursor is null

4. If cursor is not null, precursor is a reference to a node such that cursor is precursor.link. If cursor is null, precursor is null; in particular precursor does not reference tail

5. dummy is a node reference and it is never null (dummy is not an element of the sequence); dummy.link is always head (null or not). If cursor is the head, then precursor is dummy  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote