
Java基本構文と制御構造 課題集 (全30問)
初級問題 (9問) 基本構文とデータ型 中級問題 (15問) 制御構造 配列と制御構造 上級問題 (6問) 解答例 初級問題の解答 int age = 25; […]
greet
メソッドを作成してください。greetWithName
メソッドを作成してください。add
メソッドを作成してください。add
メソッドを、double型にも対応できるようにオーバーロードしてください。checkEvenOdd
メソッドを作成してください。average
メソッドを作成してください。getStringLength
メソッドを作成してください。Person
クラスを定義し、nameとageのフィールドを持たせてください。Person
クラスに、nameとageを初期化するコンストラクタを追加してください。Person
クラスに、nameとageのゲッターとセッターを追加してください。Person
クラスに、自己紹介をするintroduce
メソッドを追加してください。Person
クラスのインスタンスを生成し、メソッドを呼び出してください。Person
オブジェクトを生成し、それぞれ異なるデータを設定してください。MathUtility
クラスを作成し、円の面積を計算するstaticメソッドを追加してください。Person
クラスに生成されたインスタンスの数をカウントするstatic変数を追加してください。BankAccount
クラスを作成し、残高(balance)をprivateで宣言し、適切なアクセスメソッドを追加してください。this
キーワードを使用する例を作成してください。Person
オブジェクトの配列を作成し、データを設定するプログラムを作成してください。Person
オブジェクトを生成して返すメソッドを作成してください。Student
クラスを作成してください。StringBuilder
のようなクラスを作成してください。public static void greet() {
System.out.println("Hello, World!");
}
public static void greetWithName(String name) {
System.out.println("Hello, " + name + "!");
}
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static void checkEvenOdd(int num) {
if (num % 2 == 0) {
System.out.println(num + "は偶数です");
} else {
System.out.println(num + "は奇数です");
}
}
public static double average(int a, int b, int c) {
return (a + b + c) / 3.0;
}
public static int getStringLength(String str) {
return str.length();
}
public static void exampleMethod() {
int localVar = 10; // ローカル変数
System.out.println(localVar);
}
public static void main(String[] args) {
greet();
greetWithName("Alice");
int sum = add(5, 3);
System.out.println("合計: " + sum);
}
class Person {
String name;
int age;
}
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Person {
// ... 他のコード ...
public void introduce() {
System.out.println("こんにちは、私は" + name + "です。" + age + "歳です。");
}
}
public static void main(String[] args) {
Person person = new Person("田中", 25);
person.introduce();
}
Person person1 = new Person("佐藤", 30);
Person person2 = new Person("鈴木", 22);
person1.introduce();
person2.introduce();
class MathUtility {
public static double calculateCircleArea(double radius) {
return Math.PI * radius * radius;
}
}
class Person {
private static int count = 0;
public Person() {
count++;
}
public static int getCount() {
return count;
}
}
class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}
class Person {
private String name;
public Person(String name) {
this.name = name; // thisでフィールドを参照
}
public void setName(String name) {
this.name = name;
}
}
Person[] people = new Person[3];
people[0] = new Person("山田", 20);
people[1] = new Person("佐藤", 25);
people[2] = new Person("田中", 30);
for (Person person : people) {
person.introduce();
}
public static int sumArray(int[] numbers) {
int sum = 0;
for (int num : numbers) {
sum += num;
}
return sum;
}
public static Person[] createPeople() {
Person[] people = new Person[2];
people[0] = new Person("Alice", 25);
people[1] = new Person("Bob", 30);
return people;
}
public static void printPersonInfo(Person person) {
if (person == null) {
System.out.println("Personオブジェクトがnullです");
return;
}
person.introduce();
}
final class Student {
private final String name;
private final int id;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
}
public static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
public static void riskyMethod() throws Exception {
throw new Exception("何か問題が発生しました");
}
public static void main(String[] args) {
try {
riskyMethod();
} catch (Exception e) {
System.out.println("エラー: " + e.getMessage());
}
}
public static double average(int... numbers) {
if (numbers.length == 0) return 0;
int sum = 0;
for (int num : numbers) {
sum += num;
}
return (double)sum / numbers.length;
}
class MyStringBuilder {
private String value;
public MyStringBuilder() {
value = "";
}
public MyStringBuilder append(String str) {
value += str;
return this;
}
public String toString() {
return value;
}
}
// 使用例
MyStringBuilder sb = new MyStringBuilder();
sb.append("Hello").append(" ").append("World");
System.out.println(sb.toString());
class Product {
private String name;
private double price;
private Product(String name, double price) {
this.name = name;
this.price = price;
}
public static Product createProduct(String name, double price) {
return new Product(name, price);
}
}
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}