!!!DO A UNIT TESTING FOR THE CODE BELOW USING JUNIT IN JAVA!!! package testing;
ID: 3714978 • Letter: #
Question
!!!DO A UNIT TESTING FOR THE CODE BELOW USING JUNIT IN JAVA!!!
package testing;
import java.io.Serializable;
public class Vector3f implements Serializable
{
private float x;
private float y;
private float z;
public Vector3f(Vector3f vec)
{
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
}
public Vector3f(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getZ()
{
return z;
}
public void setZ(float z)
{
this.z = z;
}
public Vector3f add(Vector3f rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return this;
}
public Vector3f sub(Vector3f rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return this;
}
public Vector3f mul(Vector3f rhs)
{
x *= rhs.x;
y *= rhs.y;
z *= rhs.z;
return this;
}
public Vector3f flip()
{
return new Vector3f(-x, -y, -z);
}
@Override
public String toString()
{
// TODO Auto-generated method stub
return String.format("(%f, %f, %f)", x, y, z);
}
@Override
public boolean equals(Object obj)
{
Vector3f rhs = (Vector3f) obj;
return x == rhs.x && y == rhs.y && z == rhs.z;
}
@Override
public Vector3f clone()
{
return new Vector3f(x, y, z);
}
public void add(float x, float y, float z)
{
this.x += x;
this.y += y;
this.z += z;
}
}
Explanation / Answer
package trees;
import java.io.Serializable;
public class Vector3f implements Serializable
{
private float x;
private float y;
private float z;
public Vector3f(Vector3f vec)
{
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
}
public Vector3f(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getZ()
{
return z;
}
public void setZ(float z)
{
this.z = z;
}
public Vector3f add(Vector3f rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return this;
}
public Vector3f sub(Vector3f rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return this;
}
public Vector3f mul(Vector3f rhs)
{
x *= rhs.x;
y *= rhs.y;
z *= rhs.z;
return this;
}
public Vector3f flip()
{
return new Vector3f(-x, -y, -z);
}
@Override
public String toString()
{
// TODO Auto-generated method stub
return String.format("(%f, %f, %f)", x, y, z);
}
@Override
public boolean equals(Object obj)
{
Vector3f rhs = (Vector3f) obj;
return x == rhs.x && y == rhs.y && z == rhs.z;
}
@Override
public Vector3f clone()
{
return new Vector3f(x, y, z);
}
public void add(float x, float y, float z)
{
this.x += x;
this.y += y;
this.z += z;
}
}
//2.
package trees;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class Vector3fTest {
Vector3f vf=new Vector3f(1.3f,1.5f,1.7f);
Vector3f vf2=new Vector3f(vf);
@Test
void testAddVector3f() {
Vector3f vf3=vf.add(vf2);
if(vf3!=null) System.out.println("Test case Pass");
else fail("Not yet implemented");
}
@Test
void testSub() {
Vector3f vf3=vf.sub(vf2);
if(vf3!=null) System.out.println("Test case Pass");
else fail("Not yet implemented");
}
@Test
void testMul() {
Vector3f vf3=vf.mul(vf2);
if(vf3!=null) System.out.println("Test case Pass");
else fail("Not yet implemented");
}
}
/*output:-
Test case Pass
Test case Pass
Test case Pass
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.