解きながら学ぶJava入門編

Size: px
Start display at page:

Download "解きながら学ぶJava入門編"

Transcription

1

2 44 // class Negative { System.out.print(""); int n = stdin.nextint(); if (n < 0) System.out.println(""); -10 Ÿ 35 Ÿ 0 n if statement if ( ) if i f ( ) if n < 0 < true false true false boolean literalboolean if Fig.3-1 Fig.3-1

3 45n 0 true System.out.println(""); n 0 < relational operatortable 3-1 <= >= =< => < = < = Table x < y x > y x <= y x >= y x y true false x y true false x y true false x y true false if if-then if-then- if Fig.3-2 if if ( ) Fig.3-2 if if ( ) if ( ) if-then if-then- if

4 46 Java3-1p.59 // class Absolute1 { System.out.print(""); int n = stdin.nextint(); if (n >= 0) System.out.println("" + n + ""); System.out.println("" + (-n) + ""); 62 Ÿ Ÿ 35 // class Absolute2 { System.out.print(""); int n = stdin.nextint(); int abs; if (n >= 0) abs = n; abs = -n; System.out.println("" + abs + ""); n n 0 n n n -n n 0 Absolute1 if if ( ) if true false Fig.3-3 n 0

5 47 Fig.3-3 Absolute2 if-then- n abs Absolute1 Absolute2 if-then if-then n 0 Fig.3-1 if-then if-then- 3 if (n < 0) System.out.println(""); ; if empty statement Fig.3-4 ; Fig.3-4

6 48 Java3-3p.59 // class Measure1 { 12 Ÿ 4 Ÿ System.out.print(""); int a = stdin.nextint(); System.out.print(""); int b = stdin.nextint(); if (a % b == 0) System.out.println(""); System.out.println(""); b a a b if == ==!= equality operatortable 3-2 true false Table 3-2 x == y x!= y x y true false x y true false!= if if (a % b!= 0) System.out.println(""); System.out.println("");

7 49! // class Measure2 { System.out.print(""); int a = stdin.nextint(); System.out.print(""); int b = stdin.nextint(); if (!(a % b == 0)) System.out.println(""); System.out.println(""); 12 Ÿ 5 Ÿ 3! logical complement operator false true true false Table 3-3 if a % b 0 a % b == 0 true!(a % b == 0) false a % b 0 a % b == 0 false!(a % b == 0) true Table 3-3!x x false true true false a <= a <= 3 // && a >= 1 && a <= 3 // a1a3 a b c a == b == c // && a == b && b == c // abbc a == b && a == c // abac

8 50 JavaList 3-5p.58 // class Sign { System.out.print(""); int n = stdin.nextint(); if (n > 0) System.out.println(""); if (n < 0) System.out.println(""); System.out.println(""); 37 Ÿ 0 Ÿ -35 Ÿ 0 if if ( ) if ( ) if if if Fig.3-5 if if System.out. Fig.3-7 if (n > 0) println(""); if (n < 0) println(""); println(""); Fig.3-5

9 51 Java3-2p.59 if (n == 0) if if (n > 0) System.out.println(""); if (n < 0) System.out.println(""); if (n == 0) System.out.println(""); n > 0 n < 0 false n 0 n n 0 if 3 if (n == 1) System.out.println(""); if (n == 2) System.out.println(""); if (n == 3) System.out.println(""); n if if ( ) if ( ) if n if (n == 1) System.out.println(""); if (n == 2) System.out.println(""); if (n == 3) System.out.println(""); ; if Sign if if ( )

10 52 Java3-4p.61 a, b a b a b // class Balance { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); if (a > b) System.out.println("a"); if (a < b) System.out.println("b"); System.out.println("ab"); a12 Ÿ b3 Ÿ a a5 Ÿ b15 Ÿ b a b if if Fig.3-6 a b a b Fig.3-6

11 53// class BalanceWrong { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); int diff = a - b; if (diff > 0) System.out.println("a"); if (diff < 0) System.out.println("b"); System.out.println("ab"); a Ÿ b-1 Ÿ b a Ÿ b1 Ÿ a 3 BalanceWrong a b diff 0 a b int int

12 54 Java3-5p.61 // class GoInto5 { System.out.print(""); int n = stdin.nextint(); if (n > 0) if (n % 5 == 0) System.out.println(""); System.out.println(""); System.out.println(""); 35 Ÿ 36 Ÿ 5 if Fig.3-7 if if if if Fig.3-8 Fig if (n > 0) if (n % 5 == 0) println(""); println(""); println(""); Fig.3-7

13 55 Fig expression abc + 32 abc 32 + abc + 32 xyz = abc + 32 xyz, abc, 32, abc + 32, xyz = abc xyz abc + 32 xyz = abc assignment expression ; a = c Fig.3-9 expression statement ; Fig.3-9

14 56 Java3-6p // class MultipleOf10 { System.out.print(""); int n = stdin.nextint(); 1250 Ÿ Ÿ 10 if (n > 0) if (n % 10 == 0) System.out.println("10"); System.out.println("10"); System.out.println(""); evaluation Fig.3-10 int n 1254 n, 10, n % 10 n , 10, 4 int int 1254 int 10 n % 10 int 4 Fig.3-10

15 57 Java3-7p // class Modulo3 { System.out.print(""); int n = stdin.nextint(); 1251 Ÿ Ÿ 32 if (n > 0) if (n % 3 == 0) System.out.println("3"); if (n % 3 == 1) System.out.println("31"); System.out.println("32"); System.out.println(""); 3 if if if if if n if (n > 0); System.out.println(""); n (n > 0) ; if if (n > 0) ; // ifn > 0 System.out.println(""); // if if ( )

16 58 Java3-8p // && class Grade1 { 68 Ÿ System.out.print(""); int point = stdin.nextint(); if (point >= 0 && point <= 59) System.out.println(""); if (point >= 60 && point <= 69) System.out.println(""); if (point >= 70 && point <= 79) System.out.println(""); if (point >= 80 && point <= 100) System.out.println(""); System.out.println(""); 89 Ÿ 102 Ÿ && Fig.3-11 logical and operator x && y x y true true false x ytable 3-4 true point && x y x && y true true true true false false false true false false false false x y x y true true true true false true false true true false false false Fig.3-11

17 59 System.out.print(""); int point = stdin.nextint(); if (point < 0 point > 100) System.out.println(""); if (point <= 59) System.out.println(""); if (point <= 69) System.out.println(""); if (point <= 79) System.out.println(""); System.out.println(""); 3 // class Grade2 { Grade2 logical or operator x y x y true true false x y l point true point point Grade2 Grade1 Table 3-4 x && y x y x y true true false x y true true false

18 60 Java3-9p.71 // if class Max2A { System.out.print("a"); double a = stdin.nextdouble(); System.out.print("b"); double b = stdin.nextdouble(); double max; // if (a > b) max = a; max = b; System.out.println("" + max + ""); a25.7 Ÿ b15.3 Ÿ 25.7 a, b a b max a max b if max Max2B if? : Table 3-5 conditional operator conditional expression Fig.3-12 max a b a b if a > b? a : b a > b? a : b double 25.7 double 3.14 Fig.3-12

19 61// class Max2B { System.out.print("a"); double a = stdin.nextdouble(); System.out.print("b"); double b = stdin.nextdouble(); double max = a > b? a : b; // System.out.println("" + max + ""); Table 3-5 x? y : z x true y z 3 x true z false y println max System.out.println("" + (a > b? a : b) + ""); ( ) Grade1 point -10 if point >= 0 && point <= 59 point >= 0 false point <= 59 false && false Grade2 point < 0 point > 100 point -10 point < 0 true point > 100 true true short circuit evaluation && & & & &

20 62 Java3-10p.71 // if class Diff2A { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); int diff; if (a >= b) diff = a - b; diff = b - a; System.out.println("" + diff + ""); a12 Ÿ b3 Ÿ 9 a3 Ÿ b12 Ÿ 9 // class Diff2B { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); int diff = a >= b? a - b : b - a; System.out.println("" + diff + ""); 0 Diff2A if Diff2B println diff System.out.println("" + (a >= b? a - b : b - a) + "");

21 63 Java3-11p // 10 class Diff2digits1 { System.out.print(""); int a = stdin.nextint(); System.out.print(""); int b = stdin.nextint(); int diff = a >= b? a - b : b - a; if (diff <= 10) System.out.println("10"); System.out.println("11"); 12 Ÿ 3 Ÿ Ÿ 23 Ÿ 11 3 // 10 class Diff2digits2 { System.out.print(""); int a = stdin.nextint(); System.out.print(""); int b = stdin.nextint(); int diff = a >= b? a - b : b - a; System.out.println("" + (diff <= 10? "10" : "11") + ""); a b Diff2B Diff2digits1 if Diff2digits2 Diff2digits2 "" ""

22 64 Java3-12p.73 // class Min3 { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); System.out.print("c"); int c = stdin.nextint(); int min = a; if (b < min) min = b; if (c < min) min = c; System.out.println("" + min + ""); a3 Ÿ b1 Ÿ c2 Ÿ 1 a, b, c min a b min min b c min min c algorithm Fig.3-13 Fig.3-13

23 65 a, b, c 3, 1, 2 Fig.3-14 min a, b, c 1, 2, 3 3, 2, 1 5, 5, 5 3, 1, 3 int min = a; if (b < min) min = b; a = 3 b = 1 c = 2 min 3 1 a = 1 b = 2 c = 3 min 1 1 a = 3 b = 2 c = 1 min 3 2 a = 5 b = 5 c = 5 min 5 5 a = 3 b = 1 c = 3 min if (c < min) min = c; Fig.3-14

24 66 Java3-13p.73 1, 2, 3 2 3, 2, 3 3 4, 4, 4 4 // class Med3 { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); System.out.print("c"); int c = stdin.nextint(); int med; if (a >= b) if (b >= c) med = b; if (a <= c) med = a; med = c; if (a > c) med = a; if (b > c) med = c; med = b; a152 Ÿ b324 Ÿ c75 Ÿ 152 System.out.println("" + med + ""); Fig.3-15 a, b, c median Med3 med,,, Fig.3-15 if ((b >= a && c <= a) (b <= a && c >= a)) med = a; if ((a > b && c < b) (a < b && c > b)) med = b; med = c;

25 67 3 Fig.3-15 Fig.3-16 Fig.3-16

26 68 Java3-14p.77 // class MinMaxEq { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); if (a == b) System.out.println(""); { a12 Ÿ b3 Ÿ 3 12 int min, max; // if (a < b) { // ab min = a; max = b; a17 Ÿ { b17 Ÿ min = b; max = a; System.out.println("" + min + ""); System.out.println("" + max + ""); a == b Fig.3-17 if (a == b) System.out.println(""); { int min, max; if (a < b) { min = a; max = b; { min = b; max = a; System.out.println("" + min + ""); System.out.println("" + max + ""); if () { if Fig.3-17

27 69{ if { block Fig.3-18 { Fig if if ( ) if ( ) if if min max if if a b { min = a; max = b; { min = b; max = a; if { if if if if (a < b) min = a; max = b; min = b; max = a; if ( ) ;

28 70 Java3-15p.77 // class Sort2Descending { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); if (a < b) { // ab int t = a; a = b; b = t; // System.out.println("ab"); System.out.println("a" + a + ""); System.out.println("b" + b + ""); a13 Ÿ b57 Ÿ ab a57 b13 a, b a b sort a b a b a b t a t b a t a b a b a 13 b 57 Fig.3-19 a 57 b 13

29 71 a a 13 b t b 57 Fig.3-19 t t = a; a = b; b = t; if if if (a == 1) if (b == 1) 1 2 if if if if (a == 1) if (b == 1) 3 1 a 1 b 1 1 a 1 b 1 2 a 1 2 a 1 b 1 if a 1 if (a == 1) if (b == 1) 1 2 if (a == 1) { if (b == 1) 1 2

30 72 Java3-16p.77 // class Sort3 { System.out.print("a"); int a = stdin.nextint(); System.out.print("b"); int b = stdin.nextint(); System.out.print("c"); int c = stdin.nextint(); if (a > b) { // abab int t = a; a = b; b = t; if (b > c) { // bcbc int t = b; b = c; c = t; if (a > b) { // abab int t = a; a = b; b = t; System.out.println("abc"); System.out.println("a" + a + ""); System.out.println("b" + b + ""); System.out.println("c" + c + ""); a53 Ÿ b35 Ÿ c42 Ÿ abc a35 b42 c53 a, b, c if Fig.3-20 a, b, c 5, 3, 2 if t t a b a b if b c b c c if c c a, b b if

31 73if (a > b) ab if (b > c) bc if (a > b) ab Fig.3-20 a b c a b b a b c c b a Fig.3-21 a, b, c 5, 3, 4 a > b 3 if (a > b) ab if (b > c) bc if (a > b) ab a b c a b c Fig.3-21

32 74 Java3-17p.83 0, 1, 2 0 "" 1 "" 2 "" // import java.util.random; class FingerFlashing { Random rand = new Random(); System.out.print(""); int hand = rand.nextint(3); // 02 switch (hand) { case 0: System.out.println(""); break; case 1: System.out.println(""); break; case 2: System.out.println(""); break; 0 ""1 ""2 "" switch statement switch switch ( ) switch hand 1 case 1: case 1 label hand 1 Fig.3-22 System.out.println(""); break statement break; switch

33 75 switch ( hand ) { case 0: System.out.println(""); break; case 1: System.out.println(""); break; case 2: System.out.println(""); break; Fig.3-22 break switch hand 1 "" "" hand 0 "" 2 "" break switch switch hand 0, 1, 2 switch 3 case 2 "" break break switch break 3 switch switch (hand) { case 0: System.out.println(""); break; case 1: System.out.println(""); break; case 2: System.out.println(""); break; case 3: System.out.println(""); break; switch case 2: break case 2: break break break

34 76 Java3-18p // class Season { System.out.print(""); int month = stdin.nextint(); 6 Ÿ 11 Ÿ switch (month) { case 3 : case 4 : case 5 : System.out.println(""); break; case 6 : case 7 : case 8 : System.out.println(""); break; case 9 : case 10 : case 11 : System.out.println(""); break; case 12 : case 1 : case 2 : System.out.println(""); break; default : System.out.println(""); break; 13 Ÿ switch default: case default break month switch switch if switch selection statement Fig.3-23 switch ( ) char, byte, short, int, Character, Byte, Short, Integer,

35 77{ case : default Fig.3-23 break Fig.3-24 break 3 switch switch ( ) break break ; Fig.3-24 if switch switch Fig.3-25 if if a 4 if b 4 c 80 a 1, 2, 3 b 4 if if (a == 4) if (a == 4) switch if (a == 1) c = 10; if (a == 2) c = 20; if (a == 3) c = 50; if (b == 4) c = 80; // ifswitch switch (a) { case 1 : c = 10; break; case 2 : c = 20; break; case 3 : c = 50; break; default : if (b == 4) c = 80; break; Fig.3-25

36 78 if keyword Table 3-6 Table 3-6 abstract assert boolean break byte case catch char class const continue default do double enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch syncronized this throw throws transient try void volatile while const goto truefalsenull separator Table 3-7 Table 3-7 [ ] ( ) {, :. identifier $ _ $ _ true false null $

37 79 v v1 va $x $1 _1 If X $!! if #911 -x {x #if operator Table 3-8 precedence * / + - a + b * c (a + b) * c a + (b * c) + * + ( ) // *+( ) System.out.println("ab" + a * b + ""); //?:+( ) System.out.println("" + (a > b? a : b) + ""); 3 + ( ) ( ) associativity a b c (a b) c a (b c) (5-3) - 1 // (3-1)

38 80 Table 3-8 [ ] x[y] ( ) x(arg opt ). x.y ++ x++ -- x x -- --x + +x + - -x -!!x ~ ~x new new new ( ) ( ) * x * y / x / y % x % y + x + y - x - y << x << y >> x >> y >>> x >>> y < x < y > x > y <= x <= y >= x >= y instanceof x instanceof y instanceof == x == y!= x!= y & x & y ^ x ^ y x y && x && y

39 81 /= x /= y %= x %= y += x += y -= x -= y <<= x <<= y >>= x >>= y >>>= x >>>= y 3 x y? : x? y : z = x = y *= x *= y &= x &= y ^= x ^= y = x = y x int x = 2 x int 2 a b int Fig.3-26 a = b = 1 a = (b = 1) // = b = 1 b 1 b = 1 b int 1 a a b 1 int 1 int 1 Fig.3-26 a = b = 1

40 82 (1) (1) (2) (3) { (4) a + b * c * (5) + (5) (6) <, >, <=, >= (7) ==,!= (8) &&, (9)! (10)? : (11) (12) if (13) (14) (15) (16) (17) int a = 1, b = 3, c = 5; System.out.println("a < b : " + (a < b)); System.out.println("a <= b : " + (a <= b)); System.out.println("b > c : " + (b > c)); System.out.println("b >= c : " + (b >= c)); System.out.println("a == b : " + (a == b)); System.out.println("a!= b : " + (a!= b)); System.out.println("a - b - c : " + (a - b - c)); System.out.println("c - b - a : " + (c - b - a)); System.out.println("a = b = c : " + (a = b = c)); System.out.println("c = b = a : " + (c = b = a)); a < b : a <= b : b > c : b >= c : a == b : a!= b : a - b - c : c - b - a : a = b = c : c = b = a : (18)

41 83 x y x && y false false (19) false true (20) true false (21) true true (22) x y x y false false (23) false true (24) true false (25) true true (26) c c 0 (27) (c (28) 10 == 0) System.out.println("c0"); 3 x 0 (29) (x == 0) System.out.println(""); (30) System.out.println(""); a System.out.println("a" + (31) ); a b System.out.println("ab" + (32) ); System.out.println("ab" + (33) ); w (34) (w) { (35) 0: System.out.println(""); (36) ; (35) 1: System.out.println(""); (36) ; (35) 2: System.out.println(""); (36) ;

42 84 (37) (38) + (39) = (40) m 3, 4, 5 if ( (41) (42) (43) ) System.out.println(""); System.out.println(""); if ( (44) && (45) ) System.out.println(""); System.out.println(""); if ( (46) ( (47) (48) )) System.out.println(""); System.out.println(""); m3 if ( (49) ) System.out.println(""); (50) System.out.println(""); (51) System.out.println(""); switch ( (52) ) { case (53) System.out.println(""); break; case (54) System.out.println(""); break; case (55) System.out.println(""); break; a b if ( (56) ) System.out.println("ab"); System.out.println("ab");

43 85 switch int n 1, 2, 3 (60) (61) (62) int n 0, 1 (63) (64) switch (n) { case 1: System.out.print("A"); case 2: System.out.print("B"); break; default: System.out.print("C"); if (n == 0); System.out.println("A"); 3 a b a b if ( (57) ) { int t = a; a = (58) ; b = (59) ; a b a b if (a * (65) < (66) ) System.out.println("ab"); a, b, c if ( (67) && (68) ) System.out.print(""); a b import (69).util.Scanner; (70) Even { public (71) void main( (72) args) { System.out.print(""); int a = (73).nextInt(); System.out.print(""); int b = (73).nextInt(); int c = 0; if ( (74) ) c = c + 1; if ( (75) ) c = c + 1; if ( (76) == 0) System.out.println(""); if ( (77) ) System.out.println(""); if ( (78) ) System.out.println("");

新・明解Java入門

新・明解Java入門 537,... 224,... 224,... 32, 35,... 188, 216, 312 -... 38 -... 38 --... 102 --... 103 -=... 111 -classpath... 379 '... 106, 474!... 57, 97!=... 56 "... 14, 476 %... 38 %=... 111 &... 240, 247 &&... 66,

More information

Java演習(4) -- 変数と型 --

Java演習(4)   -- 変数と型 -- 50 20 20 5 (20, 20) O 50 100 150 200 250 300 350 x (reserved 50 100 y 50 20 20 5 (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics; (reserved public class Blocks1 extends

More information

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF pref : 2004/6/5 (11:8) pref : 2004/6/5 (11:8) pref : 2004/6/5 (11:8) 3 5 14 18 21 23 23 24 28 29 29 31 32 34 35 35 36 38 40 44 44 45 46 49 49 50 pref : 2004/6/5 (11:8) 50 51 52 54 55 56 57 58 59 60 61

More information

明解Javaによるアルゴリズムとデータ構造

明解Javaによるアルゴリズムとデータ構造 21 algorithm List 1-1 a, b, c max Scanner Column 1-1 List 1-1 // import java.util.scanner; class Max3 { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); Chap01/Max3.java

More information

新・明解Javaで学ぶアルゴリズムとデータ構造

新・明解Javaで学ぶアルゴリズムとデータ構造 第 1 章 基本的 1 n 21 1-1 三値 最大値 algorithm List 1-1 a, b, c max // import java.util.scanner; class Max3 { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); List 1-1 System.out.println("");

More information

JavaプログラミングⅠ

JavaプログラミングⅠ Java プログラミング Ⅰ 3 回目変数 今日の講義で学ぶ内容 変数とは 変数の使い方 キーボード入力の仕方 変 数 変 数 一時的に値を記憶させておく機能です 変数は 型 ( データ型ともいいます ) と識別子をもちます 2 型 変数に記憶できる値の種類です型は 値の種類に応じて次の 8 種類があり これを基本型といいます 基本型値の種類値の範囲または例 boolean 真偽値 true または

More information

Java Java Java Java Java 4 p * *** ***** *** * Unix p a,b,c,d 100,200,250,500 a*b = a*b+c = a*b+c*d = (a+b)*(c+d) = 225

Java Java Java Java Java 4 p * *** ***** *** * Unix p a,b,c,d 100,200,250,500 a*b = a*b+c = a*b+c*d = (a+b)*(c+d) = 225 Java Java Java Java Java 4 p35 4-2 * *** ***** *** * Unix p36 4-3 a,b,c,d 100,200,250,500 a*b = 20000 a*b+c = 20250 a*b+c*d = 145000 (a+b)*(c+d) = 225000 a+b*c+d = 50600 b/a+d/c = 4 p38 4-4 (1) mul = 1

More information

JavaプログラミングⅠ

JavaプログラミングⅠ Java プログラミング Ⅰ 3 回目変数 今日の講義で学ぶ内容 変数とは 変数の使い方 キーボード入力の仕方 変 数 変 数 一時的に値を記憶させておく機能です 変数は 型 ( データ型ともいいます ) と識別子をもちます 2 型 変数に記憶できる値の種類です型は 値の種類に応じて次の 8 種類があり これを基本型といいます 基本型値の種類値の範囲または例 boolean 真偽値 true または

More information

K227 Java 2

K227 Java 2 1 K227 Java 2 3 4 5 6 Java 7 class Sample1 { public static void main (String args[]) { System.out.println( Java! ); } } 8 > javac Sample1.java 9 10 > java Sample1 Java 11 12 13 http://java.sun.com/j2se/1.5.0/ja/download.html

More information

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の break; まで処理しますどれにも一致致しない場合 def

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の break; まで処理しますどれにも一致致しない場合 def Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の まで処理しますどれにも一致致しない場合 default: から直後の まで処理します 式の結果 ラベル 定数 整数または文字 (byte, short, int,

More information

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV [email protected] [email protected] http://www.misojiro.t.u-tokyo.ac.jp/ tutimura/sem3/ 2002 12 11 p.1/33 10/16 1. 10/23 2. 10/30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20

More information

Java学習教材

Java学習教材 Java 2016/4/17 Java 1 Java1 : 280 : (2010/1/29) ISBN-10: 4798120987 ISBN-13: 978-4798120980 2010/1/29 1 Java 1 Java Java Java class FirstExample { public static void main(string[] args) { System.out.println("

More information

情報技術 Java の特徴 Java は現在 事務処理計算用プログラミング言語として開発された COBOL に取って代わり C 言語や C++ と並んで 現在最も使われているプログラミング言語の一つである Java は Write Once, Run Anywhere( プログラムを一度作成したらど

情報技術 Java の特徴 Java は現在 事務処理計算用プログラミング言語として開発された COBOL に取って代わり C 言語や C++ と並んで 現在最も使われているプログラミング言語の一つである Java は Write Once, Run Anywhere( プログラムを一度作成したらど 情報技術 Java の特徴 Java は現在 事務処理計算用プログラミング言語として開発された COBOL に取って代わり C 言語や C++ と並んで 現在最も使われているプログラミング言語の一つである Java は Write Once, Run Anywhere( プログラムを一度作成したらどこでも動く ) という構想で設計されており 以下のような特徴を持つ 一度作成したら どんなプラットフォーム上でも動作する

More information

r1.dvi

r1.dvi 2006 1 2006.10.6 ( 2 ( ) 1 2 1.5 3 ( ) Ruby Java Java Java ( Web Web http://lecture.ecc.u-tokyo.ac.jp/~kuno/is06/ / ( / @@@ ( 3 ) @@@ : ( ) @@@ (Q&A) ( ) 1 http://www.sodan.ecc.u-tokyo.ac.jp/cgi-bin/qbbs/view.cgi

More information

新・明解Javaで学ぶアルゴリズムとデータ構造

新・明解Javaで学ぶアルゴリズムとデータ構造 第 3 章 探索 Arrays.binarySearch 743 3-1 探索 探索 searching key 配列 探索 Fig.3-1 b c 75 a 6 4 3 2 1 9 8 2 b 64 23 53 65 75 21 3-1 53 c 5 2 1 4 3 7 4 Fig.3-1 a 763 3-2 線形探索 線形探索 linear search sequential search 2

More information

Javaによるアルゴリズムとデータ構造

Javaによるアルゴリズムとデータ構造 1 algorithm List 1-1 a, b, c List 1-1 // import java.util.scanner; class Max3 { public static void main(string[] args) { Scanner stdin = new Scanner(System.in); int a, b, c; int max; // Chap01/Max3.java

More information

明解Java入門編

明解Java入門編 1 Fig.1-1 4 Fig.1-1 1-1 1 Table 1-1 Ease of Development 1-1 Table 1-1 Java Development Kit 1 Java List 1-1 List 1-1 Chap01/Hello.java // class Hello { Java System.out.println("Java"); System.out.println("");

More information

8 if switch for while do while 2

8 if switch for while do while 2 (Basic Theory of Information Processing) ( ) if for while break continue 1 8 if switch for while do while 2 8.1 if (p.52) 8.1.1 if 1 if ( ) 2; 3 1 true 2 3 false 2 3 3 8.1.2 if-else (p.54) if ( ) 1; else

More information

Java updated

Java updated Java 2003.07.14 updated 3 1 Java 5 1.1 Java................................. 5 1.2 Java..................................... 5 1.3 Java................................ 6 1.3.1 Java.......................

More information

(Eclipse\202\305\212w\202\324Java2\215\374.pdf)

(Eclipse\202\305\212w\202\324Java2\215\374.pdf) C H A P T E R 11 11-1 1 Sample9_4 package sample.sample11; public class Sample9_4 { 2 public static void main(string[] args) { int[] points = new int[30]; initializearray(points); double averagepoint =

More information

解きながら学ぶC++入門編

解きながら学ぶC++入門編 !... 38!=... 35 "... 112 " "... 311 " "... 4, 264 #... 371 #define... 126, 371 #endif... 369 #if... 369 #ifndef... 369 #include... 3, 311 #undef... 371 %... 17, 18 %=... 85 &... 222 &... 203 &&... 40 &=...

More information

break 文 switch ブロック内の実行中の処理を強制的に終了し ブロックから抜けます switch(i) 強制終了 ソースコード例ソースファイル名 :Sample7_1.java // 入力値の判定 import java.io.*; class Sample7_1 public stati

break 文 switch ブロック内の実行中の処理を強制的に終了し ブロックから抜けます switch(i) 強制終了 ソースコード例ソースファイル名 :Sample7_1.java // 入力値の判定 import java.io.*; class Sample7_1 public stati Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の まで処理しますどれにも一致しない場合 default: から直後の まで処理します 式は byte, short, int, char 型 ( 文字または整数 ) を演算結果としますラベルには整数リテラル

More information

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1 Java (9) 1 Lesson 7 2008-05-20 Java System.out.println() 1 Java API 1 Java Java 1 GUI 2 Java 3 1.1 5 3 1.0 10.0, 1.0, 0.5 5.0, 3.0, 0.3 4.0, 1.0, 0.6 1 2 4 3, ( 2 3 2 1.2 Java (stream) 4 1 a 5 (End of

More information

明解Javaによるアルゴリズムとデータ構造

明解Javaによるアルゴリズムとデータ構造 74 searching 3 key Fig.3-1 75 2を探索 53を探索 3-1 5 2 1 4 3 7 4 を探索 Fig.3-1 76 3 linear searchsequential search 2 Fig.3-2 0 ❶ ❷ ❸ 配列の要素を先頭から順に走査していく 探索すべき値と等しい要素を発見 Fig.3-2 6 4 3 2 3-2Fig.3-3 77 5 Fig.3-3 0

More information

Java講座

Java講座 ~ 第 1 回 ~ 情報科学部コンピュータ科学科 2 年竹中優 プログラムを書く上で Hello world 基礎事項 演算子 構文 2 コメントアウト (//, /* */, /** */) をしよう! インデントをしよう! 変数などにはわかりやすい名前をつけよう! 要するに 他人が見て理解しやすいコードを書こうということです 3 1. Eclipse を起動 2. ファイル 新規 javaプロジェクト

More information

コーディング基準.PDF

コーディング基準.PDF Java Java Java Java.java.class 1 private public package import / //////////////////////////////////////////////////////////////////////////////// // // // // ////////////////////////////////////////////////////////////////////////////////

More information

2

2 問題 次の設問に答えよ 設問. Java のソースコードをコンパイルするコマンドはどれか a) java b) javac c) javadoc d) javaw 設問. Java のバイトコード ( コンパイル結果 ) を実行するコマンドはどれか a) java b) javac c) javadoc d).jar 設問. Java のソースコードの拡張子はどれか a).c b).java c).class

More information

目的 泡立ち法を例に Comparableインターフェイスの実装 抽象クラスの利用 型パラメタの利用 比較 入替 の回数を計測

目的 泡立ち法を例に Comparableインターフェイスの実装 抽象クラスの利用 型パラメタの利用 比較 入替 の回数を計測 泡立ち法とその実装 計算機アルゴリズム特論 :2017 年度只木進一 目的 泡立ち法を例に Comparableインターフェイスの実装 抽象クラスの利用 型パラメタの利用 比較 入替 の回数を計測 Comparable インターフェイ ス クラスインスタンスが比較可能であることを示す Int compareto() メソッドを実装 Integer Double String などには実装済み public

More information

アルゴリズムとデータ構造1

アルゴリズムとデータ構造1 1 2005 7 22 22 (sakai.keiichi@kochi [email protected]) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2005/index.html tech.ac.jp/k1sakai/lecture/alg/2005/index.html f(0) = 1, f(x) =

More information

問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。

問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。 ソフトウェア基礎演習課題 文法理解度確認範囲 問題 1 データ型 ( 変数, データ型 ) 問題 2 制御構造 (switch 文 ) 問題 3 制御構造 (while 文 ) 問題 4 制御構造と配列 ( 総和 ) 問題 5 制御構造と配列 ( 総和, 平均 ) 問題 6 データ型と各種演算子 ( 文字列, 検索 ) 問題 7 クラスの定義 ( メソッドの定義, コンストラクタの定義, キャスト

More information

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value = Part2-1-3 Java (*) (*).class Java public static final 1 class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value

More information

untitled

untitled II yacc 005 : 1, 1 1 1 %{ int lineno=0; 3 int wordno=0; 4 int charno=0; 5 6 %} 7 8 %% 9 [ \t]+ { charno+=strlen(yytext); } 10 "\n" { lineno++; charno++; } 11 [^ \t\n]+ { wordno++; charno+=strlen(yytext);}

More information

新・明解C言語で学ぶアルゴリズムとデータ構造

新・明解C言語で学ぶアルゴリズムとデータ構造 第 1 章 基本的 1 n 141 1-1 三値 最大値 algorithm List 1-1 a, b, c max /* */ #include int main(void) { int a, b, c; int max; /* */ List 1-1 printf("\n"); printf("a"); scanf("%d", &a); printf("b"); scanf("%d",

More information

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value =

class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value = Part2-1-3 Java (*) (*).class Java public static final 1 class IntCell { private int value ; int getvalue() {return value; private IntCell next; IntCell next() {return next; IntCell(int value) {this.value

More information

2

2 問題 1 次の設問 1~5 に答えよ 設問 1. Java のソースプログラムをコンパイルするコマンドはどれか a) java b) javac c) javadoc d) jdb 設問 2. Java のバイトコード ( コンパイル結果 ) を実行するコマンドはどれか a) java b) javac c) javadoc d) jdb 設問 3. Java のソースプログラムの拡張子はどれか a).c

More information

I java A

I java A I java 065762A 19.6.22 19.6.22 19.6.22 1 1 Level 1 3 1.1 Kouza....................................... 3 1.2 Kouza....................................... 4 1.3..........................................

More information

3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello World");

3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println(Hello World); (Basic Theory of Information Processing) Java (eclipse ) Hello World! eclipse Java 1 3 Java 3.1 Hello World! Hello World public class HelloWorld { public static void main(string[] args) { System.out.println("Hello

More information

ALG2012-C.ppt

ALG2012-C.ppt 2012717 ([email protected]) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2012/index.html 1 1. 2. 2 .. 3 public class WeightedNode { private E value; // private Map

More information

226

226 226 227 Main ClientThread Request Channel WorkerThread Channel startworkers takerequest requestqueue threadpool WorkerThread channel run Request tostring execute name number ClientThread channel random

More information

VB.NETコーディング標準

VB.NETコーディング標準 (C) Copyright 2002 Java ( ) VB.NET C# AS-IS [email protected] [email protected] Copyright (c) 2000,2001 Eiwa System Management, Inc. Object Club Kenji Hiranabe02/09/26 Copyright

More information

問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。

問 次の Fortran プログラムの説明及びプログラムを読んで、設問に答えよ。 解答例 問題 1 変数 a が 3 以上でかつ 7 以下の場合 true と表示し そうでない場合は false と表示するプログラムである public class Prog061004_01 { int a; boolean b; a = Integer.parseInt(buf.readLine()); b = (a >= 3) && (a

More information

r02.dvi

r02.dvi 172 2017.7.16 1 1.1? X A B A X B ( )? IBMPL/I FACOM PL1 ( ) X ( ) 1.2 1 2-0 ( ) ( ) ( ) (12) ( ) (112) (131) 281 26 1 (syntax) (semantics) ( ) 2 2.1 BNF BNF(Backus Normal Form) Joun Backus (grammer) English

More information

$ java StoreString abc abc ed abced twice abcedabced clear xyz xyz xyz bingo! abc bingo!abc ^Z mport java.io.*; ublic class StoreString { public static void main(string[] args) throws IOException{ BufferedReader

More information

r3.dvi

r3.dvi 00 3 2000.6.10 0 Java ( 7 1 7 1 GSSM 1? 1 1.1 4 4a 4b / / 0 255 HTML X 0 255 16 (0,32,255 #0020FF Java xclock -bg #0020FF xclock ^C (Control C xclock 4c 1 import java.applet.applet; import java.awt.*;

More information

新・明解Java入門

新・明解Java入門 第 1 章 画面 文字 表示 Java Java Java Java Java JRE Java JDK 21 1-1 Java Java Java Java 誕生 Fig.1-1 Oak Java Sun Microsystems 2010 Oracle Java Oracle 4 Java http://www.java.com/ http://www.alice.org/ Fig.1-1Java

More information

19 3!! (+) (>) (++) (+=) for while 3.1!! (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics;

19 3!! (+) (>) (++) (+=) for while 3.1!! (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics; 19 3!!...... (+) (>) (++) (+=) for while 3.1!! 3.1.1 50 20 20 5 (20, 20) 3.1.1 (1)(Blocks1.java) public class Blocks1 extends JApplet { public void paint(graphics g){ 5 g.drawrect( 20, 20, 50, 20); g.drawrect(

More information

IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap

IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap 1-1 1-2 IE6 2 BMI 3-1 3-2 4 5 chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chapter8 : 13-1 13-2 14 15 PersonTest.java KazuateGame.java

More information

2.2 Java C main Java main 2 C 6 C Java 3 C Java ( ) G101Hello.java G101Hello main G101Hello.java /* G101Hello */ class G101Hello { /* main */ public s

2.2 Java C main Java main 2 C 6 C Java 3 C Java ( ) G101Hello.java G101Hello main G101Hello.java /* G101Hello */ class G101Hello { /* main */ public s 2 2013 4 16 2.1............................... 2 1 2.2 Java......................... 2 2 2.3............. 2 2 2.4................................ 2 4 2.5............................ 2 5 2.6............................

More information

Microsoft Word - keisankigairon.ch doc

Microsoft Word - keisankigairon.ch doc 1000000100001010 1000001000001011 0100001100010010 1010001100001100 load %r1,10 load %r2,11 add %r3,%r1,%r2 store %r3,12 k = i + j ; = > (* 1 2 3 4 5 6 7 8 9 10) 3628800 DO 3 I=1,3 DO3I=1.3 DO3I 1.3

More information

2

2 プログラミング応用演習 b 10 月 5 日演習課題 2016/10/05 PAb 演習課題 プログラム仕様書作成課題 課題クラスを読み 次に示すクラスの仕様書を完成させよ なお 仕様書は クラス 1 つに付き 1 つ作成す る 加えて 図 1 のようなクラス継承の模式図を作成せよ < クラス名 のプログラム仕様書 > 作成者 : 学籍番号 名前 (1) クラスクラス名 : クラス名 説明 : クラスが何を表現しているか

More information

新版明解C言語 実践編

新版明解C言語 実践編 2 List - "max.h" a, b max List - max "max.h" #define max(a, b) ((a) > (b)? (a) : (b)) max List -2 List -2 max #include "max.h" int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max(x,

More information

class TestPrimitiveType{ public static

class TestPrimitiveType{ public static プリミティブ型 ( 基本データ型 ) プリミティブ型 ( 基本データ型 ) 浮動小数点の数値範囲が正負対称でないのは, べき乗表示にバイアスがかかっているのと 0 以外すべて最上位 bit が 1 と決まっているので最上位を省略しているためである 分類 型 ビット数数値の範囲 符号付き整数 byte 8-2 7 ~+2 7-1(-128~+127) 符号付き整数 short 16-2 15 ~+2

More information

ex01.dvi

ex01.dvi ,. 0. 0.0. C () /******************************* * $Id: ex_0_0.c,v.2 2006-04-0 3:37:00+09 naito Exp $ * * 0. 0.0 *******************************/ #include int main(int argc, char **argv) double

More information

問題1 以下に示すプログラムは、次の処理をするプログラムである

問題1 以下に示すプログラムは、次の処理をするプログラムである 問題 1 次に示すプログラムは 配列 a の値を乱数で設定し 配列 a の値が 333 より大きく 667 以下の値 の合計値を求めるプログラムである 1 と 2 に適切なコードを記述してプログラムを完 成させよ class TotalNumber { public static void main(string[] args) { int[] a = new int[1000]; // 1 解答条件

More information

解答上の注意 1 解答は 解答 紙の問題番号に対応した解答欄にマークしなさい 2 選択肢は 問ごとに 意されています 問 1の選択肢は 問 2で使 しません 3 選択肢は量が多いため 探しやすさの観点よりグループ分けされています グループ分けに合わせて解答欄が区切られていますが 横 1 列で問題 1

解答上の注意 1 解答は 解答 紙の問題番号に対応した解答欄にマークしなさい 2 選択肢は 問ごとに 意されています 問 1の選択肢は 問 2で使 しません 3 選択肢は量が多いため 探しやすさの観点よりグループ分けされています グループ分けに合わせて解答欄が区切られていますが 横 1 列で問題 1 解答上の注意 1 解答は 解答 紙の問題番号に対応した解答欄にマークしなさい 2 選択肢は 問ごとに 意されています 問 1の選択肢は 問 2で使 しません 3 選択肢は量が多いため 探しやすさの観点よりグループ分けされています グループ分けに合わせて解答欄が区切られていますが 横 1 列で問題 1つ分となっています 4 問題の 中の 1 2 などには 特に指 がないかぎり 与えられた 問選択肢群が

More information

ALG2012-F.ppt

ALG2012-F.ppt 2012 7 26 ([email protected]) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2012/index.html 5 2 3 4 - 5 .. 6 - 7 public class KnapsackBB { // 0-1 private static double maxsofar; private

More information

JAVA 11.4 PrintWriter 11.5

JAVA 11.4 PrintWriter 11.5 JAVA 11.4 PrintWriter 11.5 PrintWriter Writer Int, float, char Object print() println() tostring() PrintWriter PrintWriter(OutputStream outputstream) PrintWriter(OutputStream outputstream, boolean flushonnewline)

More information

アルゴリズムとデータ構造1

アルゴリズムとデータ構造1 1 200972 (sakai.keiichi@kochi [email protected]) http://www.info.kochi ://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2009/index.html 29 20 32 14 24 30 48 7 19 21 31 Object public class

More information

2 1 Web Java Android Java 1.2 6) Java Java 7) 6) Java Java (Swing, JavaFX) (JDBC) 7) OS 1.3 Java Java

2 1 Web Java Android Java 1.2 6) Java Java 7) 6) Java Java (Swing, JavaFX) (JDBC) 7) OS 1.3 Java Java 1 Java Java 1.1 Java 1) 2) 3) Java OS Java 1.3 4) Java Web Start Web / 5) Java C C++ Java JSP(Java Server Pages) 1) OS 2) 3) 4) Java Write Once, Run Anywhere 5) Java Web Java 2 1 Web Java Android Java

More information

JavaプログラミングⅠ

JavaプログラミングⅠ Java プログラミング Ⅰ 12 回目クラス 今日の講義で学ぶ内容 クラスとは クラスの宣言と利用 クラスの応用 クラス クラスとは 異なる複数の型の変数を内部にもつ型です 直観的に表現すると int 型や double 型は 1 1 つの値を管理できます int 型の変数 配列型は 2 5 8 6 3 7 同じ型の複数の変数を管理できます 配列型の変数 ( 配列変数 ) クラスは double

More information

ALG2012-A.ppt

ALG2012-A.ppt 21279 ([email protected]) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/212/index.html (, )ε m = n C2 = n ( n 1) / 2 m = n ( n 1) 1 11 11 111 11 111 111 1111 1 1 11 1 11 11 111 4-dimentional

More information