Let's Re⇒move -38ページ目

Javaの授業 まとめ②

Javaの授業 [ 応用課題2 ]


今週は講義はなく応用課題2の作成期間でした。


応用課題1の時同様選択、記述問題は割愛。



実践問題

次の条件を満たすプログラムを作成しなさい。


・クラスの宣言とインスタンス生成を行うこと。

・継承を行うこと。

・try - catchの例外処理を行うこと。

・一つのプログラム(mainメソッドが1つ)となるように作成すること。


ブログで公開するサイズじゃなくなったので略+q+

公開するほどのものでもないですしね。


自習期間の間に[ Java言語プログラムレッスン 下 ]の内容が完了。

応用課題3を提出。

これでいよいよAndoroidへ。

ブログは授業内容ベースで続けます。

Javaの授業 20 [ 例外処理 ]

例外処理


記述

------------------------------------------

try{
 処理①  ←実行したい処理
} catch(~~){
  処理②  ←エラー(~~)が起こった時の処理
} finally {
処理③  ←最後に必ず実行する処理
}

------------------------------------------


【サンプル1】
-------------------------------------------------
class Execute1 {
public static void main(String[] args) {
System.out.println("処理を開始します。");
ExeptionCheck1 exp = new ExeptionCheck1();
exp.check();
System.out.println("処理を終了します。");
}
}

class ExeptionCheck1 {
public void check() {
System.out.println("checkメソッドが呼び出されました。");
String strParam = "abc";
int intParam = 0;

try {
intParam = Integer.parseInt(strParam);
System.out.println("文字列から数値への変換を行いました。");
} catch(NumberFormatException e) {
System.out.println("catch節の処理を実行しました。");
} finally {
System.out.println("finally節の処理を実行しました。");
}
}
}
------------------------------------------------

【サンプル2】
------------------------------------------------
class Execute2 {
public static void main(String[] args) {
ExeptionCheck2 exp = new ExeptionCheck2();
System.out.println("処理を開始します。");
try{
exp.check();
} catch (NumberFormatException e) {
System.out.println("catch節の処理を実行しました。");
}
System.out.println("処理を終了します。");
}
}

class ExeptionCheck2 {
public void check() throws FileNotFoundException {

File f = new File("test.txt");

System.out.println("ファイルを読み込みました");
}
}
------------------------------------------------

【サンプル3】

以下のプログラムを読んで出力結果を予想してください。
class Execute3 {
public static void main(String[] args) {
ExeptionCheck3 exp = new ExeptionCheck3();
int inputParam = 0;
//入力チェック
if(args.length != 1) {
System.out.println("使用方法:java Execute3 数字");
System.exit(0);
}
try{
inputParam = Integer.parseInt(args[0]);
try{
exp.check1(inputParam);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("check1メソッドは正しく実行されませんでした。");
}
} catch (NumberFormatException e) {
exp.check2(args[0]);
}
}
}

【実行方法】
①java Execute3 4
②java Execute3 ABC
③java Execute3 10

------------------------------------------------

Execute3.java

------------------------------------------------

class Execute3 {
public static void main(String[] args) {
ExeptionCheck3 exp = new ExeptionCheck3();
int inputParam = 0;

//入力チェック
if(args.length != 1) {
System.out.println("使用方法:java Execute3 数字");
System.exit(0);
}
try{
inputParam = Integer.parseInt(args[0]);

try{
exp.check1(inputParam);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("check1メソッドは正しく実行されませんでした。");
}
} catch (NumberFormatException e) {
exp.check2(args[0]);
}
}
}

-------------------------------------------------

ExeptionCheck3.java

-------------------------------------------------

class ExeptionCheck3 {
public void check1(int inputParam) throws ArrayIndexOutOfBoundsException {
int[] intArray = new int[5];

for(int i = 0; i < inputParam; i++ ) {
intArray[i] = i;
}
System.out.println("check1メソッドが正しく実行されました。");
}
public void check2(String inputParam) {
System.out.println(inputParam + "は数字ではありません。");
}
}
-------------------------------------------------


おまけ。

応用課題2に着手するにあたって。
コマンドプロンプトからの入力を求める
Bufferedreaderを使った記述を省略する為に、
Scanner というクラスを調べて使用してみて下さい。
Scanner scan = new Scanner(System.in);