1102自主学習
This commit is contained in:
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>
|
||||
1
Question17-StackTrace/.gitignore
vendored
Normal file
1
Question17-StackTrace/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build/
|
||||
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>
|
||||
18
Question17-StackTrace/src/main/java/Product.java
Normal file
18
Question17-StackTrace/src/main/java/Product.java
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
public class Product {
|
||||
private String name;
|
||||
private int price;
|
||||
|
||||
public Product(String name, int price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getPrice() {
|
||||
return price;
|
||||
}
|
||||
}
|
||||
42
Question17-StackTrace/src/main/java/Question17_1.java
Normal file
42
Question17-StackTrace/src/main/java/Question17_1.java
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
public class Question17_1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
int[] nums1 = {80, 90, 70};
|
||||
int[] nums2 = {};
|
||||
scoreexception(nums1);
|
||||
scoreexception(nums2);
|
||||
}
|
||||
|
||||
//例外処理
|
||||
public static void scoreexception(int[]nums) {
|
||||
try {
|
||||
report(nums);
|
||||
}catch(ArithmeticException e) {
|
||||
System.out.println("採点データがないため、平均点を計算できませんでした");
|
||||
}
|
||||
}
|
||||
|
||||
//平均点レポート処理(最上位)
|
||||
public static void report(int[]nums) {
|
||||
int score = average(nums);
|
||||
System.out.println("平均点は" + score + "点です");
|
||||
}
|
||||
|
||||
//平均点算出処理(中間)
|
||||
public static int average(int[]nums) {
|
||||
int total = 0;
|
||||
for(int i = 0; i < nums.length; i++) {
|
||||
total += nums[i];
|
||||
}
|
||||
int result = divide(total, nums.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
//割り算処理(最下位)
|
||||
public static int divide(int 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 product1 = new Product("りんご", 150);
|
||||
Product product2 = new Product("みかん", 100);
|
||||
Product[]product = new Product[2];
|
||||
product[0] = product1;
|
||||
product[1] = product2;
|
||||
|
||||
searchResult(product, "りんご");
|
||||
searchResult(product, "バナナ");
|
||||
}
|
||||
|
||||
|
||||
//商品検索処理
|
||||
public static Product search(Product[]product, String targetName) {
|
||||
for(int i = 0; i < product.length; i++) {
|
||||
if(product[i].getName().equals(targetName)) {
|
||||
return product[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//検索結果表示処理
|
||||
public static void searchResult(Product[]product, String targetName) {
|
||||
Product result = search(product, targetName);
|
||||
if(result == null) {
|
||||
System.out.println(targetName + "は見つかりませんでした");
|
||||
}else {
|
||||
result.getPrice();
|
||||
System.out.println(targetName + "の価格は" + result.getPrice() + "円です");
|
||||
}
|
||||
}
|
||||
}
|
||||
39
Question17-StackTrace/src/main/java/Question17_3.java
Normal file
39
Question17-StackTrace/src/main/java/Question17_3.java
Normal file
@@ -0,0 +1,39 @@
|
||||
|
||||
public class Question17_3 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
register("花子", 20);
|
||||
register("", 20);
|
||||
register("太郎", -5);
|
||||
}
|
||||
|
||||
//会員登録処理(最上位)
|
||||
public static void register(String name, int age) {
|
||||
try{
|
||||
ageCheck(name, age);
|
||||
System.out.println(name + "を登録しました");
|
||||
}catch(IllegalArgumentException e) {
|
||||
System.out.println("年齢は0以上を入力してください");
|
||||
}catch(NullPointerException e) {
|
||||
System.out.println("名前を入力してください");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//入力チェック処理(中間)
|
||||
public static void ageCheck(String name, int age) {
|
||||
nameCheck(name);
|
||||
if(age < 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
//名前チェック処理(最下位)
|
||||
public static void nameCheck(String name) {
|
||||
if(name == null || name.isEmpty()) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user