Part 1 You have found the following legacy code in your code base public class T
ID: 3751108 • Letter: P
Question
Part 1 You have found the following legacy code in your code base public class Triple 7fields private String left; private String middle: private String right; /constructor public Triple (String left, string middle, String right) this.left left: this.middle middle: this.rightright; 7getters and setters public string getLeft ) return left: ) public void setLeft (String left)this.left-left: public String getMiddle) return middle: public void setMiddle (String middle) this.middle middle: b public String getRight) return right: public void setRight (String right) this.right-right ) public string toString() { return "("+1eft+", "+middle+", "+right+")" As you can see, you can only have strings in a Triple. Your assignment is to 1. Convert this class to use generics so that you can store any types in a Triple. The types do not need to match for a Triple. For example, I should be able to store String, Double, Integer into a single Triple Write Junit tests to test this class using different data types. 2.Explanation / Answer
Please let me know if you have any doubts or you want me to modify the code. And if you find this code useful then don't forget to rate my answer as thumps up. Thank you! :)
import java.lang.reflect.Array;
public class MyUnimprovedArrayList<E> {
private E[] array;
private int index;
private static final int DEFAULT_SIZE = 10;
public MyUnimprovedArrayList() {
array = (E[]) new Object[DEFAULT_SIZE];
index = 0;
}
public E get(int index) throws MyArrayIndexOutOfBounds {
if (index > this.index - 1) {
throw new MyArrayIndexOutOfBounds("The index entered is out of bounds.");
}
if (index < 0) {
throw new MyArrayIndexOutOfBounds("The index must be a positive number.");
}
return array[index];
}
public void add(E obj) {
if (index == array.length - 1) {
E[] bigger = (E[]) new Object[array.length * 2];
for (int i = 0; i < array.length; i++) {
bigger[i] = array[i];
}
array = bigger;
}
array[index] = obj;
index++;
}
public void remove(int index) throws MyArrayIndexOutOfBounds {
if (index > this.index - 1) {
throw new MyArrayIndexOutOfBounds("The index entered is out of bounds.");
}
if (index < 0) {
throw new MyArrayIndexOutOfBounds("The index must be a positive number.");
}
for (int i = index; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
this.index--;
}
public int size() {
return index;
}
}
---------------------------------------------------------------------------------------------------------------
public class MyArrayIndexOutOfBounds extends Exception {
public MyArrayIndexOutOfBounds() {
super();
}
public MyArrayIndexOutOfBounds(String message) {
super(message);
}
public MyArrayIndexOutOfBounds(String message, Throwable cause) {
super(message, cause);
}
public MyArrayIndexOutOfBounds(Throwable cause) {
super(cause);
}
}
-------------------------------------------------------------------------------------------------------
public class Triple<L, M, R> {
// fields
private L left;
private M middle;
private R right;
// constructor
public Triple(L left, M middle, R right) {
this.left = left;
this.middle = middle;
this.right = right;
}
// getters and setters
public L getLeft() {
return left;
}
public void setLeft(L left) {
this.left = left;
}
public M getMiddle() {
return middle;
}
public void setMiddle(M middle) {
this.middle = middle;
}
public R getRight() {
return right;
}
public void setRight(R right) {
this.right = right;
}
@Override
public String toString() {
return "(" + left + ", " + middle + ", " + right + ")";
}
}
--------------------------------------------------------------------------------------------------------------
import org.junit.Assert;
import org.junit.Test;
public class TripleTests {
private Triple<Integer, String, Double> triple = new Triple<>(1, "Middle", 2.0);
@Test
public void constructorTest() {
Assert.assertEquals(Triple.class, triple.getClass());
Assert.assertFalse(triple.getMiddle() == "Right");
}
@Test
public void getLeftTest() {
Assert.assertEquals((Integer) 1, triple.getLeft());
}
@Test
public void setLeftTest() {
triple.setLeft(21);
Assert.assertEquals((Integer) 21, triple.getLeft());
}
@Test
public void getMiddleTest() {
Assert.assertEquals("Middle", triple.getMiddle());
}
@Test
public void setMiddleTest() {
triple.setMiddle("I am changed");
Assert.assertEquals("I am changed", triple.getMiddle());
}
@Test
public void getRightTest() {
Assert.assertEquals((Double) 2.0, triple.getRight());
}
@Test
public void setRightTest() {
triple.setRight(35.0);
Assert.assertEquals((Double) 35.0, triple.getRight());
}
@Test
public void toStringTest() {
Assert.assertEquals("(1, Middle, 2.0)", triple.toString());
}
}
--------------------------------------------------------------------------------------------------------
import org.junit.Assert;
import org.junit.Test;
public class MyUnimprovedArrayListTests {
MyUnimprovedArrayList<Integer> arrayList = new MyUnimprovedArrayList<>();
private void buildArrayList() {
arrayList.add(7);
arrayList.add(13);
arrayList.add(124);
arrayList.add(2329);
arrayList.add(22);
arrayList.add(267);
arrayList.add(732);
arrayList.add(754);
arrayList.add(35);
arrayList.add(542);
}
@Test
public void constructorTest() {
buildArrayList();
Assert.assertEquals(MyUnimprovedArrayList.class, arrayList.getClass());
Assert.assertEquals(10, arrayList.size());
}
@Test
public void getTest() throws MyArrayIndexOutOfBounds {
buildArrayList();
Assert.assertEquals((Integer) 7, arrayList.get(0));
}
@Test(expected = MyArrayIndexOutOfBounds.class)
public void getIndexOutOfBoundsTest() throws MyArrayIndexOutOfBounds {
buildArrayList();
arrayList.get(425);
}
@Test(expected = MyArrayIndexOutOfBounds.class)
public void getLessThanZeroTest() throws MyArrayIndexOutOfBounds {
buildArrayList();
arrayList.get(-4);
}
@Test
public void addTest() {
buildArrayList();
arrayList.add(7);
Assert.assertEquals(11, arrayList.size());
}
@Test
public void removeTest() throws MyArrayIndexOutOfBounds {
buildArrayList();
arrayList.remove(0);
Assert.assertEquals(9, arrayList.size());
}
@Test(expected = MyArrayIndexOutOfBounds.class)
public void removeIndexOutOfBoundsTest() throws MyArrayIndexOutOfBounds {
buildArrayList();
arrayList.remove(425);
}
@Test(expected = MyArrayIndexOutOfBounds.class)
public void removeLessThanZeroTest() throws MyArrayIndexOutOfBounds {
buildArrayList();
arrayList.remove(-4);
}
@Test
public void sizeTest() throws MyArrayIndexOutOfBounds {
buildArrayList();
arrayList.add(12);
Assert.assertEquals(11, arrayList.size());
arrayList.remove(1);
Assert.assertEquals(10, arrayList.size());
}
}