List of usage examples for org.apache.commons.math.complex ComplexFormat parse
public Complex parse(String source) throws ParseException
From source file:org.sakaiproject.tool.assessment.jsf.validator.FinQuestionValidator.java
static boolean isComplexNumber(String value) { boolean isComplex = true; Complex complex = null;/* w ww.j a va 2 s . c om*/ try { DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); df.setGroupingUsed(false); // Numerical format ###.## (decimal symbol is the point) ComplexFormat complexFormat = new ComplexFormat(df); complex = complexFormat.parse(value); // This is because there is a bug parsing complex number. 9i is parsed as 9 if (complex.getImaginary() == 0 && value.contains("i")) isComplex = false; } catch (Exception e) { isComplex = false; } return isComplex; }
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 ww w . ja v a 2 s . co 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; }
From source file:org.sakaiproject.tool.assessment.services.GradingService.java
/** * Validate a students numeric answer //from w ww. j a v a2s . c om * @param The answer to validate * @return a Map containing either Real or Complex answer keyed by {@link #ANSWER_TYPE_REAL} or {@link #ANSWER_TYPE_COMPLEX} */ public Map validate(String value) { HashMap map = new HashMap(); if (value == null || value.trim().equals("")) { return map; } String trimmedValue = value.trim(); boolean isComplex = true; boolean isRealNumber = true; BigDecimal studentAnswerReal = null; try { studentAnswerReal = new BigDecimal(trimmedValue); } catch (Exception e) { isRealNumber = false; } // Test for complex number only if it is not a BigDecimal Complex studentAnswerComplex = null; if (!isRealNumber) { try { DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); df.setGroupingUsed(false); // Numerical format ###.## (decimal symbol is the point) ComplexFormat complexFormat = new ComplexFormat(df); studentAnswerComplex = complexFormat.parse(trimmedValue); // This is because there is a bug parsing complex number. 9i is parsed as 9 if (studentAnswerComplex.getImaginary() == 0 && trimmedValue.contains("i")) { isComplex = false; } } catch (Exception e) { isComplex = false; } } Boolean isValid = isComplex || isRealNumber; if (!isValid) { throw new FinFormatException("Not a valid FIN Input. studentanswer=" + trimmedValue); } if (isRealNumber) { map.put(ANSWER_TYPE_REAL, studentAnswerReal); } else if (isComplex) { map.put(ANSWER_TYPE_COMPLEX, studentAnswerComplex); } return map; }