Download result == `1`

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
復習ー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 className extends superClass implements interfaces {
variable1;
クラスとインスタンス変数
– global variables
variable2;
クラスの中でどこでも使える。
......
className(parameters) { //constructor
//initialize variable1, variable2, …, and variables in superClass
}
......
method1(arguments1) {メソッド1の中のインスタンス変数
– local variables
method1_variables;
メソッド1の中で使える。
action_statements;
}
method2(arguments2) {メソッド2の中のインスタンス変数
– local variables
method2_variables;
メソッド2の中で使える。
action_statements;
}
......
}
Javaアプレット(applet)対Javaアプリケーション(application)
import java.applet.Applet;
必ず必要なパッケージ import Java-APIs; //Java パッケージのインポート
import otherJava-APIs;
class className extends SuperClass
class className extends Applet implements Interface{
implements Interface{
variables; //データとインスタンス変数の宣言
//SuperClassはAppletではない。
className(parameters){…} //constructors
variables; //データとインスタンス変数の宣言
yourOwnMethod(arguments){…}
className(parameters){…} //constructors
yourOwnMethod(arguments){…}
必ず必要なメソット
public void init() { // init()メソッド
変数の初期化とGUIコンポーネットの設定
public static void main ( String args[] ) {
}
local_variables; // ローカル変数の宣言と初期化
public void paint( Graphics g ) {
action statements; // 一連のアクション
画面に書き込む
//宣言したメソッドの呼び出し
}
call yourOwnMethod;
public void actionPerformed ( ActionEvent e){
データを入力し、処理する
System.out.println(…);
repaint(); //画面の更新
//画面に書き込む
}
Javaアプレットを実行するために、
}
System.in.read();
HTML ファイルを作成します。
//データを入力し、処理する
}
<html>
}
<Applet code=”className.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 IOException
{ int passes = 0, failures = 0, student = 1, result;
implements ActionListener {
while ( student <= 10 ) {
Label prompt; TextField input; int number, x;
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 TextField( 10 ); add( input );
student = student + 1;
input.addActionListener( this ); }
System.in.skip( 2 ); }
public void paint( Graphics g )
System.out.println( "Passed " + passes );
{ g.drawString( Integer.toString( x), 50, 50 ); }
System.out.println( "Failed " + failures );
public void actionPerformed( ActionEvent e )
if ( passes > 8 )
{ number = Integer.parseInt( e.getActionCommand() );
System.out.println( "Raise tuition " );
}
x = x * number; input.setText( "" ); repaint(); }
}
}
条件制御 (condition control)
start
問題:10人の試験結果を入力し,合格者と不合格者を数えてプリントする.
合格者数が8以上なら,ほめ言葉をプリントする.1: 合格, 0: 不合格
passes = 0
failures = 0
Flowchart (流れ図)
student=1
while (conditional expression){
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" );
}
}
for Loop
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 }
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
switch ( result ) {
case ‘1’:
passes = passes +1;
break;
case ‘0’:
failures = failures + 1;
break;
default:
System.out.println( “Input 1 or 0 again”);
break;
}
resultを入力
if (conditional expression 1){
switch (control variable){
case label 1: action statements;
action statements }
no
yes
else if (conditional expression2 ){
result == ‘1’?
action statements }
passes + 1
no
input 1 or 0
result == ‘0’?
yes
failures + 1
else 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”);
配列 (Array)
宣言
double b[];
宣言時に
初期化
要素の確保
int c[][] = { {1, 2, 3}, {4, 5, 6} };
配列の長さ
b = new double[8];
for (int i = 0; i < c.length; i++)
for (int i = 0; i < b.length; i++)
for (int j = 0; j < c[i].length; j++)
b[i] = 3.5 * i;
c[i][j] = i * j;
値を与える
初期化と
値を与える
配列のメモリー上の表現
int a[] = {3, 5, 7, 9, 11, 13};
int b = 10;
配列名(の箱)には配列へ
の参照が入る。
int a[ ]
a
参照
reference
pointer
アドレス
が入る
array
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
3
5
7
9
11
13
int b
10
整数値
が入る
配列をメソッドに渡す方法
仮引数
public void modifyArray( 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]の場所
modifyArray( a ); //call-by-reference
modifyElement( a[3] ); //call-by-value
public void modifyArray( int e)
{ // e = a[3] (eにa[3]の値6が入る
e *= 2; // eの値が変更される
引数がintやdoubleの変数
や値であるときは、その値
渡される(call-by-value)。
int a[ ]
a
array
a[0]
a[1]
a[2]
a[3]
a[4]
0 -> 0
2
4
6
8
-> 4
->8
->12
->16
//a[3]の値は変わらない
}
int b[ ]
b[0]
b[1]
b[2]
b[3]
b[4]
After call
modifyArray(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
modifyArray(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 s1 = new String(“hello”);
String s2 = new String(“Hello there”);
5
String(byte[], int, int)
s1.length()
String(char[])
s1.charAt(0)
String(char[], int, int)
String.valueOf(s1.charAt(0))
String(String)
s1.equals(“hello”)
String(StringBuffer)
s1.equalsIgnoreCase(“Hello”)
true
……
s2.regionMatches(0, s1 , 0, 5)
false
h
char -> String
true
s2.regionMatches( true, 0, s1 , 0, 5)
文字列の比較
comparison
文字列の検索
searching
s2.startsWith(“the”, 6)
true
s2.endsWith(“ere”)
true
s2.indexOf((int) ‘e’)
1
s2.indexOf(“there”,4)
6
s2.lastIndexOf(“Hello”, 6)
s2.lastIndexOf((int) ‘e’)
0
10
true
文字列
Continue ……
methods
String s1 = new String(“ hello ”);
String s2 = new String(“Hello there”);
文字列の連結
concatenation
s1.concat(s2)
hello Hello there
g.drawString(“s1” + s1.toString(), 25, 25)
文字列の抽出
extraction
s2.substring(0, 5)
s1.replace(‘h’, ‘H’)
s1.toUpCase()
replace
s1.trim()
Hello
HELLO
s2.toLowerCase()
文字列の置き換え
Hello
hello there
S1の先頭または末尾にある全ての空白文字を削除する
char charArray[] = s1.toCharArray()
String -> char 配列
課題 (Exercise)
1.左のJAVAアプレットのスケルトンプロ
グラムを完成させなさい。このアプレッ
トは配列を用いて10個の整数を格納す
る。また、minimum()メソッド、
maximum()メソッドとsorting()メソッド
を持つ。ここでsorting()メソッドは整数
を昇順(最小値から最大値へ)で並び
替える。このメソッドは引数として配列
を使用する(reference参照)。アプレッ
トを実行するための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[]) {
2.上のアプレットプログラムをアプリケー
ションプログラムに書き換えよ。
3. 先週の課題である (optional)
RectangleCreationApplicationのア
プリケーションプログラムを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 setSize(int w, int h){
??? }
int getWidth() { ??? }
int getHeight() {??? }
int getArea() { ??? }
public String toString(){
??? // number,width, height, area }
}
2. 次のサブクラスの宣言を完成しなさい。
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を使って、生成したオブジェクトの数を出力しなさい
}
課題 (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-byreference). 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
???
}
}