II General Review II Java program structure Variable

復習ーII (General Review II) • • プログラムの構造 Java program structure 変数の範囲 Variable scope: global and local Javaアプレット対Javaアプリケーション 条件制御文: if, if/else, switch 繰返し文: while, for 配列 Array 文字列 String class: constructors and methods

プログラムの構造(structure)と変数の範囲(scope) import Java-APIs; class. Name extends super. Class implements interfaces { variable 1; クラスとインスタンス変数 – global variables variable 2; クラスの中でどこでも使える。. . . class. Name(parameters) { //constructor //initialize variable 1, variable 2, …, and variables in super. Class }. . . method 1(arguments 1) {メソッド1の中のインスタンス変数 – local variables method 1_variables; メソッド1の中で使える。 action_statements; } method 2(arguments 2) {メソッド2の中のインスタンス変数 – local variables method 2_variables; メソッド2の中で使える。 action_statements; }. . . }

Javaアプレット(applet)対Javaアプリケーション(application) import java. applet. Applet; 必ず必要なパッケージ import Java-APIs; //Java パッケージのインポート import other. Java-APIs; class. Name extends Super. Class class. Name extends Applet implements Interface{ variables; //データとインスタンス変数の宣言 //Super. ClassはAppletではない。 class. Name(parameters){…} //constructors variables; //データとインスタンス変数の宣言 your. Own. Method(arguments){…} class. Name(parameters){…} //constructors your. Own. Method(arguments){…} 必ず必要なメソット public void init() { // init()メソッド 変数の初期化とGUIコンポーネットの設定 public static void main ( String args[] ) { } local_variables; // ローカル変数の宣言と初期化 public void paint( Graphics g ) { action statements; // 一連のアクション 画面に書き込む //宣言したメソッドの呼び出し } call your. Own. Method; public void action. Performed ( Action. Event e){ データを入力し、処理する System. out. println(…); repaint(); //画面の更新 //画面に書き込む } Javaアプレットを実行するために、 } System. in. read(); HTML ファイルを作成します。 //データを入力し、処理する } <html> } <Applet code=”class. Name. class” width=300 height=200> JavaアプリケーションはMS-DOSとShellウィンッドと </applet> < 他のアプリケーションから、実行されることができま </html> す。HTMLファイル作成のは必要がない。

Javaアプレット対Javaアプリケーションの例 import java. applet. Applet; import java. io. *; import java. awt. *; public class Analysis { import java. awt. event. *; public static void main( String args[] ) public class Multiply extends Applet throws implements Action. Listener { { int passes = 0, failures = 0, student = 1, result; Label prompt; Text. Field input; int number, x; while ( student <= 10 ) { public void init() System. out. print( "Enter result (1=pass, 2=fail): " ); { x = 1; result = System. in. read(); String s = "Enter integer and press Enter: " ; if ( result == '1' ) passes = passes + 1; prompt = new Label(s); add( prompt ); else failures = failures + 1; input = new Text. Field( 10 ); add( input ); student = student + 1; input. add. Action. Listener( this ); } System. in. skip( 2 ); } public void paint( Graphics g ) System. out. println( "Passed " + passes ); { g. draw. String( Integer. to. String( x), 50 ); } System. out. println( "Failed " + failures ); public void action. Performed( Action. Event e ) if ( passes > 8 ) { number = Integer. parse. Int( e. get. Action. Command() ); System. out. println( "Raise tuition " ); x = x * number; input. set. Text( "" ); repaint(); } } IOException } }

条件制御 (condition control) start 問題: 10人の試験結果を入力し,合格者と不合格者を数えてプリントする. 合格者数が8以上なら,ほめ言葉をプリントする.1: 合格, 0: 不合格 passes = 0 failures = 0 Flowchart (流れ図) while (conditional expression){ student=1 action statements no counter+1} Java Application Code (if-else, while) import java. io. *; public class Analysis { public static void main(String args[]) throws IOExecption { int passes = 0, failures = 0, result, student; student <= 10? student = 1; while ( student <= 10 ) { System. out. print( "Enter result (1=pass, 2=fail): " ); result = System. in. read(); System. in. skip( 2 ); if ( result == ‘ 1’ ) if (conditional expression){ passes = passes + 1; else action statements } failures = failures + 1; student = student + 1; else {action statements} } yes passesとfailuresを出力 resultを入力 yes no no passes > 8? result == ‘ 1’? passes + 1 failures + 1 yes “Well done”を出力 Student + 1 End } System. out. println( "Passed " + passes ); System. out. println( "Failed " + failures ); if ( passes > 8 ) System. out. println( “Well done" ); } If (conditional expression){ action statements } If (conditional expression){ action statement }

while文対for文 while Loop import java. io. *; public class Analysis { public static void (String args[]) throws IOExecption { int passes = 0, failures = 0, result, student; student = 1; 初期化 while ( student <= 10 ) { System. out. print( "Enter result (1=pass, 2=fail): "); result = System. in. read(); 終了条件 System. in. skip( 2 ); 初期化 if ( result == '1' ) passes = passes + 1; else failures = failures + 1; 増分 student = student + 1; } System. out. println( "Passed " + passes ); System. out. println( "Failed " + failures ); if ( passes > 8 ) System. out. println( “Well done" ); } } while (conditional expression){ action statements counter+1} for Loop import java. io. *; public class Analysis. For { public static void (String args[]) throws IOExecption { int passes = 0, failures = 0, result, student; for (student = 1; student <= 10; student++ ) { System. out. print( "Enter result (1=pass, 2=fail): "); result = System. in. read(); System. in. skip( 2 ); 終了条件 増分 if ( result == '1' ) passes = passes + 1; else failures = failures + 1; } System. out. println( "Passed " + passes ); System. out. println( "Failed " + failures ); if ( passes > 8 ) System. out. println( “Well done" ); } } for ( expression 1; expression 2, expression 3 ) { action statements }

if 文対 switch文 resultを入力 switch (result) case `1`? no case `0`? yes passes+1 break failures+1 break input 1 or 0 break yes no default yes resultを入力 if (conditional expression 1){ action statements } no yes switch ( result ) { case ‘ 1’: passes = passes +1; break; case ‘ 0’: failures = failures + 1; break; default: System. out. println( “Input 1 or 0 again”); break; } else if (conditional expression 2 ){ result == ‘ 1’? action statements } passes + 1 no input 1 or 0 result == ‘ 0’? yes failures + 1 else action statements switch (control variable){ case label 1: action statements; break; …… default: action statements; break; if (result == ‘ 1’) passes = passes + 1; else if (result == ‘ 0’) failures = failures + 1; else System. out. println( “Input 1 or 0 again”);

![配列をメソッドに渡す方法 仮引数 public void modify. Array( int b[] ) 引数が配列名であるとき は、配列への参照が渡さ int a[ ] 配列をメソッドに渡す方法 仮引数 public void modify. Array( int b[] ) 引数が配列名であるとき は、配列への参照が渡さ int a[ ]](http://slidetodoc.com/presentation_image_h2/a53913b3a7ef81cdd625d208db8b4b8b/image-9.jpg)
配列をメソッドに渡す方法 仮引数 public void modify. Array( int b[] ) 引数が配列名であるとき は、配列への参照が渡さ int a[ ] = {0, 2, 4, 6, 8} れる(call-by-reference)。 …… { // b = a (bはaの配列を指す(参照する)) for (j =0; j < b. length-1; j++) b[j] *= 2; // a[j]の値が変更される } a[0]の場所=b[0]の場所 。。。。。。 a[4]の場所=b[4]の場所 modify. Array( a ); //call-by-reference modify. Element( a[3] ); //call-by-value public void modify. Array( int e) { // e = a[3] (eにa[3]の値6が入る e *= 2; 引数がintやdoubleの変数 や値であるときは、その値 渡される(call-by-value)。 int a[ ] a array a[0] a[1] a[2] a[3] a[4] 0 2 4 6 8 -> 0 -> 4 ->8 ->12 ->16 // eの値が変更される //a[3]の値は変わらない } int b[ ] b[0] b[1] b[2] b[3] b[4] After call modify. Array(a) call-by-reference: one array two references/names int a[ ] a array a[0] a[1] a[2] a[3] a[4] 0 2 4 6 8 int e 6 ->12 After call modify. Array(a[3]) a[3] has no change call-by-value: two values (6) two variable names
![文字列 String constructors methods offset String() length String(byte[]) String s 1 = new String(“hello”); 文字列 String constructors methods offset String() length String(byte[]) String s 1 = new String(“hello”);](http://slidetodoc.com/presentation_image_h2/a53913b3a7ef81cdd625d208db8b4b8b/image-10.jpg)
文字列 String constructors methods offset String() length String(byte[]) String s 1 = new String(“hello”); String s 2 = new String(“Hello there”); 5 String(byte[], int) s 1. length() String(char[]) s 1. char. At(0) String(char[], int) String. value. Of(s 1. char. At(0)) String(String) s 1. equals(“hello”) String(String. Buffer) s 1. equals. Ignore. Case(“Hello”) true …… s 2. region. Matches(0, s 1 , 0, 5) false h char -> String true s 2. region. Matches( true, 0, s 1 , 0, 5) 文字列の比較 comparison 文字列の検索 searching s 2. starts. With(“the”, 6) true s 2. ends. With(“ere”) true s 2. index. Of((int) ‘e’) 1 s 2. index. Of(“there”, 4) 6 0 s 2. last. Index. Of(“Hello”, 6) s 2. last. Index. Of((int) ‘e’) 10 true

文字列 Continue …… methods String s 1 = new String(“ hello ”); String s 2 = new String(“Hello there”); 文字列の連結 concatenation s 1. concat(s 2) hello Hello there g. draw. String(“s 1” + s 1. to. String(), 25) 文字列の抽出 extraction s 2. substring(0, 5) s 1. replace(‘h’, ‘H’) s 1. to. Up. Case() replace s 1. trim() Hello HELLO s 2. to. Lower. Case() 文字列の置き換え Hello hello there S 1の先頭または末尾にある全ての空白文字を削除する char. Array[] = s 1. to. Char. Array() String -> char 配列

課題 (Exercise) 1. 左のJAVAアプレットのスケルトンプロ グラムを完成させなさい。このアプレッ トは配列を用いて 10個の整数を格納す る。また、minimum()メソッド、 maximum()メソッドとsorting()メソッドを 持つ。ここでsorting()メソッドは整数を 昇順(最小値から最大値へ)で並び替 える。このメソッドは引数として配列を 使用する(reference参照)。アプレットを 実行するためのHTMLファイルも書くこ と。 ? ? ? //import necessary packages public class Array. Sorting extends Applet { public void paint( Graphics g ){ ? ? ? //define an array a[ ] and initialize it ? ? ? //print the array a[ ] ? ? ? //print the minimum value by call the minimum method ? ? ? //print the maximum value by call the maximum method ? ? ? //call the sorting method and print the array a[] after sorting } public static int minimum(int b[]) { 2. 上のアプレットプログラムをアプリケーシ ョンプログラムに書き換えよ。 3. 先週の課題である (optional) Rectangle. Creation. Applicationのアプリ ケーションプログラムをJAVAアプレット に書き換えよ。全てのRectangleオブジ ェクトのデータと生成したRectangleのオ ブジェクト個数が示されるようにせよ。 最後にアプレットをスタートさせる HTMLを記述せよ。 ? ? ? //to find the minimum value in an array } public static int maximum(int b[]) { ? ? ? //to find the maximum value in an array } public static void sorting(int c[]) { ? ? ? // the detail of sorting method } }

課題 (Exercise) 1. 次のクラスの宣言を完成しなさい。 class Rectangle{ private int width; private int height; private int number; static int counter=0; Rectangle(){ ? ? ? } Rectangle(int w, int h){ ? ? ? } void set. Size(int w, int h){ ? ? ? } int get. Width() { ? ? ? } int get. Height() {? ? ? } int get. Area() { ? ? ? } public String to. String(){ ? ? ? // number, width, height, area } } 2. 次のサブクラスの宣言を完成しなさい。 class Named. Rectangle extends Rectangle{ String name; Named. Rectangle(){ ? ? ? // no name } Named. Rectangle(String s, int w, int h){ ? ? ? } void scale. Size(int s){ ? ? ? //拡大又は縮小 s 倍 } public String to. String(){ ? ? ? //name, number, width, height, area を出力 } } 3. RectangleとNamed. Rectangleのいくつかのオブジェクトの生成と生成したオブジェクトの出 力のJava application プログラムを作成しなさい。 public class Rectangle. Creation. Application { ? ? ? // main メソッド ? ? ? // Rectangle() コンストラクタ 使って、 Rectangleのオブジェクトを生成します ? ? ? // Rectangle(int w, int h) コンストラクタ 使って、 Rectangleのオブジェクトを生成します ? ? ? // Name. Rectangle() コンストラクタ 使って、 Named. Rectangleのオブジェクトを生成します ? ? ? // Named. Rectangle オブジェクトを二倍で拡大します ? ? ? // Named. Rectangle(String s, int w, int h)コンストラクタ 使って、 Rectangleのオブジェクトを生成します ? ? ? // 生成したオブジェクトを出力しなさい ? ? ? // static変数counterを使って、生成したオブジェクトの数を出力しなさい }

課題 (Exercise) 1. Complete the left skeleton program of a Java applet that uses an array to store 10 integers, and have a minimum method, a maximum method and a sorting method to sort the integers in ascending order (from the minimum to the maximum). The methods use an array as its parameter (call-by-reference). Write a HTML file to run the applet. 2. Change the above applet program into an application program. 3. Change the application program of Rectangle. Creation. Application (an exercise in the last week) into a Java applet that will show the data of the all rectangle objects and the total number of the created rectangles. Finally, write a HTML file to start the applet. //import necessary packages ? ? ? public class Array. Sorting extends Applet { public void paint( Graphics g ){ ? ? ? //define an array a[ ] and initialize it ? ? ? //print the array a[ ] ? ? ? //print the minimum value by call the minimum method ? ? ? //print the maximum value by call the maximum method ? ? ? //call the sorting method and print the array a[] after sorting } ? ? ? //define a minimum method ? ? ? //define a maximum method public void sorting(int b[]) { // sorting method is defined here ? ? ? } }
- Slides: 14