diff --git a/knoda/src/C005_AddressCorrecter.java b/knoda/src/C005_AddressCorrecter.java new file mode 100644 index 0000000000000000000000000000000000000000..5b07a2240a09cafc15b925a6c64ec42eb402d282 --- /dev/null +++ b/knoda/src/C005_AddressCorrecter.java @@ -0,0 +1,71 @@ +package hellojunit; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class C005_AddressCorrecter { + + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + // 入力するIPアドレスの個数を入力する + final int IPNum = scan.nextInt(); + + // IPAddressList : IPアドレスが格納されているリスト + final List IPAddressList = new ArrayList(); + + // IPアドレスを入力する + for (int IP = 1; IP <= IPNum; IP++) { + IPAddressList.add(scan.next()); + } + + IPAddressCorrecter ipAddressCorrecter = new IPAddressCorrecter(IPAddressList); + + // 入力したそれぞれのIPアドレスのフォーマットが適切かどうかを取得し、出力する + final List correctIPAddress = ipAddressCorrecter.getIsCorrectIPAddress(); + for (String trueOrFalse : correctIPAddress) { + System.out.println(trueOrFalse); + } + scan.close(); + } + +} + +// IPアドレスのフォーマットが適切かを調べるクラス +class IPAddressCorrecter { + private final List IPAddressList; + + public IPAddressCorrecter(final List IPAddressList) { + this.IPAddressList = IPAddressList; + } + + // IPアドレスのフォーマットが適切かをTrueもしくはFalseを用い、リストとして取得 + public List getIsCorrectIPAddress() { + + // correctIPAddress : IPアドレスのフォーマットが適切かを格納するリスト(要素の値はTrueもしくはFalse) + List correctIPAddress = new ArrayList(); + + // 入力したそれぞれのIPアドレスのフォーマットが適切かを調べる + for (String IPAddress : IPAddressList) { + + if (isCorrectIPAddress(IPAddress)) { + correctIPAddress.add("True"); + } else { + correctIPAddress.add("False"); + } + } + return correctIPAddress; + } + + + // IPアドレスのフォーマットが正しいかどうかを判定するメソッド + private boolean isCorrectIPAddress(final String IPAddress) { + Pattern pattern = Pattern.compile("^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"); + Matcher matcher = pattern.matcher(IPAddress); + return matcher.find(); + } + +} diff --git a/knoda/src/C069_Festival.java b/knoda/src/C069_Festival.java deleted file mode 100644 index b64383cdc058fda88a43357cca4ab58d5715cee2..0000000000000000000000000000000000000000 --- a/knoda/src/C069_Festival.java +++ /dev/null @@ -1,82 +0,0 @@ -package hellojunit; - -import java.util.Scanner; - -public class C069_Festival { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - final int year = scan.nextInt(); - final int month = scan.nextInt(); - final int day = scan.nextInt(); - final int festivalMonth = scan.nextInt(); - final int festivalDay = scan.nextInt(); - - Festival festival = new Festival(year, month, day, festivalMonth, festivalDay); - System.out.println(festival.getDayUntilFestival()); - - scan.close(); - } -} - - -class Festival { - private int year; - private int month; - private int day; - private final int festivalMonth; - private final int festivalDay; - private static final int ODD_NUMBER_MONTH_DAY = 13; // 奇数月の日数 - private static final int EVEN_NUMBER_MONTH_DAY = 15; // 偶数月の日数 - - public Festival(final int year, final int month, final int day, final int festivalMonth, final int festivalDay) { - this.year = year; - this.month = month; - this.day = day; - this.festivalMonth = festivalMonth; - this.festivalDay = festivalDay; - } - - // ある月の日数を取得するメソッド - private int getDaysOfMonth(final int month) { - if (month % 2 == 0) { - return EVEN_NUMBER_MONTH_DAY; - } - return ODD_NUMBER_MONTH_DAY; - } - - // 祭りが開催される年であるかを判定するメソッド - private boolean isFestivalYear(final int year) { - return year % 4 == 1; - } - - // 年と月をカウントアップするメソッド - private void countUpMonth() { - month++; - if (month > 13) { - month = 1; - year++; - } - } - - // 祭り開催までの残り日数を取得するメソッド - public int getDayUntilFestival() { - int tempYear = year + 1; - int dayUntilFestival = 0; - while (!isFestivalYear(tempYear)) { - tempYear++; - } - - final int festivalYear = tempYear; - - dayUntilFestival += getDaysOfMonth(month) - day; - countUpMonth(); - - while ((year != festivalYear) || (month != festivalMonth)) { - dayUntilFestival += getDaysOfMonth(month); - countUpMonth(); - } - dayUntilFestival += festivalDay; - return dayUntilFestival; - } -} diff --git a/knoda/test/C005_AddressCorrecterTest.java b/knoda/test/C005_AddressCorrecterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2369e8348c119d87b7aded7791db45464f4c239a --- /dev/null +++ b/knoda/test/C005_AddressCorrecterTest.java @@ -0,0 +1,62 @@ +package hellojunit; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +public class C005_AddressCorrecterTest { + + @Test + public void 入力したIPアドレスがすべてTrueのとき() { + List IPAddressList = new ArrayList(); + IPAddressList.add("192.168.0.1"); + IPAddressList.add("192.168.0.2"); + IPAddressList.add("192.168.0.3"); + IPAddressList.add("192.168.0.4"); + + IPAddressCorrecter sut = new IPAddressCorrecter(IPAddressList); + List expectedList = new ArrayList(Arrays.asList("True", "True", "True", "True")); + List actualList = sut.getIsCorrectIPAddress(); + for (int i = 0; i < expectedList.size(); i++) { + assertThat(actualList.get(i), is(expectedList.get(i))); + } + } + + @Test + public void 入力したIPアドレスが整数値でTrueとFalseの両方が存在するとき() { + List IPAddressList = new ArrayList(); + IPAddressList.add("192.400.1.10.1000..."); + IPAddressList.add("4.3.2.1"); + IPAddressList.add("0..33.444..."); + IPAddressList.add("1.2.3.4"); + + IPAddressCorrecter sut = new IPAddressCorrecter(IPAddressList); + List expectedList = new ArrayList(Arrays.asList("False", "True", "False", "True")); + List actualList = sut.getIsCorrectIPAddress(); + for (int i = 0; i < expectedList.size(); i++) { + assertThat(actualList.get(i), is(expectedList.get(i))); + } + } + + @Test + public void 入力したIPアドレスで整数以外が存在するとき() { + List IPAddressList = new ArrayList(); + IPAddressList.add("192.Noda.0.Keisuke"); + IPAddressList.add("4.3.2.1"); + IPAddressList.add("0.野田.0.啓介"); + IPAddressList.add("1.2.3.4.5.6.7.8.9.noda"); + + IPAddressCorrecter sut = new IPAddressCorrecter(IPAddressList); + List expectedList = new ArrayList(Arrays.asList("False", "True", "False", "False")); + List actualList = sut.getIsCorrectIPAddress(); + for (int i = 0; i < expectedList.size(); i++) { + assertThat(actualList.get(i), is(expectedList.get(i))); + } + } + +} diff --git a/knoda/test/C069_FestivalTest.java b/knoda/test/C069_FestivalTest.java deleted file mode 100644 index cf883eb8f76fc71e41dfabdcc441df2eea29a2a3..0000000000000000000000000000000000000000 --- a/knoda/test/C069_FestivalTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package hellojunit; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import org.junit.Test; - -public class C069_FestivalTest { - - @Test - public void 日数を取得するテスト1() { - Festival sut = new Festival(2000, 12, 10, 1, 10); - int expected = 28; - int actual = sut.getDayUntilFestival(); - assertThat(actual, is(expected)); - } - - @Test - public void 日数を取得するテスト2() { - Festival sut = new Festival(1994, 4, 8, 7, 13); - int expected = 591; - int actual = sut.getDayUntilFestival(); - assertThat(actual, is(expected)); - } - -} diff --git a/ykoiso/src/B108_FerrisWheel.java b/ykoiso/src/B108_FerrisWheel.java deleted file mode 100644 index da85cc55f4e188fb8772e1a7404736929ec96203..0000000000000000000000000000000000000000 --- a/ykoiso/src/B108_FerrisWheel.java +++ /dev/null @@ -1,93 +0,0 @@ -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class B108_FerrisWheel { - public static void main(String[] args) { - final Scanner scan = new Scanner(System.in); - // 定数の読み込み - int GONDOLANUM = scan.nextInt(); - int GROUPNUM = scan.nextInt(); - List capacity = new ArrayList<>(); - List group = new ArrayList<>(); - // ゴンドラの乗車上限 - for (int i = 0; i < GONDOLANUM; i++) { - int num = scan.nextInt(); - capacity.add(num); - } - // グループごとの人数 - for (int i = 0; i < GROUPNUM; i++) { - int num = scan.nextInt(); - group.add(num); - } - // 観覧車作成 - B108_FerrisWheel b = new B108_FerrisWheel(); - FerrisWheel ferrisWheel = b.new FerrisWheel(GONDOLANUM, GROUPNUM, capacity, group); - // 乗車人数計算 - ferrisWheel.calcPassengers(); - // 乗車人数の表示 - ferrisWheel.showCountPassengers(); - scan.close(); - } - - class FerrisWheel { - private final int GONDOLANUM, GROUPNUM; - private List gondolaCapacity, countPassengers, groupPeopleNum; - - // コンストラクタ - FerrisWheel(final int GONDOLANUM, final int GROUPNUM, List gondolaCapacity, - List groupPeopleNum) { - this.GONDOLANUM = GONDOLANUM; - this.GROUPNUM = GROUPNUM; - this.gondolaCapacity = gondolaCapacity; - countPassengers = new ArrayList<>(); - for (int i = 0; i < GONDOLANUM; i++) { - countPassengers.add(0); - } - this.groupPeopleNum = groupPeopleNum; - } - - // テスト用 - public List getCountPassengers() { - return countPassengers; - } - - // 乗車人数の計算 - public void calcPassengers() { - // ゴンドラの番号 - int j = 0; - // グループごとの処理 - for (int i = 0; i < GROUPNUM; i++) { - // グループが乗り終わったかの判定 - boolean isRideAll = false; - while (isRideAll == false) { - // グループの人数が乗車上限より多かったら - // 上限人数分乗せてグループの人数を減らす - if (groupPeopleNum.get(i) > gondolaCapacity.get(j)) { - countPassengers.set(j, countPassengers.get(j) + gondolaCapacity.get(j)); - groupPeopleNum.set(i, groupPeopleNum.get(i) - gondolaCapacity.get(j)); - } else { - // グループの人数が乗車上限以下なら - // 上限人数分乗せてフラグを立てる - countPassengers.set(j, countPassengers.get(j) + groupPeopleNum.get(i)); - isRideAll = true; - } - // 次のゴンドラへ - ++j; - // ゴンドラが1周したら最初のゴンドラへ - if (j == GONDOLANUM) { - j = 0; - } - // フラグが立ったら次のグループへ - } - } - } - - // 乗車人数表示 - public void showCountPassengers() { - for (int i = 0; i < GONDOLANUM; i++) { - System.out.println(countPassengers.get(i)); - } - } - } -} diff --git a/ykoiso/src/B108_kanransya.java b/ykoiso/src/B108_kanransya.java deleted file mode 100644 index c65b0af994ef2a710c534f18adcd63464962898f..0000000000000000000000000000000000000000 --- a/ykoiso/src/B108_kanransya.java +++ /dev/null @@ -1,87 +0,0 @@ -import java.util.Scanner; - -public class B108_kanransya { - public static void main(String[] args) { - final Scanner scan = new Scanner(System.in); - // 定数の読み込み - int N = scan.nextInt(); - int M = scan.nextInt(); - int[] capacity = new int[N]; - int[] group = new int[M]; - // ゴンドラの乗車上限 - for (int i = 0; i < N; i++) { - capacity[i] = scan.nextInt(); - } - // グループごとの人数 - for (int i = 0; i < M; i++) { - group[i] = scan.nextInt(); - } - // 観覧車作成 - Kanransya kanransya = new Kanransya(N, M, capacity, group); - // 乗車人数計算 - kanransya.calcPassengers(); - // 乗車人数の表示 - kanransya.showCountPassengers(); - scan.close(); - } - - static class Kanransya { - private int N, M; - private int[] gondoraCapacity, countPassengers, groupNum; - - // コンストラクタ - Kanransya(int N, int M, int[] gondoraCapacity, int[] groupNum) { - this.N = N; - this.M = M; - this.gondoraCapacity = gondoraCapacity; - countPassengers = new int[N]; - this.groupNum = groupNum; - } - - // テスト用 - public int[] getCountPassengers() { - return countPassengers; - } - - // 乗車人数の計算 - public void calcPassengers() { - // ゴンドラの番号 - int j = 0; - // グループごとの処理 - for (int i = 0; i < M; i++) { - // グループが乗り終わったかの判定 - boolean isRideAll = false; - while (true) { - // グループの人数が乗車上限より多かったら - // 上限人数分乗せてグループの人数を減らす - if (groupNum[i] > gondoraCapacity[j]) { - countPassengers[j] += gondoraCapacity[j]; - groupNum[i] -= gondoraCapacity[j]; - } else { - // グループの人数が乗車上限以下なら - // 上限人数分乗せてフラグを立てる - countPassengers[j] += groupNum[i]; - isRideAll = true; - } - // 次のゴンドラへ - ++j; - // ゴンドラが1周したら最初のゴンドラへ - if (j == N) { - j = 0; - } - // フラグが立ったら次のグループへ - if (isRideAll == true) { - break; - } - } - } - } - - // 乗車人数表示 - public void showCountPassengers() { - for (int i = 0; i < N; i++) { - System.out.println(countPassengers[i]); - } - } - } -} diff --git a/ykoiso/test/B108_FerrisWheelTest.java b/ykoiso/test/B108_FerrisWheelTest.java deleted file mode 100644 index b1907b78165572f62268de8cc366fc289f1b2bfa..0000000000000000000000000000000000000000 --- a/ykoiso/test/B108_FerrisWheelTest.java +++ /dev/null @@ -1,47 +0,0 @@ -import static org.junit.Assert.assertThat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.Test; -import static org.hamcrest.CoreMatchers.*; - -public class B108_FerrisWheelTest { - - // ゴンドラは3つ - B108_FerrisWheel b = new B108_FerrisWheel(); - B108_FerrisWheel.FerrisWheel sut; - - @Test - public void 全員乗れた時() { - List gondolaCapacity = new ArrayList<>(Arrays.asList(3, 3, 3)); - List groupPeopleNum = new ArrayList<>(Arrays.asList(3)); - sut = b.new FerrisWheel(3, 1, gondolaCapacity, groupPeopleNum); - sut.calcPassengers(); - List actual = sut.getCountPassengers(); - List expected = new ArrayList<>(Arrays.asList(3, 0, 0)); - assertThat(actual, is(expected)); - } - - @Test - public void 全員乗れなかった時() { - List gondolaCapacity = new ArrayList<>(Arrays.asList(3, 3, 3)); - List groupPeopleNum = new ArrayList<>(Arrays.asList(4)); - sut = b.new FerrisWheel(3, 1, gondolaCapacity, groupPeopleNum); - sut.calcPassengers(); - List actual = sut.getCountPassengers(); - List expected = new ArrayList<>(Arrays.asList(3, 1, 0)); - assertThat(actual, is(expected)); - } - - @Test - public void グループが2つの時() { - List gondolaCapacity = new ArrayList<>(Arrays.asList(3, 3, 3)); - List groupPeopleNum = new ArrayList<>(Arrays.asList(4, 3)); - sut = b.new FerrisWheel(3, 2, gondolaCapacity, groupPeopleNum); - sut.calcPassengers(); - List actual = sut.getCountPassengers(); - List expected = new ArrayList<>(Arrays.asList(3, 1, 3)); - assertThat(actual, is(expected)); - } -} - diff --git a/ykoiso/test/B108_kanransyaTest.java b/ykoiso/test/B108_kanransyaTest.java deleted file mode 100644 index 86219cc87e621ea56a1d7bc5a7582dfbe957baed..0000000000000000000000000000000000000000 --- a/ykoiso/test/B108_kanransyaTest.java +++ /dev/null @@ -1,43 +0,0 @@ -import static org.junit.Assert.assertThat; -import org.junit.Test; -import static org.hamcrest.CoreMatchers.*; - -public class B108_kanransyaTest { - - // ゴンドラは3つ - B108_kanransya.Kanransya sut; - - @Test - public void 全員乗れた時() { - int[] gondoraCapacity = {3, 3, 3}; - int[] groupNum = {3}; - sut = new B108_kanransya.Kanransya(3, 1, gondoraCapacity, groupNum); - sut.calcPassengers(); - int[] actual = sut.getCountPassengers(); - int[] expected = {3, 0, 0}; - assertThat(actual, is(expected)); - } - - @Test - public void 全員乗れなかった時() { - int[] gondoraCapacity = {3, 3, 3}; - int[] groupNum = {4}; - sut = new B108_kanransya.Kanransya(3, 1, gondoraCapacity, groupNum); - sut.calcPassengers(); - int[] actual = sut.getCountPassengers(); - int[] expected = {3, 1, 0}; - assertThat(actual, is(expected)); - } - - @Test - public void グループが2つの時() { - int[] gondoraCapacity = {3, 3, 3}; - int[] groupNum = {4, 3}; - sut = new B108_kanransya.Kanransya(3, 2, gondoraCapacity, groupNum); - sut.calcPassengers(); - int[] actual = sut.getCountPassengers(); - int[] expected = {3, 1, 3}; - assertThat(actual, is(expected)); - } -} -