Presentation is loading. Please wait.

Presentation is loading. Please wait.

復習ーII (General Review II)

Similar presentations


Presentation on theme: "復習ーII (General Review II)"— Presentation transcript:

1 復習ー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

2 プログラムの構造(structure)と変数の範囲(scope)
import Java-APIs; class className extends superClass implements interfaces { variable1; variable2; className(parameters) { //constructor //initialize variable1, variable2, …, and variables in superClass } method1(arguments1) { method1_variables; action_statements; method2(arguments2) { method2_variables; クラスとインスタンス変数 – global variables クラスの中でどこでも使える。 メソッド1の中のインスタンス変数 – local variables メソッド1の中で使える。 メソッド2の中のインスタンス変数 – local variables メソッド2の中で使える。

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

4 Javaアプレット対Javaアプリケーションの例
import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Multiply extends Applet              implements ActionListener { Label prompt; TextField input; int number, x;  public void init() {  x = 1; String s = "Enter integer and press Enter:" ; prompt = new Label(s); add( prompt ); input = new TextField( 10 );  add( input ); input.addActionListener( this );  }  public void paint( Graphics g ) { g.drawString( Integer.toString( x), 50, 50 ); } public void actionPerformed( ActionEvent e ) { number = Integer.parseInt( e.getActionCommand() ); x = x * number;  input.setText( "" ); repaint(); } } import java.io.*; public class Analysis { public static void main( String args[] )         throws IOException { int passes = 0, failures = 0, student = 1, result; while ( student <= 10 ) { System.out.print( "Enter result (1=pass,2=fail): " ); result = System.in.read(); if ( result == '1' ) passes = passes + 1; else failures = failures + 1; student = student + 1; System.in.skip( 2 ); } System.out.println( "Passed " + passes ); System.out.println( "Failed " + failures ); if ( passes > 8 ) System.out.println( "Raise tuition " ); }

5 while (conditional expression){ If (conditional expression){
条件制御 (condition control) start 問題:10人の試験結果を入力し,合格者と不合格者を数えてプリントする.    合格者数が8以上なら,ほめ言葉をプリントする.1: 合格, 0: 不合格 passes = 0 failures = 0 Flowchart (流れ図) Java Application Code (if-else,while) student=1 while (conditional expression){ action statements counter+1} import java.io.*; public class Analysis { public static void main(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" ); no student <= 10? yes passesとfailuresを出力 resultを入力 if (conditional expression){ action statements } else {action statements} no yes no passes > 8? result == ‘1’? yes passes + 1 failures + 1 “Well done”を出力 Student + 1 If (conditional expression){ action statements } End If (conditional expression){ action statement }

6 while (conditional expression){
while文対for文 while Loop for 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" ); import java.io.*; public class AnalysisFor { 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" ); 初期化 終了条件 初期化 終了条件 増分 増分 while (conditional expression){ action statements counter+1} for ( expression1; expression2, expression3 ) { action statements }

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

8 配列 (Array) i n t a [ ] 1 2 3 4 5 7 9 b 参照 r e f c p o アドレス が入る 整数値 宣言
宣言時に初期化 要素の確保   double b[];   b = new double[8];   for (int i = 0; i < b.length; i++) b[i] = 3.5 * i;  int c[][] = { {1, 2, 3}, {4, 5, 6} };  for (int i = 0; i < c.length; i++)   for (int j = 0; j < c[i].length; j++) c[i][j] = i * j; 配列の長さ 値を与える 初期化と 値を与える 配列のメモリー上の表現 int a[] = {3, 5, 7, 9, 11, 13}; int b = 10; i n t a [ ] 1 2 3 4 5 7 9 array b 参照 r e f c p o アドレス が入る 整数値 配列名(の箱)には配列への参照が入る。

9 配列をメソッドに渡す方法 call-by-reference: one array two references/names
仮引数 public void modifyArray( int b[] ) { // b = a (bはaの配列を指す(参照する)) for (j =0; j < b.length-1; j++) b[j] *= 2; // a[j]の値が変更される } 引数が配列名であるときは、配列への参照が渡される(call-by-reference)。 int a[ ] = {0, 2, 4, 6, 8} …… modifyArray( a ); //call-by-reference modifyElement( a[3] ); //call-by-value a[0]の場所=b[0]の場所 。。。。。。 a[4]の場所=b[4]の場所 public void modifyArray( int e) { // e = a[3] (eにa[3]の値6が入る e *= 2; // eの値が変更される //a[3]の値は変わらない } 引数がintやdoubleの変数や値であるときは、その値渡される(call-by-value)。 i n t a [ ] array i n t b [ ] i n t a [ ] array i n t e a a [ ] 0 -> 0 b [ ] a a [ ] 6 ->12 a [ 1 ] 2 -> 4 b [ 1 ] a [ 1 ] 2 a [ 2 ] 4 ->8 b [ 2 ] a [ 2 ] 4 a [ 3 ] 6 ->12 b [ 3 ] After call modifyArray(a[3]) a[3] has no change a [ 3 ] 6 a [ 4 ] 8 ->16 b [ 4 ] a [ 4 ] 8 After call modifyArray(a) call-by-reference: one array two references/names call-by-value: two values (6) two variable names

10 文字列 String constructors methods String() String(byte[])
offset String() String(byte[]) String(byte[], int, int) String(char[]) String(char[], int, int) String(String) String(StringBuffer) …… String s1 = new String(“hello”); String s2 = new String(“Hello there”); s1.length() s1.charAt(0) String.valueOf(s1.charAt(0)) s1.equals(“hello”) s1.equalsIgnoreCase(“Hello”) s2.regionMatches(0, s1 , 0, 5) s2.regionMatches( true, 0, s1 , 0, 5) s2.startsWith(“the”, 6) s2.endsWith(“ere”) s2.indexOf((int) ‘e’) s2.indexOf(“there”,4) s2.lastIndexOf(“Hello”, 6) s2.lastIndexOf((int) ‘e’) length 5 h char -> String true true false true 文字列の比較 comparison true true 1 6 文字列の検索 searching 10

11 S1の先頭または末尾にある全ての空白文字を削除する
文字列 Continue …… methods String s1 = new String(“ hello ”); String s2 = new String(“Hello there”); s1.concat(s2) g.drawString(“s1” + s1.toString(), 25, 25) s2.substring(0, 5) s1.replace(‘h’, ‘H’) s1.toUpCase() s2.toLowerCase() s1.trim() char charArray[] = s1.toCharArray() 文字列の連結 concatenation hello Hello there 文字列の抽出 extraction Hello Hello HELLO hello there 文字列の置き換え replace S1の先頭または末尾にある全ての空白文字を削除する String -> char 配列

12 課題 (Exercise) 1.左のJAVAアプレットのスケルトンプログラムを完成させなさい。このアプレットは配列を用いて10個の整数を格納する。また、minimum()メソッド、maximum()メソッドとsorting()メソッドを持つ。ここでsorting()メソッドは整数を昇順(最小値から最大値へ)で並び替える。このメソッドは引数として配列を使用する(reference参照)。アプレットを実行するためのHTMLファイルも書くこと。 2.上のアプレットプログラムをアプリケーションプログラムに書き換えよ。   3. 先週の課題である (optional) RectangleCreationApplicationのアプリケーションプログラムをJAVAアプレットに書き換えよ。全てのRectangleオブジェクトのデータと生成したRectangleのオブジェクト個数が示されるようにせよ。最後にアプレットをスタートさせるHTMLを記述せよ。 ??? //import necessary packages public class ArraySorting 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[]) { ??? //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

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

14 課題 (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 RectangleCreationApplication (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 ArraySorting 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


Download ppt "復習ーII (General Review II)"

Similar presentations


Ads by Google