In: Computer Science
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
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