Please I need this to be done in Java, can I have it answered by anonymous who a
ID: 3793465 • Letter: P
Question
Please I need this to be done in Java, can I have it answered by anonymous who answered my last question regarding java?
Thank you
You will need to implement a specific algorithm implementation to match the class definition AND implement a unit test using JUnit that conforms the specific naming convention.
Algorithm:
Inputs: integers m and n , assumes m and n are >= 1
Order is not important for this algorithm
Local variables integers:
remainder Initialize:
No initialization required
while(true)
{
remainder = n modulo m;
if(remainder == 0)
{
break;
}
n = m;
m = remainder
}
return m;
You will need to use the following test data to verify your implementation
m
n
GCD
4567820
2147483640
20
545690876L
3456901294L
2
546587619L
21474836121L
3
951987545L
21474836651L
1
10
5
5
1542354865L
3216548445L
5
-100
325
InvalidParametersException
4951987545L
3216548445L
15
94951987542L
33216548448L
6
import java.util.List;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestHarness
{ public static void main(String[] args)
{
testWithStudentTest();
}
private static void testWithStudentTest()
{
try{ Result result = org.junit.runner.JUnitCore.runClasses(Week04JUnitTest.class);
int failCount = result.getFailureCount();
if( failCount > 0 )
{ List<Failure> failures = result.getFailures();
for(Failure fail : failures)
{
trace("FAILED: " + fail.getMessage());
}
} else{
trace("SUCCESS");
}
}catch(Exception ex) {
trace("Unexpected exception: " + ex.getMessage());
}
}
private static void trace(String msg)
{ System.out.println(msg);
}
}
Turn in
Week04JUnitTest.java, TimedGcd.java
m
n
GCD
4567820
2147483640
20
545690876L
3456901294L
2
546587619L
21474836121L
3
951987545L
21474836651L
1
10
5
5
1542354865L
3216548445L
5
-100
325
InvalidParametersException
4951987545L
3216548445L
15
94951987542L
33216548448L
6
Explanation / Answer
program
package com.ap.beans;
import java.security.InvalidParameterException;
import java.util.Scanner;
public class AlgorithamTest {
public int m;
public int n;
public String toString() {
return "AlgorithamTest [m=" + m + ", n=" + n + "]";
}
public void algorithamtest(int m,int n)
{
int remainder=0;
while(true)
{
remainder = n % m;
if(remainder == 0)
{
break;
}
n = m;
m = remainder;
}
System.out.println(m);
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("enter m value");
int m=scanner.nextInt();
System.out.println("enter n value");
int n=scanner.nextInt();
AlgorithamTest test=new AlgorithamTest();
if(m<0||n<0)
{
throw new InvalidParameterException();
}else
test.algorithamtest(m,n);
}
}
output
enter m value
4567820
enter n value
2147483640
20