Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please make the comment code by java /****************************************************************************** * This class is a homework assignment; * A TransactionSeq is a collection of Transactions. * The

please make the comment code by java

/****************************************************************************** * This class is a homework assignment; * A TransactionSeq is a collection of Transactions. * The sequence can have a special "current element," which is specified and * accessed through four methods of the sequence class * (start, getCurrent, advance and isCurrent). * The semantics of the current element are slightly changed from the textbook. * This is especially the case with removeCurrent. Please read the assignment sheet. * * @note * (1) The capacity of one a sequence can change after it's created, but * the maximum capacity is limited by the amount of free memory on the * machine. The constructor, addAfter, addBefore, clone, * and concatenation will result in an * OutOfMemoryError when free memory is exhausted. *

* (2) A sequence's capacity cannot exceed the maximum integer 2,147,483,647 * (Integer.MAX_VALUE). Any attempt to create a larger capacity * results in a failure due to an arithmetic overflow. * * NB: Neither of these conditions require any work for the implementors (students). * * @see * * Java Source Code for the original class by Michael Main * * ******************************************************************************/ public class TransactionSeq implements Cloneable { // Implementation of the TransactionSeq class: // 1. The number of elements in the sequences is in the instance variable // manyItems. The elements may be Transaction objects or nulls. // 2. For any sequence, the elements of the // sequence are stored in data[0] through data[manyItems-1], and we // don't care what's in the rest of data. // 3. We remember whether we're currently looking at an element, or whether // it has been removed. We use a boolean to remember this. // 3. If there is a current element, then it lies in data[currentIndex]; // if there is no current element, then currentIndex can be any index // in the array or the same as manyItems

private Transaction[ ] data; private int manyItems; private boolean isCurrent; private int currentIndex;

private static int INITIAL_CAPACITY =1;

private static Consumer reporter = (s) -> System.out.println("Invariant error: "+ s); private boolean report(String error) { reporter.accept(error); return false; }

private boolean wellFormed() { // Check the invariant. // 1. data is never null if (data == null) return report("data is null"); // test the NEGATION of the condition // Check if isCurrent is set to true if there is a current element if(data.length

// 3. currentIndex is never negative and never more than the number of // items claimed by the sequence. // TODO if(currentIndex<0 || currentindex>manyItems) return report("we cant be negative number"); // 4. if isCurrent is true, then current index must be a valid index // (it cannot be equal to the number of items) // TODO if(isCurrent==true) if( currentIndex>=manyItems) return report("we cant out of boundse "); // If no problems discovered, return true return true; }

// This is only for testing the invariant. Do not change! private TransactionSeq(boolean testInvariant) { data = new Transaction[INITIAL_CAPACITY]; manyItems = 0;

}

/** * Initialize an empty sequence with an initial capacity of INITIAL_CAPACITY. * The addAfter and addBefore methods work * efficiently (without needing more memory) until this capacity is reached. * @param - none * @postcondition * This sequence is empty and has an initial capacity of INITIAL_CAPACITY * @exception OutOfMemoryError * Indicates insufficient memory for initial array. **/ public TransactionSeq( ) { // NB: NEVER assert the invariant at the START of the constructor. // (Why not? Think about it.) // TODO: Implement this code. assert wellFormed() : "Invariant false at end of constructor"; }

/** * Initialize an empty sequence with a specified initial capacity. Note that * the addAfter and addBefore methods work * efficiently (without needing more memory) until this capacity is reached. * @param initialCapacity * the initial capacity of this sequence * @precondition * initialCapacity is non-negative. * @postcondition * This sequence is empty and has the given initial capacity. * @exception IllegalArgumentException * Indicates that initialCapacity is negative. * @exception OutOfMemoryError * Indicates insufficient memory for an array with this many elements. * new Transaction[initialCapacity]. **/ public TransactionSeq(int initialCapacity) { // TODO: Implement this code. assert wellFormed() : "Invariant false at end of constructor"; }

/** * Determine the number of elements in this sequence. * @param - none * @return * the number of elements in this sequence **/ public int size( ) { assert wellFormed() : "invariant failed at start of size"; // TODO: Implement this code. return 0; // size() should not modify anything, so we omit testing the invariant at the end }

/** * The first element (if any) of this sequence is now current. * @param - none * @postcondition * The front element of this sequence (if any) is now the current element (but * if this sequence has no elements at all, then there is no current * element). **/

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

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions