Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please, need this to be done in java, Thank you Implement identifySequence method to determine the type of sequence (arithmetic or geometric) of the provided

Please, need this to be done in java, Thank you

Implement identifySequence method to determine the type of sequence (arithmetic or geometric) of the provided sequence. Return the appropriate enumeration value. Save the value of the sequence type in the instance attribute.

If the data is invalid throw an InvalidDataException

3. Calculate the nth term passed into the method. Save the value of the term in the instance attribute.

If the data was invalid or we haven't calculated the data yet, throw the InvalidStateException

4. The provided TestHarness invokes your implementation and verifies the correctness of your implementation

This first class is the one needed to be completed I have put some comments for you to understand what I want.

package week02;

import week02.SequenceIdentifier.SEQUENCE_TYPE;

public class SequenceIdentifier {

public static class SEQUENCE_TYPE {

public static final SEQUENCE_TYPE ARITHMETIC = null;

public static final SEQUENCE_TYPE GEOMETRIC = null;

public static final SEQUENCE_TYPE NONE = null;

}

public SEQUENCE_TYPE identifySequence(int[] m_array, int m_termIndex) {

/*public SequenceIdentifier.SEQUENCE_TYPE identifySequence(int[] array,

int termIndex)

throws InvalidStateException,

InvalidDataException

Evaluates an array of integers to determine if they are a sequence and which type. If a sequence,

it calculates the value at the requested term index. Calling this method resets an previous state information.

To calculate the nth term of an arithmetic sequence you use formula a(n) = a + (n - 1)d where a = first term value n = the term index d = the difference

Geometric Sequences Geometric sequences have a common ratio between adjacent terms.

To calculate the nth term of an geometric sequence you use formula a[n] = a*r^(n-1)

Parameters:

array - Array of integers to evaluate

termIndex - index of term to calculate

Returns:

Sequence type and save the type and calculated term.

Throws:

InvalidStateException - If state isn't correct

InvalidDataException - If data isn't valid*/

Public int getCalculatedTerm(){

throws InvalidStateException

Returns the calculated term or InvalidStateException if no sequence has been processed. Calling this method resets an previous state information.

Returns:

the calculated term from the sequence.

Throws:

InvalidStateException - if class hasn't successfully processed an array

*/

return 0;

}

public SEQUENCE_TYPE getSequenceType() {

/*Returns the SEQUENCE_TYPE enumeration or InvalidStateException if no sequence has been processed

Returns:

SEQUENCE_TYPE value*/

return null;

}

}

/******************************************

package week02;

import test.AbstractTestCase;

public class TestSequenceIdentifier

extends AbstractTestCase

{

public TestSequenceIdentifier()

{

super("TestSequenceIdentifier");

// other initialization code

}

@Override

protected boolean runTest()

{

boolean result = true;

SequenceIdentifier sequence

= new SequenceIdentifier();

try

{

// check for appropriate invalid state exception

sequence.getCalculatedTerm();

result = false;

this.addResultMessage("Expected InvalidStateException");

return result;

}

catch(InvalidStateException ex)

{

// OK

}

int count = 1;

for(TestData data : testData)

{

addResultMessage(String.format("Testing data set %d", count++));

try

{

// Execute the sequence identifier method

SequenceIdentifier.SEQUENCE_TYPE type = sequence

.identifySequence(data.m_array, data.m_termIndex);

// check the results

if(type != data.m_expectedType)

{

result = false;

String errMsg = String.format(

"Wrong sequence identified: expected %s, actual %s",

data.m_expectedType, type);

this.addResultMessage(errMsg);

continue; // go to next test data

}

if( type == SequenceIdentifier.SEQUENCE_TYPE.NONE)

{

// we don't need need to continue processing, it is not a sequence

continue;

}

int actualTerm = sequence.getCalculatedTerm();

if(actualTerm != data.m_expectedTermValue)

{

result = false;

String errMsg = String.format(

"Wrong term value calculated: expected %d, actual %d",

data.m_expectedTermValue, actualTerm);

this.addResultMessage(errMsg);

}

SequenceIdentifier.SEQUENCE_TYPE actualType = sequence.getSequenceType();

if(actualType != data.m_expectedType)

{

result = false;

String errMsg = String.format(

"Wrong sequence type was returned: expected %s, actual %s",

data.m_expectedType, actualType);

this.addResultMessage(errMsg);

}

}

catch(InvalidDataException ex)

{

if(!data.m_invalidData)

{

result = false;

this.addResultMessage(ex.getMessage());

}

else

{

addResultMessage("Caught invalid data");

}

}

catch(InvalidStateException ex)

{

if(!data.m_invalidState)

{

result = false;

this.addResultMessage(ex.getMessage());

}

else

{

addResultMessage("Caught invalid data");

}

}

}

trace(" ");

return result;

}

TestData[] testData = {

new TestData(new int[] { 2, 4, 6, 8, 10 }, 20,SequenceIdentifier.SEQUENCE_TYPE.ARITHMETIC, 40, false, false),

new TestData(new int[] {}, 20,SequenceIdentifier.SEQUENCE_TYPE.ARITHMETIC, 54, true, false),

new TestData(null, 20,SequenceIdentifier.SEQUENCE_TYPE.ARITHMETIC, 40, true, false),

new TestData(new int[] { 2, 4, 8, 16, 32 }, 20,SequenceIdentifier.SEQUENCE_TYPE.GEOMETRIC, 1048576, false, false),

new TestData(new int[] { 7, 9, 11, 13, 15 }, 27,SequenceIdentifier.SEQUENCE_TYPE.ARITHMETIC, 59, false, false),

new TestData(new int[] { -3, 9,-27 }, 5,SequenceIdentifier.SEQUENCE_TYPE.GEOMETRIC, -243, false, false),

new TestData(new int[] { 1, 7, 14, 28 }, 6,SequenceIdentifier.SEQUENCE_TYPE.NONE, 1048576, false, false),

new TestData(new int[] { 97, 84, 71 }, 15,SequenceIdentifier.SEQUENCE_TYPE.ARITHMETIC, -85, false, false),

new TestData(new int[] { 102, 94, 86, 78, 70, 62, 54, 46, 38, 32 }, 23,SequenceIdentifier.SEQUENCE_TYPE.NONE, -85, false, false),

new TestData(new int[] { 1, 33, 65 }, 6,SequenceIdentifier.SEQUENCE_TYPE.ARITHMETIC, 161, false, false),

new TestData(new int[] { 302, 604, 1208 }, 2,SequenceIdentifier.SEQUENCE_TYPE.GEOMETRIC, 604, false, false)

};

class TestData

{

TestData(int[] array, int termIndex,

SequenceIdentifier.SEQUENCE_TYPE type,

int termValue,

boolean invalidDataException,

boolean invalidStateException)

{

m_array = array;

m_termIndex = termIndex;

m_expectedType = type;

m_expectedTermValue = termValue;

m_invalidData = invalidDataException;

m_invalidState = invalidStateException;

}

// Test data and expected results

int[] m_array;

int m_termIndex;

SequenceIdentifier.SEQUENCE_TYPE m_expectedType;

int m_expectedTermValue;

boolean m_invalidData;

boolean m_invalidState;

}

}

/*******************

package week02;

public class InvalidStateException extends Exception

private static final long serialVersionUID = -1399110648203989509L;

public InvalidStateException()

{

}

public InvalidStateException(String arg0)

{

super(arg0);

}

public InvalidStateException(Throwable arg0)

{

super(arg0);

}

public InvalidStateException(String arg0, Throwable arg1)

{

super(arg0, arg1);

}

public InvalidStateException(String arg0, Throwable arg1, boolean arg2,

boolean arg3)

{

super(arg0, arg1, arg2, arg3);

}

}

/*****************************

package week02;

public class InvalidDataException extends Exception

{

private static final long serialVersionUID = -7094835315596432869L;

public InvalidDataException()

{

}

public InvalidDataException(String message)

{

super(message);

}

public InvalidDataException(Throwable cause)

{

super(cause);

}

public InvalidDataException(String message, Throwable cause)

{

super(message, cause);

}

public InvalidDataException(String message, Throwable cause,

boolean enableSuppression, boolean writableStackTrace)

{

super(message, cause, enableSuppression, writableStackTrace);

}

}

/***************************/

package week02;

import test.TestEngine;

public class TestHarness

{

public static void main(String[] args)

{

trace("Starting test...");

trace(" -- setup test data");

TestEngine engine = new TestEngine();

engine.addTest(new TestSequenceIdentifier());

engine.runTests();

trace("Completed test");

}

static private void trace(String msg)

{

System.out.println(msg);

}

}

/***********************************

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions

Question

Are we managing our human capital as a strategic asset?

Answered: 1 week ago