完了済
This commit is contained in:
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