実験課題9

Exercise 9(課題9) See the section 4.8.1. "Inheritance". Explain the ID rule of getID method. Further, show the execution result and complete source code of Figure 18, Figure 32, and Figure 33. Follow instructions below to show the result.

  1. create the Java application project named chapter4kadai9.
  2. add User class and Student class to this project.
  3. create main method to run the program such as Figure 33.
  4. take the screenshot of the execution result.

(4.8.1.節をよく読み、プロジェクトchapter4kadai9を作成し、図18のUserクラスを追加した後、図32のStudentクラスを作成して、図33のようにmainメソッドから実行しなさい。またソースコードとスクリーンショットを報告しなさい。また、getIDメソッドが返すIDのルールを説明しなさい。

なお、図に示す、文字列オブジェクトが持つchar charAt(int i)メソッドは、文字列オブジェクトのi+1番目の文字を返却するメソッドである。

System.out.println("Hello,World.".charAt(4)); // oが表示される。
図:char charAt(int i)メソッドの利用例

また、C言語におけるsprintfのような整形した文字列を作成するメソッドとしてString String.format(String formatStr, Object... args)メソッドがある。引数のformatStrがフォーマット文字列である。)

System.out.println(String.format("%02d", 3));	// 03が表示される。%02dとは整数型(d)として2桁まで0埋めした文字列を返すフォーマット文字列である。
図:String String.format(String formatStr, Object... args)メソッドの利用例