Skip to content

Instantly share code, notes, and snippets.

@itokami1123dev
Last active August 29, 2015 14:26
Show Gist options
  • Save itokami1123dev/ef9d856577a489b4d7be to your computer and use it in GitHub Desktop.
Save itokami1123dev/ef9d856577a489b4d7be to your computer and use it in GitHub Desktop.

Java初心者勉強会

2015/8/7(金) 19:00 プログラムを実際に書きながら基礎から学ぶ勉強会 http://www.zusaar.com/event/6237003

1. プログラム開始を記述

public class Main {

    public static void main(String... args) {
        System.out.println("Hello Calculator!");
    }
}
SET PATH
SET PATH=C:\Program Files\Java\jdk1.8.0_45\bin;%PATH%

javac Main.java
java Main
Hello Calculator!

2. 計算クラスを作成

import java.util.Stack;

public class Calculator {
    public Calculator() {
    }

    public int execute(String input) {
        Stack<Integer> stack = new Stack<>();

        for (String s : input.split("\\s")) {
            if ("+".equals(s)) {
                Integer num1 = stack.pop();
                Integer num2 = stack.pop();
                stack.push(Integer.valueOf(num1 + num2));

            } else {
                stack.push(Integer.valueOf(s));

            }
        }

        return stack.pop();
    }
}
public class Main {
  public static void main(String... args) {
    System.out.println("Hello Calculator!");
    Calculator cal = new Calculator();
    String test = "1 2 +";
    System.out.format(" %s = %d ", test, cal.execute(test));
  }
}
javac Main.java
java Main

Hello Calculator!
 1 2 + = 3

3.処理の追加を分業可能にする

2つ引数の返し値一つのインターフェースを作る

public interface Operator {
    Integer execute(Integer num1, Integer num2);
}

足し算の計算クラス

public class Add implements Operator {
    @Override
    public Integer execute(Integer num0, Integer num1) {
        return Integer.valueOf(num0 + num1);
    }
}

掛け算の計算クラス

public class Multi implements Operator {
    @Override
    public Integer execute(Integer num0, Integer num1) {
        return Integer.valueOf(num0 * num1);
    }
}

計算クラスを修正

import java.util.*;

public class Calculator {
    private final Map<String, Operator> operators;

    public Calculator(Map<String, Operator> operators) {
        this.operators = operators;
    }

    public int execute(String input) {
        Stack<Integer> stack = new Stack<>();

        for (String s : input.split("\\s")) {

            Operator operator = operators.get(s);

            if (operator != null) {
                Integer num1 = stack.pop();
                Integer num2 = stack.pop();
                stack.push(operator.execute(num1, num2));

            } else {
                stack.push(Integer.valueOf(s));

            }
        }

        return stack.pop();
    }
}

起動クラスを修正

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String... args) {
        System.out.println("Hello Calculator!");

        Map<String, Operator> operators = new HashMap<>();
        operators.put("+", new Add());
        operators.put("*", new Multi());
        Calculator cal = new Calculator(operators);

        String test = "3 2 1 + *";
        System.out.format(" %s = %d ", test, cal.execute(test));
    }
}

4.匿名クラスで計算式を追加

package com.company;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello Calculator!");

        Map<String, Operator> operators = new HashMap<>();
        operators.put("+", new Add());
        operators.put("*", new Multi());
        operators.put("-", new Operator() {
            @Override
            public Integer execute(Integer num1, Integer num2) {
                return num2 - num1;
            }
        });
        Calculator cal = new Calculator(operators);

        String test = "15 3 2 1 + * -";
        System.out.format(" %s = %d ", test, cal.execute(test));    }
}

5. ラムダで書くと

package com.company;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello Calculator!");

        Map<String, Operator> operators = new HashMap<>();
        operators.put("+", new Add());
        operators.put("*", new Multi());
        operators.put("-", (Integer num1, Integer num2) -> {
            return num2 - num1;
        });
        Calculator cal = new Calculator(operators);

        String test = "15 3 2 1 + * -";
        System.out.format(" %s = %d ", test, cal.execute(test));
    }
}

もっと短く書くと

package com.company;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello Calculator!");

        Map<String, Operator> operators = new HashMap<>();
        operators.put("+", new Add());
        operators.put("*", new Multi());
        operators.put("-", (num1, num2) -> num2 - num1);
        Calculator cal = new Calculator(operators);

        String test = "15 3 2 1 + * -";
        System.out.format(" %s = %d ", test, cal.execute(test));
    }
}

6. 割り算を足して終わりです

package com.company;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello Calculator!");

        Map<String, Operator> operators = new HashMap<>();
        operators.put("+", new Add());
        operators.put("*", new Multi());
        operators.put("-", (num1, num2) -> num2 - num1);
        operators.put("/", (num1, num2) -> num2 / num1);
        Calculator cal = new Calculator(operators);

        String test = "12 15 3 2 1 + * - /";
        System.out.format(" %s = %d ", test, cal.execute(test));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment