Example usage for org.apache.commons.math.complex ComplexFormat ComplexFormat

List of usage examples for org.apache.commons.math.complex ComplexFormat ComplexFormat

Introduction

In this page you can find the example usage for org.apache.commons.math.complex ComplexFormat ComplexFormat.

Prototype

public ComplexFormat() 

Source Link

Document

Create an instance with the default imaginary character, 'i', and the default number format for both real and imaginary parts.

Usage

From source file:org.sakaiproject.tool.assessment.services.GradingService.java

public boolean getFINResult(ItemGradingData data, ItemDataIfc itemdata, Map publishedAnswerHash)
        throws FinFormatException {
    String studentanswer = "";
    boolean range;
    boolean matchresult = false;
    ComplexFormat complexFormat = new ComplexFormat();
    Complex answerComplex = null;//from  w  w w . j a va2  s  .  c  o  m
    Complex studentAnswerComplex = null;
    BigDecimal answerNum = null, answer1Num = null, answer2Num = null, studentAnswerNum = null;

    if (data.getPublishedAnswerId() == null) {
        return false;
    }

    AnswerIfc answerIfc = (AnswerIfc) publishedAnswerHash.get(data.getPublishedAnswerId());
    if (answerIfc == null) {
        return matchresult;
    }
    String answertext = answerIfc.getText();
    if (answertext != null) {
        StringTokenizer st = new StringTokenizer(answertext, "|");
        range = false;
        if (st.countTokens() > 1) {
            range = true;
        }

        String studentAnswerText = null;
        if (data.getAnswerText() != null) {
            studentAnswerText = data.getAnswerText().replaceAll("\\s+", "").replace(',', '.'); // in Spain, comma is used as a decimal point
        }

        if (range) {

            String answer1 = st.nextToken().trim();
            String answer2 = st.nextToken().trim();

            if (answer1 != null) {
                answer1 = answer1.trim().replace(',', '.'); // in Spain, comma is used as a decimal point     
            }
            if (answer2 != null) {
                answer2 = answer2.trim().replace(',', '.'); // in Spain, comma is used as a decimal point     
            }

            try {
                answer1Num = new BigDecimal(answer1);
                answer2Num = new BigDecimal(answer2);
            } catch (Exception e) {
                log.debug("Number is not BigDecimal: " + answer1 + " or " + answer2);
            }

            Map map = validate(studentAnswerText);
            studentAnswerNum = (BigDecimal) map.get(ANSWER_TYPE_REAL);

            matchresult = (answer1Num != null && answer2Num != null && studentAnswerNum != null
                    && (answer1Num.compareTo(studentAnswerNum) <= 0)
                    && (answer2Num.compareTo(studentAnswerNum) >= 0));
        } else { // not range
            String answer = st.nextToken().trim();

            if (answer != null) {
                answer = answer.replaceAll("\\s+", "").replace(',', '.'); // in Spain, comma is used as a decimal point     
            }

            try {
                answerNum = new BigDecimal(answer);
            } catch (NumberFormatException ex) {
                log.debug("Number is not BigDecimal: " + answer);
            }

            try {
                answerComplex = complexFormat.parse(answer);
            } catch (ParseException ex) {
                log.debug("Number is not Complex: " + answer);
            }

            if (data.getAnswerText() != null) {
                Map map = validate(studentAnswerText);

                if (answerNum != null) {
                    studentAnswerNum = (BigDecimal) map.get(ANSWER_TYPE_REAL);
                    matchresult = (studentAnswerNum != null && answerNum.compareTo(studentAnswerNum) == 0);
                } else if (answerComplex != null) {
                    studentAnswerComplex = (Complex) map.get(ANSWER_TYPE_COMPLEX);
                    matchresult = (studentAnswerComplex != null && answerComplex.equals(studentAnswerComplex));
                }
            }
        }
    }
    return matchresult;
}