From 6e804fff63fc14e492dc4bd5700b2ecd364e7395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=88=B4=E6=9C=A8=E3=80=80=E7=9A=93=E4=BB=8B?= <26-k-suzuki-avnz@it-college.co.jp> Date: Wed, 21 Oct 2026 17:58:59 +0900 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E4=BA=86=E6=B8=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Question16-Exception/.classpath | 6 +++ Question16-Exception/.project | 23 +++++++++ .../.settings/org.eclipse.jdt.core.prefs | 9 ++++ ....eclipse.wst.common.project.facet.core.xml | 4 ++ Question16-Exception/build/classes/.gitignore | 6 +++ .../src/main/java/Question16_1.java | 21 ++++++++ .../src/main/java/Question16_2.java | 41 ++++++++++++++++ .../src/main/java/Question16_3.java | 25 ++++++++++ .../src/main/java/Question16_4.java | 31 ++++++++++++ Question17-StackTrace/.classpath | 6 +++ Question17-StackTrace/.project | 23 +++++++++ .../.settings/org.eclipse.jdt.core.prefs | 9 ++++ ....eclipse.wst.common.project.facet.core.xml | 4 ++ .../build/classes/.gitignore | 6 +++ .../src/main/java/Product.java | 25 ++++++++++ .../src/main/java/Question17_1.java | 38 +++++++++++++++ .../src/main/java/Question17_2.java | 37 ++++++++++++++ .../src/main/java/Question17_3.java | 48 +++++++++++++++++++ 18 files changed, 362 insertions(+) create mode 100644 Question16-Exception/.classpath create mode 100644 Question16-Exception/.project create mode 100644 Question16-Exception/.settings/org.eclipse.jdt.core.prefs create mode 100644 Question16-Exception/.settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 Question16-Exception/build/classes/.gitignore create mode 100644 Question16-Exception/src/main/java/Question16_1.java create mode 100644 Question16-Exception/src/main/java/Question16_2.java create mode 100644 Question16-Exception/src/main/java/Question16_3.java create mode 100644 Question16-Exception/src/main/java/Question16_4.java create mode 100644 Question17-StackTrace/.classpath create mode 100644 Question17-StackTrace/.project create mode 100644 Question17-StackTrace/.settings/org.eclipse.jdt.core.prefs create mode 100644 Question17-StackTrace/.settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 Question17-StackTrace/build/classes/.gitignore create mode 100644 Question17-StackTrace/src/main/java/Product.java create mode 100644 Question17-StackTrace/src/main/java/Question17_1.java create mode 100644 Question17-StackTrace/src/main/java/Question17_2.java create mode 100644 Question17-StackTrace/src/main/java/Question17_3.java diff --git a/Question16-Exception/.classpath b/Question16-Exception/.classpath new file mode 100644 index 0000000..c771657 --- /dev/null +++ b/Question16-Exception/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Question16-Exception/.project b/Question16-Exception/.project new file mode 100644 index 0000000..16fb9ed --- /dev/null +++ b/Question16-Exception/.project @@ -0,0 +1,23 @@ + + + Question16-Exception + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/Question16-Exception/.settings/org.eclipse.jdt.core.prefs b/Question16-Exception/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..23fa13b --- /dev/null +++ b/Question16-Exception/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/Question16-Exception/.settings/org.eclipse.wst.common.project.facet.core.xml b/Question16-Exception/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..c1a8d07 --- /dev/null +++ b/Question16-Exception/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/Question16-Exception/build/classes/.gitignore b/Question16-Exception/build/classes/.gitignore new file mode 100644 index 0000000..31b9224 --- /dev/null +++ b/Question16-Exception/build/classes/.gitignore @@ -0,0 +1,6 @@ +/Question16_1.class +/Question16_2.class +/EmptyException.class +/Question16_3.class +/Question16_4.class +/InvalidAgeException.class diff --git a/Question16-Exception/src/main/java/Question16_1.java b/Question16-Exception/src/main/java/Question16_1.java new file mode 100644 index 0000000..229737b --- /dev/null +++ b/Question16-Exception/src/main/java/Question16_1.java @@ -0,0 +1,21 @@ + +public class Question16_1 { + + public static void main(String[] args) { + // TODO 自動生成されたメソッド・スタブ + + System.out.println(Divide(10,2)); +// System.out.println(Divide(10,0)); + try { + System.out.println(Divide(10,0)); + } catch (Exception e) { + System.out.println("0で割ることはできません"); + System.exit(-1); + } + } + + public static int Divide(int num1,int num2) { + return num1/num2; + } + +} diff --git a/Question16-Exception/src/main/java/Question16_2.java b/Question16-Exception/src/main/java/Question16_2.java new file mode 100644 index 0000000..ebfbf31 --- /dev/null +++ b/Question16-Exception/src/main/java/Question16_2.java @@ -0,0 +1,41 @@ + +public class Question16_2 { + + public static void main(String[] args) { + // TODO 自動生成されたメソッド・スタブ + + System.out.println(x2Multiple("10")); + +// System.out.println(x2Multiple("abc")); + try { + System.out.println(x2Multiple("abc")); + } catch(Exception e) { + System.out.println("数値に変換できません"); + } + + String test = ""; +// System.out.println(""); + try { + if (test.length()==0) { + throw new EmptyException("入力が空です"); + } + } catch(EmptyException e) { + System.out.println("入力が空です"); + } + + } + + public static int x2Multiple(String str) { + return 2*Integer.parseInt(str); + } + +} + +class EmptyException extends Exception { + + public EmptyException(String msg) { + super(msg); + } + +} + diff --git a/Question16-Exception/src/main/java/Question16_3.java b/Question16-Exception/src/main/java/Question16_3.java new file mode 100644 index 0000000..19fb7de --- /dev/null +++ b/Question16-Exception/src/main/java/Question16_3.java @@ -0,0 +1,25 @@ + +public class Question16_3 { + + public static void main(String[] args) { + // TODO 自動生成されたメソッド・スタブ + + int[] nums = {10,20,30}; + getValue(nums,1); + getValue(nums,5); + + } + + public static void getValue(int[] nums,int index) { + + try { + System.out.println(nums[index]); + } catch(Exception e) { + System.out.println("指定した位置にデータがありません"); + } finally { + System.out.println("処理を終了します"); + } + + } + +} diff --git a/Question16-Exception/src/main/java/Question16_4.java b/Question16-Exception/src/main/java/Question16_4.java new file mode 100644 index 0000000..005f517 --- /dev/null +++ b/Question16-Exception/src/main/java/Question16_4.java @@ -0,0 +1,31 @@ + +public class Question16_4 { + + public static void main(String[] args) { + // TODO 自動生成されたメソッド・スタブ + + age(20); + age(-5); + + } + + public static void age(int num) { + try { + if (num<0) { + throw new InvalidAgeException(num); + } else { + System.out.println("年齢は"+num+"です"); + } + } catch(Exception e) { + + } + } + +} + +class InvalidAgeException extends Exception { + public InvalidAgeException(int num) { + System.out.println(num+"は不正な年齢です"); + } +} + diff --git a/Question17-StackTrace/.classpath b/Question17-StackTrace/.classpath new file mode 100644 index 0000000..c771657 --- /dev/null +++ b/Question17-StackTrace/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Question17-StackTrace/.project b/Question17-StackTrace/.project new file mode 100644 index 0000000..45afb02 --- /dev/null +++ b/Question17-StackTrace/.project @@ -0,0 +1,23 @@ + + + Question17-StackTrace + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/Question17-StackTrace/.settings/org.eclipse.jdt.core.prefs b/Question17-StackTrace/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..23fa13b --- /dev/null +++ b/Question17-StackTrace/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/Question17-StackTrace/.settings/org.eclipse.wst.common.project.facet.core.xml b/Question17-StackTrace/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..c1a8d07 --- /dev/null +++ b/Question17-StackTrace/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/Question17-StackTrace/build/classes/.gitignore b/Question17-StackTrace/build/classes/.gitignore new file mode 100644 index 0000000..b864ab9 --- /dev/null +++ b/Question17-StackTrace/build/classes/.gitignore @@ -0,0 +1,6 @@ +/Question17_1.class +/Question17_2.class +/Product.class +/Question17_3.class +/nullNameException.class +/invalidAgeException.class diff --git a/Question17-StackTrace/src/main/java/Product.java b/Question17-StackTrace/src/main/java/Product.java new file mode 100644 index 0000000..5890111 --- /dev/null +++ b/Question17-StackTrace/src/main/java/Product.java @@ -0,0 +1,25 @@ + +public class Product { + + private String name; + private int price; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public int getPrice() { + return price; + } + public void setPrice(int price) { + this.price = price; + } + + public Product(String name,int price) { + this.name = name; + this.price = price; + } + +} diff --git a/Question17-StackTrace/src/main/java/Question17_1.java b/Question17-StackTrace/src/main/java/Question17_1.java new file mode 100644 index 0000000..db37abb --- /dev/null +++ b/Question17-StackTrace/src/main/java/Question17_1.java @@ -0,0 +1,38 @@ + +public class Question17_1 { + + public static void main(String[] args) { + // TODO 自動生成されたメソッド・スタブ + + try { + int[] points = {80,90,70}; + printAverage(points); + int[] nullpoints = {}; + printAverage(nullpoints); + } catch(Exception e) { + System.out.println("採点データがないため、平均点を計算できませんでした"); + System.exit(-1); + } + + } + + public static void printAverage(int[] points) throws Exception { + System.out.println("平均点は"+getAverage(points)+"点です"); + } + + public static double getAverage(int[] points) throws Exception { + if (points.length==0) { + throw new Exception(); + } + int sum = 0; + for (int i=0;i