Hello, I need this to be done in java eclipse, test harness and JUnit are provid
ID: 3820792 • Letter: H
Question
Hello, I need this to be done in java eclipse, test harness and JUnit are provided.
This java program is about calculating a factorial with big integer
I need to implement two classes
StopWatch class to calculate the elapsed time for the operation to be done
LargeFactorial to calculate the factorial for Biginteger
Here is the uml
package week14;
import java.util.List;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
/**
* This class executes the JUnit Test specified from the command line This will
* be used by the reference system for testing your code.
*/
public class TestHarness
{
public static void main(String[] args)
{
trace("TestHarness");
try
{
Result result = org.junit.runner.JUnitCore
.runClasses(Week14JUnitTest.class);
int runs = result.getRunCount();
int ignores = result.getIgnoreCount();
trace(String.format("Runs: %d", runs));
trace(String.format("Ingores: %d", ignores));
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("Unhandled exception: " + ex.getMessage());
}
}
private static void trace(String msg)
{
System.out.println(msg);
}
}
/************************************************************************/
package week14;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
/**
* Tests the week14 application
*/
public class Week14JUnitTest
{
/**
* Pass in invalid guesses and get an InvalidArgumentException
*/
@Test
public void testBigInteger()
{
trace("testBigInteger");
setupTestData();
StopWatch watch = new StopWatch();
try
{
for(TestData data : m_testData)
{
int number = data.getFactorial();
String expected = data.getExpected();
watch.start();
BigInteger result = LargeFactorial.factorial(number);
watch.stop();
long elapsed = watch.getElapsedTimeMilliSeconds();
String actual = result.toString();
String msg = String.format("%dms: %d! is ", elapsed, number);
trace(msg);
trace(actual);
assertTrue(String.format("Factorial: %d, Expected: %s, Actual: %s",
number, expected, actual),
expected.equals(actual));
}
}
catch(Exception ex)
{
trace(ex.toString());
fail("Error testBigInteger " + ex.getMessage());
}
}
private void trace(String msg)
{
System.out.println(msg);
}
private void setupTestData()
{
m_testData = new ArrayList<TestData>();
m_testData.add(new TestData(10, "3628800"));
m_testData.add(new TestData(50, "30414093201713378043612608166064768844377641568960512000000000000"));
m_testData.add(new TestData(100, "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"));
m_testData.add(new TestData(500, "1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
}
private List<TestData> m_testData = new ArrayList<TestData>();
/**
* Represents a specific test data point
* @author Scott
*/
class TestData
{
TestData(int factorial, String expected)
{
m_factorial = factorial;
m_expected = expected;
}
int getFactorial()
{
return m_factorial;
}
String getExpected()
{
return m_expected;
}
private int m_factorial;
private String m_expected;
}
}
/******************************************************
Large Factorial factorial long) BigInteger StopWatch m start Time long m stop Time :long finalize() :void getElapsed Time Seconds 0 :long get StartTime0 long get StopTime() :long start() :void stop0 void Stop Watch 0 Week 14JUnitTest m testData :ListExplanation / Answer
Hi,
Please see below teh required classes.
Please comment for any querie/feedbacks.
Thanks
LargeFactorial.java
import java.math.BigInteger;
public class LargeFactorial {
/**
* to find the factorial of agiven number
* @param value
* @return BigIntegr
*/
public BigInteger factorial(long value){
BigInteger fact = new BigInteger("1");
System.out.println("Initial vla: "+fact);
for(int i=1;i<=value;i++){
BigInteger iBigInt = new BigInteger(String.valueOf(i));
fact=fact.multiply(iBigInt); //Multiplication of BigInteger
}
System.out.println("Factorial of "+value+" is: "+fact.toString());
return fact;
}
}
StopWatch.java
public class StopWatch {
//member variables to hold start time and stop time
private long m_startTime;
private long m_stopTime;
//No arg constructor
public StopWatch(){
this.m_startTime = 0;
this.m_stopTime = 0;
}
//Getters and setters
public long getM_startTime() {
return m_startTime;
}
public void setM_startTime(long m_startTime) {
this.m_startTime = m_startTime;
}
public long getM_stopTime() {
return m_stopTime;
}
public void setM_stopTime(long m_stopTime) {
this.m_stopTime = m_stopTime;
}
//finalize method
protected void finalize() throws Throwable {
super.finalize();
}
//To return the elapsed time for teh operation
public long getElapsedTimeMilliSeconds(){
long ellapsedTimeInMS = this.getM_stopTime() - this.getM_startTime();
return ellapsedTimeInMS;
}
//Start method to set the start time as current system time
public void start(){
this.setM_startTime(System.currentTimeMillis());
}
//Stop method to set the stop time as current system time
public void stop(){
this.setM_stopTime(System.currentTimeMillis());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.