一週間で身につくJava言語の基本の解答集 ~基本編の問題①~

こちらの記事は、下記のサイトを使用して教育を行っている際に
解答がなくて不便だなと思い、復習もかねてのお勉強で解答集を作成したものになります！

出来るだけ難しい書き方をせずに書くことを目的に記載中
https://java.sevendays-study.com/


※量が多いのでまずは基本編の問題の0日目～7日目まで

## 基本問題(1)：最も基本的なプログラム、演算と変数
prob1-1.(難易度：★)
```java
    public static void main(String[] args) {
        System.out.println("田中　太郎");
    }
```
***
prob1-2.(難易度：★)
```java
    public static void main(String[] args) {
        System.out.println("ABCDEF\nGHI");
    }
```
***
prob1-3.(難易度：★)
```java
    public static void main(String[] args) {
        System.out.println(1 + " + " + 1 + " = " + (1 + 1));
        System.out.println(2 + " + " + 3 + " = " + (2 + 3));
    }
```
***
prob1-4(難易度：★)
```java
    public static void main(String[] args) {
        int num1 = 1;
        int num2 = 2;
        int num3 = 3;

        int sum = num1 + num2 + num3;

        System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + sum);
    }
```
***
prob2-1.(難易度：★)
```java
    public static void main(String[] args) {
        int a = 5, b = 3;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("a + b = " + (a + b));
        System.out.println("a - b = " + (a - b));
        System.out.println("a * b = " + (a * b));
        System.out.println("a / b = " + (a / b));
        System.out.println("a % b = " + (a % b));
    }
```
***
prob2-2.(難易度：★★)
```java
    public static void main(String[] args) {
        int a = 1, b = 2, c = 3, d = 4, e = 5;
        a += 2;
        b -= 1;
        c *= 3;
        d /= 2;
        e %= 2;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("c = " + c);
        System.out.println("d = " + d);
        System.out.println("e = " + e);
    }
```

## 基本問題(2)：条件分岐
prob3-1.(難易度：★)
```java
    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(10) + 1;

        System.out.println("乱数: " + randomNumber);

        if (randomNumber >= 5) {
            System.out.println("5以上です");
        }
    }
```
***
prob3-2.(難易度：★)
```java
    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(10) + 1;

        System.out.println("乱数: " + randomNumber);

        if (randomNumber != 1) {
            System.out.println("1ではありません");
        }
    }
```
***
prob3-3.(難易度：★)
```java
    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(100) + 1;

        System.out.println("乱数: " + randomNumber);

        if (randomNumber < 50) {
            System.out.println("50未満です");
        }
    }
```
***
prob3-4.(難易度：★)
```java
    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(100) + 1;

        System.out.println("乱数: " + randomNumber);

        if (randomNumber <= 10 || randomNumber >= 90) {
            System.out.println("10以下か90以上の値です");
        }
    }
```
***
prob3-5.(難易度：★)
```java
    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(100) + 1;

        System.out.println("乱数: " + randomNumber);

        if (randomNumber >= 20 && randomNumber < 80) {
            System.out.println("20以上80未満です");
        }
    }
```
***
prob3-6.(難易度：★)
```java
    public static void main(String[] args) {
        int num = (int) (Math.random() * 6) + 1;
        System.out.println("数値 : " + num);

        if (num >= 3) {
            System.out.println("3以上です");
        }
    }
```
***
prob3-7.(難易度：★)
```java
import java.util.Random;

public class Problem3_7 {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(10) + 1;
        System.out.println(num);

        if (num >= 5) {
            System.out.println("5以上です");
        } else {
            System.out.println("5未満です");
        }
    }
}
```
***
prob3-8.(難易度：★)
```java
import java.util.Random;

public class Problem3_8 {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(10) + 1;
        System.out.println(num);

        if (num != 1) {
            System.out.println("1ではありません");
        } else {
            System.out.println("1です");
        }
    }
}
```
***
prob3-9.(難易度：★)
```java
import java.util.Random;

public class Problem3_9 {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(100) + 1;
        System.out.println(num);

        if (num < 50) {
            System.out.println("50未満です");
        } else {
            System.out.println("50以上です");
        }
    }
}
```
***
prob3-10.(難易度：★)
```java
import java.util.Random;

public class Problem3_10 {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(100) + 1; // 1から100までの乱数を生成
        System.out.println("数値: " + num);

        if (num <= 10 || num >= 90) {
            System.out.println("10以下か90以上の値です");
        } else {
            System.out.println("10より大きく90未満です");
        }
    }
}
```
***
prob3-11.(難易度：★)
```java
import java.util.Random;

public class Problem3_11 {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(100) + 1; // 1から100までの乱数を生成
        System.out.println("数値: " + num);

        if (num >= 20 && num < 80) {
            System.out.println("20以上80未満です");
        } else {
            System.out.println("20未満か、80以上です");
        }
    }
}
```
***
prob3-12.(難易度：★)
```java
import java.util.Random;

public class Problem3_12 {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(21) - 10; // -10から10までの乱数を生成
        System.out.println("数値: " + num);

        if (num < 0) {
            System.out.println("負の値です");
        } else if (num > 0) {
            System.out.println("正の値です");
        } else {
            System.out.println("0です");
        }
    }
}
```
***
prob3-13.(難易度：★)
```java
import java.util.Random;

public class Problem3_13 {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(3) + 1; // 1から3までの乱数を生成
        System.out.print("数値: " + num + " - ");

        if (num == 1) {
            System.out.println("グー");
        } else if (num == 2) {
            System.out.println("パー");
        } else {
            System.out.println("チョキ");
        }
    }
}
```
***
prob3-15.(難易度：★★)
```java
import java.util.Random;

public class Problem3_15 {

    public static void main(String[] args) {
        Random random = new Random();
        int score = random.nextInt(100) + 1; // 1から100までの乱数を生成
        System.out.println("点数: " + score);

        if (score >= 80) {
            System.out.println("優");
        } else if (score >= 70) {
            System.out.println("良");
        } else if (score >= 60) {
            System.out.println("可");
        } else {
            System.out.println("不可");
        }
    }
}
```
***
prob3-16.(難易度：★★)
```java
import java.util.Random;

public class Problem3_16 {

    public static void main(String[] args) {
        Random random = new Random();
        int number = random.nextInt(100) + 1; // 1から100までの乱数を生成
        System.out.println("数値: " + number);

        if (number % 2 == 0 && number % 3 == 0) {
            System.out.println("2と3の公倍数です");
        } else if (number % 2 == 0) {
            System.out.println("2の倍数です");
        } else if (number % 3 == 0) {
            System.out.println("3の倍数です");
        }
    }
}
```
***
prob3-17.(難易度：★★)
```java
import java.util.Random;

public class Problem3_17 {

    public static void main(String[] args) {
        Random random = new Random();
        int number = random.nextInt(100) + 1; // 1から100までの乱数を生成
        System.out.println("数値: " + number);

        if (number <= 50 && number % 2 == 0) {
            System.out.println("50以下の偶数です");
        } else if (number <= 50) {
            System.out.println("50以下です");
        } else if (number % 2 == 0) {
            System.out.println("偶数です");
        }
    }
}
```
***
prob3-18.(難易度：★★)
```java
import java.util.Random;

public class Problem3_18 {

    public static void main(String[] args) {
        Random random = new Random();
        int temperature = random.nextInt(46) - 10; // -10から35までの乱数を生成
        System.out.println("摂氏" + temperature + "度");

        if (temperature >= 30) {
            System.out.println("真夏日です");
        } else if (temperature >= 25) {
            System.out.println("夏日です");
        } else if (temperature < 0) {
            System.out.println("真冬日です");
        }
    }
}
```

## 基本問題(3)：繰り返し処理
prob4-1.(難易度：★)
```java
import java.util.Random;

public class Problem4_1 {

    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(10) + 1; // 1から10までの乱数を生成
        System.out.println("数: " + randomNumber);

        for (int i = 0; i < randomNumber; i++) {
            System.out.print("■");
        }
        System.out.println(); // 改行
    }
}
```
***
prob4-2.(難易度：★)
```java
import java.util.Random;

public class Problem4_2 {

    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(10) + 1; // 1から10までの乱数を生成
        System.out.println("数: " + randomNumber);

        int i = 0;
        while (i < randomNumber) {
            System.out.print("■");
            i++;
        }
        System.out.println(); // 改行
    }
}
```
***
prob4-3.(難易度：★)
```java
import java.util.Random;

public class Problem4_3 {

    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(10) + 1; // 1から10までの乱数を生成
        System.out.println("数: " + randomNumber);

        int i = 0;
        do {
            System.out.print("■");
            i++;
        } while (i < randomNumber);
        System.out.println(); // 改行
    }
}
```
***
prob4-4(難易度：★)
```java
public class Problem4_4 {

    public static void main(String[] args) {
        int a = 0;
        while (a < 4) {
            System.out.println("a = " + a);
            a++;
        }
    }
}
```
***
prob4-5(難易度：★)
```java
public class Problem4_5 {

    public static void main(String[] args) {
        for (int a = 0; a < 4; a++) {
            System.out.println("a = " + a);
        }
    }
}
```
***
prob4-6(難易度：★)
```java
public class Problem4_6 {

    public static void main(String[] args) {
        int a = 3;
        while (a >= 0) {
            System.out.println("a = " + a);
            a--;
        }
    }
}
```
***
prob4-7(難易度：★)
```java
public class Problem4_7 {

    public static void main(String[] args) {
        for (int a = 3; a >= 0; a--) {
            System.out.println("a = " + a);
        }
    }
}
```
***
prob4-8.(難易度：★★)
```java
import java.util.Random;

public class Problem4_8 {

    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber;
        
        while (true) {
            randomNumber = random.nextInt(10) + 1; // 1から10までの乱数を生成
            System.out.println(randomNumber);
            if (randomNumber == 10) {
                System.out.println("終了します");
                break;
            }
        }
    }
}
```
***
prob4-9.(難易度：★★)
```java
import java.util.Random;

public class Problem4_9 {

    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber;
        
        while (true) {
            randomNumber = random.nextInt(100) + 1; // 1から100までの乱数を生成
            System.out.println(randomNumber);
            if (randomNumber % 10 == 0) {
                System.out.println("終了します");
                break;
            }
        }
    }
}
```
***
prob4-10.(難易度：★)
```java
import java.util.Random;

public class Problem4_10 {

    public static void main(String[] args) {
        Random random = new Random();

        for (int i = 0; i < 5; i++) {
            int randomNumber = random.nextInt(100) + 1; // 1から100までの乱数を生成
            System.out.println(randomNumber);
        }
    }
}
```
***
prob4-11.(難易度：★★)
```java
import java.util.Random;

public class Problem4_11 {

    public static void main(String[] args) {
        Random random = new Random();
        int maxNumber = 0;

        for (int i = 0; i < 5; i++) {
            int randomNumber = random.nextInt(100) + 1; // 1から100までの乱数を生成
            System.out.println(randomNumber);
            
            if (randomNumber > maxNumber) {
                maxNumber = randomNumber;
            }
        }

        System.out.println("最大値: " + maxNumber);
    }
}
```
***
prob4-12.(難易度：★★)
```java
import java.util.Random;

public class Problem4_12 {

    public static void main(String[] args) {
        Random random = new Random();
        int minNumber = 101; // 最小値を初期化

        for (int i = 0; i < 5; i++) {
            int randomNumber = random.nextInt(100) + 1; // 1から100までの乱数を生成
            System.out.println(randomNumber);

            if (randomNumber < minNumber) {
                minNumber = randomNumber;
            }
        }

        System.out.println("最小値: " + minNumber);
    }
}
```
***
prob4-13.(難易度：★★)
```java
import java.util.Random;

public class Problem4_13 {

    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(10) + 1; // 1から10までの乱数を生成

        if (randomNumber >= 5) {
            for (int i = 0; i < randomNumber; i++) {
                System.out.print("★");
            }
        } else {
            System.out.println("発生した数値: " + randomNumber);
        }
    }
}
```
***
prob4-14.(難易度：★★)
```java
import java.util.Random;

public class Problem4_14 {

    public static void main(String[] args) {
        Random random = new Random();
        int randomNumber = random.nextInt(10) + 1; // 1から10までの乱数を生成

        if (randomNumber % 2 == 0) {
            for (int i = 0; i < randomNumber; i++) {
                System.out.print("★");
            }
        } else {
            for (int i = 0; i < randomNumber; i++) {
                System.out.print("☆");
            }
        }
    }
}
```
***
prob4-15.(難易度：★★)
```java
public class Problem4_15 {

    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            System.out.print(i + " ");
            if (i % 10 == 0) {
                System.out.println(); // 10の倍数で改行
            }
        }
    }
}
```
***
prob4-16.(難易度：★)
```java
public class Problem4_16 {

    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                System.out.print(i + "×" + j + "=" + (i * j) + " ");
            }
            System.out.println(); // 改行
        }
    }
}
```
***
prob4-17.(難易度：★★)
```java
public class Problem4_17 {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j <= i) {
                    System.out.print("■");
                } else {
                    System.out.print("□");
                }
            }
            System.out.println(); // 改行
        }
    }
}
```
***
prob4-18.(難易度：★★)
```java
public class Problem4_18 {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j == i) {
                    System.out.print("□");
                } else {
                    System.out.print("■");
                }
            }
            System.out.println(); // 改行
        }
    }
}
```

## 基本問題(4)：配列変数
prob5-1.(難易度：★)
```java
import java.util.Random;

public class Problem5_1 {

    public static void main(String[] args) {
        int[] a = new int[7];
        Random random = new Random();

        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(10) + 1; // 1から10までの乱数を代入
            System.out.print("a[" + i + "]=" + a[i] + " ");
        }
    }
}
```
***
prob5-2.(難易度：★)
```java
public class Problem5_2 {

    public static void main(String[] args) {
        double[] d = {0.2, -5.1, 3.2, 1.8};

        for (int i = 0; i < d.length; i++) {
            System.out.print("d[" + i + "]=" + d[i] + " ");
        }
    }
}
```
***
prob5-3.(難易度：★★)
```java
import java.util.Random;

public class Problem5_3 {

    public static void main(String[] args) {
        int[] data = new int[10];
        Random random = new Random();
        int evenCount = 0;
        int oddCount = 0;

        for (int i = 0; i < data.length; i++) {
            data[i] = random.nextInt(100) + 1; // 1から100までの乱数を代入
            System.out.print(data[i] + " ");
            if (data[i] % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }

        System.out.println("\n奇数 : " + oddCount);
        System.out.println("偶数 : " + evenCount);
    }
}
```
***
prob5-4.(難易度：★★)
```java
import java.util.Random;

public class Problem5_4 {

    public static void main(String[] args) {
        int[] data = new int[100];
        Random random = new Random();
        int below50Count = 0;
        int aboveOrEqual50Count = 0;

        for (int i = 0; i < data.length; i++) {
            data[i] = random.nextInt(10) + 1; // 1から10までの乱数を代入
            System.out.print(data[i] + " ");
            if (data[i] >= 50) {
                aboveOrEqual50Count++;
            } else {
                below50Count++;
            }
        }

        System.out.println("\n50以上の数 ： " + aboveOrEqual50Count);
        System.out.println("50未満の数 ： " + below50Count);
    }
}
```
***
prob5-5.(難易度：★★)
```java
import java.util.Random;

public class Problem5_5 {

    public static void main(String[] args) {
        int[] data = new int[10];
        Random random = new Random();
        int multipleOf3Count = 0;
        int notMultipleOf3Count = 0;

        for (int i = 0; i < data.length; i++) {
            data[i] = random.nextInt(10) + 1; // 1から10までの乱数を代入
            System.out.print(data[i] + " ");
            if (data[i] % 3 == 0) {
                multipleOf3Count++;
            } else {
                notMultipleOf3Count++;
            }
        }

        System.out.println("\n3の倍数 ： " + multipleOf3Count);
        System.out.println("3の倍数以外の数 ： " + notMultipleOf3Count);
    }
}
```
***
prob5-6.(難易度：★★)
```java
import java.util.Random;

public class Problem5_5 {

    public static void main(String[] args) {
        int[] data = new int[10];
        Random random = new Random();
        int multipleOf3Count = 0;
        int notMultipleOf3Count = 0;

        for (int i = 0; i < data.length; i++) {
            data[i] = random.nextInt(10) + 1; // 1から10までの乱数を代入
            System.out.print(data[i] + " ");
            if (data[i] % 3 == 0) {
                multipleOf3Count++;
            } else {
                notMultipleOf3Count++;
            }
        }

        System.out.println("\n3の倍数 ： " + multipleOf3Count);
        System.out.println("3の倍数以外の数 ： " + notMultipleOf3Count);
    }
}
```
***
prob5-7.(難易度：★★★)
```java
import java.util.Random;

public class Problem5_7 {

    public static void main(String[] args) {
        int[] array = new int[5];
        Random random = new Random();
        int sum = 0;
        int average;
        int greaterThanAverage = 0;
        int lessThanAverage = 0;

        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(10) + 1; // 1から10までの乱数を代入
            System.out.print(array[i] + " ");
            sum += array[i];
        }

        average = sum / array.length;
        System.out.println("\n合計値：" + sum);
        System.out.println("平均値：" + average);

        for (int i = 0; i < array.length; i++) {
            if (array[i] > average) {
                greaterThanAverage++;
            } else if (array[i] < average) {
                lessThanAverage++;
            }
        }

        System.out.println("\n平均値より大きい数：" + greaterThanAverage);
        System.out.println("平均値より小さい数：" + lessThanAverage);
    }
}
```
***
prob5-8.(難易度：★★)
```java
import java.util.Random;

public class Problem5_8 {

    public static void main(String[] args) {
        int[] array = new int[5];
        Random random = new Random();
        int greaterThanZero = 0;
        int lessThanZero = 0;
        int equalToZero = 0;

        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(21) - 10; // -10から10までの乱数を代入
            System.out.print(array[i] + " ");
            if (array[i] > 0) {
                greaterThanZero++;
            } else if (array[i] < 0) {
                lessThanZero++;
            } else {
                equalToZero++;
            }
        }

        System.out.println("\n0より大きい数：" + greaterThanZero);
        System.out.println("0より小さい数：" + lessThanZero);
        System.out.println("0の個数：" + equalToZero);
    }
}
```
***
prob5-9.(難易度：★★)
```java
import java.util.Random;

public class Problem5_9 {

    public static void main(String[] args) {
        int[] array = new int[10];
        Random random = new Random();
        int aboveOrEqual50Count = 0;
        int below50Count = 0;

        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(101); // 0から100までの乱数を代入
            System.out.print(array[i] + " ");
            if (array[i] >= 50) {
                aboveOrEqual50Count++;
            } else {
                below50Count++;
            }
        }

        System.out.println("\n50以上の数：" + aboveOrEqual50Count);
        System.out.println("50未満の数：" + below50Count);
    }
}
```
***
prob5-10.(難易度：★★)
```java
import java.util.Random;

public class Problem5_10 {

    public static void main(String[] args) {
        int[] array = new int[7];
        Random random = new Random();

        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(10) + 1; // 1から10までの乱数を代入
            for (int j = 0; j < array[i]; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
```
***
prob5-11.(難易度：★★)
```java
import java.util.Random;

public class Problem5_11 {

    public static void main(String[] args) {
        int[] array = new int[5];
        Random random = new Random();

        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100) + 1; // 1から100までの乱数を代入
            System.out.print(array[i] + " ");
        }

        System.out.println("\n\n0以上60未満：");
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= 0 && array[i] < 60) {
                System.out.print(array[i] + " ");
            }
        }

        System.out.println("\n\n60以上80未満：");
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= 60 && array[i] < 80) {
                System.out.print(array[i] + " ");
            }
        }

        System.out.println("\n\n80以上：");
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= 80) {
                System.out.print(array[i] + " ");
            }
        }
    }
}
```
***
prob5-12.(難易度：★)
```java
import java.util.Random;

public class Problem5_12 {

    public static void main(String[] args) {
        int[][]

 matrix = new int[3][3];
        Random random = new Random();

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                matrix[i][j] = random.nextInt(10); // 0から9までの乱数を代入
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}
```
***
prob5-13.(難易度：★★)
```java
import java.util.Random;

public class Problem5_13 {

    public static void main(String[] args) {
        int[][] matrix = new int[3][3];
        Random random = new Random();
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                matrix[i][j] = random.nextInt(10); // 0から9までの乱数を代入
                System.out.print(matrix[i][j] + " ");
                if (matrix[i][j] > max) {
                    max = matrix[i][j];
                }
                if (matrix[i][j] < min) {
                    min = matrix[i][j];
                }
            }
            System.out.println();
        }

        System.out.println("\n最大値：" + max);
        System.out.println("最小値：" + min);
    }
}
```

## 基本問題(5)：クラスとオブジェクト
prob6-1.(難易度★)
```java
package problem6;

public class Problem6_1 {

    public static void main(String[] args) {
        Minmax m = new Minmax();
        int a = 4, b = 2, c = 6;
        System.out.println(a + "と" + b + "と" + c + "のうち、最大のものは" + m.max(a, b, c));
        System.out.println(a + "と" + b + "と" + c + "のうち、最小のものは" + m.min(a, b, c));
    }
}
```
```java
package problem6;

public class Minmax {
    // 最大値の取得
    public int max(int n1, int n2, int n3) {
        int max = n1;
        if (n2 > max) {
            max = n2;
        }
        if (n3 > max) {
            max = n3;
        }
        return max;
    }
    // 最小値の取得
    public int min(int n1, int n2, int n3) {
        int min = n1;
        if (n2 < min) {
            min = n2;
        }
        if (n3 < min) {
            min = n3;
        }
        return min;
    }
}
```
***
prob6-2.(難易度★)
```java
package problem6;

public class Problem6_2 {

    public static void main(String[] args) {
        Calc c = new Calc();
        double a = 4.0, b = 2.0;
        System.out.println(a + " + " + b + " = " + c.add(a, b));
        System.out.println(a + " - " + b + " = " + c.sub(a, b));
        System.out.println(a + " * " + b + " = " + c.mul(a, b));
        System.out.println(a + " / " + b + " = " + c.div(a, b));
    }
}
```
```java
package problem6;

public class Calc {
    // 加算
    double add(double a, double b) {
        return a + b;
    }
    // 減算
    double sub(double a, double b) {
        return a - b;
    }
    // 乗算
    double mul(double a, double b) {
        return a * b;
    }
    // 除算
    double div(double a, double b) {
        if (b != 0) {
            return a / b;
        } else {
            System.out.println("0で割ることはできません。");
            return 0;
        }
    }
}
```
***
prob6-3.(難易度★)
```java
package problem6;

public class Problem6_3 {

    public static void main(String[] args) {
        Circle c = new Circle();
        // 円の半径を設定
        c.r = 4.0;
        System.out.println("半径" + c.r + "の円の円周の長さは" + c.circumference());
        System.out.println("半径" + c.r + "の円の面積の長さは" + c.area());
    }
}
```
```java
package problem6;

public class Circle {
    // 半径
    public double r;
    // 円周の長さを求める
    public double circumference() {
        return 2 * 3.14 * r;
    }
    // 面積を求める
    public double area() {
        return 3.14 * r * r;
    }
}
```

## 基本問題(6)：アクセス指定子とカプセル化
prob7-1.(難易度★)
```java
package problem7;

public class Data {
    // メンバ変数number
    private int number = 0;
    // メンバ変数comment
    private String comment = "";

    // numberのセッターメソッド
    public void setNumber(int number) {
        this.number = number;
    }

    // numberのゲッターメソッド
    public int getNumber() {
        return number;
    }

    // commentのセッターメソッド
    public void setComment(String comment) {
        this.comment = comment;
    }

    // commentのゲッターメソッド
    public String getComment() {
        return comment;
    }
}
```
***
prob7-2.(難易度★)
```java
package problem7;

public class TwoStrings {
    // 一つ目の文字列
    private String string1;
    // 二つ目の文字列
    private String string2;

    // 一つ目の文字列を設定
    public void setString1(String string1) {
        this.string1 = string1;
    }

    // 二つ目の文字列を設定
    public void setString2(String string2) {
        this.string2 = string2;
    }

    // 一つ目の文字列を取得
    public String getString1() {
        return string1;
    }

    // 二つ目の文字列を取得
    public String getString2() {
        return string2;
    }

    // 二つの文字列を合成して返すメソッド
    public String getConnectedString() {
        return string1 + string2;
    }
}
```
***
prob7-3.(難易度★)
```java
package problem7;

public class Calculation {
    // フィールド
    private int number1;
    private int number2;

    // number1のセッターメソッド
    public void setNumber1(int number1) {
        this.number1 = number1;
    }

    // number2のセッターメソッド
    public void setNumber2(int number2) {
        this.number2 = number2;
    }

    // number1のゲッターメソッド
    public int getNumber1() {
        return number1;
    }

    // number2のゲッターメソッド
    public int getNumber2() {
        return number2;
    }

    // 二つの数の和を計算するメソッド
    public int add() {
        return number1 + number2;
    }

    // 二つの数の差を計算するメソッド
    public int sub() {
        return number1 - number2;
    }
}
```