public static void main(String[] args) {
    // Create a new Adder object
    Adder a = new SolutionInheritance2().new Adder();

    // Print the name of the superclass on a new line
    System.out.println("My superclass is: " + a.getClass().getSuperclass().getName());

    // Print the result of 3 calls to Adder's `add(int,int)` method as 3 space-separated integers:
    System.out.print(a.add(10, 32) + " " + a.add(10, 3) + " " + a.add(10, 10) + "\n");
  }
Exemple #2
0
  public void runLoop() throws IOException {

    while (true) {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      System.out.println("Welcome to supercalc");
      System.out.println("enter command:");
      String line = br.readLine();

      if (line.equals("e")) {
        System.out.println("good bye, have a nice day...");
        break;
      } else {
        String[] args = line.split(" ");
        if (args.length == 3) {
          String opCode = args[0];
          Integer op1 = Integer.parseInt(args[1]);
          Integer op2 = Integer.parseInt(args[2]);

          if (opCode.equals("+")) {
            System.out.println("result: " + adder.add(op1, op2));
          } else if (opCode.equals("-")) {
            System.out.println("result: " + sub.subtract(op1, op2));
          }
        }
      }
    }
  }
  public static void main(String[] args) {

    Adder adder1 =
        (x, y) -> {
          return x + y;
        };

    int x1 = adder1.add(40, 2);

    Adder adder2 =
        new Adder() {
          @Override
          public int add(int x, int y) {
            return x + y;
          }
        };

    int x2 = adder2.add(39, 3);

    if (x1 == x2) {
      VMTest.markResult(false);
    }
  }
  ///////////////////////////////////////////
  //   Exercise: class that adds Euro and CHF
  //
  //   Wechselkurs
  //   W�hrung
  //   Zielw�hrung ber�cksichtigen!
  //   done Summands are changeable
  //   Wie kommt der Wechselkurs in die Methode?
  //
  //
  @Test
  public void addCurrency() {

    Adder adder = new Adder();
    ExchangeRateService service = new ExchangeRateService();

    assertEquals(5 + 10 * service.getRate(chf, euro), adder.add(5, euro, 10, chf, euro), 0.00001);
    assertEquals(7 + 10 * service.getRate(chf, euro), adder.add(7, euro, 10, chf, euro), 0.00001);
    assertEquals(7 + 10 * service.getRate(chf, euro), adder.add(10, chf, 7, euro, euro), 0.00001);
    assertEquals(
        (7 + 10 * service.getRate(chf, euro)) * service.getRate(euro, chf),
        adder.add(10, chf, 7, euro, chf),
        0.00001);
    assertEquals(5 + 10, adder.add(5, euro, 10, euro, euro), 0.00001);
    assertEquals(5 + 10, adder.add(5, chf, 10, chf, chf), 0.00001);
    assertEquals(
        5 + 10 * service.getRate(dollar, euro), adder.add(5, euro, 10, dollar, euro), 0.00001);
  }
 public static void main(String[] argh) {
   Adder X = new Adder();
   System.out.println("My superclass is: " + X.getClass().getSuperclass().getName());
   System.out.print(X.add(10, 32) + " " + X.add(10, 3) + " " + X.add(10, 10) + "\n");
 }
 @当("^加上一个正数(\\d+)$")
 public void 加上一个正数(int arg1) throws Throwable {
   adder.add(arg1);
 }
 public void testAdd() {
   Adder adder = new Adder();
   int re = adder.add(100, 200);
   assertEquals(300, re);
 }
Exemple #8
0
  public Any exec(Any a) throws AnyException {
    Transaction t = getTransaction();

    Composite sumRoot = (Composite) EvalExpr.evalFunc(t, a, sumRoot_, Composite.class);

    if (sumRoot == null) nullOperand(sumRoot_);

    Any expression = null;
    Any sum = null;
    Any tmp = null;
    Adder adder = null;

    if (takeAverage_ && sumRoot.entries() == 0) return null;

    Iter i = sumRoot.createIterator();

    Any loop = t.getLoop();

    try {
      while (i.hasNext()) {
        Any child = i.next();

        // Set $loop
        t.setLoop(child);

        // Lazy clone of expression
        if (expression == null) {
          expression = expression_.cloneAny();
        }

        if (sum == null) {
          sum = EvalExpr.evalFunc(t, a, expression);

          if (sum == null)
            throw new NullPointerException("Failed to resolve " + expression_ + "during summation");

          // To save on the creation of temporaries, the first sum
          // item is used as the prototype for the result.  The
          // remaining items, if not the same, must be convertible to it.
          adder = new Adder(sum);
          sum = adder.add(sum);
          tmp = sum.buildNew(null);
          adder.setTmp(tmp);
        } else {
          Any next = EvalExpr.evalFunc(t, a, expression);
          if (next == null)
            throw new NullPointerException("Failed to resolve " + expression_ + "during summation");

          adder.add(next);
        }

        // No point in continuing the iteration if we hit a null
        if (adder.isNull()) break;
      }

      if (adder != null && adder.isNull()) return AnyNull.instance();

      if (takeAverage_ && sum != null) return adder.doAvg(sum, sumRoot.entries());

      return sum == null ? AnyNull.instance() : sum;
    } finally {
      t.setLoop(loop);
    }
  }