diff --git a/Frac.java b/Frac.java new file mode 100644 index 0000000..aabb0cd --- /dev/null +++ b/Frac.java @@ -0,0 +1,95 @@ +import java.util.Scanner; + +public class Frac { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + int a = scan.nextInt(); + int b = scan.nextInt(); + Frac f1 = new Frac(a, b); // ここで、コンストラクタを呼び、オブジェクトを生成する + + // (参考)privateなメンバー変数なので、コンパイルエラーになる。 + // f1.a = 1; + f1.yakubun(); + System.out.println(f1.toString()); + scan.close(); + } + + private int a, b; + + Frac(int a, int b) { // コンストラクタ + setBunsi(a); + setBumbo(b); + } + + public void setBunsi(int _a) { + a = _a; + this.checkSign(); + } + + public void setBumbo(int _b) { + if (_b == 0) { + // Javaで、実行時に「例外を発生させる」コード例は、以下です。 + throw new RuntimeException("Bumbo is zero"); + } + b = _b; + this.checkSign(); + } + + public void checkSign() { + /* 分子も分母も、マイナスだったら、符号を反転させてください */ + if (a < 0 && b < 0) { + a = -a; + b = -b; + } + } + + public String toString() { + return a + "/" + b; + } + + public void yakubun() { + // ユークリッドの互除法を使って、最大公約数を求める + int ta = Math.abs(a); + int tb = Math.abs(b); + while (tb != 0) { + int r = ta % tb; + ta = tb; + tb = r; + } + // 最大公約数で割る + a /= ta; + b /= ta; + } + + public Frac plus(Frac f) { + int newa = this.a * f.b + f.a * this.b; + int newb = this.b * f.b; + return new Frac(newa, newb); + } + public Frac minus(Frac f) { + /* ここを、返却値をふくめ、編集する */ + return new Frac(1, 2); + } + public Frac 逆数(){ + /* ここを、返却値をふくめ、編集する */ + return new Frac(1, 2); + } + public Frac mul(Frac f) { + /* ここを、返却値をふくめ、編集する */ + return new Frac(1, 2); + } + public Frac div(Frac f) { + /* ここを、返却値をふくめ、編集する + 逆数() をうまく使うとシンプルに書けます。 */ + return this.mul(f.逆数()); + } + // 分数の数値としては等しいとき true(約分してなくてもよい。例:1/2 equals 2/4 ) + public boolean equals(Frac f) { + return this.a * f.b == this.b * f.a; + } + // 分数の数値として等しく、かつ、分子分母の値もすべて等しいとき true + public boolean same(Frac f) { + return this.a == f.a && this.b == f.b; + } +} diff --git a/TestFrac.java b/TestFrac.java new file mode 100644 index 0000000..31c1a1d --- /dev/null +++ b/TestFrac.java @@ -0,0 +1,71 @@ +public class TestFrac { + public static void main(String[] args) { + Frac f1 = new Frac(1, 2); + Frac f2 = new Frac(3, 4); + + boolean allOk = true; + allOk &= assertEquals(f1, new Frac(1, 2)); + allOk &= assertEquals(f2, new Frac(3, 4)); + allOk &= assertEquals(f1.plus(f2), new Frac(5, 4)); + allOk &= assertEquals(f1.minus(f2), new Frac(-1, 4)); + allOk &= assertEquals(f1.mul(f2), new Frac(3, 8)); + allOk &= assertEquals(f1.div(f2), new Frac(2, 3)); + allOk &= assertEquals(f1.逆数(), new Frac(2, 1)); + + Frac f3 = new Frac(42, 18); + Frac f4 = new Frac(7, 3); + f3.yakubun(); + allOk &= assertSame(f3, f4); + if (allOk) { + System.out.println("All tests passed."); + } else { + System.out.println("Some tests failed."); + } + } + + /** + * 分数を値としてみたときに同じであればtrue + * @param a1 + * @param a2 + * @return + */ + private static boolean assertEquals(Frac a1, Frac a2) { + if (a1.equals(a2)) { + return true; + } else { + System.out.println("NG "+ a1 + " != " + a2); + printCurrentLocation(); + return false; + } + } + /** + * 分子・分母も同じであればtrue + * @param a1 + * @param a2 + * @return + */ + private static boolean assertSame(Frac a1, Frac a2) { + if (a1.same(a2)) { + return true; + } else { + System.out.println("NotSame "+ a1 + " != " + a2); + printCurrentLocation(); + return false; + } + } + + public static void printCurrentLocation() { + StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + StackTraceElement element = stackTrace[3]; + String fileName = element.getFileName(); + int lineNumber = element.getLineNumber(); + String methodName = element.getMethodName(); + String className = element.getClassName(); + + System.out.println(" アサーション失敗箇所"); + System.out.println(" > File: " + fileName); + System.out.println(" > Line: " + lineNumber); + System.out.println(" > Class: " + className); + System.out.println(" > Method: " + methodName); + } +} \ No newline at end of file