예제 #1
0
  public void options() {
    int option;
    System.out.println("Options: 1 = Add");
    System.out.println("Options: 2 = Subtract");
    System.out.println("Options: 3 = Divide");
    System.out.println("Options: 4 = Multiply");
    option = m.op.nextInt();

    if (option == 1) {
      getValue();
      c.addNumbers();
    }
    if (option == 2) {
      getValue();
      c.subtractNumbers();
    }
    if (option == 3) {
      getValue();
      c.divideNumbers();
    }
    if (option == 4) {
      getValue();
      c.multiplyNumbers();
    }
    if (option > 4 || option < 1) {
      System.out.println("You have entered an invalid number");
      options();
    }
  }
예제 #2
0
 @Test
 public void testSqrt() {
   Double x = 81.0;
   String operation = "sqrt";
   Double result = 9.0;
   Calculation object = new Calculation();
   object.calculate(x, operation);
   assertEquals(result, object.getTotal());
 }
예제 #3
0
 @Test
 public void testAdd() {
   Double x = 4.0;
   Double y = 2.0;
   String operation = "+";
   Double result = 6.0;
   Calculation object = new Calculation();
   object.calculate(x, y, operation);
   assertEquals(result, object.getTotal());
 }
예제 #4
0
 @Test
 public void testCos() {
   Double x = 90.0;
   String operation = "cos";
   Double result = Math.cos(x);
   Calculation object = new Calculation();
   object.setTotal(x);
   object.calculate(operation);
   assertEquals(result, object.getTotal());
 }
 public int calculate(Calculation calculation, int num1, int num2) {
   if (num1 > 10 && num2 < num1) {
     return calculation.calculate(num1, num2);
   } else {
     throw new IllegalArgumentException("Invalid input num1: " + num1 + ", num2: " + num2);
   }
 }
  @Override
  public void processCalculations(final User u, final Entity point, final Value value)
      throws NimbitsException {

    final List<Entity> calculations =
        EntityServiceFactory.getInstance().getEntityByTrigger(u, point, EntityType.calculation);
    for (final Entity entity : calculations) {
      Calculation c = (Calculation) entity;
      try {

        final List<Entity> target =
            EntityServiceFactory.getInstance().getEntityByKey(u, c.getTarget(), EntityType.point);
        if (target.isEmpty()) {
          log.severe("Point target was null " + c.getTarget());
          log.severe(c.getFormula());
          log.severe("trigger: " + c.getTrigger());
          disableCalc(u, c);
        } else {
          log.info("Solving calc" + c.getFormula());
          final Value result = solveEquation(u, c);
          log.info("result" + result);
          ValueServiceFactory.getInstance().recordValue(u, target.get(0), result);
        }
      } catch (NimbitsException e1) {
        LogHelper.logException(this.getClass(), e1);
        disableCalc(u, c);
      }
    }
  }
예제 #7
0
  public void calculate(View view) {
    EditText flyingDistanceTextBox =
        (EditText) findViewById(R.id.flyingdistancecourseFlyingDistanceOutput);
    EditText courseTextBox = (EditText) findViewById(R.id.flyingdistancecourseCourse_2);

    double flyingDistance = 0;
    double course = 0;
    Distance d = new Distance();
    flyingDistance = Calculation.calcFlyDis(airportOneChoice, airportTwoChoice);
    course = Calculation.calcCourBwAir(airportOneChoice, airportTwoChoice);

    if (distanceUnitChoice.equalsIgnoreCase("SM")) {
      flyingDistance = d.kilomToStat(flyingDistance);
      flyingDistanceTextBox.setText((String.format("%.2f", flyingDistance)));
    } else if (distanceUnitChoice.equalsIgnoreCase("NM")) {
      flyingDistance = d.kilomToNaut(flyingDistance);
      flyingDistanceTextBox.setText((String.format("%.2f", flyingDistance)));
    } else {
      flyingDistanceTextBox.setText((String.format("%.2f", flyingDistance)));
    }

    courseTextBox.setText(String.format("%.2f", course));
  }
예제 #8
0
파일: Calc.java 프로젝트: sabadosh/calc
 public static void main(String[] args) {
   Calculation calculation = new Calculation();
   System.out.println("-------------------------------");
   System.out.print(" Enter the argument:");
   Double x = getArgumentFromConsole();
   int i = 0;
   while (true) {
     System.out.print(" Enter the operation:");
     String operation = getOperationFromConsole();
     if (!(operation.equals("+")
         || operation.equals("-")
         || operation.equals("*")
         || operation.equals("/"))) {
       if (i == 0) {
         calculation.setTotal(x);
         calculation.calculate(operation);
         i++;
       } else {
         calculation.calculate(operation);
         i++;
       }
     } else {
       System.out.print(" Enter the argument:");
       Double y = getArgumentFromConsole();
       if (i == 0) {
         calculation.calculate(x, y, operation);
       } else {
         calculation.calculate(y, operation);
       }
       i++;
     }
     System.out.println("-------------------------------");
     System.out.println("Result:" + calculation.getTotal());
     System.out.println();
     System.out.println();
   }
 }
예제 #9
0
 Sellall(String args[], Player p) {
   hc = HyperConomy.hc;
   s = hc.getShop();
   calc = hc.getCalculation();
   tran = hc.getTransaction();
   ench = hc.getETransaction();
   player = p;
   try {
     s.setinShop(player);
     if (s.inShop() != -1) {
       if (!hc.getYaml().getConfig().getBoolean("config.use-shop-permissions")
           || player.hasPermission("hyperconomy.shop.*")
           || player.hasPermission("hyperconomy.shop." + s.getShop(player))
           || player.hasPermission("hyperconomy.shop." + s.getShop(player) + ".sell")) {
         if (args.length == 0) {
           int slotn = 0;
           Inventory invent = player.getInventory();
           int heldslot = player.getInventory().getHeldItemSlot();
           int itd = 0;
           // Sells the held item slot first.
           if (invent.getItem(heldslot) != null) {
             itd = invent.getItem(heldslot).getTypeId();
           }
           if (itd != 0) {
             int da = calc.getpotionDV(invent.getItem(heldslot));
             int newdat = calc.newData(itd, da);
             String ke = itd + ":" + newdat;
             String nam = hc.getnameData(ke);
             int amount = tran.countInvitems(itd, newdat, player);
             if (nam != null) {
               if (s.has(s.getShop(player), nam)) {
                 tran.sell(nam, itd, newdat, amount, player);
               } else {
                 sendMessage("§9Sorry, one or more of those items cannot be traded at this shop.");
               }
             }
           }
           // Sells remaining items after the held slot.
           while (slotn < 36) {
             if (invent.getItem(slotn) == null) {
               itd = 0;
             } else {
               itd = invent.getItem(slotn).getTypeId();
             }
             if (itd != 0) {
               ItemStack itemn = invent.getItem(slotn);
               if (ench.hasenchants(itemn) == false) {
                 int da = calc.getpotionDV(invent.getItem(slotn));
                 int newdat = calc.newData(itd, da);
                 String ke = itd + ":" + newdat;
                 String nam = hc.getnameData(ke);
                 if (nam != null) {
                   int amount = tran.countInvitems(itd, newdat, player);
                   if (s.has(s.getShop(player), nam)) {
                     tran.sell(nam, itd, newdat, amount, player);
                   } else {
                     sendMessage(
                         "§9Sorry, one or more of those items cannot be traded at this shop.");
                   }
                 }
               } else {
                 sendMessage("§9You cannot buy or sell enchanted items!");
               }
             }
             slotn++;
           }
         } else {
           player.sendMessage("§4Invalid Parameters. Use /sellall");
           return;
         }
       } else {
         player.sendMessage("§9Sorry, you don't have permission to trade here.");
         return;
       }
     } else {
       player.sendMessage("§4You must be in a shop to buy or sell!");
       return;
     }
   } catch (Exception e) {
     player.sendMessage("§4Invalid Parameters. Use /sellall");
     return;
   }
 }
  @Override
  public Value solveEquation(final User user, final Calculation calculation)
      throws NimbitsException {

    final MathEvaluator m = new MathEvaluatorImpl(calculation.getFormula());
    log.info(calculation.getFormula());

    if (!Utils.isEmptyString(calculation.getX()) && calculation.getFormula().contains("x")) {
      //  Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getX());
      final Entity p =
          EntityServiceFactory.getInstance()
              .getEntityByKey(user, calculation.getX(), EntityType.point)
              .get(0);

      if (p != null) {
        log.info("calc has an x car and i found " + p.getName());
        final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p);

        final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue();

        m.addVariable("x", d);
      } else {
        log.severe("calc has an x car and x not found");
      }
    }
    if (!Utils.isEmptyString(calculation.getY()) && calculation.getFormula().contains("y")) {
      final Entity p =
          EntityServiceFactory.getInstance()
              .getEntityByKey(user, calculation.getY(), EntityType.point)
              .get(0);

      // Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getY());
      if (p != null) {
        final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p);
        final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue();
        m.addVariable("y", d);
      }
    }
    if (!Utils.isEmptyString(calculation.getZ()) && calculation.getFormula().contains("z")) {
      final Entity p =
          EntityServiceFactory.getInstance()
              .getEntityByKey(user, calculation.getZ(), EntityType.point)
              .get(0);

      //  Point p = PointServiceFactory.getInstance().getPointByKey(calculation.getZ());
      if (p != null) {
        final List<Value> val = ValueServiceFactory.getInstance().getCurrentValue(p);
        final double d = val.isEmpty() ? 0.0 : val.get(0).getDoubleValue();
        m.addVariable("z", d);
      }
    }

    final Double retVal = m.getValue();

    if (retVal == null) {

      throw new NimbitsException("Formula returned a null value: " + calculation.getFormula());
    }

    return ValueFactory.createValueModel(retVal, "CV");
  }
예제 #11
0
 public void getValue() {
   System.out.println("enter first digit");
   c.value1 = m.op.nextInt();
   System.out.println("enter second digit");
   c.value2 = m.op.nextInt();
 }
예제 #12
0
 @Test
 public void test() {
   Calculation calculation = new Calculation();
   assertEquals(10, calculation.calcCharge());
   assertNotSame(9, calculation.calcCharge());
 }
 public static void main(String args[]) {
   Calculation obj = new Calculation();
   obj.sum(10, 10, 10);
   obj.sum(20, 20);
 }