@Override
  public PersistentQuantity createQuantity(
      final PersistentAbsUnit unit, final common.Fraction amount) throws PersistenceException {
    // amount in FractionManager suchen oder neu erstellen.
    final String fractionStroke = "/";
    final String enumeratorString = amount.getEnumerator().toString();
    final String denominatorString = amount.getEnumerator().toString();
    final String fractionSearchString = enumeratorString + fractionStroke + denominatorString;
    common.Fraction persistentAmount;

    try {
      persistentAmount = FractionManager.getTheFractionManager().getFraction(fractionSearchString);

    } catch (final NotFoundException e) {
      persistentAmount = amount;
      try {
        FractionManager.getTheFractionManager().addFraction(fractionSearchString, persistentAmount);
      } catch (final DoubleDefinitionException e1) {
        System.out.println("error while fraction creation");
      }
    }

    final PersistentQuantity newQuantity = Quantity.createQuantity(amount, unit);
    getThis().getQuantities().add(newQuantity);
    return newQuantity;
  }
Exemplo n.º 2
0
  @Test
  public void testAdd() {
    assertEquals(one, zero.add(one));
    assertEquals(subtractOne, zero.add(subtractOne));
    assertEquals(posRat, zero.add(posRat));
    assertEquals(negRat, zero.add(negRat));
    assertEquals(zero, one.add(subtractOne));

    assertEquals(one, new Fraction(3, 2).add(new Fraction(-1, 2)));
    assertEquals(new Fraction(-3, 4), new Fraction(-1, 4).add(new Fraction(-1, 2)));
    assertEquals(new Fraction(-21, 20), new Fraction(-3, 10).add(new Fraction(-3, 4)));
    assertEquals(new Fraction(9587, 8547), new Fraction(100, 111).add(new Fraction(17, 77)));
  }
Exemplo n.º 3
0
  @Test
  public void testsubtract() {
    assertEquals(subtractOne, zero.subtract(one));
    assertEquals(one, zero.subtract(subtractOne));
    assertEquals(posRat, posRat.subtract(zero));
    assertEquals(negRat, negRat.subtract(zero));
    assertEquals(zero, subtractOne.subtract(subtractOne));

    assertEquals(new Fraction(1, 2), new Fraction(3, 2).subtract(one));
    assertEquals(new Fraction(1, 4), new Fraction(-1, 4).subtract(new Fraction(-1, 2)));
    assertEquals(new Fraction(-3, 10), new Fraction(-21, 20).subtract(new Fraction(-3, 4)));
    assertEquals(new Fraction(100, 111), new Fraction(9587, 8547).subtract(new Fraction(17, 77)));
  }
  /**
   * testet die Multiplikation mit der gleichen Einheit .
   *
   * @throws PersistenceException
   * @throws NotComputableException
   */
  @Test
  public void testBasicCalculation_Same_Unit_mul()
      throws PersistenceException, NotComputableException {
    final PersistentQuantity two = quanMan.createQuantity(meter, Fraction.parse("2"));
    final PersistentQuantity eight = quanMan.createQuantity(meter, Fraction.parse("8"));

    /*
     * Test 1: 2m * 8m = 16m^2 (simple)
     */
    final PersistentQuantity test1_act = (PersistentQuantity) quanMan.mul(two, eight);
    final PersistentQuantity test1_exp = quanMan.createQuantity(squareMeter, Fraction.parse("16"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());
  }
Exemplo n.º 5
0
 @Test(expected = Exception.class)
 public void testDivideZeroByZeroError() {
   try {
     zero.divide(zero);
   } catch (ArithmeticException e) {
     assertTrue(true);
   }
 }
Exemplo n.º 6
0
 @Test
 public void testDivideByZeroError() {
   try {
     posRat.divide(zero);
   } catch (ArithmeticException e) {
     assertTrue(true);
   }
 }
Exemplo n.º 7
0
 public common.Fraction calcFraction(final common.Fraction arg1, final common.Fraction arg2)
     throws model.NotComputableException, PersistenceException {
   try {
     return arg1.div(arg2);
   } catch (final Throwable e) {
     throw new model.NotComputableException(e.getMessage());
   }
 }
  /**
   * testet die Division mit der gleichen Einheit .
   *
   * @throws PersistenceException
   * @throws NotComputableException
   */
  @Test
  public void testBasicCalculation_Same_Unit_div()
      throws PersistenceException, NotComputableException {
    final PersistentQuantity two = quanMan.createQuantity(meter, Fraction.parse("2"));
    final PersistentQuantity eight = quanMan.createQuantity(squareMeter, Fraction.parse("8"));

    final PersistentQuantity fiveMeter = quanMan.createQuantity(meter, Fraction.parse("5"));
    final PersistentQuantity twoSeconds = quanMan.createQuantity(second, Fraction.parse("2"));

    /*
     * Test 16m^2 / 8m = 2m (simple)
     */
    final PersistentQuantity test1_act = (PersistentQuantity) quanMan.div(eight, two);
    final PersistentQuantity test1_exp = quanMan.createQuantity(meter, Fraction.parse("4"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());

    /*
     * Test 5m / 2s = 5/2 m/s
     */
    final PersistentQuantity test2_act = (PersistentQuantity) quanMan.div(fiveMeter, twoSeconds);
    final PersistentQuantity test2_exp =
        quanMan.createQuantity(meterPerSecond, Fraction.parse("5/2"));
    assertEquals(test2_exp.getUnit(), test2_act.getUnit());
    assertEquals(test2_exp.getAmount(), test2_act.getAmount());
  }
Exemplo n.º 9
0
 @Test
 public void testFormatDec() {
   Fraction r1 = new Fraction(2, 3);
   assertEquals("0,66", r1.formatDec(2));
   r1 = new Fraction(-0, 1);
   assertEquals("0,00", r1.formatDec(2));
   r1 = new Fraction(-2, -3);
   assertEquals("0,66", r1.formatDec(2));
   r1 = new Fraction(-7, 3);
   assertEquals("-2,33", r1.formatDec(2));
   r1 = new Fraction(-7, 1);
   assertEquals("-7,00", r1.formatDec(2));
   r1 = new Fraction(-7, 1);
   assertEquals("-7", r1.formatDec(0));
   r1 = new Fraction(-15, 2);
   assertEquals("-7", r1.formatDec(0));
   r1 = new Fraction(-10000, 1);
   assertEquals("-10000,0000000000", r1.formatDec(10));
 }
Exemplo n.º 10
0
  /**
   * testet die Addition mit der gleichen Einheit.
   *
   * @throws PersistenceException
   * @throws NotComputableException
   */
  @Test
  public void testBasicCalculation_Same_Unit_add()
      throws PersistenceException, NotComputableException {
    final PersistentQuantity two = quanMan.createQuantity(meter, Fraction.parse("2"));
    final PersistentQuantity minusOne = quanMan.createQuantity(meter, Fraction.parse("-1"));
    final PersistentQuantity eight = quanMan.createQuantity(meter, Fraction.parse("8"));

    /*
     * Test 1: -1 +2 = 1
     */
    final PersistentQuantity test1_act = (PersistentQuantity) quanMan.add(minusOne, two);
    final PersistentQuantity test1_exp = quanMan.createQuantity(meter, Fraction.parse("1"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());

    /*
     * Test 2: 8 + -1 = 7
     */
    final PersistentQuantity test2_act = (PersistentQuantity) quanMan.add(eight, minusOne);
    final PersistentQuantity test2_exp = quanMan.createQuantity(meter, Fraction.parse("7"));
    assertEquals(test1_exp.getUnit(), test1_act.getUnit());
    assertEquals(test1_exp.getAmount(), test1_act.getAmount());
  }
Exemplo n.º 11
0
  @Test
  public void testDivide() {
    assertEquals(zero, zero.divide(one));
    assertEquals(zero, zero.divide(subtractOne));
    assertEquals(zero, zero.divide(posRat));
    assertEquals(zero, zero.divide(negRat));

    assertEquals(one, one.divide(one));
    assertEquals(posRat, posRat.divide(one));
    assertEquals(negRat, negRat.divide(one));

    assertEquals(new Fraction(1, 2), new Fraction(1, 4).divide(new Fraction(1, 2)));
    assertEquals(new Fraction(1, 2), new Fraction(-1, 4).divide(new Fraction(-1, 2)));
    assertEquals(new Fraction(2, 5), new Fraction(-3, 10).divide(new Fraction(-3, 4)));
    assertEquals(new Fraction(100, 111), new Fraction(1700, 8547).divide(new Fraction(17, 77)));

    assertEquals(posRat, posRat.multiply(negRat).divide(negRat));
  }
Exemplo n.º 12
0
  @Test
  public void testMultiply() {
    assertEquals(zero, zero.multiply(zero));
    assertEquals(zero, one.multiply(zero));
    assertEquals(zero, zero.multiply(posRat));
    assertEquals(zero, zero.multiply(negRat));

    assertEquals(one, one.multiply(one));
    assertEquals(posRat, one.multiply(posRat));
    assertEquals(negRat, one.multiply(negRat));
    assertEquals(negRat, negRat.multiply(one));

    assertEquals(new Fraction(1, 4), new Fraction(1, 2).multiply(new Fraction(1, 2)));
    assertEquals(new Fraction(-1, 4), new Fraction(1, 2).multiply(new Fraction(-1, 2)));
    assertEquals(new Fraction(-3, 10), new Fraction(2, 5).multiply(new Fraction(-3, 4)));
    assertEquals(new Fraction(1700, 8547), new Fraction(17, 77).multiply(new Fraction(100, 111)));
  }
Exemplo n.º 13
0
  public void test1() {
    Fraction r1 = new Fraction(2, 3);
    Fraction r2 = new Fraction(4, 6);
    Fraction r3 = new Fraction(4, 9);
    Fraction r4 = new Fraction(4, 3);
    Fraction r5 = new Fraction(0, 1);
    Fraction r6 = new Fraction(1, 1);
    Fraction r7 = new Fraction(1, 3);
    Fraction r8 = new Fraction(2, 1);
    Fraction r9 = new Fraction(2, 9);
    Fraction r10 = new Fraction(-1, 3);

    assertEquals(r1, r2);
    assertEquals(r3, r1.multiply(r2));
    assertEquals(r1, r1.multiply(r6));
    assertEquals(r1, r6.multiply(r1));
    assertEquals(r5, r5.multiply(r1));
    assertEquals(r10, r10.multiply(r6));

    assertEquals(r6, r1.divide(r2));
    assertEquals(r5, r5.divide(r1));
    assertEquals(r1, r1.divide(r6));
    assertEquals(r7, r3.divide(r4));
    try {
      assertEquals(r7, r3.divide(r5));
    } catch (ArithmeticException e) {
      assertTrue(true);
    }

    assertEquals(r4, r1.add(r2));
    assertEquals(r8, r2.add(r4));
    assertEquals(r6, r1.add(r7));
    assertEquals(r6, r5.add(r6));
    assertEquals(r1, r10.add(r6));

    assertEquals(r5, r1.subtract(r2));
    assertEquals(r9, r2.subtract(r3));
    assertEquals(r4, r4.subtract(r5));
    assertEquals(r10, r7.subtract(r1));
  }