Question
In Java Given Code To modify: LengthMismatchException Class: public class LengthMismatchException { } VectorM Class: public class VectorM { public VectorM(){} public VectorM(int size) {
In Java
Given Code To modify:
LengthMismatchException Class:
public class LengthMismatchException { } VectorM Class:
public class VectorM { public VectorM(){} public VectorM(int size) {
} public int size() {
return 0; } public int dot(VectorM v) {
return 0; } public VectorM add(VectorM v) {
return null; } public void set(int idx, int value) {
} public int get(int idx) {
return 0; } }
Given Code to Test: LengthMismatchException Test:
import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import static org.junit.Assert.*;
public class LengthMismatchExceptionTest { public LengthMismatchExceptionTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { } @After public void tearDown() { }
@Test public void testExceptionType() { LengthMismatchException e = new LengthMismatchException(); RuntimeException r = new RuntimeException(); assertTrue(r.getClass().isInstance(e)); } } VectorM Class: public class VectorMTest { public VectorMTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { } @After public void tearDown() { }
@Test public void testDotL3() { VectorM v1 = new VectorM(3); VectorM v2 = new VectorM(3); for (int i=0; i
@Test public void testDotL4() { VectorM v1 = new VectorM(4); VectorM v2 = new VectorM(4); for (int i=0; i
@Test public void testAddL3() { VectorM v1 = new VectorM(3); VectorM v2 = new VectorM(3); for (int i=0; i
}
@Test public void testAddL4() { VectorM v1 = new VectorM(4); VectorM v2 = new VectorM(4); for (int i=0; i
@Test (expected = LengthMismatchException.class) public void testBadSizeDot() { boolean good=false; VectorM v1 = new VectorM(3); VectorM v2 = new VectorM(2); v1.dot(v2); }
@Test (expected = LengthMismatchException.class) public void testBadSizeAdd() { boolean good=false; VectorM v1 = new VectorM(3); VectorM v2 = new VectorM(2); v1.add(v2); } }
LengthMismatchException The must be modified to be an unchecked exception. You must extend the appropriate class and write the two constructors discussed in class. Note that no tests will run before you complete this portion of the lab Non-default constructor The non-default constructor in Vectorm takes a size. You will need to create a new array of ints with the specified size and store the reference in an instance variable (that you will need to create in the class). You may do any other initialization you think you need in this constructor as well. size, get, and set The size method should return the length of the array that is stored in the The get(int idx ) method should return the value at the index that is the parameter. Don't worry about bounds checking the index; the IndexOutOfBoundsException that would be thrown in the event of an index that is too large or too small is sufficient. The set(int idx, int value) method should set the array at the specified index to the specified value. The dot method The dot product of two vectors is a single integer. It is calculated by taking pairs of numbers with the same index in two vectors, multiplying them, and then adding all of the resulting products together. For example, if I have the vectors [2,4,5,8] and [1,3,6,0], the dot product would be 21+43+56+80=44. If the vectors do not have the same size, you should throw a The add method The result of adding two vectors is another vector of the same size. The value at index i of the resulting vector should be the sum of the two original vectors at index i. For example, if I have the vectors [2,4,5,8] and [1,3,6,0], the sum of these vectors would be [2+1,4+3,5+6,8+0]=[3,7,11,8]. If the vectors do not have the same size, you should throw aStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started