完了済
This commit is contained in:
6
Question16-Exception/.classpath
Normal file
6
Question16-Exception/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
||||
23
Question16-Exception/.project
Normal file
23
Question16-Exception/.project
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question16-Exception</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -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
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="21"/>
|
||||
</faceted-project>
|
||||
6
Question16-Exception/build/classes/.gitignore
vendored
Normal file
6
Question16-Exception/build/classes/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/Question16_1.class
|
||||
/Question16_2.class
|
||||
/EmptyException.class
|
||||
/Question16_3.class
|
||||
/Question16_4.class
|
||||
/InvalidAgeException.class
|
||||
21
Question16-Exception/src/main/java/Question16_1.java
Normal file
21
Question16-Exception/src/main/java/Question16_1.java
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
41
Question16-Exception/src/main/java/Question16_2.java
Normal file
41
Question16-Exception/src/main/java/Question16_2.java
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
25
Question16-Exception/src/main/java/Question16_3.java
Normal file
25
Question16-Exception/src/main/java/Question16_3.java
Normal file
@@ -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("処理を終了します");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
31
Question16-Exception/src/main/java/Question16_4.java
Normal file
31
Question16-Exception/src/main/java/Question16_4.java
Normal file
@@ -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+"は不正な年齢です");
|
||||
}
|
||||
}
|
||||
|
||||
6
Question17-StackTrace/.classpath
Normal file
6
Question17-StackTrace/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
|
||||
<classpathentry kind="src" path="src/main/java"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
||||
23
Question17-StackTrace/.project
Normal file
23
Question17-StackTrace/.project
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question17-StackTrace</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -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
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<installed facet="java" version="21"/>
|
||||
</faceted-project>
|
||||
6
Question17-StackTrace/build/classes/.gitignore
vendored
Normal file
6
Question17-StackTrace/build/classes/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/Question17_1.class
|
||||
/Question17_2.class
|
||||
/Product.class
|
||||
/Question17_3.class
|
||||
/nullNameException.class
|
||||
/invalidAgeException.class
|
||||
25
Question17-StackTrace/src/main/java/Product.java
Normal file
25
Question17-StackTrace/src/main/java/Product.java
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
38
Question17-StackTrace/src/main/java/Question17_1.java
Normal file
38
Question17-StackTrace/src/main/java/Question17_1.java
Normal file
@@ -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<points.length;i++) {
|
||||
sum += points[i];
|
||||
}
|
||||
return divide(sum,points.length);
|
||||
}
|
||||
|
||||
public static double divide(double num1, int num2) {
|
||||
return num1/num2;
|
||||
}
|
||||
|
||||
}
|
||||
37
Question17-StackTrace/src/main/java/Question17_2.java
Normal file
37
Question17-StackTrace/src/main/java/Question17_2.java
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
public class Question17_2 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
Product[] products = new Product[2];
|
||||
Product product1 = new Product("りんご",150);
|
||||
Product product2 = new Product("みかん",100);
|
||||
products[0] = product1;
|
||||
products[1] = product2;
|
||||
|
||||
confirmStock(products,"りんご");
|
||||
confirmStock(products,"バナナ");
|
||||
|
||||
}
|
||||
|
||||
public static void confirmStock(Product[] products,String name) {
|
||||
Product product = search(products,name);
|
||||
try {
|
||||
System.out.println(product.getName()+"の価格は"+product.getPrice()+"です");
|
||||
} catch(Exception err) {
|
||||
System.out.println(name+"は見つかりませんでした");
|
||||
}
|
||||
}
|
||||
|
||||
public static Product search(Product[] products,String name) {
|
||||
Product product = null;
|
||||
for (Product p:products) {
|
||||
if(p.getName().equals(name)) {
|
||||
product = p;
|
||||
}
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
}
|
||||
48
Question17-StackTrace/src/main/java/Question17_3.java
Normal file
48
Question17-StackTrace/src/main/java/Question17_3.java
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
public class Question17_3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
|
||||
register("花子",20);
|
||||
register("",20);
|
||||
register("太郎",-1);
|
||||
|
||||
}
|
||||
|
||||
public static void register(String name,int age) {
|
||||
|
||||
try {
|
||||
check(name,age);
|
||||
System.out.println(name+"を登録しました");
|
||||
}
|
||||
catch (nullNameException e) {
|
||||
System.out.println("名前を入力してください");
|
||||
}
|
||||
catch (invalidAgeException e) {
|
||||
System.out.println("年齢は0以上を入力してください");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void check(String name,int age) throws invalidAgeException,nullNameException {
|
||||
checkName(name);
|
||||
if (age<0) {
|
||||
throw new invalidAgeException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkName(String name) throws nullNameException {
|
||||
if (name.length()==0) {
|
||||
throw new nullNameException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class nullNameException extends Exception {
|
||||
}
|
||||
|
||||
class invalidAgeException extends Exception {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user