Question

In: Computer Science

Provide code and assertion statements for the pre/post-conditions. (To execute the assertion code, the java.exe requires...

Provide code and assertion statements for the pre/post-conditions. (To execute the assertion code, the java.exe requires the option -ea for the VM).

// ***  MyArrayList is limited to 100 elements.  ***
public class MyArrayList<E> extends ArrayList<E> {

    public MyArrayList() {
        // assert postcondition
    }

    @Override
    public int size() {
        // assert postcondition
        // code
    }

    // Insert e as a new first element to mal
    public void insertFirst(E e) {
        // assert precondition
        // code
        // assert postcondition
    }

    // Insert e as a new last element
    public void insertLast(E e) {
        // assert precondition
        // code
        // assert postcondition
    }

    // Delete my first element
    public void deleteFirst() {
        // assert precondition
        // code
        // assert postcondition
    }

    // Delete my last element
    public void deleteLast() {
        // assert precondition
        // code
        // assert postcondition
    }

    public void show() {
        for (E e : this) {
            System.out.println(e);
        }
    }
}

public class Student {
     // code
}

public class MyProg {
    public static void main(String[] args) {
        MyArrayList<Student> mal = new MyArrayList<>();
        mal.insertFirst(new Student(1, "John"));
        mal.insertFirst(new Student(2, "Mary"));
        mal.insertLast(new Student(3, "Mike"));
        mal.show();
        mal.deleteLast();
        mal.show();       
        mal.deleteFirst();
        mal.show();

    }
  }

// ------------------- EXPECTED OUTPUT --------------------- //

The following is the execution output if all assertions are valid and passed:

2, Mary

1, John

3, Mike

2, Mary

1, John

1, John

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// MyArrayList.java

import java.util.ArrayList;

public class MyArrayList<E> extends ArrayList<E> {

      // maximum number of elements in MyArrayList

      private static final int MAX_SIZE = 100;

      public MyArrayList() {

            // assert postcondition

            assert size() == 0;

      }

      @Override

      public int size() {

            // assert postcondition, that size is always less than or equal to

            // MAX_SIZE

            assert super.size() <= MAX_SIZE;

            // code, invoking size method of super class and returning it

            return super.size();

      }

      // Insert e as a new first element

      public void insertFirst(E e) {

            // assert precondition, that MyArrayList is not full

            assert size() < MAX_SIZE;

            // code

            super.add(0, e);

            // assert postcondition, that first element is e

            assert get(0).equals(e);

      }

      // Insert e as a new last element

      public void insertLast(E e) {

            // assert precondition, that MyArrayList is not full

            assert size() < MAX_SIZE;

            // code

            super.add(e);

            // assert postcondition, that last element is e

            assert get(size() - 1).equals(e);

      }

      // Delete my first element

      public void deleteFirst() {

            // assert precondition, that list is not empty

            assert size() > 0;

            // recording current size

            int currSize = size();

            // code

            super.remove(0);

            // assert postcondition, that size is now reduced by 1

            assert size() == currSize - 1;

      }

      // Delete my last element

      public void deleteLast() {

            // assert precondition that list is not empty

            assert size() > 0;

            int currSize = size();

            // code

            super.remove(currSize - 1);

            // assert postcondition, that size is now reduced by 1

            assert size() == currSize - 1;

      }

      public void show() {

            for (E e : this) {

                  System.out.println(e);

            }

      }

}

// Student.java

public class Student {

      // id and name of student

      private int id;

      private String name;

      // constructor initializing id and name

      public Student(int id, String name) {

            this.id = id;

            this.name = name;

      }

      @Override

      public String toString() {

            // returning id and name separated by comma and space

            return id + ", " + name;

      }

}

//No change is made to MyProg.java

/*OUTPUT*/

2, Mary

1, John

3, Mike

2, Mary

1, John

1, John


Related Solutions

What do you understand by pre- and post-conditions of a function? Write the pre- and post-conditions...
What do you understand by pre- and post-conditions of a function? Write the pre- and post-conditions to axiomatically specify the following functions: (a) A function takes two floating point numbers representing the sides of a rectangle as input and returns the area of the corresponding rectangle as output. (b) A function accepts three integers in the range of -100 and +100 and determines the largest of the three integers. (c) A function takes an array of integers as input and...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order methods. Make sure to validate that your output is correct. Review the terminology for tree structures and list structures. What is the root of the tree, what’s a parent, what’s a child, what’s an ancestor, what is an external node, what is an internal node, what is the head of the list, what is the tail of the list, etc. Traverse the binary tree...
Question: what is the difference between rights of assertion and observation assertion. Include which financial statements...
Question: what is the difference between rights of assertion and observation assertion. Include which financial statements are they related to and what accounts are relevant to each.
Discuss Pre-money and Post-money valuation.
Discuss Pre-money and Post-money valuation.
post and pre exercise and how it affects the lungs
post and pre exercise and how it affects the lungs
The population of Enchancia is much reduced from its pre-warlevels. Princess Sofia’s decision to execute...
The population of Enchancia is much reduced from its pre-war levels. Princess Sofia’s decision to execute all those peasants who supported James and Amber has greatly diminished the number of workers. Sofia knows that the population will take some time to recover. Without more workers, economic growth can only come if those workers become more productive. Sofia would like you (on pain of death) to provide her with some policy suggestions as to how to achieve this.
Provide the AICPA code reference to the rule that requires even part time faculty members to...
Provide the AICPA code reference to the rule that requires even part time faculty members to comply with the integrity and objectivity rule.
Given the following pre-condition and program segment, what is the post-condition for y? // Pre-condition: -2...
Given the following pre-condition and program segment, what is the post-condition for y? // Pre-condition: -2 <= x < 4 y = 2*x*x - x +3
can you explain the etiology of pre-renal failure? also is it pre-renal or post renal failure...
can you explain the etiology of pre-renal failure? also is it pre-renal or post renal failure if - urine output has been less than 100 mls per day for the last 3 days since the hypotensive episode. - His Creatinine is 3.5 and BUN is 39 - has been diagnosed with acute renal failure caused by acute tubular necrosis. also, can you explain why it is or is not pre-renal? thank you!
As the following statements execute, what is the content of the priority queue? Show the content...
As the following statements execute, what is the content of the priority queue? Show the content of the queue after each step: PriorityQueue<String> myPriorityQueue = new PriorityQueue<>(); myPriorityQueue.offer("Jim"); myPriorityQueue.offer ("Jess"); myPriorityQueue.offer ("Jill"); myPriorityQueue.offer ("Jane"); String name = myPriorityQueue.poll(); myPriorityQueue.offer (name); myPriorityQueue.offer (myPriorityQueue.peek()); myPriorityQueue.offer ("Jim"); myPriorityQueue.poll();
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT