i I Java Java Java

Size: px
Start display at page:

Download "i I Java Java Java"

Transcription

1 Masami WADA ver.3.2 ( ) (ver.3.2.2) 10 (ver.3.2.1) 9 (ver.3.2) (^_^;)

2 i I Java Java Java Eclipse Pleiades JDK JRE Java I I System.out.print System.out.println Unicode Javadoc

3 ii (integer type): byte, short, int, long (real number type): float, double : boolean : char if switch boolean for while do while break continue null for

4 iii Math Math System.in System.out Scanner(System.in) II System.out.printf() I API (Application Programming Interface) ( ) main System.arraycopy ( ) (Selection sort) (Insertion sort) (Bubble sort) (Quicksort) (Heap sort) (Merge sort) (Shell s sort) Java II Java Java II

5 iv private package-private protected public ( ) Object super final ( ) (Exception) Exception try catch finally Scanner next nextline File Scanner(File) FileInputStream / FileOutputStream FileReader / FileWriter BufferedReader / BufferedWriter InputStreamReader / OutputStreamWriter Java Excel Mathematica import import java.lang

6 v Generic Java API Collection ArrayList LinkedList Stack Queue static ( ) Thread Runnable JavaFX ( ) e(fx)clipse Scene Builder JavaFX Stage Scene enum

7 vi III Apx.1 A Apx.2 A Apx.2 A Apx.3 A Apx.4 A Apx.4 A Apx.5 A Apx.5 A Apx.5 A Apx.6 A Apx.7 A Apx.8 A Apx.8 A Apx.9 A Apx.10 A.9 IEEE Apx.10 A.9.1 IEEE Apx.10 A Apx.12 B Eclipse Apx.13 B Apx.13 B.2 Pleiades Apx.13 B.3 Java API Apx.14 B.4 Eclipse Apx.15 B Apx.15 B Apx.15 B.5 Java API Apx.17 B.6 Eclipse Apx.17 B.6.1 workspace Apx.17 B Apx.20 B.6.3 API Apx.20 B.6.4 Eclipse Apx.22 B.7 Eclipse Apx.25 C javadoc Apx.27 C.1 Javadoc Apx.27 C Apx.27 C.3 Javadoc Apx.31 D debug Apx.32 D Apx.32 D Apx.32

8 vii D Apx.32 D Apx.33 D Apx.33 D Apx.33 D Apx.33 D Apx.34 Ind.1

9 viii 2.1 HelloJava.java ( Duke) HelloJava2.java ( Duke ) UnicodeCheck.java (UNICODE ) Comments.java ( ) IntegerMaxAndMin.java ( ) RealNumberMaxAndMin.java ( ) CharIsInteger.java ( ) Substitution.java ( ) Arithmetics.java ( ) PAdditionValues.java ( ) SwapValues.java ( ) AdditionValues2.java ( ) CompoundAssignment.java ( ) ErrorSubst.java ( ) SubstInEquation.java ( ) SubstInEquation2.java ( ) MultipleSubst.java ( ) MultipleSubst2.java ( ) MultipleSubst3.java ( ) Casting1.java ( ) Casting2.java ( ) CompoundAssign.java ( ) IfElse.java (if else ) OddOrEven.java ( ) Vote.java ( ) RankEvaluation.java ( ) IsVariable.java (boolean ) DoubleCounter.java ( ) FibonacciNumbers.java ( ) NoMultiple.java (3 ) CollatzSequence.java ( ) WhileVSDoWhile.java (while do while ) InfiniteLoop.java ( )

10 ix 2.35 GuessingGame.java ( ) MultiplicationTable.java ( ) DoubleLoop.java ( ) DoubleLoop2.java ( break ) DoubleLoop3.java ( continue ) LoopParameter.java ( ) ArrayInitialization.java ( ) PrintArray.java ( ) ArrayInitialization2.java ( ) SumOfArray.java ( ) MaxOfArray.java ( ) AverageOfPositive.java ( ) OlympicGame.java ( ) DifferenceTwoArray.java ( ) SwapArray.java ( ) ToReverseOrder.java ( ) EvaluateGrade.java ( ) ShiftArray.java ( ) ArrayInitialization3.java ( ) StudentDegrees.java ( ) SetArrays.java ( ) ArrayIndexOutOfBounds.java ( ) ArrayCopy.java ( ) ArrayCopy2.java ( ) ArrayCopy3.java ( ) ExtendedForEach.java ( for ) ExtendedForEach2.java ( for ) ExtendedForEach3.java ( for ) MatrixVector.java ( ) MatrixMatrix.java ( ) TrigonometricTable.java ( ) MathMethods.java ( ) TriangulumArea.java ( ) OddEven.java ( ) QuadraticEquation.java ( ) TrigonometricTable2.java (printf ) NewtonMethod.java ( Newton ) CircleArea.java (main ) CircleArea2.java ( ) CircleArea3 ( ) SwapValues2.java ( ) FormalParameter.java ( )

11 x 2.77 LinearAlgebra.java ( ) FibonacciRecursive.java ( ) FactorialRecursive.java ( ) HanoiTower.java ( ) MainParameters.java ( ) ArrayCopyExample1.java ( ) ArrayCopyExample2.java ( ) ArrayCopyExample3.java ( ) ArrayCloneExample.java ( ) StringvsChar.java ( ) StringMethods.java ( ) MyTools.java ( ) SortingExample.java ( ) ArraySorting.java#selectionSort ( ) SortingExample.java#main ( ) ArraySorting.java#selectionSort ( ) SortingExample.java#main ( ) ArraySorting.java#bubbleSort ( ) SortingExample.java#main ( ) ArraySorting.java#quickSort ( ) SortingExample.java#main ( ) ArraySorting.java#heapSort ( ) SortingExample.java#main ( ) ArraySorting.java#mergeSort ( ) SortingExample.java#main ( ) ArraySorting.java#shellSort ( ) TimePrecisionExample.java ( ) WrongAverage.java ( ) SwapRowOfMatrix.java ( ) VectorNormalize.java ( ) Combination.java ( ) Maclaurin.java ( e ) MonteCarlo.java ( ) MargeArrays.java ( ) ArrayTransform.java ( ) InverseMatrix.java ( ) Student.java ( ) StudentExample.java ( ) Course.java ( ) ( ) StudentExample.java ( ) ( ) Student.java ( ) Point.java ( )

12 xi 3.9 PointExample.java ( ) ( ) Student.java ( ) MembershipExample.java ( ) Entry.java ( ) Member.java ( ) TaroExample.java ( ) ComplexNumberExample.java ( ) ComplexNumber.java ( ) MyMathExample.java ( Math ) CentroidExample.java ( Point[]) PrivateFieldExample.java (private field ) ( )PrivateFieldExample.java (private field ) InheritanceExample.java ( ) Human.java ( ) Professor.java ( ) Lecture.java ( ) Student.java ( ) PointExample3.java ( ) WeightedPoint.java ( ) ObjectExample.tex (Object ) ArrayIsObject.java ( Object ) StringIsObject.java ( == equals() ) PointExample4.tex ( ) ColoredWeightedPoint.java ( ) OverrideExample2.java ( ) SuperExample.java (supar. ) FinalParameter.java ( final) FinalExample.java (final ) OverrideExample3.java ( ) InstanceOfExample.java ( ) PolymorphismExample.java ( (?)) DivideZeroExample.java ( ) OutOfBoundExample.java ( ) ( ) DivideZeroExample.java ( ) TwoCatchExample.java ( ) RedirectExample.java ( ) FinallyExample.java ( finally ) MyExceptionExample.java ( ) ScannerExample.java ( ) ScannerExample2.java (next) ScannerExample3.java (nextline) ScannerExample4.java (Scanner )

13 xii 3.61 DelimiterExample.java ( ) ScannerExample5.java ( ) HelloJavaFromFile.java ( Duke) FileScanner.java (Scanner ) ScannerReadFile.java (hasnext() ) StreamExample01.java (FileOutputStream ) StreamExample02.java (FileOutputStream ) StreamExample03.java (FileWriter ) StreamExample04.java (BufferedWriter ) StreamExample05.java (OutputStreamWriter ) CSV Example.java (CSV ) ImportExamaple.java ( ) StaticImportExample.java ( ) AbstractClass.java ( (?)) InterfaceExample.java ( ) InterfaceExample2.java ( instanceof) InterfaceExample3.java ( ) BoxExample1.java ( ) BoxExample2.java ( ) BoxExample3.java ( ) IntegersToObjects.java (Integer Object ) IntegersToObjects3.java ( ) GenericMethod.java (Generic method ) IntegerExample.java (Integer ) ArrayListExample.java (ArrayList ) ArrayListExample2.java ( ArrayList ) Point.java (Comparable ) ShapesExample.java ( ) LinkedListExample.java (LinkedList ) StackQueue.java (Stack Queue ) students.txt ( ):src/section StudentListExample.java (main ) Student.java ( ) Date.java ( ) StudentCell.java ( ) StudentList.java ( ) BinarySearchTreeExample.java NestedClassExample.java ( ) InnerClassExample.java ( ) AnonymouseClassExample.java ( ) ThreadExample01.java (Thread ) ThreadExample02.java (Thread )

14 xiii ThreadExample04.java (Runnable ) BankBooks.java ( ) JavaFXExample01.java ( JavaFX) JavaFXExample02.java (JavaFX ) JavaFXExample03.java ( ) JavaFXExample04.java ( ) PlayCardExample01.java ( ) PlayCardExample02.java ( ) PlayCardExample new.java (enum ) RGBColor.java ( ) EnumImportExample.java ( ) DeprecatedExample.java ( : ) VariableArityExample.java ( ) C.1 Circle.java ( ) Apx.27 C.2 Point.java ( ) Apx.28 C.3 Circle.java ( ) Apx.29 C.4 Point.java ( ) Apx.30

15 I Java 1

16 2 1 Java (machine language) (compiler) ( ) (compile) (programming language) Java Java PS 4 Java A B Eclipse Java OK! ) Java Java 1.1 Java Java ( Java ) 1990 Sun Microsystems ( Oracle ) ( Oak ) C C ++ C Web 1996 Java Java 2017/9 ver. 9 ( 2018/2 ) Java

17 1.2 3 Java manual.xml Java (Object oriented) (paradigm : ) (^^;) 2 ( ) 3 Java Java 1.2 C ++ Java Java Windows OS Mac OS OS (Operating System) (^^;) C ++ OS OS Java JVM (Java Virtual Machine Java ) Java JVM OS Java Write Once, Run Anywhere. Windows OS Mac OS linux ( ) OS JVM ( ) ( ) Java C ) C ++ Java 2018 Python (!?) ( Java ) Youtube Instagram Web Python Google Java C ++ Python Java Python Python Java Java Python Eclipse Pleiades IDE (Integrated Development Environment ) Eclipse ( ) Java

18 4 1 Java Java Eclipse NetBeans IntelliJ IDEA Eclipse IBM Java plugin ( ) C ++ PHP Perl C D TeX Python Ruby JavaScript AspectJ Mathematica SQL UML Eclipse 2018/2 ver.4.7 (Oxygen) (Oxygen ) Pleiades ( ) Eclipse Eclipse Pleiades All in one ( ) pleiades Pleiades Eclipse Eclipse JDK JRE Java Eclipse 1.1 Java JVM JRE (Java Runtime Environment Java ) JRE Java Java JDK (Java Development Kit Java ) JDK Oracle OS JDK JRE JDK Eclipse JDK JRE Eclipse JDK JDK Eclipse JRE JVM : Java Virtual Machine OS: Operating System 1.1 JDK Eclipse ( )

19 5 2 Java I 2.1 Java HalloJava.java Eclipse Java Java (^_^) 2.1 package section0201; public class HelloJava { public static void main( String[] args ) { System.out.print( " " ); System.out.print( "Duke " ); HelloJava.java ( Duke) 2.1 Duke (Duke Java ) Java ( ) Eclipse Java 1 2

20 6 2 Java I Eclipse (F) (N) Java Java (P) (F) ( 2.2 ) ( ) Java src Java src src 2.3 ) 2.3 (1) src (W) 2.4 ) section**** **** 0 2

21 (2) 2.1 HelloJava section0201 Java (M) section0201 (F) section0201 src ( 2.5 ) 2.5 (3) ( ) (source code)

22 8 2 Java I (binary code) (^^;) package section0201; public class HelloJava { public static void main( String[] args ) { System.out.print( " " ); System.out.print( "Duke " ); public class HelloJava Java ( ) java HelloJava.java HelloJava java. ( ) section0201 (W) Java (M) HelloJava ( 2.6 ).java public static void main(string[] args) (V) (F) 2.6 (1) Eclipse ( 2.7 ) // TODO

23 (2) (?) (indent) ( 3 4 ) tab ( ) ( ) ( ) 1 1 package section System.out.print( ) 1 Java (F) (S) Command+s HelloJava.java HalloJava.java (e a) ( HalloJava.java) (Alt+Shift+T) (Alt+Shift+R)

24 10 2 Java I ( ) Eclipse (F) (S) Eclipse Eclipse ( ) JVM Java Eclipse JVM Eclipse HelloJava ( ) (R) (S) Java ( ) (R) Java (N) ( ) (W) (P) Java (class) HelloJava HelloJava 1

25 (source file).java ( HelloJava ) ( ) public public public public ( ) (^^;) A B A.java 2. public A B A.java 3. A public B A.java 4. public A public B A.java Java ( graphical_tools) ( graphicaltools) ( GraphicalTools) (under score: ) OS ( )

26 12 2 Java I ( jp.ac.tus.ed.jmwada.programmingexamples) Java Eclipse 2.8 Eclipse 2.8 ( J ) Java Eclipse Java OS (^^;).java.java ) ( ) ( )

27 2.3 I Eclipse workspace ( ) C:Yjava C:Yjava.workspace Eclipse Eclipse ( Eclipse ) 2.3 I HelloJava section0201 HelloJava 2 7 HelloJava public static void main( String[] args ) (3 6 ) main main System.out.print System.out.print package section0201; public class HelloJava { public static void main( String[] args ) { System.out.print( " " ); System.out.print( "Duke " ); System.out.print Java (console) Eclipse ( ) System.out print method : 3 2 ( ) System.out print System.out.print (^^) (double quotations) print (semicolon) ; Java

28 14 2 Java I Duke Duke HelloJava System.out.println print Yn println println ( ) (^^)b? 2.2 package section0203; public class HelloJava2 { public static void main( String[] args ) { System.out.println( " " ); System.out.println( "Duke " ); HelloJava2.java ( Duke ) HelloJava2.java section0203 Duke System.out println System.out printf ( ) ( ) print println println ln line

29 Duke println System.out.println(); HalloJava2.java 2.4 a Z ASCII ( :American Standard Code for Information Interchange) NUL DEL SP P ` p 1 SCH DC1! 1 A Q a q 2 STX DC2 2 B R b r 3 ETX DC3 # 3 C S c s 4 EQT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v 7 BEL ETB 7 G W g w 8 BS CAN ( 8 H X h x 9 HT EM ) 9 I Y i y A LF SUB * : J Z j z B VT ESC + ; K [ k { C FF FS, < L \ l D CR GS - = M ] m E SO RS. > N ^ n ~ F SI US /? O _ o DEL 2.9 ASCII ASCII ( 2.9 ) 16 A (10 65) A ASCII bits ASCII 8 bits 7 7 bits bit 0 ASCII 8 bits

30 16 2 Java I JIS Shift JIS Java ( ) (Unicode) Unicode Unicode Xerox Microsoft Apple IBM Sun Microsystems Hewlett-Packard JustSystems The Unicode Consortium 1993 Java Unicode Unicode A (^^) 2.3 UnicodeCheck.java (UNICODE ) package section0204; public class UnicodeCheck { public static void main( String[] args ) { System.out.println( "A Unicode " + Integer.toHexString( A ) ); System.out.println( " Unicode " + Integer.toHexString( ) ); System.out.println( " Unicode " + Integer.toHexString( ) ); System.out.println( " Unicode " + Integer.toHexString( ) ); A Unicode ASCII A Unicode Unicode ( ) ASCII ASCII 5C ASCII ( 5C) Unicode 00A5 Unicode 005C 2.5 ( )

31 NUL DEL SP P ` p 1 SCH DC1! 1 A Q a q 2 STX DC2 2 B R b r 3 ETX DC3 # 3 C S c s 4 EQT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v 7 BEL ETB 7 G W g w 8 BS CAN ( 8 H X h x 9 HT EM ) 9 I Y i y A LF SUB * : J Z j z B VT ESC + ; K [ k { C FF FS, < L \ l D CR GS - = M ] m E SO RS. > N ^ n ~ NUL DEL SP P ` p 1 SCH DC1! 1 A Q a q 2 STX DC2 2 B R b r 3 ETX DC3 # 3 C S c s 4 EQT DC4 $ 4 D T d t 5 ENQ NAK % 5 E U e u 6 ACK SYN & 6 F V f v 7 BEL ETB 7 G W g w 8 BS CAN ( 8 H X h x 9 HT EM ) 9 I Y i y A LF SUB * : J Z j z B VT ESC + ; K [ k { C FF FS, < L l D CR GS - = M ] m E SO RS. > N ^ n ~ F SI US /? O _ o DEL F SI US /? O _ o DEL 2.10 ASCII 3 2 // /* */ Javadoc /** */ 2.4 Comments.java ( ) package section0205; public class Comments { int x = 10; // // int y = 20; 2 /* int z = 30; */ /* * Eclipse * */ /**

32 18 2 Java I * Javadoc C */ Eclipse 5 ( ) warning ( ) Eclipse x x Eclipse Eclipse 2.11 Eclipse ( 2.11 'unused' 'x' Enter Suppress warnings unused "unused" ) 1 (annotation: ) Java 5 Java ( ) ( ) // 1

33 ( x = 1; // x 1 ) ( // x 1 x = 1; ) 2 ( ) Eclipse Ctrl + 7 Ctrl + / // /* */ Eclipse Ctrl + Shift + / Ctrl + Shift + \ Javadoc C ( ) Javadoc Javadoc /** (* 1 ) */ 2.6 Java (primitive type) (reference type) 3 (floating point number type)

34 20 2 Java I (integer type): byte, short, int, long byte ( ) 8bits short ( ) 16bits int ( ) 32bits long ( ) 64bits = Java OS int 32bits C int 16bits 32bits bits 1bit bit bits Mathematica byte 8bits 1bit 7bits Mathematica Shift+Enter 127 byte 128 int 32bits long 1234 long 1234L 1234l ( l L) 64bits int short byte 2.5 IntegerMaxAndMin.java ( ) package section0206; public class IntegerMaxAndMin { System.out.println( " " ); System.out.println( "byte "); System.out.println( " = " + Byte.MIN_VALUE ); System.out.println( " = " + Byte.MAX_VALUE ); System.out.println( "short "); System.out.println( " = " + Short.MIN_VALUE ); System.out.println( " = " + Short.MAX_VALUE ); System.out.println( "int ");

35 System.out.println( " = " + Integer.MIN_VALUE ); System.out.println( " = " + Integer.MAX_VALUE ); System.out.println( "long "); System.out.println( " = " + Long.MIN_VALUE ); System.out.println( " = " + Long.MAX_VALUE ); (real number type): float, double Java (floating point number type) (fixed point number type) (^^;)? Java float ( ) 32bits IEEE754 double ( ) 64bits IEEE754 IEEE (Institude of Electrical and Elctrinics Engineers) 754 IEEE IEEE ( double float double float float 12.34F 12.34f double 12.34D 12.34d ( ) ( ) ( A ) 2.6 RealNumberMaxAndMin.java ( ) package section0206; public class RealNumberMaxAndMin { System.out.println( " " ); System.out.println( "float " ); System.out.println( " = " + Float.MIN_VALUE ); System.out.println( " = " + Float.MAX_VALUE ); System.out.println( "double " ); System.out.println( " = " + Double.MIN_VALUE );

36 22 2 Java I System.out.println( " = " + Double.MAX_VALUE ); float 7 double : boolean etc. Java boolean ( ) true ( ) false ( ) x = 1 true false (1) x + 1 = 2 (2) 4 < x (3) 0 < x x < 2 (4) x < 0 2 < x C boolean false 0 true 0 2 < 1 0 C ++ boolean bool true, false C false true false true Java : char char char ( character!? ) char ( forum=3) (^^) int long char ( ) Unicode 29356(=0x72AC) CharIsInteger.java ( ) package section0206; public class CharIsInteger { System.out.println( " " ); System.out.println( " " + (int) ); System.out.println( "16 " + Integer.toHexString( ) );

37 System.out.println( " = " + (char)( + 127) ); Java long ( ) Java double ( ) Java Java ( ) ( ) $ Java int ( ) Java

38 24 2 Java I xy 2. x-y 3. $xy 4. 10xy 5. xy 6. int 7. main 8. Class main Class Java main Class Java abstract assert boolean break byte case catch char class const continue default do double else 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 synchronized this throw throws transient try void volatile while Java Unicode ( 0 ) int x Java int x int x; int x y int x, y; C 1 1 ( int x; int y; 2 ) = ; x 10 x = 10; Java =

39 = 10; 10 = x; int x = 10; 2.8 package section0207; public class Substitution { byte i = 12; short j = 123; int k = 12345; long l = L; float x = f; double y = ; boolean b = true; char c = 'A'; Substitution.java ( 'unused' 'main()' Java ( double x = 123;) ( int y = ;) int long float double ( int i = 1L; ) byte i = 12; short j = 123; (^^) byte short long ( L ) 2 int 12 [ 128, 127] byte i = 12; OK byte i = 200; Java byte x = 1234; 2. short x = 1234; 3. int x = 1234; 4. long x = 1234; 5. long x = ; 6. float x = 1234; 7. float x = ; 8. double x = f; 9. double x = 1234L;

40 26 2 Java I 2.8 Java + ) % / + / % 5 % ( 1.2 % 0.5 = ) (arithmetic operator symbol) println 2.9 package section0208; public class Arithmetics { System.out.println( ); System.out.println( 13-5 ); System.out.println( 13 * 5 ); System.out.println( 13 / 5 ); System.out.println( 13 % 5 ); Arithmetics.java ( ) / int int int 30 int double double /5 int int int 2 ( ) /5, 13/5.0, 13./5. OK x x 1. int x = ; 2. int x = 11-4; 3. int x = 11 * 4; 4. int x = 11 / 4; 5. int x = 11 % 4; 6. double x = 11 / 4; 7. int x = 11.0 / 4; 8. double x = 11.0 / 4; 9. int x = 1 / 2; 10. double x = -1.0 / 2; 11. int x = -1 / 2.0;

41 int x y x + y int z PAdditionValues.java ( ) package section0208; public class AdditionValues { // x y z System.out.println( "x=" + x + ", y=" + y + ", x+y=" + z ); SwapValues.java 2.11 SwapValues.java ( ) package section0208; public class SwapValues { public static void main(strings[] args) { int a = 2; int b = 3; // a b // System.out.println( "a=" + a + ", b=" + b ); int x = (1+2)*(3+4); double y = ((1+2)*(3+4))/(2.0*5); 2 (1+2)*(3+4) (1+2)*(3+4)/(2.0*5) (1+2)*(3+4)/2.0* Java ( ) ( )

42 28 2 Java I System.out.println( "x=" + x + ", y=" + y + ", x+y=" + z ); + + " " + " " " " " " + 10 " 10" System.out.println( " " ); double x 1.5 int y 2 (System.out.println) x = 1.5, y = int a b a=10, b= = AdditionValues2.java ( ) package section0209; public class AdditionValues2 { int a = 12; int b = 34; System.out.println( /* */ ); = 46 b = -10 if = -10

43 int x 1 Java x = x + 1; ( ) = x 1 x x 1 x + 1; x x 2 x = x + x; OK x y x = x + y; x y 1 x,y = x,y + 1; x = x + 1; y = y + 1; x 2 3 Java x x x += 2; x -= 2; x *= 2; x /= 2; x %= 2; ( ) + = x += 2; x = x + 2; x =+ 2; x = +2; (2 ) x y (a ) 2.13 CompoundAssignment.java ( )

44 30 2 Java I package section0210; public class CompoundAssignment { public static void main(string[] arge) { int a = 0; a += 7; System.out.println( "a = " + a ); a -= 2; System.out.println( "a = " + a ); a *= 5; System.out.println( "a = " + a ); a /= 3; System.out.println( "a = " + a ); a %= 3; System.out.println( "a = " + a ); a /= 3 (int ) x package section0210; public class ErrorSubst { int a; a += 5; System.out.println( "a = " + a ); ErrorSubst.java ( ) Eclipse ( ) a 3 5 int a = 0; 5 (Java )

45 ± x 1 x++; ++x; x 1 x--; --x; x ++; ( ) ++ (increment operator symbol) -- (decrement operator symbol) = ( x++;) x 1 ( x x++; x++; ) C C C ( ) C# C ++ (++ ) x 1 4 ( 2 ) x 2 2 x = x + 2; x++++; x++ x = x + 1 x++++; (x = x + 1)++; x = x + 2; x += 2; x y 1 z z = (x + y)++; (x + y)++ (x + y) = (x + y) + 1 z = x + y + 1; ( ) ++x x x; x++; x 1 int y = ++x + 3; int y = x++ + 3; int y = ++x + 3; int y = (x = x + 1) + 3; x = x x 3 y 2.15 package section0210; public class SubstInEquation { SubstInEquation.java ( )

46 32 2 Java I int x = 10; int y = ( x = x + 1 ) + 3; System.out.println( "x = " + x ); System.out.println( "y = " + y ); ( x = x + 1 ) ++x x++ y ++x x = x + 1 x++ y 1 ( 1 ) (int y = (++x) + 3;) x = x + 1; int y = x + 3; x=11, y=14 (int y = (x++) + 3;) int y = x + 3; x = x + 1; x=11, y=13 ( ) (++x; int y = x + 3; ) SubstInEquation2.java ( ) package section0210; public class SubstInEquation2 { int x = 1; int y = ( ++x ) + ( x++ ); System.out.println( "x = " + x + ", y = " + y ); Java (multiple assignment statement) 2.17 package section0210; public class MultipleSubst { int a; int b; int c; MultipleSubst.java ( )

47 c = b = a = 1; System.out.println( "a=" + a + ", b=" + b + ", c=" + c ); c = b = a = 1; a 1 b = (a = 1) b 1 c = (b = 1) c 1 (^^;) 2.18 MultipleSubst2.java ( ) package section0210; public class MultipleSubst2 { int a = 1; int b = 1; int c = 1; System.out.println( "a=" + a + ", b=" + b + ", c=" + c ); c = b*2 = a = 1; c = b++ = a = 1; MultipleSubst3.java ( ) package section0210; public class MultipleSubst3 { int x = 2; int y = 3; int z = 4; z += y *= x; System.out.println( "x=" + x + ", y=" + y + ", z=" + z ); 2.11 ( ) ( ) int double (double x = 2;) (int y = 2.0;) double int (cast) 2.0 y int y = (int) 2.0; y 2

48 34 2 Java I int y = (int) ; (int) 2.0 int y = ; double 3.0 int int y = (int)( ); 2 ( y 3 ) double x int y y = (int) x; x 1.5 y y int x = (int) 2.5-1; 2. int x = ; 3. float x = (float) ; 4. int x = (int) 12345L; 5. long x = (long) 123; 6. double x = (double) 12.3F; 7. byte x = (byte) 100; 8. long x = (long) 1234; 9. char x = (char) 12354; 10. int x = (int) true; 11. int x = (int) 'A'; 7 int short, byte int short, byte byte x = 100; 100 byte [ 128, 127] OK int a = 100; int a = 100; byte x = a; byte x = (byte) a; a 100 byte x = 200; byte x = (byte) a; 200 [ 128, 127] 2.20 package section0211; public class Casting1 { public static void main(string[] arge) { byte x = (byte) 200; System.out.println( "x = " + x ); Casting1.java ( ) 200 int Java 32bits byte x 8bits (byte) 32bits 8bits 24bits ) x 56 byte x = 100; 2.11 Java

49 bits 符号付き整数の キャスト (byte) 8bits 符号付き整数 -56 (2 の補数表現に注意 ) byte byte [ 128, 127] (byte) (x ) 1. byte x = (byte) -129; 2. byte x = (byte) -128; 3. byte x = (byte) 127; 4. byte x = (byte) 128; 2.21 package section0211; public class Casting2 { public static void main(string[] arge) { double x = 10.1; int y = (int)( x / 0.1 ); System.out.println( "y = " + y ); Casting2.java ( ) y = 101 y = System.out.println( x / 0.1 ); int x y ( ) Math.round() x = 2 int x double CompoundAssign.java ( )

50 36 2 Java I package section0211; public class CompoundAssign { public static void main(string[] arge) { int x = 0; x += 2.5; System.out.println( "x = " + x ); x += 2.5; x = x + 2.5; x = (int)( x ); ( ) a 1. double a = / 2 + 2; 2. double a = / 2 + 2; 3. double a = (4 + 3) / 2 + 2; 4. double a = (double) / 2 + 2; 5. double a = (double)(4 + 3) / 2 + 2; 6. double a = / ( ); (comparison operator) ( (relational operator) ) 2 2 boolean ( true false) 6 true ( ) 2 int double OK 2 == 2 1 = 1 (1 1 )

51 A == B A B 1 == 1 A!= B A B 1!= 2 A > B A B 2 > 1 A >= B A B 2 >= 1 A < B A B 1 < 2 A <= B A B 1 <= true false == <= > % 3!= == 1 / 'B' == 'A' (2 <= 3)!= true boolean true false A && B A & B 2 A B true true A B A B A B true true!a A false true A ^ B A B true A B A && B A B!A A ^ B true true true true false false true false false true false true false true false true true true false false false false true false int x 1 true false 1. x <= < x && x <=5 3. (x % 2 == 0) (x % 2 == 1) 4.!(x > 2) 5. (x - 0.1) / 7 == (x % 2 == 0)^(x % 2 == 1)

52 38 2 Java I 2 0 < x <= 5 ( ) (0 < x) <= 5 true <= 5 2 A && B A & B (short-circuit evalution) A && B A false B true false false ( ) B A B A true B true false true B A & B A B A B A && B A B A && B A B B A A false B a!= 0 && b / a > c) A & B A B ) ( methoda() & methodb()) Java 1. x 5 true 2. x true x true x y 5 true int year true

53 (Exclusive OR) ( ) 2 true Java ( = ) A ^ B = (A &&!B) (!A && B) = (A B) && (!A!B) A && B =!(!A!B), A B =!(!A &&!B) if (brunch) true A false B Java if x 0 y 1 0 y package section0212; public class IfElse { int x = 3; int y = 0; if( x >= 0 ) { y += 1; else { y -= 1; System.out.println( "y = " + y ); if if( ) { // true else { // false IfElse.java (if else ) if true if ( ) true else else ( ) else

54 40 2 Java I false if boolean boolean x if( x > 1000 ) OK if( x ) int x x x 2.24 package section0212; public class OddOrEven { int x = 100; // OddOrEven.java ( ) else else else if if( ) { // true false if if if ( if ) if if if 2 3 if x if( ) { if( ) { // true true else { // true false else { if( ) { // false true else { // false false

55 else int x y 4 x y int x x x x if( x > 0 ) { System.out.println( "x " ); if( x == 0 ) { System.out.println( "x " ); if( x < 0 ) { System.out.println( "x " ); x=1 2 3 true true int vote Vote.java 2.25 package section0212; public class Vote { int vote = 1; // Vote.java ( ) int vote = 1; if( vote == 1 ) {

56 42 2 Java I System.out.println( " " ); if( vote == 2 ) { System.out.println( " " ); if( vote == 3 ) { System.out.println( " " ); else { System.out.println( " " ); vote == 1 2 if vote == 3 else if( x > 0 ) { if( y > 0 ) { // x 0 y 0 else { // x 0 y 0 else { if( y > 0 ) { // x 0 y 0 else { // x 0 y 0 Java else if ( 1 ) if( x > 0 ) { if( y > 0 ) { // x 0 y 0 else { // x 0 y 0 else if( y > 0 ) { // x 0 y 0 else { // x 0 y 0 int x = 123; if( x == 2 ) { // x 2 else if( x % 2 == 0 ) { // x 2 else if( x > 2 ) { // x 2 (x = 123 ) else {

57 // x 0 else int degree S A B C D RankEvaluation.java S 90 A 80 B 70 C 60 D RankEvaluation.java ( ) package section0212; public class RankEvaluation { int degree = 85; // if A if( degree >= 90 ) { Systerm.out.println( " S " ); else if( degree < 90 && degree >= 80 ) { Systerm.out.println( " A " ); else if( degree < 80 && degree >= 70 ) { // : // ( ) if false degree < 90 degree < 90 ( ) degree >= 70 RankEvaluation2.java 2 RankEvalution RankEvalution2 ( )

58 44 2 Java I if ( ) if condition_a condition_b boolean if( condition_a && condition_b ) { System.out.println( true ); else { System.out.println( false ); if else 1 if( ) { // else { // if( ) // else // int x 0 x 2 2 // if( x >= 0 ) x *= 2; else x *= -2; if else // if( x >= 0 ) x *= 2; else x *= -2; int x = 1; int y = 2; if( x >= 0 ) x *= 2; y += 2;

59 y += 2; x >= 0 y +=2 x int x = 1; int y = 2; if( x >= 0 ) x *= 2; y += 2; 1 int x = 1; int y = 2; if( x >= 0 ) { x *= 2; y += 2; switch 1 switch switch if boolean case case defult int x = 123; switch( x % 3 ) { case 0: // x 3 break; case 1: // x 3 break; default: // 2 ( 3 2 ) break; char rank switch switch( rank ) { case 'S': System.out.println( " S " ); break; case 'A': System.out.println( " A " ); break; case 'B': System.out.println( " B " ); break;

60 46 2 Java I case 'C': System.out.println( " C " ); break; default: System.out.println( " " ); break; break; case switch rank == 'S' S break switch default case break; default switch switch if switch if ( ) drink switch drink == "orange" drink == "apple" drink == "coffee" drink == "tea" switch 'S' 'B' case rank rank == 'S' case 'S': break; case 'A': case 'B': case 'C': " " break; switch rank == 'A' rank == 'B' case 'C': break; switch ( rank=='d' ) defalut " " break; switch( rank ) { case 'S': case 'A': case 'B': case 'C': System.out.println( " " ); break;

61 default: System.out.println( " " ); a b x if if( a > b ) { x = a; else { x = b; if( a > b ) x = a; else x = b;?: x = ( a > b )? a : b;? true? false : a > b ( x = a > b? a : b; ) x = ( a > b? a : b ); degree 60 ( ) System.out.println( " " + (degree >= 60? " " : " ") + " " ); boolean ismale true false boolean if boolean

62 48 2 Java I 2.27 IsVariable.java (boolean ) package section0212; public class IsVariable { boolean ismale = true; // true false if( ismale == true ) { System.out.println( " " ); else { System.out.println( " " ); ismale ismale == true is ismale true false ismale == true true false if( ismale == true ) if( ismale ) boolean is is+ can+ has+ if( ismale ) ( ismale )? " " : " " for 10 for for(int i = 1; i <= 10; i++) { // for ( ) for 3 ( i++ ) for( ; ; )

63 i, j, k, l, m, n counter 1 ( 1 ) i++ i+=1 i=i+1 ++i OK ( i++ ) x for for for for ( 1 ) for ( 0 ) for 1. for(int i=0; i<10; i++) 2. for(int i=10; i>0; i--) 3. for(int k=1; k<=100; k+=2) 4. for(int j=1; j<100; j*=2) 5. for(int i=1; i>-100 && i<100; i*=-2) 6. for(char c='a'; c<='z'; c++) 7. for(double x=-1.0; x<=1.0; x+=1.0/50) double 2.28 DoubleCounter.java ( )

64 50 2 Java I package section0213; public class DoubleCounter { int count = 0; for(double x=-1.0; x<=1.0; x+=1.0/50) { System.out.println( count + ":" + x ); count++; count 1.0/ x==1.0 1 (1.0 ) for for while int n '*' PrintStar.java for for n n-1 1 PrintStar2.java 2 for i n 1 for 1 i ********** ********* *** ** * package section0213; public class FibonacciNumbers { int a = 1; int b = 1; System.out.print( a + " " + b + " " ); for(int i=3; i<=10; i++) { int c = a + b; System.out.print( c + " " ); a = b; b = c; FibonacciNumbers.java ( )

65 System.out.println(); n 3 1 n 2, 3, 5 NoMultiple2.java 2.30 NoMultiple.java (3 ) package section0213; public class NoMultiple { int n = 100; System.out.println( "1 " + n + " " ); for(int i=1; i<=n; i++) { if( i % 3!= 0 ) { System.out.println( "3 " + i ); n 2, 3, NoMultiple3.java while 2 while while for for while 10 while int i = 1; while( i <= 10 ) { // i++; for 3

66 52 2 Java I while Java int long n CollatzSequence.java Collatz n 1 n n 2 n CollatzSequence.java ( ) package section0213; public class CollatzSequence { int n = 7; System.out.print( "n = " + n ); // " " // n == 1 // // do while 1 do while 10 do while int i = 1; do { // i++; while( i <= 10 ); for 3 do while while do while 2 2

67 i <= 10 while do while 2.32 package section0213; public class WhileVSDoWhile { int count = 0; while( count > 0 ) { System.out.println( "while " ); do { System.out.println( "do while " ); while( count > 0 ); WhileVSDoWhile.java (while do while ) do while FibonacciNumbers.java 3 a, b, c System.out.println( "a="+a+", b="+b+", c="+c ); c 2.33 FibonacciNumbers.java ( ) package section0213; public class FibonacciNumbers { int a = 1; int b = 1; System.out.print( a + " " + b + " " ); for(int i=3; i<=10; i++) { int c = a + b; System.out.print( c + " " ); a = b; b = c; System.out.println(); System.out.println( "a="+a+", b="+b+", c="+c );

68 54 2 Java I if a b main ( ) c 8 for ( ) 14 3 a, b 2 c c 1 i i for i 7 12 for ( ) 14 i 4 int a = 1; ( {int a = 1; ) a 2 int i; for(i=1; i<=10; i++) { System.out.println( "i=" + i ); for(i=1; i<=20; i+=2) { System.out.println( "i=" + i ); 2 for main Java CPU 2 i CPU CPU for(int i=1; i<=10; i++) { System.out.println( "i=" + i ); for(int i=1; i<=20; i+=2) { System.out.println( "i=" + i ); while do while

69 int i = 1; while( i <= 10 ) { System.out.println( i ); i++; for(int i=1; i<=10; i++) { System.out.println( i ); for... for (i=1; i<=10; i++) while { int i = 1; while( i <= 10 ) { System.out.println( i ); i++; for(int i=1; i<=10; i++) { System.out.println( i ); while i for while do while ( ) 2.34 package section0213; public class InfiniteLoop { int i = 10; // int i = -1; while( i!= 0 ) { System.out.println( "i=" + i ); i--; InfiniteLoop.java ( )

70 56 2 Java I 10 1 i 0 4 i 1 ( ) Eclipse ( ) (^^;) for(int i=0; i<=10; i--) // i++ int i=0; while( i < 10 ) { System.out.println( i ); // i++ ( ) 10 while( true ) 20 break; 2.35 GuessingGame.java ( ) package section0213; import java.util.scanner; public class GuessingGame { Scanner scan = new Scanner(System.in); System.out.println( " " ); System.out.println( " 1 99 " ); int value = (int)( 1 + Math.random()*99 ); int count = 0; while( true ) { count++; System.out.print( " Enter!" ); int n = scan.nextint(); if( n < value ) { System.out.println( " " ); else if( n > value ) { System.out.println( " " ); else { System.out.println( " "); break; System.out.println( " " + count + " "); scan.close();

71 while( true ) boolean for for(;;) for 3 3 for(;;) break; switch break; for while do while switch FibonacciNumbers.java Fibonacci FibonacciNumbers2.java ( ) System.out.printf i j (i*j) System.out.println(); ( ) 2.36 package section0213; public class MultiplicationTable { System.out.print( " " ); for(int j=1; j<=9; j++) { System.out.printf( "%2d ",j ); System.out.println(); System.out.print( "---+" ); for(int j=1; j<=9; j++) { System.out.print( "---" ); System.out.println(); for(int i=1; i<=9; i++) { System.out.printf( "%2d ", i ); for(int j=1; j<=9; j++) { MultiplicationTable.java ( )

72 58 2 Java I System.out.printf( "%2d ", (i*j) ); System.out.println(); for(int i=0; i<5; i+=2){ for(int j=4; j>=0; j-=2){ System.out.println( i*j ); int i = 1; while( i < 5 ){ int j = 10; while( j > 4 ){ System.out.println( i*j ); j -= 2; i+=1; Fifteen.java Fifteen2.java % ( ) 2

73 money rate 10 ( CompoundInterest.java ) ( CompoundInterest2.java ) break continue break; break; continue; continue; for(int i=1; i<=100; i++) { if( i % 2 == 0 i % 5 == 0 ) continue; System.out.println( i ); if for continue; break; break; 2.37 DoubleLoop.java ( ) package section0213; public class DoubleLoop { for(int i=1; i<=3; i++) { System.out.println( "i=" + i +": "); for(int j=1; j<=3; j++) { System.out.println( " j=" + j + ": "); if( i == 2 && j == 2 ) break; break (^^;)

74 60 2 Java I 2.38 DoubleLoop2.java ( break ) package section0213; public class DoubleLoop2 { loop_i: for(int i=1; i<=3; i++) { System.out.println( "i=" + i +": "); for(int j=1; j<=3; j++) { System.out.println( " j=" + j + ": "); if( i == 2 && j == 2 ) break loop_i; i for ( loop_i) break break continue 2.39 DoubleLoop3.java ( continue ) package section0213; public class DoubleLoop3 { loop_i: for(int i=1; i<=3; i++) { System.out.println( "i=" + i +": "); for(int j=1; j<=3; j++) { System.out.println( " j=" + j + ": "); if( i == 2 && j == 2 ) continue loop_i; break i loop_i: for(int i=1; i<=3; i++) { for(int j=1; j<=3; j++) { if( i * j == 4 ) break loop_i; // i j 2

75 LoopParameter.java ( ) package section0213; public class LoopParameter { int ii = 0; int jj = 0; loop_i: for(int i=1; i<=3; i++) { for(int j=1; j<=3; j++) { if( i * j == 4 ) { ii = i; jj = j; break loop_i; System.out.println( " " + ii + " " + jj + " " ); int 1 x int int[] x; 1 y double[] y; ( ) int x[]; Java Java int[] a, b; // int int c[], d; // c int d int 1 1

76 62 2 Java I int[] x; JVM x 1 ( 2.13 ) x 1 5 x = new int[5]; new JVM ( ) ( int[5]: 5 ) [0] [1] [4] x [i] x[i] x[0] x ( x ) new x x ( ) int[] x = new int[5]; 1 x!"# int[] x;$x%#&'( 1)*+!"#$%&!"#$'()* [0] [1] [2] [3] [4]!"# new int[5]$%&'()*+,-./ int0512#)*345!"#$int[] x = new int[5];%&'()*'+%,-.$ /0%12 x[0]%34567 x#,-89:$;<=>?@a double x boolean check int[] x = new int[5]; ( (^^;)) new int[5]; 5 (int 0 ) double 0 boolean false 0 ( 2.13 ) [0] [1] [4] [1] [2]

77 [5] Java 0 5 [5] 5 x[0] = 2; x 2 x[1] = -1; x[2] = 3; x[3] = 0; x[4] = 5; ( 2.14 ) x [0] [1] [2] [3] [4] package section0214; public class ArrayInitialization { int[] x = new int[5]; x[0] = 2; x[1] = -1; x[2] = 3; x[3] = 0; x[4] = 5; ArrayInitialization.java ( ) Java 5 1 int[] x = {2, -1, 3, 0, 5; new int[5]; int int[] x = {2, -1, 3, 0, 5; int[] x; x = {1, 2, 3, 4, 5; // 2 (!?) (new int[5]{1, 2, 3, 4, 5; ) int[] x; x = new int[]{1, 2, 3, 4, 5; // 2

78 64 2 Java I new int[] ( ) i++; i = i + 1; double y package section0214; public class PrintArray { int[] degree = { 90, 85, 72, 100, 95 ; for(int i=0; i<degree.length; i++) { System.out.println( degree[i] ); PrintArray.java ( ) for 2 degree.length.length degree.length 5 degree[0] degree[4] 5 for(int i=0; i<=4; i++) degree.length for degree.length for for(int i=0; i<=degree.length-1; i++) Java for(int i=0; i<degree.length; i++) i int PrintArray.java ( ) 1 PrintArray2.java 2. PrintArray2.java PrintArray3.java ( )

79 Java degree System.out.println( Arrays.toString( degree ) ); import java.util.arrays; PrintArray4.java 10 { 1, 2, 3,..., 10 i ArrayInitialization2.java ( ) package section0214; import java.util.arrays; public class ArrayInitialization2 { int[] array = new int[10]; for(int i=0; i<array.length; i++) { array[i] = i + 1; System.out.println( "array = " + Arrays.toString( array ) ); a = {2, 4, 6, 8, 10,, a = {1, 2, 4, 8, 16, 32,, a = {1, 1, 1, 1, 1, 1,, 1 sum int int int sum = 0; int sum; sum += degree[i]; (int sum = 0;) int sum = 0; for(int i=0; i<degree.length; i++) { sum for(int i=0; i<degree.length; i++) { sum 2.44 package section0214; public class SumOfArray { int[] degree = { 90, 85, 72, 100, 95 ; SumOfArray.java ( )

80 66 2 Java I int sum = 0; for(int i=0; i<degree.length; i++) { // int sum = 0; sum += degree[i]; System.out.println( " " + sum ); max int max = 0; 2.45 package section0214; public class MaxOfArray { int[] degree = { 90, 85, 72, 100, 95 ; int max = 0; for(int i=0; i<degree.length; i++) { if( degree[i] > max ) { max = degree[i]; System.out.println( " " + max ); MaxOfArray.java ( ) max 1 max 0 max 2 1. Java int Integer.MIN_VALUE ( 2.5 IntegerMaxAndMin.java ) degree degree[0] max = degree[0]; 2. degree[0] 1. max = degree[0]; max = degree[0]; int i=1 1 0 ( ) degree

81 degree MinOfArray.java degree AverageOfArray.java x AverageOfPositive.java 2.46 AverageOfPositive.java ( ) package section0214; public class AverageOfPositive { int[] x = { 15, -30, -18, 0, 25, 32, -5, 16, 51 ; // x // // // OlympicGame.java OlympicGame.java ( ) package section0214; public class OlympicGame { double[] points = {9.95, 9.82, 9.95, 10.0, 9.64, 9.55, 10.0; // points // points 2 // // " "

82 68 2 Java I DifferenceTwoArray.java ( ) package section0214; import java.util.arrays; public class DifferenceTwoArray { int[] x = { 90, 85, 72, 100, 95 ; int[] y = { 82, 70, 98, 68, 70 ; int[] z = new int[x.length]; for(int i=0; i<z.length; i++) { z[i] = x[i] - y[i]; System.out.println( "x - y = " + Arrays.toString( z ) ); SwapArray.java 2.49 SwapArray.java ( ) package section0214; public class SwapArray { int[] a = {13, 53, 24, 67, 83, 40; int[] b = {20, 14, 61, 94, 92, 50; // // a[0] b[0],..., a[a.length-1] b[b.length-1] // System.out.println("a = " + Arrays.toString( a ) ); System.out.println("b = " + Arrays.toString( b ) );

83 a a[0] a[a.length 1] a[1] a[a.length 2] ToReverseOrder.java 2.50 ToReverseOrder.java ( ) package section0214; public class ToReverseOrder { int[] a = { 11, 19, 15, 17, 21, 13, 23, 27, 25 ; // // // int degree ( S, A, B, C, D ) int grade EvaluateGrade.java 2.51 EvaluateGrade.java ( ) package section0214; public class EvaluateGrade { int[] degree = {82, 90, 65, 30, 80, 66, 72, 59, 20, 90, 75, 84, 68, 75, 45, 76, 90, 98, 55, 42, 100, 92, 78, 85, 72, 45, 62, 70, 82, 95, 60, 38, 92, 85, 78, 55, 80, 74, 92, 83 ; // grade[0]: S... grade[4]: D int[] grade = { 0, 0, 0, 0, 0 ; String[] gradename = { "S", "A", "B", "C", "D" ; // degree // grade // 90 S grade[0] // for(int i=0; i<grade.length; i++ ) { System.out.println( " " + gradename[i] + " " + grade[i] + " " );

84 70 2 Java I n ShiftArray.java int[] array = { 1, 2, 3, 4, 5 ; int n = 2; array = { 4, 5, 1, 2, 3; ( %) 2.52 ShiftArray.java ( ) package section0214; import java.util.arrays; public class ShiftArray { int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ; int n = 5; // array n // System.out.println( Arrays.toString( array ) ); Java 2 3 A int[][] A = new int[2][3]; int[] x = new int[4]; int[][] A new int A ( A[0] ) A ( 2.15 ) A 2 A[0], A[1] A[0] 2 A[0][0] A[0][1] A[0][2] A 参照 参照参照 A[1][0] A[1][1] A[1][2] A[0] A[1] 2.15

85 A[0][0] = 3; A[0][1] = -2; A[0][2] = 5; A[1][0] = 0; A[1][1] = 4; A[1][2] = -1; ( 2.16 ) A[0][0] A[0][1] A[0][2] A A[0] A[1] A[1][0] A[1][1] A[1][2] package section0214; public class ArrayInitialization3 { // int[][] A = new int[2][3]; A[0][0] = 3; A[0][1] = -2; A[0][2] = 5; A[1][0] = 0; A[1][1] = 4; A[1][2] = -1; int[][] B = { { 3, -2, 5, { 0, 4, -1 ; ArrayInitialization3.java ( ) int[][] C; C = new int[][]{ { 3, -2, 5, { 0, 4, -1 ; Java 1 A int[][] A[0] A[0][0] A[0][0] int A[0] A[0][0] A[0][2] 1 int[] 12 A[0] = new int[] { 1, 2, 3 ; (A[0] A[0] = { 1, 2, 3 ;

86 72 2 Java I ) 3 1, 2, 3 A[0] A[0] { 3, -2, 5 { 1, 2, 3 { 3, -2, 5 (^^;) Java GC (Garbage Collection) GC Java 1 C ++ GC GC Java GC ( ) GC A ( 2 ) (1) (2) (3) (4) (5) (6) Java int 3 A int B {{{1,2,3,4,{5,6,7,8,{9,10,11,12,{{13,14,15,16,{17,18,19,20,{21,22,23, B Java A A[0] A[1]

87 A[0][0] A[0][1] A[0][2] A[0][3] A 参照 参照 A[0] A[1] 参照 A[1][0] A[1][1] 2.17 (int[][] A = new int[2][];) A[0] A[1] int[] 1 A[0] int[] A[0] 1 A[0] = new int[4]; A[0] 4 int[][] A = new int[2][]; A[0] = new int[4]; A[1] = new int[2]; A[0][0] = 1; : A[1][1] = 5; // int[][] B = {{1, -2, 0, 3, {-3, 5; // int[][] C; C = new int[][]{{1, -2, 0, 3, {-3, 5; // int[][] D = new int[2][]; D[0] = new int[]{1, -2, 0, 3; D[1] = new int[]{-3, 5; int A StudentDegrees.java 2.54 package section0214; public class StudentDegrees { int[] sno = {1401, 1402, 1403; int[][] degrees = { {80, 95, 65, 70, {100, 55, 82, 78, 90, {95, 85, 72; StudentDegrees.java ( )

88 74 2 Java I // int SetArrays.java x [0] [1] [2] [3] [4] [5] y z [0] [1] [2] [3] [4] [0] [1] [2] [0] [1] [2] [0] [1] [2] [3] [4] [0] [1] package section0214; public class SetArrays { // x, y, z // x y z // SetArrays.java ( ) //, x 1, 2, 3, 4, 5 // for x[0]=1, x[1]=2,... // //, y int sum, // System.out.println( " y " + sum + " " ); //, z -1

89 null ArrayIndexOutOfBoundsException package section0214; public class ArrayIndexOutOfBounds { int[] array = { 1, 2, 3, 4, 5 ; System.out.println( array[5] ); ArrayIndexOutOfBounds.java ( ) 0 int[] x = new int[0]; int[] y = { ; int[] x = null; 0 ( ) null int x int y null ( 0 ) 2.57 package section0214; import java.util.arrays; public class ArrayCopy { int[] x = { 1, 3, 0, -2, 4 ; ArrayCopy.java ( )

90 76 2 Java I int[] y = new int[x.length]; for(int i=0; i<x.length; i++) { y[i] = x[i]; // y System.out.println( "y = " + Arrays.toString( y ) ); x y int[] y = new int[x.length]; x.length y for 1 (^^;) 2.58 ArrayCopy2.java ( ) package section0214; import java.util.arrays; public class ArrayCopy2 { int[] x = { 1, 3, 0, -2, 4 ; int[] y = x; System.out.println( "y = " + Arrays.toString( y ) ); int[] y = x; x OK int[] y = x; x[0] = -1; y y y[0] -1 int[] y = x; ( 2.18 ) x 5 x[0] int[] y = x; y x[0] y x x[0] = -1; y[0] -1 x 参照 [0] [1] [2] [3] [4] コピー y 参照 代入 ArrayCopy3.java

91 ArrayCopy3.java ( ) package section0214; public class ArrayCopy3 { int[][] A = { {1, 3, 0, {-2, 4 ; // A int B // A // 2 1 OK for Java for Java 5 10 Java 9 for C ++ for-each 1 for 1 [0] for 2.60 package section0214; public class ExtendedForEach { int[] x = { 1, 3, 0, -2, 4 ; for(int xi : x) { System.out.print( xi + " " ); System.out.println(); ExtendedForEach.java ( for ) for ( ) x int int x xi x x xi xi 1 xi ( )

92 78 2 Java I double y ExtendedForEach2.java for 2.61 ExtendedForEach2.java ( for ) package section0214; public class ExtendedForEach2 { double[] y = { 12.34, 35.79, , 20.52, ; // for y sum System.out.println( " " + sum ); ExtendedForEach3.java 2.62 ExtendedForEach3.java ( for ) package section0214; public class ExtendedForEach3 { int[][] A = { {1, 3, 0, {-2, 4 ; // for A for for ( for OK ) MatrixVector.java ( ) package section0214; public class MatrixVector { int[][] A = {{1,2,3,{1,1,1,{0,1,-1; int[] x = {1,1,1; // System.out.println( "A=" ); for(int i=0; i<a.length; i++) { for(int j=0; j<a[i].length; j++) { System.out.printf( "%3d", A[i][j] );

93 System.out.println(); System.out.println(); // System.out.println( "x=" ); for(int i=0; i<x.length; i++) { System.out.printf( "%3d%n", x[i] ); System.out.println(); // int[] y = new int[a.length]; for(int i=0; i<a.length; i++) { y[i] = 0; for(int j=0; j<a[i].length; j++) { y[i] += A[i][j] * x[j]; // System.out.println( "Ax=" ); for(int i=0; i<x.length; i++) { System.out.printf( "%3d%n", y[i] ); System.out.println(); java.util.arrays.deeptostring() 2.64 MatrixMatrix.java ( ) package section0214; import java.util.arrays; public class MatrixMatrix { int[][] A = {{1,2,3,{1,1,1,{0,1,-1; int[][] B = {{1,0,1,{0,1,1,{1,1,0; // System.out.println( " A = " + Arrays.deepToString( A ) ); System.out.println( " B = " + Arrays.deepToString( B ) ); // int[][] C = new int[a.length][b[0].length]; for(int i=0; i<c.length; i++) { for(int j=0; j<c[i].length; j++) { C[i][j] = 0; for(int k=0; k<b.length; k++) { C[i][j] += A[i][k] * B[k][j]; // System.out.println( "AB = " + Arrays.deepToString( C ) );

94 80 2 Java I 2.15 Math Math Math ( ) (B.6.3 ) 2.65 TrigonometricTable.java ( ) package section0215; public class TrigonometricTable { int sep = 12; for(int k = 0; k<=sep; k++) { double theta = Math.PI * k / sep; double costheta = Math.cos(theta); double sintheta = Math.sin(theta); System.out.println( " =" + k + " /" + sep + ", cos =" + costheta + ", sin =" + sintheta ); Math.PI Java 3.14 Math 1 ( e = ) Math.E (^^;) 2.66 MathMethods.java ( ) package section0215; public class MathMethods { double a = ; System.out.println( a + " " + Math.abs(a) ); double x = 100; System.out.println( "log_e(" + x + ") " + Math.log(x) ); System.out.println( "log_10(" + x + ") " + Math.log10(x) ); int p = 3; System.out.println( "e^" + p + " " + Math.pow(Math.E,p) ); System.out.println( " " + p + " " + Math.sqrt(p) ); int m = 5; int n = 3; System.out.println( "max(" + m + "," + n + ") " + Math.max(m,n) ); System.out.println( "min(" + m + "," + n + ") " + Math.min(m,n) );

95 2.15 Math Math ( ) int abs(int) double abs(double) double cos(double) radian cos double sin(double) radian sin double tan(double) radian tan double log(double) ( e) double log10(double) ( 10) double pow(double, double) x y pow(x, y) int max(int, int) double max(double, double) 2 int min(int, int) double min(double, double) 2 double sqrt(double) double random() [0, 1) long round(double) int round(float) System.out.println( "[0,1) 2 " + Math.random() + "," + Math.random()); System.out.println( a + " " + (int)(math.round(a))); static void mein(string args) System.out.Println( ' ' + r + ' ' + area ); pakage section0215; double r = 1.5; pablic class CircleArea { double area = r * r * Math.PAI; ABC AB AC a, b 2 θ ABC (1/2)ab sin θ theta 30 Math.sin(double)

96 82 2 Java I 2.67 package section0215; public class TriangulumArea { double a = 1.0; double b = 1.0; double theta = 30.0; // TriangulumArea.java ( ) double area = System.out.println( " " + area ); Math.random() 1 6 Dice.java Dice2.java Math.random() ( ) Math.random() Java (Mersenne twister) SFMT Math.random() Math.random()

97 Scanner System.in System.out System.out.println() print() Math.cos() Math System Math System GC (Garbage Collection) System Math PI in out 2 (PI ) Math.PI System.in System.out System.out.println() System.out println() System.out System.out.println() System.in System.in (^^;) Scanner Scanner(System.in) System Math Scanner Java Java Scanner import 1 2 import Scanner java.util.scanner Eclipse Scanner java.util.scanner import import 1

98 84 2 Java I 2.68 package section0216; import java.util.scanner; public class OddEven { Scanner scan = new Scanner( System.in ); System.out.print( " " ); int n = scan.nextint(); scan.close(); if( n % 2 == 0 ) { System.out.println( n + " " ); else { System.out.println( n + " " ); OddEven.java ( ) 5 System.in Scanner Scanner scan Scanner 1 scan scan OK scan scan 1 nextint() int 1 7 n 6 6 print 1 nextint() Scanner ( close() ) Eclipse Eclipse Eclipse n 8 (scan.close()) 2 ax 2 + bx + c = 0 3 nextdouble() ax 2 + bx + c 3 (a 0 ) d = b 2 4ac d < 0 d > 0 2 x1 = + ( b+ d)/(2a) x2 = + ( b d)/(2a)

99 2.17 II 85 Math.sqrt() d == 0 x= + (-b)/(2a) 2.69 QuadraticEquation.java ( ) package section0216; import java.util.scanner; public class QuadraticEquation { Scanner scan = new Scanner( System.in ); System.out.println("ax^2+bx+c "); System.out.print( "a = " ); double a = scan.nextdouble(); System.out.print( "b = " ); double b = scan.nextdouble(); System.out.print( "c = " ); double c = scan.nextdouble(); scan.close(); // a, b, c // Scanner 2.17 II 2 System.out.print() System.out.println() ( ) System.out.printf() C System.out.printf() System.out printf() package section0217; public class TrigonometricTable2 { TrigonometricTable2.java (printf )

100 86 2 Java I int sep = 12; for(int k = 0; k<=sep; k++) { double theta = Math.PI * k / sep; double costheta = Math.cos(theta); double sintheta = Math.sin(theta); System.out.printf( " =%02d /%2d, cos =%9.6f, sin =%9.6f%n", k, sep, costheta, sintheta ); printf() 1 " =%02d /%2d, cos =%9.6f, sin =%9.6f%n" % %kd k (%2d) 0 0 (%02d) %k.mf k m (%9.6f) k %n printf() (print() ) %n %02d %2d %9.6f %9.6f k sep costheta sintheta ( ) %b, %B boolean %c, %C char %e, %E %o 8 %s, %S %x, %X 16 %E Newton 2.71 NewtonMethod.java ( Newton ) package section0217; public class NewtonMethod { // f(x) = cos(1/log x) = 0 x 1.2 Newton // f'(x) = sin(1/log x)/(x(log x)^2) int count = 0; double x = 1.6;

101 2.17 II 87 String format = "count=%d : x=%10.7f f(x)=%13.5e%n"; while( Math.abs(f(x)) > 1.0e-15 ) { System.out.printf( format, count, x, f(x) ); x = x - f( x )/df( x ); count++; System.out.printf( format, count, x, f(x) ); static double f(double x) { return Math.cos(1.0/Math.log( x )); static double df(double x) { return Math.sin(1.0/Math.log( x ))/(x*math.pow(math.log( x ),2)); 8 10, 14 printf() %10.7f 10 7 %13.5e Mathematica (! (^^;)) f[x_] := Cos[1/Log[x]]; Print["f(x)=", f[x]] Print["f'(x)=", D[f[x], x]] Plot[f[x], {x, 1, 3] NSolve[{f[x] == 0, x >= 1.2, x] 1 f(x)=cos Log[x] f'(x)= Sin 1 Log[x] xlog[x] {{x , {x Mathematica

102 88 2 Java I 2.18 I API (Application Programming Interface) 1 main main main main 1 Java API (Application Programming Interface : 200 3, 500 ) Math.cos() java.lang.math 1 main main CircleArea.java (main ) package section0218; public class CircleArea { for(int r=1; r<=10; r++) { double area = r * r * Math.PI; System.out.printf( " =%2d %8.4f %n", r, area ); 2.73 CircleArea2.java ( ) package section0218; public class CircleArea2 { for(int r=1; r<=10; r++) { printcirclearea( r ); // radius area static void printcirclearea( int r ) { double area = r * r * Math.PI; System.out.printf( " =%2d %8.4f %n", r, area );

103 2.18 I CircleArea.java main CircleArea2.java main printcirclearea main printcirclearea (main printcirclearea ) main main main static main (static ) static ( ) static public ( ) public main public printcirclearea printcirclearea section0218 ( ) Java double x = Math.cos(Math.PI/4); cos π/ ( ) x printcirclearea void void Java main void main String args printcirclearea int radius main String[] args ( ) y = f(x) x f y f(x) 2.74 package section0218; public class CircleArea3 { for(int r=1; r<=10; r++) { printcirclearea( r ); // CircleArea3 ( )

104 90 2 Java I static void printcirclearea( int r ) { double area = circlearea( r ); System.out.printf( " =%2d %8.4f %n", r, area ); // radius static double circlearea( double radius ) { double area = radius * radius * Math.PI; return area; circlearea void double circlearea() printcirclearea circlearea area circlearea ( printcirclearea()) return area; return void printcirclearea return (return void ) CircleArea3 CircleArea2 circlearea() printcirclearea() circlearea() ( ) double int circlearea double // radius static double circlearea( int radius ) { System.out.println( " " ); double area = radius * radius * Math.PI; return area; circlearea Java main r

105 2.18 I 91 circlearea (int ) double method(int a, int b) double method(int x, int y) 2. double method(int a, int b) int method(int a, int b) 3. double method(int a, double b) double method(double a, int b) 4. double method(int a, int b) double method(int a) 5. double method(int a, int b) double method(double x, double y) 6. double method(int a, int b) void method(int a, int b) method( 1, 2 ); printcirclearea() circlearea() r circlearea() radius 2 circlearea() r radius printcirclearea() circlearea() r 1 circlearea() radius ( 2.20 ) int r 1 代入 printcirclearea( ) メソッド側 double area = circlearea( r ); circlearea( ) メソッド側コピー static double circlearea( double radius ){ return area; 値を戻す 2.20

106 92 2 Java I A B double max(double, double) B 2 A A B void swap(int, int) B SwapValues.java 2.75 SwapValues2.java ( ) package section0218; public class SwapValues2 { int a = 1; int b = 2; swap( a, b ); System.out.println( " " + a ); System.out.println( " " + b ); static void swap( int x, int y ) { // x >= y x < y if( x < y ) { int temp = x; x = y; y = temp; int a int b 1 2 代入 代入 main メソッド側 swap メソッド側 swap( a, b ); コピーコピー static void swap( int x, int y ){ 交換しても main に戻していない (^^)b

107 2.18 I 93 ( 2.21 ) Java positivearray() 2.76 FormalParameter.java ( ) package section0218; import java.util.arrays; public class FormalParameter { int[] array = { 1, -2, -3, 4, -5 ; positivearray( array ); // printarray( "array", array ); // static void positivearray( int[] x ) { for(int i=0; i<x.length; i++) { x[i] = Math.abs( x[i] ); static void printarray( String name, int[] x ) { System.out.println( name + " = " + Arrays.toString( x ) ); array 5 5 postitivearray() main void main array ( ) ( ) positivearray static void positivearray( int[] x ) { x = new int[]{ 1, 1, 1, 1, 1 ; 1 1 x main main array { 1, -2, -3, 4, ( ) main

108 94 2 Java I 2.77 package section0218; import java.util.arrays; public class LinearAlgebra { int[][] A = {{1,2,3,{1,1,1,{0,1,-1; printm( "A", A ); int[] x = {1,1,1; printv( "x", x ); // y = Ax int[] y = prodmv( A, x ); printv( "Ax", y ); int[][] B = {{1,0,1,{0,1,1,{1,1,0; printm( "B", B ); LinearAlgebra.java ( ) // C = AB int[][] C = prodmm( A, B ); printm( "AB", C ); // static void printm( String name, int[][] A ) { System.out.println( name + " = " + Arrays.deepToString( A ) ); // static void printv( String name, int[] x ) { System.out.println( name + " = " + Arrays.toString( x ) ); // static int[] prodmv( int[][] A, int[] x ) { int[] y = new int[a.length]; for(int i=0; i<a.length; i++) { y[i] = 0; for(int j=0; j<a[i].length; j++) { y[i] += A[i][j] * x[j]; return y; // static int[][] prodmm( int[][] A, int[][] B ) { int[][] C = new int[a.length][b[0].length]; for(int i=0; i<c.length; i++) { for(int j=0; j<c[i].length; j++) { C[i][j] = 0; for(int k=0; k<b.length; k++) { C[i][j] += A[i][k] * B[k][j];

109 2.18 I 95 return C; main A 2 static int[][] transmnew( int[][] A ) static void transmself( int[][] A ) A main x static double lengthv( int[] x ) Ã n x = x 2 i, (n = x ) i=1 double length = lengthv( {1, 2, 3, 4, 5 ); double length = lengthv( new int[]{1, 2, 3, 4, 5 ); // static int[] toarray( int x, int y ) { return {x, y; return new int[]{x, y; (recurrence relation) 2.78 FibonacciRecursive.java ( ) package section0218; public class FibonacciRecursive { int n = 10; System.out.println( "Fibonacci " + n + " " + fibo( n ) );

110 96 2 Java I static int fibo( int n ) { if( n == 1 n == 2 ) return 1; else return fibo( n-1 ) + fibo( n-2 ); n F n F n = F n 1 + F n 2, F 1 = F 2 = 1 n fibo n (return 1;) n > 2 fibo n 1 n 2 main FactorialRecursive.java ( ) package section0218; public class FactorialRecursive { int n = 10; System.out.println( n + "! " + fact( n ) ); static int fact( int n ) { if( n == 1 ) return 1; else return n * fact( n-1 ); 10 10! = n! = n (n 1)!, 1! = 1 (recursive program) (Tower of Hanoi) package section0218; public class HanoiTower { int n = 3; // hanoi( n, 'A', 'B', 'C' ); HanoiTower.java ( )

111 2.18 I 97 static void hanoi( int n, char a, char b, char c ) { if( n == 0 ) return; hanoi( n-1, a, c, b ); System.out.println( n + " " + a + " " + b + " " ); hanoi( n-1, c, b, a ); hanoi hanoi n 2 ( a ) 3 ( b ) 4 ( c ) A B C A B C 2.22 (1) 2.23 (2) n n hanoi hanoi( n-1, 'A', 'C', 'B' ) 1 hanoi( n-1, 'C', 'B', 'A' ) n n n 1 n 2 hanoi n 0 hanoi( 0, a, b, c ) 9 hanoi void return return void return 1 return return hanoi 9 return 1 12 return return

112 98 2 Java I return return if 5 static void method( int a ) { if( a < 0 ) // 2 System.out.println( " return" ); return; System.out.println( " " ); n = 3 hanoi A B C A A B n HanoiTower2.java 2. A C n HanoiTower3.java 3. A, B, C, D A B n HanoiProblem4.java main main static main(string[] args) null args 2.81 package section0218; public class MainParameters { int a = Integer.valueOf( args[0] ); int b = Integer.valueOf( args[1] ); MainParameters.java ( )

113 2.18 I 99 System.out.println( a + " + " + b + " = " + (a + b) ); ( ) (N) ( 2.24 ) 2.25 (x) = ( 2.26 ) (A) main main String[] args args[0] = "12" args[1] = "34" main Integer Integer.valuesOf() int C main main 1 int main(void) int main(int args, char *argv[]) 2 int System.arraycopy?? 2.82 package section0214; import java.util.arrays; public class ArrayCopy { int[] x = { 1, 3, 0, -2, 4 ; int[] y = new int[x.length]; for(int i=0; i<x.length; i++) { ArrayCopy.java ( )

114 100 2 Java I y[i] = x[i]; // y System.out.println( "y = " + Arrays.toString( y ) ); 2.83 ArrayCopyExample1.java ( ) package section0218; import java.util.arrays; public class ArrayCopyExample1 { int[] x = { 1, 3, 0, -2, 4 ; int[] y = new int[x.length]; System.arraycopy(x, 0, y, 0, x.length); // y System.out.println( "y = " + Arrays.toString( y ) ); System arraycopy arraycopy(a1,s1,a2,s2,len) a1 [s1] a2 [s2] len len 0 len ArrayIndexOutOfBound ArrayCopy3.java ( ) package section0214; import java.util.arrays; public class ArrayCopy3 { int[][] A = { {1, 3, 0, {-2, 4 ; int[][] B = new int[2][]; for(int i=0; i<a.length; i++) { B[i] = new int[a[i].length]; for(int j=0; j<a[i].length; j++) { B[i][j] = A[i][j]; System.out.println( "B = " + Arrays.deepToString( B ) ); 2.85 ArrayCopyExample2.java ( )

115 2.18 I 101 package section0218; import java.util.arrays; public class ArrayCopyExample2 { int[][] A = { {1, 3, 0, {-2, 4 ; int[][] B = new int[2][]; System.arraycopy(A, 0, B, 0, A.length); A[0][0] = 0; // System.out.println( "B = " + Arrays.deepToString( B ) ); B[0][0] 0 (Shallow copy) (Deep copy) Arrays.toString() Arrays.deepToString() 2.86 ArrayCopyExample3.java ( ) package section0218; import java.util.arrays; public class ArrayCopyExample3 { int[][] A = { {1, 3, 0, {-2, 4 ; int[][] B = new int[2][]; for(int i=0; i<a.length; i++) { B[i] = new int[a[i].length]; for(int j=0; j<a[i].length; j++) { System.arraycopy(A[i], 0, B[i], 0, A[i].length); System.out.println( "B = " + Arrays.deepToString( B ) ); clone() clone 2.87 ArrayCloneExample.java ( ) package section0218; import java.util.arrays; public class ArrayCloneExample { // int[] x = { 1, 3, 0, -2, 4 ; int[] y = x.clone(); x[0] = 0; // Deep copy // y System.out.println( "y = " + Arrays.toString( y ) ); // int[][] A = { {1, 3, 0, {-2, 4 ; int[][] B = new int[a.length][];

116 102 2 Java I for(int i=0; i<a.length; i++) { B[i] = A[i].clone(); A[0][0] = 0; // Deep copy // B System.out.println( Arrays.deepToString( B )); clone arraycopy arraycopy clone Java 'A' "A" 1 ( String S) (^^;) String String char String ( ) " " ( ) String String hello; ; 1 String hello = " "; char String char Unicode ( ) String "" String char '' 'a' "a" char String 2.88 package section0219; public class StringvsChar { char a = 'a'; a = 'b'; System.out.println( a ); String s = "a"; s = "b"; StringvsChar.java ( )

117 System.out.println( s ); ( ) ( 'a' ) ( 'b' 'a' ) a a s b a b 2.27 ( "a" s ) (C ) ( "b" ) ( s ) ( "a" ) ( ) char String String 2.89 StringMethods.java ( ) package section0219; public class StringMethods { String name = " "; int length = name.length(); // System.out.println( "\"" + name + "\" " + length + " " ); int sep = name.indexof( " " ); // String familyname = name.substring(0, sep); // String firstname = name.substring(sep+1); System.out.println( " :" + familyname ); System.out.println( " :" + firstname ); String name length indexof substring String

118 104 2 Java I length length() 4 indexof 0 1 sep = 2 substring 2 "abcdefg".substring( 3 ) "defg" ( 0 ) "abcdefg".substring( 2, 5 ) "cde" String StringMethods \" 2 2 (""") 1 2 \" ( ) Java ( ) \b \n \r ( ) \t \u Unicode ( \u3042) \\ \' 1 \" Windows UNIX (MacOS ) MacOS UNIX (Linux ) MacOS UNIX 2 Windows \r\n UNIX \n OS Java Unicode Eclipse Eclipse UTF-8 UNICODE Unix UNIX

119 2.20 ( ) 105 Eclipse UNIX Windows (^^;) 2.20 ( ) (sorting) Java API ( ) (sorting method) 2.90 MyTools.java ( ) package section0220; import java.util.arrays; public class MyTools { /** * n min max */ public static int[] makerandomintarray( int n, int min, int max ) { int[] array = new int[n]; for(int i=0; i<n; i++) { array[i] = (int)( Math.random() * ( max - min + 1 ) ) + min; return array; /** * name array n : n 2n */ public static void printintarray( String name, int[] array, int n ) { System.out.print( name + ": " ); if( n*2 >= array.length ) { // System.out.println( Arrays.toString( array ) ); else { // n System.out.print( "[" ); for(int i=0; i<n; i++) { System.out.print( array[i] + ", " ); System.out.print( "..." ); for(int i=array.length-n; i<array.length; i++) { System.out.print( ", " + array[i] ); System.out.println( "]" );

120 106 2 Java I /** * array i j */ public static void swap( int[] array, int i, int j ) { int dummy = array[i]; array[i] = array[j]; array[j] = dummy; printintarray Arrays tostring() (Selection sort) 最小値 1を見つけ 先頭 5と交換する 次に小さな値 2を見つけ 3と交換する SortingExample.java ( ) package section0220; public class SortingExample { // [1,1000] 1000 int[] data = MyTools.makeRandomIntArray( 1000, 1, 1000 ); MyTools.printIntArray( " ", data, 5 ); /* * * * * */ int[] array = new int[data.length];

121 2.20 ( ) 107 System.arraycopy( data, 0, array, 0, data.length ); // long starttime = System.nanoTime(); ArraySorting.selectionSort( array ); long endtime = System.nanoTime(); MyTools.printIntArray( " ", array, 5 ); printtime( endtime - starttime ); // private static void printtime( long time ) { System.out.printf( " : %10d nsec (10^-9 sec)%n", time ); 2.92 ArraySorting.java#selectionSort ( ) package section0220; public class ArraySorting { /** * (Selection method) array */ public static void selectionsort( int[] array ) { for(int i=0; i<array.length - 1; i++) { int min = i; for(int j=i+1; j<array.length; j++) { if( array[j] < array[min] ) min = j; MyTools.swap( array, i, min ); System.nanoTime() min,i,j Java ( ) (Insertion sort)

122 108 2 Java I 既に整列の済んでいる部分に新たに4を挿入する 4を挿入するには 7と5を移動させて空きを作ってから次は2を挿入するので 4つの値を移動させる SortingExample.java#main ( ) // main System.arraycopy( data, 0, array, 0, data.length ); // starttime = System.nanoTime(); ArraySorting.insertionSort( array ); endtime = System.nanoTime(); MyTools.printIntArray( " ", array, 5 ); printtime( endtime - starttime ); 2.94 ArraySorting.java#selectionSort ( ) // (selectionsort ) /** * (Insertion method) array */ public static void insertionsort( int[] array ) { for(int i=1; i<array.length; i++) { int val = array[i]; int j = i; while( j > 0 ) { if( array[j-1] <= val ) break; array[j] = array[j-1]; j--; array[j] = val; (Bubble sort) ( )

123 2.20 ( ) 109 ( ) 隣り合う 2 つを比較 順序が逆のとき交換する 順に比較していくと 途中から最大値が 右に移動を始める 回目の走査で 最大値が最後尾に 次は 残りの要素で再度走査を行う SortingExample.java#main ( ) // main System.arraycopy( data, 0, array, 0, data.length ); // starttime = System.nanoTime(); ArraySorting.bubbleSort( array ); endtime = System.nanoTime(); MyTools.printIntArray( " ", array, 5 ); printtime( endtime - starttime ); 2.96 ArraySorting.java#bubbleSort ( ) // (insertionsort ) /** * (Bubble sorting method) array */ public static void bubblesort( int[] array ) { for(int i=array.length - 1; i>=1; i--) { for(int j=0; j<i; j++) { if( array[j] > array[j+1] ) MyTools.swap( array, j, j+1 );

124 110 2 Java I (Quicksort) (Divide and Conquer method) 1960 ( ) ( ) center left right i j 左より center より大きな値 右より center より小さな値を見つけては交換する操作をすれ違うまで行う i j 分割した列ごとに 再帰的に クィックソートを施していく 2.31 ( ) 2.97 SortingExample.java#main ( ) // main System.arraycopy( data, 0, array, 0, data.length ); // starttime = System.nanoTime(); ArraySorting.quickSort( array ); endtime = System.nanoTime(); MyTools.printIntArray( "quick ", array, 5 ); printtime( endtime - starttime );

125 2.20 ( ) ArraySorting.java#quickSort ( ) // (bubblesort ) /** * (Quick sorting method) array */ public static void quicksort( int[] array ) { quicksort( array, 0, array.length - 1 ); private static void quicksort( int[] array, int left, int right ) { if( left >= right ) return; int center = (array[left] + array[(left + right)/2] + array[right])/3; int i = left; int j = right; while( i<= j ) { while( array[i] < center ) i++; while( array[j] > center ) j--; if( i <= j ) { MyTools.swap( array, i, j ); i++; j--; quicksort( array, left, j ); quicksort( array, i, right ); (Heap sort) (Heap tree) [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [0] 5 [1] [2] 6 3 [3] [4] [5] [6] [7] [8] [9] ( ) (tree structure) 2.32 (root)

126 112 2 Java I ( ) (leaces) (inner node) ( ) (binary tree) ( 2.32 ) = = n n/2 ( ) ( ) ( ) ( ) ( ) a b c [n/2] [0] ( 2.32 [4] [0] )

127 2.20 ( ) SortingExample.java#main ( ) // main System.arraycopy( data, 0, array, 0, data.length ); // starttime = System.nanoTime(); ArraySorting.heapSort( array ); endtime = System.nanoTime(); MyTools.printIntArray( "heap ", array, 5 ); printtime( endtime - starttime ); ArraySorting.java#heapSort ( ) // (quicksort ) /** * (Heap sorting method) array */ public static void heapsort( int[] array ) { buildheap( array ); for(int i=array.length-1; i>0; i--) { MyTools.swap( array, 0, i ); heapupdate( array, 0, i ); private static void buildheap( int[] array ) { for(int i=array.length/2-1; i>=0; i--) { heapupdate( array, i, array.length ); private static void heapupdate( int[] array, int parent, int last ) { int left_child = 2 * parent + 1; int right_child = left_child + 1; int largest = parent; if( left_child < last && array[left_child] > array[largest] ) largest = left_child; if( right_child < last && array[right_child] > array[largest] ) largest = right_child; if( largest!= parent ) { MyTools.swap( array, parent, largest ); heapupdate( array, largest, last ); n O(n log n) O(n log n) ( ) (Merge sort)

128 114 2 Java I k 2k k 2k ( ) array array array array array SortingExample.java#main ( ) // main System.arraycopy( data, 0, array, 0, data.length ); // starttime = System.nanoTime(); ArraySorting.mergeSort( array ); endtime = System.nanoTime(); MyTools.printIntArray( "merge ", array, 5 ); printtime( endtime - starttime ); ArraySorting.java#mergeSort ( ) // (heapsort ) /** * (Merge sorting method) array */ public static void mergesort( int[] array ) { int[] array2 = new int[array.length]; int size = 1; while( size < array.length ) {

129 2.20 ( ) 115 merge( array, array2, size ); merge( array2, array, 2 * size ); size *= 4; private static void merge( int[] from, int[] to, int size ) { int start = 0; while( start < from.length ) { int k = start; int i = start; int j = start + size; int iend = Math.min( start + size, from.length ); int jend = Math.min( start + 2 * size, from.length ); while( i < iend && j < jend ) { if( from[i] < from[j] ) to[k++] = from[i++]; else to[k++] = from[j++]; while( i < iend ) to[k++] = from[i++]; while( j < jend ) to[k++] = from[j++]; start += 2 * size; (Shell s sort) D.L.Shell 1959 h n {h i : h i+1 = 3h i +1, h 1 = 1, h i < n O(n 1.25 ) Knuth ({h i = {1, 4, 13, 40, 121, 364,... ) h = 4 離れた ( 同じ色 ) 同士で挿入法ソートを実行 h = 1 : デフォルトの挿入法を実行 SortingExample.java#main ( ) // main System.arraycopy( data, 0, array, 0, data.length ); //

130 116 2 Java I starttime = System.nanoTime(); ArraySorting.shellSort( array ); endtime = System.nanoTime(); MyTools.printIntArray( "Shell ", array, 5 ); printtime( endtime - starttime ); ArraySorting.java#shellSort ( ) // (mergesort ) /** * (Shell's sorting method) array */ public static void shellsort( int[] array ) { int h = 1; while( h < array.length / 3 ) { h = 3 * h + 1; while( h > 0 ) { for(int i=h; i<array.length; i++) { int dummy = array[i]; // int j = i; while( j > h-1 && array[j-h] >= dummy ) { array[j] = array[j-h]; j -= h; array[j] = dummy; // h = (h - 1)/3; Java Java System.currentTimeMillis() (1 msec = 10 3 sec) System.nanoTime() (1 nsec = 10 9 sec) currenttimemillis() nanotime() API TimePrecisionExample.java ( )

131 2.20 ( ) 117 package section0220; public class TimePrecisionExample { final int N = ; int count = 50; int x = 0; for(int k=0; k<count; k++) { long starttime = System.nanoTime(); for(int i=0; i<n; i++) { x++; long endtime = System.nanoTime(); System.out.printf( "%d nsec%n", (endtime - starttime) ); System.out.println( "x = " + x ); Windows 10 Pro / Intel Core i5-3337u 1.80GHz MacOS X / Intel Core i7 2.20GHzz ( x 1 N ) Java Java class JVM ( ) (Windows 0 Mac 38) 0 nsec Windows MacOS Windows nsec (?) 570 nsec (?) MacOS k Windows MacOS X k Windows MacOS X Java N N 0

132 118 2 Java I Java Java Java ( n n (Windows)

133 int a = 123 / 4 % 8; 2. double x = (double) 123 / 4; 3. int a = 1; int b = 2; String s = " " + a + b; 4. int a = 10; int b = a++; 5. int a = 12; a /= 5; ( ) WrongAverage.java ( ) pakage section0220; pablic class WrongAverage { pablic void mein(string[] args) { int a = { 1, 3, -2, 5, 2, 0, 9 ; int i; for(i=1; i<=7; i++) { int sum += a[i]; ; double average = sum / a.length; system.out.println( " ", average, " " ); SwapRowOfMatrix.java ( ) package section0221; public class SwapRowOfMatrix { int[][] A = {{1, 2, 3, 4, {5, 6, 7, 8, {9, 10, 11, 12; int i = 0; int j = 2; // A i j // printf

134 120 2 Java I for(int i=0; i<a.length; i++) { for(int j=0; j<a[i].length; j++) { System.out.printf( "%3d", A[i][j]); System.out.println(); ( ) 1. \ =, = Java a for ( for( 1; 2; 3) ) 1. i i = 0, 2, 4,, i i = 5, 10, 15, 20,, i i = 1, 5, 25, 125, 625, x x = 0.0, 0.01, 0.02,, 1.0 x 1.0

135 n 1. n 2. Math.random() n [0, 1) x = (x 1, x 2,, x n ) Ã n 3. x x = 4. x 1 y y = x x 5. y i=1 x 2 i VectorNormalize.java ( ) package section0221; import java.util.scanner; public class VectorNormalize { // (1) Scanner scan = new Scanner(System.in); System.out.println(" "); int n = scan.nextint(); // (2)(3)(4) // (5) System.out.println( " "); for(int i=0; i<n; i++) { System.out.println( "y[" + i + "]=" + y[i] ); n C m n m n n C 0, nc 1, nc 2,..., nc n 1, nc n Combination.java (n, m) n C m long n package section0221; import java.util.scanner; public class Combination { Combination.java ( )

136 122 2 Java I Scanner scan = new Scanner(System.in); System.out.println("n "); int n = scan.nextint(); for(int m=0; m<=n; m++) { // ncm comb long comb = // comb System.out.println( "(" + n + "," + m +")=" + comb ); e x e = e 1 e x = n=0 x n n! n = 0 n 1 0! + 1 1! + 1 2! n! for e = package section0221; public class Maclaurin { double x = 1.0; Maclaurin.java ( e ) // // // x [1, 3] (^^;) f(x) = sin ( e cos x x log x) 3 1 f(x) dx

137 double dx = ; sum = 0; for(double x = 1; x<=3-dx; x+=dx) sum += f(x)*dx sum Math 1. sin(x) Math.sin( x ); 2. cos(x) Math.cos( x ); 3. log(x) Math.log( x ); 4. e x Math.exp( x ); Mathematica dx = dx = 0.1 dx R T T R p R T p (0, 0), (1, 0), (1, 1), (0, 1) 4 T T 1 ( ) R T (x, y) 100, 000 R 1 100, 000 p T 1 R p π R π/4... Math.random() [0, 1) double double x = Math.random(); double y = Math.random(); (x, y)

138 124 2 Java I MonteCarlo.java ( ) package section0221; public class MonteCarlo { int count = ; // // count // 1 // p ( ) a b c a b 1 MargeArrays.java 2 a b MargeArrays.java ( ) package section0221; import java.util.arrays; public class MargeArrays { int[] a = { 2, 5, 7, 8, 12, 15, 21, 24, 25; int[] b = { 1, 4, 5, 9, 11, 23, 26, 30; int[] c = new int[a.length+b.length]; // // System.out.println( "c = " + Array.toString( c ) );

139 m 1 a m 2 b ArrayTransform.java ArrayTransform.java ( ) package section0221; import java.util.scanner; public class ArrayTransform { int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; // Scanner m // a m // n // b int[][] b = new int[n][m]; // a b // 0 // b for(int i = 0; i < b.length; i++) { for(int j = 0; j < b[i].length; j++) { System.out.printf( b[i][j] + "\t" ); // "\t" System.out.println(); b Math.random() double [0, 1) ( ) 1. (0, 1] double rand int rand

140 126 2 Java I 3. true false boolean rand n n 1. n n p[][] p[i][j] = ß 1 (i = 0 j = 0) p[i 1][j] + p[i][j 1] (i 0 & j = 0) 2. n p[] n p[j] = ß 1 (j = 0) p[j 1] + p[j] (j = 1, 2,..., n 1) 3. p[i][j] = i+j C j (i, j = 0,..., n 1) 4. 1 (i = 0 j = 0) p[i][j] = i + j p[i][j 1] j (i 0 & j = 1, 2,..., n 1) InverseMatrix.java 1. n n A = (a ij ) n 2n B = (b ij ) A 2. i = 1, 2,..., n B i b ii (b ii = 1 ) k = 1, 2,..., n (k i) k i b ki 3. A 1 b ii 0

141 InverseMatrix.java ( ) package section0221; public class InverseMatrix { double[][] A = { {2, 6, 9, {1, 4, 6, {0, 1, 2 ; double[][] B = new double[a.length][a[0].length*2]; // B printmatrix( "%5.1f", "B", B ); // B printmatrix( "%5.1f", "B", B ); // A static void printmatrix( String format, String name, double[][] A ) { System.out.println( name + " = "); for(int i=0; i<a.length; i++) { for(int j=0; j<a[0].length; j++) { System.out.printf( format, A[i][j] ); System.out.println(); System.out.println(); // B B = // B =

142 II Java 2

143 129 3 Java II 3.1 (Object oriented) ( ) (Programming paradigm) ( ) COBOL (1959) BCPL (1966) Perl (1987) PHP (1995) Ruby (1993) FORTRAN (1954) Algol (1958) Lisp (1958) PL/I (1964) BASIC (1963) Simlula (1964) C (1971) PASCAL (1969) Smalltalk (1969) Scheme (1975) ML (1973) Ada (1979) C++ (1983) Common Lisp (1984) Tcl/Tk (1988) Python (1991) Java (1991) Haskell (1990) Delphi (1995) JavaScript (1995) OCaml (1996) C# (2001) Swift (2010) Computer Languages Timeline ( などより抜粋 3.1 ( ) FORTRAN ( ) (Procedural) 2 Java C (

144 130 3 Java II ) ( object) ( (^^;)) Java Java (Functional programming) Java (Aspect oriented) Java AspectJ Java ( Wikipedia: ) 3 (encapsulation) (inheritance) (polymorphism) (dynamic binding) ( ) (property) (method) GPA (^^;) (object) (instance)

145 (Database : ) 2000 Java (^_^;) Java main 3.1 Student.java ( ) package section0302; // public class Student { // private int idno; // private String name; // private int countcourses; // private Course[] course; // private static final int MAXCOUNT_COURSES = 100; // // public Student( int idno, String name ) { this.idno = idno; this.name = name; this.countcourses = 0; this.course = new Course[MAXCOUNT_COURSES]; // // public void addrecord( Course course ) { this.course[countcourses++] = course; // public void printrecords() { System.out.println( idno + ":" + name + " " ); if( countcourses == 0 ) { System.out.println( " " ); return; for(int i=0; i<countcourses; i++) { System.out.printf( " %s %d %n", course[i].gettitle(), course[i].getdegree() ); this.printaverage(); //

146 132 3 Java II private void printaverage() { int sum = 0; for(int i=0; i<countcourses; i++) { sum += course[i].getdegree(); System.out.printf( " %f %n", (double) sum / countcourses ); main Java main main ( ) Java Applet Java main ( Java Applet Java 9 HTML5 JavaScript JavaScript Java ) (Pascal-case) ( ) 3 ( ) (field) (constructor) (method) 3 ( ) main ( HelloJava HelloJava main ) Course StudentExample section StudentExample.java ( ) package section0302; // public class StudentExample { // Student noda = new Student( , " "); // noda.addrecord( new Course(" ", 85) ); noda.addrecord( new Course(" ", 77) ); noda.addrecord( new Course(" ", 96) ); // noda.printrecords();

147 package section0302; // public class Course { // // private String title; // private int degree; // public Course( String title, int degree ) { this.title = title; this.degree = degree; // // public String gettitle() { return title; // public int getdegree() { return degree; Course.java ( ) main ( ) Java main public public ( )

148 134 3 Java II ( ) ( ) ( ) Java Student StudentExample.java main ( 3.2 ) ( ) section0302 Student.java Student Student noda = new Student( , " "); Student noda ( ) noda ( 3.2 ) noda Student Student ( Student noda ) 2 8 String int[] String name = " "; int[] a = {1, 2, 3; name a noda new Student( , " "); noda Student new Student( , " ") Student 1 int 2 Student ( )

149 noda Student 型インスタンス 参照 private idno private name 野田愛 private countcourses 0 Course[ ] course public void addrecord( Record ) public void printrecords() private void printaverage() 3.2 ( void ) ( 3.2 ) static ( ) ( ) this.idno = idno; this.name = name; Eclipse 3.4 Eclipse // private int idno; // private String name; // private int countcourses; // private Course[] cours; // private static final int MAXCOUNT COURSES = 100; // // Student( int idno, String name ) { this.idno = idno; this.name = name; this.countcourses = 0; this.cours = new Course[MAXCOUNT COURSES]; this.idno = idno; idno 2 idno 1 private int idno; idno idno idno 2 this.

150 136 3 Java II this. this.idno ( idno) this. 2 Student( int idnum, String sname ) this. idno = idnum; this. idno idno ( ) 3.5 this. private int idno; // private String name; // private int countcourses; // private Course[] cours; // private static final int MAXCOUNT COURSES = 100; // // Student( int idnum, String sname ) { idno = idnum; name = sname; countcourses = 0; cours = new Course[MAXCOUNT COURSES]; this. ( ) this. this.countcourses = 0; this.course = new Course[MAXCOUNT_COURSES]; this. this. 1 ( ) 3.6 ( ) StudentExample.java ( ) package section0302; // public class StudentExample { // Student noda = new Student( , " "); // noda.addrecord( new Course(" ", 85) ); noda.addrecord( new Course(" ", 77) ); noda.addrecord( new Course(" ", 96) ); Student kagura = new Student( , " ", 19 ); // // noda.printrecords(); kagura.printrecords();

151 (String, double ) (String, double ) ( ) (String, double ) (String, int ) Student kagura = new Student(" ", 80); 80 Weight Height Age Student(String, Age ) Student(String, Weight ) Age Weight Student kagura = new Student( , " ", 80); Student kagura = new Student( , " ", new Weight(80)); 80 ( ) (overload) Student 3.7 ( ) Student.java ( ) package section0302; // public class Student { // private int idno; // private String name; // private int age; // private int countcourses; // private Course[] course; // private static final int MAXCOUNT_COURSES = 100; // private static final int UNDEFINED_IDNO = 0; // private static final String UNDEFINED_NAME = ""; // private static final int UNDEFINED_AGE = -1; // // public Student() { this.idno = UNDEFINED_IDNO; this.name = UNDEFINED_NAME;

152 138 3 Java II this.age = UNDEFINED_AGE; this.countcourses = 0; this.course = new Course[MAXCOUNT_COURSES]; // public Student( int idno, String name ) { this(); this.idno = idno; this.name = name; // public Student( int idno, String name, int age ) { this( idno, name ); this.age = age; // printrecords() // public void printrecords() { String strage = (age == UNDEFINED_AGE)? "": String.valueOf(age); System.out.println( idno + ":" + name + " (" + strage + ") " ); if( countcourses == 0 ) { System.out.println( " " ); return; for(int i=0; i<countcourses; i++) { System.out.printf( " %s %d %n", course[i].gettitle(), course[i].getdegree() ); this.printaverage(); (default constructor) UNDEFINED_ private static final final ( ) this.idno = 0; 0 this.idno = 0; this.idno = UNDEFINED_IDNO; this() this( idno, name ) this(...)

153 this();,, ) // public Student() { this.idno = UNDEFINED_IDNO; this.name = UNDEFINED_NAME; this.age = UNDEFINED_AGE; this.countcourses = 0; this.course = new Course[MAXCOUNT_COURSES]; // public Student( int idno, String name ) { this.idno = idno; this.name = name; this.age = UNDEFINED_AGE; this.countcourses = 0; this.course = new Course[MAXCOUNT_COURSES]; // public Student( int idno, String name, int age ) { this.idno = idno; this.name = name; this.age = age; this.countcourses = 0; this.course = new Course[MAXCOUNT_COURSES]; UNDEFINED_AGE Eclipse countcourses numcourses

154 140 3 Java II this(...) ( ) Student sex Student( int idno, String name, String sex ) ( ) Student katushika = new Student( , " ", " "); katushika.printrecords(); Point PointExample main Point p = new Point(); Point 3.8 Point.java ( ) package section0302; // public class Point { private double x; private double y; // public void setx( double x ) { this.x = x; public void sety( double y ) { this.y = y; // public double getx() { return this.x; public double gety() { return this.y; // public void print() { System.out.printf( "(%f,%f)%n", this.x, this.y ); 3.9 package section0302; public class PointExample { PointExample.java ( )

155 Point p = new Point(); // p p.setx( 1.5 ); // p x 1.5 p.sety( -0.3 ); // p y -0.3 p.print(); // p (setter) set ( x void setx( double ) ) (getter) return p x double px = p.getx(); px 1.5 get ( ) Point( double x, double y ) ( ) ( ) Point 2 PointExample main p ( ) ( ) (instance field) ( ) idno idno (class field) ( )

156 142 3 Java II static Student MAXCOUNT_COURSES static 3.10 ( ) Student.java ( ) package section0302; public class Student { private int idno; // private String name; // private int age; // private String sex; // private int countcourses; // private Course[] course; // private static final int MAXCOUNT_COURSES = 100; // private static final int UNDEFINED_IDNO = 0; // private static final String UNDEFINED_NAME = ""; // private static final int UNDEFINED_AGE = -1; // private static final String UNDEFINED_SEX = ""; // public static final String UNIV_NAME = " "; // // ( ) final final (^^;) private int idno = UNDEFINED_IDNO; // Student final 3.11 package section0302; public class MembershipExample { MembershipExample.java ( )

157 int n = 100; // Entry entry = new Entry( n ); // entry.add(" ", 19 ); entry.add(" ", 20 ); entry.add(" ", 18 ); entry.print(); 3.12 Entry.java ( ) package section0302; public class Entry { private int membercount; private Member[] member; private static int memberno = 1000; // final public Entry( int n ) { member = new Member[ n ]; membercount = 0; public void add(string name, int age) { memberno++; // member[membercount++] = new Member( memberno, name, age ); public void print() { System.out.println( " " + membercount ); for(int i=0; i<membercount; i++) { member[i].printrecords(); 3.13 Member.java ( ) package section0302; public class Member { private int idno; private String name; private int age; Member( int idno, String name, int age ) { this.idno = idno; this.name = name; this.age = age; void printrecords() { System.out.printf( "%03d %s (%2d)%n", idno, name, age );

158 144 3 Java II ( -1 ) 2. noda.idno katushika.name ( ) int idno = noda.idno idno. Student.UNIV_NAME noda.univname (Eclipse ) Student.idNo ( ) Point getx() gety() getidno() Point package section0302; // public class Point { private double x; private double y; : 3.14 Point // public double getx() { return this.x; public double gety() { return this.y; x y private Point Point p = new Point(); Point p.x = 1.5; p.y = -0.3; ( )

159 (access modifier) ( ) int mymoney 0 private ( ) ( ) ( ) private ( ) private public ( ) Student.java 1. idno 2. idno 3. printrecords() i 4. printaverage() sum ( ) (i sum) (local variable) (instance method) (class method) main printcirclearea( r ); ( static ) 3.15 package section0218; public class CircleArea2 { CircleArea2.java ( )

160 146 3 Java II for(int r=1; r<=10; r++) { printcirclearea( r ); // radius area static void printcirclearea( int r ) { double area = r * r * Math.PI; System.out.printf( " =%2d %8.4f %n", r, area );.printcirclearea( r ) CircleArea2.printCircleArea( r ); CircleArea2 ( ) Math.cos(double) Math Math. import (import ) Student printrecords() (static ) noda.printrecords(); kagura.printrecords(); ( ) Human String name " " Human taro taro hello TaroExample.java (?) ( ) 3.16 TaroExample.java ( ) package section0302; public class TaroExample { String name = " "; hello( name, "10:00" ); static void hello( String name, String time ) { System.out.print( name + " " + time + " " ); if( time.compareto("12:00") <= 0 ) { // "12:00" System.out.println( " " ); else { System.out.println( " " );

161 main 3.8 Point public double length() double Math.sqrt(double) 3.17 ( ) PointExample.java package section0302; public class PointExample { Point p = new Point(); // p Point q = new Point( 1.5, -0.3 ); p.print(); // p q.print(); // System.out.println( " p " + p.length() ); Point public static double distance( Point, Point ) public double distance( Point ) (0,0) private static final Point ORIGIN length() ORIGIN 3.18 PointExample2.java package section0302; public class PointExample2 { Point p = new Point( 1.0, 2.0 ); Point q = new Point( 2.0, -1.0 ); // double dist1 = Point.distance( p, q ); System.out.println( "p q ( ) " + dist1 ); // double dist2 = p.distance( q ); System.out.println( "p q ( ) " + dist2 );

162 148 3 Java II ComplexNumber 3.19 ComplexNumberExample.java ( ) package section0302; public class ComplexNumberExample { ComplexNumber c1 = new ComplexNumber( 1, 2 ); // c1 = 1 + 2i ComplexNumber c2 = new ComplexNumber( 2,-3 ); // c2 = 2-3i System.out.println("c1 = " + c1.tostring() ); System.out.println("c2 = " + c2.tostring() ); // System.out.println("c1 + c2 = " + c1.add( c2 ).tostring() ); System.out.println("c2 + c1 = " + c2.add( c1 ).tostring() ); System.out.println("c1 + c2 = " + ComplexNumber.add( c1, c2 ).tostring() ); System.out.println("c1 * c2 = " + c1.multiply( c2 ).tostring() ); 3.20 ComplexNumber.java ( ) package section0302; public class ComplexNumber { private double real; // private double imag; // public ComplexNumber( double r, double i ) { this.real = r; this.imag = i; // "( real, imag public String tostring() { // // c public ComplexNumber add( ComplexNumber c ) { // // public static ComplexNumber add( ComplexNumber c1, ComplexNumber c2 ) { // // c public ComplexNumber multiply( ComplexNumber c ) { //

163 ComplexNumber (Annotation ) ( Java public void setreal(double) public ComplexNumber conjugate() public ComplexNumber divide(double) public ComplexNumber divide(complexnumber) public double abs() ( ) public double angle() x + iy θ θ = ( y arctan (x > 0) x) ( y arctan + π (x < 0, y 0) x) ( y arctan π (x < 0, y < 0) x) π/2 (x = 0, y > 0) π/2 (x = 0, y < 0) = 2 arctan Å x2 +y 2 x Math.atan2(y,x) y ã (y 0), 0 (x > 0, y = 0), π (x < 0, y = 0) Point section0302 Line ax + by + c = 0 Line 3 a, b, c 4 3 a, b, c Line( double a, double b, double c ) ( ) y ( ) Line( double m, double d ) 2 (Point ) Line( Point p, Point q ) (Point ) ( ) Line( Point p, double m ) true boolean online( Point p )

164 150 3 Java II double distance( Point p ) true boolean iscross( Line other ) Point Point crosspoint( Line other ) 2 NULL true static boolean online( Line, Point ) static double distance( Line, Point ) true static boolean iscross( Line, Line ) static Point crosspoint( Line, Line ) public Circle private Point center ( ) double r ( ) public Circle( Point, double ) public double area() public String tostring() :(1.0, 2.0) :3.0 public double distance( Point ) 2 ( ) true public boolean includes( Point ) ( ) true public boolean iscross( Circle ) MyMath plus() 3.21 package section0302; public class MyMathExample { MyMathExample.java ( Math )

165 System.out.println( MyMath.plus( 10, 20 ) ); System.out.println( MyMath.plus( 10, 20, 30 ) ); System.out.println( MyMath.plus( 10.0, 20.0 ) ); System.out.println( MyMath.plus( new int[]{ 1, 2, 3, 4, 5 ) ); package section0302; public class Problem0302 { int a; a = 123; package section0302; public class Problem0302 { int a; this.a = 123; main a 123 main a = 123; (?) ( int[]) int[] x = new int[10]; x ( ) (String[]) 2 (int[][]) (Point ) Point Point centroid( Point[] ) Point 3.22 package section0302; public class CentroidExample { Point[] points = { new Point( 0.5, 1.8 ), CentroidExample.java ( Point[])

166 152 3 Java II new Point( 1.2, 0.9 ), new Point( 1.5, 3.2 ), new Point( 1.7, 2.2 ), new Point( 3.0, 0.7 ), new Point( 3.3, 3.1 ), ; Point.centroid( points ).print(); package section0302; // public class Point { private double x; private double y; 3.23 Point.java ( centroid ) // public static Point centroid( Point[] points ) { double xsum = 0.; double ysum = 0.; for( Point p : points ) { xsum += p.x; ysum += p.y; return new Point( xsum/points.length, ysum/points.length ); main 12 Point.centroid( points ).print(); OK Point.centroid( points ) Point centroid() points ( ) Point.print() print() ( ) Point centroid print() // Point.centroid( points ).print(); Point centroid = Point.centroid( points ); centroid.print(); CentroidExample.java main ( ) points new Point( 3.3, 3.1 ) ( ) C (C ++ OK) ( Java )

167 int[] a = { 1, 2, 3, ; (Overload) Math.abs() int Math.abs(int) long Math.abs(long) float Math.abs(float) double Math.abs(double) Java ( ) ( ) public private (access modifier) Java ( ) (package-private ) private (package-private) protected public ( )

168 154 3 Java II private protected (inner class) private int x = 123; public static double[] x = new double[100]; private private 3.24 PrivateFieldExample.java (private field ) package section0303; public class PrivateFieldExample { Circle circle = new Circle( 10.0 ); double radius = circle.radius; double area = circle.getarea(); System.out.printf(" %f %f%n", radius, area ); class Circle { // private double radius; // Circle( double radius ) { // this.radius = radius; double getarea() { // return radius * radius * Math.PI; Cirlce radius private main double radius = circle.radius; radius Circle PrivateFieldExample. getarea() private double area = circle.getarea(); radius private ( package-private ) radius circle.radius private private main circle.radius = ; (^^;) ( ) private

169 ( )PrivateFieldExample.java (private field ) package section0303; public class PrivateFieldExample { Circle circle = new Circle( 10.0 ); double radius = circle.getradius(); double area = circle.getarea(); System.out.printf(" %f %f%n", radius, area ); class Circle { // private double radius; // Circle( double radius ) { // this.radius = radius; double getradius() { // return this.radius; double getarea() { // return radius * radius * Math.PI; Circle double hankei; PrivateFieldExample circle.radius; circle.hankei; radius circle.getradius() getradius() return hankei; Circle section0303 Circle.java (package-private ) private private private cos() Math Math Math Math private

170 156 3 Java II Math private ( ) private package-private ( ) (package-private) package-private public public package-private public package-private protected protected 2 (package-private ) ( ) protected package-private protected ( ) public public 4 package-private A public a() A public a() package section0303; public class Problem0332 {

171 3.4 ( ) 157 A a1 = new A(); a1.setvalue( 10 ); System.out.println( a1.value ); A a2 = new A( 10 ); System.out.println( a2.getvalue() ); class A { private int value; A( int value ) { this.value = value; void setvalue( int value ) { this.value = value; int getvalue() { return this.value; A section0303a (Eclipse import ) public public public ( ) 3.4 ( ) Java ( inheritance)

172 158 3 Java II 1 (super class) (sub class) ( subset ) Java ( ) 3.26 InheritanceExample.java ( ) package section0304; public class InheritanceExample { Student noda = new Student( , " " ); Professor hasiguchi = new Professor( " ", " ", " " ); Lecture linearalgebra1 = new Lecture( "A001", " " ); noda.take( linearalgebra1 ); hasiguchi.lectured( linearalgebra1 ); 3.27 Human.java ( ) package section0304; // public class Human { private String name; // private int age; // private String sex; // public static final String NAME_UNKNOWN = null; // public static final int AGE_UNKNOWN = -1; // public static final String SEX_UNKNOWN = null; // // public Human( String name, int age, String sex ) { this.name = name; this.age = age; this.sex = sex; // public String getname() { return this.name; public int getage() { return this.age;

173 3.4 ( ) 159 public String getsex() { return this.sex; 3.28 Professor.java ( ) package section0304; // public class Professor extends Human { private String licence; // // public Professor( String licence, String name, String sex ) { super( name, AGE_UNKNOWN, sex ); this.licence = licence; // public void lectured( Lecture lecture ) { System.out.printf( "(%s) %s %s %n", this.licence, this.getname(), lecture.getname() ); 3.29 package section0304; // public class Lecture { private String lecno; // private String lecname; // // public Lecture( String lno, String lname ) { this.lecno = lno; this.lecname = lname; // public String getlecno() { return lecno; public String getname() { return lecname; Lecture.java ( ) 3.30 package section0304; public class Student extends Human { private int idno; // // public Student( int idno, String name ) { Student.java ( )

174 160 3 Java II super( name, AGE_UNKNOWN, SEX_UNKNOWN ); this.idno = idno; // public void take( Lecture lecture ) { System.out.printf( "%s(%d) %s %n", this.getname(), this.idno, lecture.getname() ); class extends extends 3.4 UML (Unified Modeling Language) (class diagram) UML UML UML Java ( ) Human Student ( ) Java Student Human Human - name : String - age : int - sex : String + NAME_UNKNOWN : String = null + AGE_UNKNOWN : int = -1 + SEX_UNKNOWN : String = null + getname() : String + getage() : int + getsex() : String <<constructor>> + Human(name : String, age : int, sex : String ) - lecno : String - lecname : String Lecture + getlecno() : String + getname() : String <<constructor>> + Lecture( lno : String, lname : String ) + public # protected ~ package private - private 可視性 - idno : int Student - licence : String Professor + take( lecture : Lecture ) : void <<constructor>> + Student( idno : int, name : String ) + lectured( lecture : Lecture ) <<constructor>> + Professor( licence : String, name : String, sex : String ) 3.4 UML Point WeightedPoint WeightedPoint 2 WeightedPoint centroid( WeightedPoint )

175 3.4 ( ) PointExample3.java ( ) package section0304; public class PointExample3 { // double[][] data = { {0.5, 1.8, 2, {1.2, 0.9, 1, {1.5, 3.2, 3, {1.7, 2.4, 2, {2.5, 3.9, 4, {1.9, 2.2, 1, {3.0, 0.7, 3, {3.5, 3.1, 2, {4.2, 1.9, 2, {4.0, 3.7, 1 ; // points[] WeightedPoint[] points = new WeightedPoint[data.length]; for(int i=0; i<points.length; i++) { points[i] = new WeightedPoint( data[i][0], data[i][1], data[i][2] ); // ( ) WeightedPoint center = new WeightedPoint( points[0] ); // center points[i] center for(int i=1; i<points.length; i++) { center = center.centroid( points[i] ); // ( ) System.out.print( " " ); center.print(); 3.32 package section0304; import section0302.point; public class WeightedPoint extends Point { private double weight; // WeightedPoint.java ( ) // centroid 3.22 n A B B C ( 3.5 ) A A a ( ) B b C

176 162 3 Java II c A insta insta A a B b c C + c : int insta B + b : int A + a : int クラスAのインスタンス型 int a int b int c クラスBから継承した部分クラスCから継承した部分 3.5 C B (multiple inheritance) Java (C ++ ) Java (single inheritance) Java C ++ Java 3.33 // class AmphibiousCycle extends Car, Boat { // speed drive() class Car { // double speed; // km/h void drive() { // class Boat { // double speed; // knot : 1knot = 1852m/h void drive() { // 2 (

177 3.4 ( ) 163 ) 乗り物 drive() 自動車 船 drive() drive() 水陸両用車 drive() 3.6 C ++ ( ) Java ( ) 2 A C C A A C ( ) (a),(b),(c) (a) (b) (c) C + c : int C + c : int B + b : int C + c : int B + b : int A B A A D + a : int + b : int + a : int + a : int + d : int

178 164 3 Java II A クラスのインスタンスクラスの情報 + x : double + y : double + length() : double B + w : double + getweight() : double C + c : java.color + getcolor() : java.color クラス の情報 Object A Object A Object Human Student Human 3.5 A, B, C C Object Object java.lang.object java.lang Object Object public boolean equals( Object obj ) protected Object clone() public String tostring() Object Object ( ) C tostring() package section0304; 3.34 ObjectExample.tex (Object )

179 3.4 ( ) 165 public class ObjectExample { C a = new C( 100 ); C b = a; C c = new C( 100 ); System.out.println("a: " + a.tostring() ); System.out.println("b: " + b.tostring() ); System.out.println("c: " + c.tostring() ); System.out.println( "a.equals(b): " + a.equals( b ) ); System.out.println( "a.equals(c): " + a.equals( c ) ); class C { int value; C( int value ) { this.value = value; tostring() (^^;) ( ) JVM clone() Human Object Object 3.26 noda hasiguchi tostring() equals() ( ) Object 3.35 ArrayIsObject.java ( Object ) package section0304; import java.util.arrays; public class ArrayIsObject { int[] a = { 1, 2, 3 ; int[] b = { 1, 2, 3 ; System.out.println( "a: " + a.tostring() ); // System.out.println( "a == b : " + ( a == b ) ); // a b System.out.println( "a.equals(b) : " + a.equals( b ) ); // a b System.out.println( "Arrays.equals(a, b) : " + Arrays.equals( a, b) ); // a b a b

180 166 3 Java II equals == Arrays equals == equals() == ( ) equals() StringIsObject.java ( == equals() ) package section0304; public class StringIsObject { String a = "abcde"; String b = "abcde"; String c = "abc"; c += "de"; System.out.println( "a == b : " + ( a == b ) ); System.out.println( "a.equals(b) : " + a.equals( b ) ); System.out.println( "a == c : " + ( a == c ) ); System.out.println( "a.equals(c) : " + a.equals( c ) ); String a = "abcde"; Sting b = "abcde"; a b WeightedPoint ColoredWeightedPoint WeightedPoint print() 3.37 PointExample4.tex ( ) package section0304; import javafx.scene.paint.color; // javafx.scene.paint Color public class PointExample4 { ColoredWeightedPoint p = new ColoredWeightedPoint(1.0, 2.0, 3.0, Color.RED ); p.print();

181 3.4 ( ) ColoredWeightedPoint.java ( ) package section0304; import javafx.scene.paint.color; public class ColoredWeightedPoint extends WeightedPoint { private Color color; public ColoredWeightedPoint( double x, double y, double w, Color color ) { super( x, y, w ); this.color = public void print() { System.out.printf( "(%f, %f), w=%f:c=%s%n", this.getx(), this.gety(), this.getweight(), this.color.tostring() ); p.print(); ColoredWeightedPoint print() WeightedPoint print() ColoredWeightedPoint 3 UML print() Point WeightedPoint - x : double - y : double - ORIGIN : Point {final + setx( x : double ) : void + sety( y : double ) : void + getx( ) : double + gety( ) : double + print( ) : void + length( ) : double + distance( p : Point ) : double + distance( p : Point, q : Point ) : double + centroid( p : Point, q : Point ) : Point <<constructor>> + Point( x : double, y : double ) + public # protected ~ package private - private 可視性 - weight : double + getweight() : double + print( ) : void + centroid( WeightedPoint ) : WeightedPoint <<constructor>> + WeightedPoint( x : double, y : double, w : double ) + WeightedPoint( p : WeightedPoint ) - color : javafx.scene.paint.color ColoredWeightedPoint + print( ): void <<constructor>> + ColoredWeightedPoint( x : double, y : double, w : double, color : Color ) 3.7 UML SubClass method()

182 168 3 Java II 3.39 package section0304; public class OverrideExample2 { SubClass a = new SubClass(); a.method(); // class SuperClass { public void method() { System.out.println( "SuperClass.method()" ); // class SubClass extends SuperClass void method() { System.out.println( "SubClass.method()" ); OverrideExample2.java ( ) public package-private ( package-private public ) Object tostring() equals() public clone() protected tostring() Object ( ) Point tostring() Point print() tostring print() package p1; import p2.b; public class C { A a = new A(); a.method();

183 3.4 ( ) 169 System.out.println( a.x ); System.out.println( a.b[0] ); System.out.println( a.y ); System.out.println( a.z ); class A extends B { public int x = 100; int[] b = {1,2,3; void method() { A a = new A(); System.out.println( a.z ); package p2; public class B { private int y = 123; protected double z = 1.23; super this this.x x this.method() method() this( ) super super.x x super.method() method() super( ) ( ) (super.x == this.x ) super.x 3.40 package section0304; public class SuperExample { SubA b = new SubA( 10 ); b.print(); b.print2(); SuperExample.java (supar. )

184 170 3 Java II class SuperA { int x; void print() { System.out.println( "SuperA:" + x ); class SubA extends SuperA { SubA( int x ) { this.x = void print() { System.out.println( "SubA:" + x ); void print2() { super.print(); print2() A print() super.print() super. print() super.super.print() final final final final final ( ) final String System Math final String final final

185 3.4 ( ) FinalParameter.java ( final) package section0304; public class FinalParameter { String x = "pen"; String y = "pineapple"; System.out.printf( "%s + %s = %s%n", x, y, add( x, y ) ); x = "apple"; System.out.printf( "%s + %s = %s%n", x, y, add( x, y ) ); static String add( String a, String b ) { String c = a + "." + b; b = "strawberry"; return c; final static String add(final String a, final String b){ b="strawberry"; final final Point q q = new Point(0,0); final 8 p.print(); 3.42 FinalExample.java (final ) package section0304; import section0302.point; public class FinalExample { final Point p = new Point(1,2); // p p.setx( 0 ); p.sety( 0 ); p.print(); // final Point q = new Point(1,2); q = new Point(0,0); // q.print();

186 172 3 Java II final 1 ( ) final int x = 123; double y = x; double x = 123.5; int y = x; double x = 123.5; int y = (int) x; y 3.43 package section0304; public class OverrideExample3 { SubC e = new SubC(); System.out.println( "c=" + e.c ); // System.out.println( ((SuperC) e).c ); e.print(); // ((SuperC) e).print(); e.print2(); // ((SuperC) e).print2(); class SuperC { int c = 1; void print() { System.out.println( "SuperC.print()" ); class SubC extends SuperC { int c = void print() { System.out.println( "SubC.print()" ); void print2() { System.out.println( "SubC.print2()" ); OverrideExample3.java ( ) SubC e c 2 print() print2() print() // e (SuperC) SuperC

187 3.4 ( ) 173 SuperC e ~ c : int = 1 ~ print( ) : void SubC ~ c : int = 2 ~ print( ) : void ~ print2( ) : void SubC 型インスタンス int c = 1 void print( ) int c = 2 void print2( ) オーバーライドされていないオーバーライドされているクラスSuperCから継承した部分 3.8 print2() 3.8 print() 2 ( ) SuperC e SubC ( print() ) e SuperC ((SuperC) e) e SuperC print2() ( ) SubC.print() e print() 2 ((SuperC) e).c 1 c 2 1 ( ) 2 e.c SubC c ((SuperC) e).c c ( super() )

188 174 3 Java II double x = 123; class A extends B A a = new B(); B b = new A(); double x = 123; ( ) B A a = new B(); ( 3.8 ) A A a = new B(); ( ) A ( 3.9 ) B b = new A(); B b = new A(); B b = new A(); b A b B a b B A a = new B( ); B b = new A( ); A B 型インスタンス B 型インスタンス この部分の情報が無い! A 型インスタンス A 型インスタンス 3.9 class A extends B ( 3.9 ) A a = new A(); // B b = (B) a; // b A c = b; // 3 ( ) b (?) A long l = 123L; int i = (int) l; long int ( )

189 3.5 ( ) A c = (A) b; c a ( ) instanceof ( ) instanceof boolean instanceof true 3.44 InstanceOfExample.java ( ) package section0304; import section0302.point; import javafx.scene.paint.color; public class InstanceOfExample { ColoredWeightedPoint cp = new ColoredWeightedPoint( 1.0, 2.0, 10.0, Color.RED ); WeightedPoint wp = new WeightedPoint( 0.0, 0.0, 1.0 ); Point p = new WeightedPoint( -1.0, -2.0, 0.0) ; // System.out.println( "(1) " + (cp instanceof ColoredWeightedPoint) ); System.out.println( "(2) " + (cp instanceof WeightedPoint) ); System.out.println( "(3) " + (cp instanceof Point) ); System.out.println( "(4) " + (wp instanceof ColoredWeightedPoint) ); // false System.out.println( "(5) " + (wp instanceof WeightedPoint) ); System.out.println( "(6) " + (wp instanceof Point) ); System.out.println( "(7) " + (p instanceof WeightedPoint) ); // true System.out.println( "(8) " + (p instanceof Point) ); C c = new A(); Object 2. B b = new C(); 3. Object obj = new A(); C 4. A a = new Object(); B 5. A a = new A(); if( a instanceof A ) { B b = a; 6. C c = new C(); if( c instanceof C ) { B b = c; A 7. A a = new A(); if( a instanceof C ) { B b = a; 3.5 ( ) (Polymorphism) (Monomorphism) 1 Point WeightedPoint

190 176 3 Java II ColoredWeightedPoint print() 3.45 PolymorphismExample.java ( (?)) package section0305; public class PolymorphismExample { Shape[] shapes = { new Circle( 1.2 ), // new Square( 1.5, 2.4 ), // new Trapezoid( 1.2, 3.0, 2.5 ) // ; for( Shape shape : shapes ) { shape.printarea(); class Shape { // String name; // double getarea() { // return 0; void printarea() { System.out.println( this.name + " " + this.getarea() ); class Circle extends Shape { // double radius; Circle( double radius ) { this.name = " "; this.radius = double getarea() { return radius * radius * Math.PI; class Square extends Shape { // double height; double width; Square( double height, double width ) { this.name = " "; this.height = height; this.width = double getarea() { return height * width; class Trapezoid extends Shape { // double upper; double lower;

191 double height; Trapezoid( double upper, double lower, double height ) { this.name = " "; this.upper = upper; this.lower = lower; this.height = double getarea() { return ( upper + lower ) * height / 2; ( ) (Dinamic binding) 3.6 Java 1 (algorithm error) (syntax error) (execution error) Java (scan.nextint()) JVM (Java Virtual Machine) (Exception) (Exception) Java (Exception) (throw ) catch catch catch

192 178 3 Java II ( ) 3.46 DivideZeroExample.java ( ) package section0306; public class DivideZeroExample { int x = 0; int y = 10; int z = y / x; System.out.printf( "%d / %d = %d%n", y, x, z ); Exception in thread "main" java.lang.arithmeticexception: / by zero at section0306.dividezeroexample.main(dividezeroexample.java:6) main java.lang.arithmeticexception section0306 DivideZeroExample.java main (DivideZeroExample.java 6 ) (single thread) 1 main Java (multi thread) 3.47 package section0306; public class OutOfBoundExample { int[] array = new int[10]; for(int i=1; i<=array.length; i++) { array[i] = i; for(int x : array) { System.out.print( x + " " ); System.out.println(); OutOfBoundExample.java ( ) Exception in thread "main" java.lang.arrayindexoutofboundsexception: 10 at section0306.outofboundexample.main(outofboundexample.java:6) 6 java.lang.arrayindexoutofboundsexception java.lang ArrayIndexOutOfBoundsException

193 Java JVM Exception (catch ) Exception java.lang.exception java.lang.error ( Error ) java.lang.object java.lang.throwable java.lang.exception java.lang.error java.io.ioexception java.lang.runtimeexception java.io.filenotfoundexception java.lang.arithmeticexception java.lang.indexoutofboundsexception java.lang.nullpointerexception java.lang.arrayindexoutofboundsexception 3.10 Exception Error java.lang.outofmemoryerror java.lang.stackoverflowerror Error ( ) Exception 2 java.lang.runtimeexception ArithmeticException ArrayIndexOutOfBoundsException (catch ) FileNotFoundException ( )

194 180 3 Java II NullPointerException (NullPointerException ) String str1 = ""; System.out.println( "str1.length = " + str1.length() ); String str2 = null; System.out.println( "str2.length = " + str2.length() ); try catch finally (stack trace) 3.48 ( ) DivideZeroExample.java ( ) package section0306; public class DivideZeroExample { int x = 0; int y = 10; try { int z = y / x; System.out.printf( "%d / %d = %d%n", y, x, z ); catch( Exception e ) { e.printstacktrace(); try try { 6 9 try try catch catch( ) { 9 11 catch catch 2 try catch try { // catch catch( Exception e ) { try catch catch (?)

195 Exception Exception catch ( ) Exception printstacktrace() 3.49 ( ) DivideZeroExample.java package section0306; public class DivideZeroExample { int x = 0; int y = 10; try { int z = y / x; System.out.printf( " %d / %d = %d%n", y, x, z ); catch( ArithmeticException e ) { System.err.println( " 0 " ); System.err.println( " " ); (?) ArithmeticException 3.50 ( ) OutOfBoundExample.java package section0306; import java.util.arrays; public class OutOfBoundExample { int[] array = new int[10]; try { for(int i=1; i<=array.length; i++) { array[i] = i; catch( ArrayIndexOutOfBoundsException e ) { System.err.println( " " ); System.out.println( Arrays.toString( array ) ); ArrayIndexOutOfBoundsException catch (^^;) try catch Java 7 catch 3.51 ( ) DivideZeroExample.java

196 182 3 Java II package section0306; public class DivideZeroExample { int x = 0; int y = 10; try { int z = y / x; System.out.printf( " %d / %d = %d%n", y, x, z ); catch( ArithmeticException e ) { System.err.println( " 0 " ); System.err.println( " " ); catch( Exception e ) { System.err.println( " " ); System.err.println( e ); 2 catch ArithmeticException 2 Exception 2 catch catch catch ArithmeticException Exception ArithmeticException Exception catch ArithmeticException catch catch System.out.println( e ); e.printstacktrace() 3.52 TwoCatchExample.java ( ) package section0306; public class TwoCatchExample { int[] array = new int[5]; int x = (int)(math.random() * 10); try { array[x] = 10 / x; System.out.printf( " %d %d%n", x, array[x] ); catch( ArithmeticException e ) { System.err.println( " " ); System.err.println( e ); catch( ArrayIndexOutOfBoundsException e ) { System.err.println( " " ); System.err.println( e ); catch Java 7 catch

197 ( ) TwoCatchExample.java package section0306; public class TwoCatchExample { int[] array = new int[5]; int x = (int)(math.random() * 10); try { array[x] = 10 / x; System.out.printf( " %d %d%n", x, array[x] ); catch( ArithmeticException ArrayIndexOutOfBoundsException e ) { System.err.println( " " ); System.err.println( e ); ( ) System.err.println System.out ( ) System.err Eclipse System.out System.err (redirect) ( ) 3.54 RedirectExample.java ( ) package section0306; import java.io.filenotfoundexception; import java.io.printstream; public class RedirectExample { // PrintStream defaultsystemout = System.out; PrintStream defaultsystemerr = System.err; // try { // out err System.setOut( new PrintStream("src/section0306/output.txt") ); System.setErr( new PrintStream("src/section0306/error.txt") ); catch (FileNotFoundException e) { e.printstacktrace(); for(int i=-10; i<10; i++) { try { System.out.println( i + ":" + 10/i ); catch( ArithmeticException e ) { System.err.println( i + ":" + " " ); // System.setOut( defaultsystemout ); System.setErr( defaultsystemerr ); System.out.println( " " );

198 184 3 Java II try catch try catch finally finally finally catch finally 3.55 FinallyExample.java ( finally ) package section0306; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class FinallyExample { Scanner scan = null; try { scan = new Scanner( new File( "src/section0306/input.txt" ) ); while( scan.hasnext() ) { System.out.println( scan.nextline() ); // scan.close(); // return; catch( FileNotFoundException e ) { System.out.println( " " ); finally { if( scan!= null ) { System.out.println( "finally " ); scan.close(); 3 FileNotFoundException FileNotFoundException 3 ( ) scan.close() 13 scan.close() finally finally ( ) ( finally )

199 (^^;) finally ( 14 return ) Exception 3.56 MyExceptionExample.java ( ) package section0306; import java.util.inputmismatchexception; import java.util.scanner; public class MyExceptionExample { // Scanner Scanner scan = new Scanner( System.in ); int n = 0; // 10 while( true ) { try { System.out.print( "10 " ); // inputint n = inputint( scan ); // break; catch( NoWithinRangeException e ) { System.out.println( e.tostring() ); System.out.println(); System.out.println( " " + n ); // Scanner [1,9] static int inputint( Scanner scan ) throws NoWithinRangeException { int n = scan.nextint(); if( n <= 0 n >= 10 ) { throw new NoWithinRangeException(); return class NoWithinRangeException extends Exception { NoWithinRangeException() { super( "\n!" ); NoWithinRangeException Exception Exception super Exception

200 186 3 Java II \n main inputint( scan ) inputint 10 NoWithinRangeException ( main ) try catch throws throw throws MyException1, MyException2 main catch inputint catch main catch MyExceptionExample.java inputint 3.7 Scanner Scanner Scanner Java 5.0 C scanf() Scanner java.util ( ) java.util import Scanner ( ) next nextline 3.57 package section0307; import java.util.scanner; public class ScannerExample { System.out.print( " " ); Scanner scan = new Scanner( System.in ); int n = scan.nextint(); System.out.println( " " + n ); scan.close(); ScannerExample.java ( )

201 3.7 Scanner Scanner Scanner(InputStream source[, String charsetname]) [throws IllegalArgumentException] (System.in) Scanner(File source[, String charsetname]) throws FileNotFoundException[, IllegalArgumentException] Scanner(Path source[, String charsetname]) throws IOException[, IllegalArgumentException] Scanner(Readable source) Readable Scanner(ReadableByteChannel source[, String charsetname]) [throws IllegalArgumentException] Scanner(String source) Scanner void close() Pattern delimiter() boolean hasnext([pattern pattern j String pattern]) true boolean hasnextint([int radix]) int true boolean hasnextline() true String next([pattern pattern j String pattern]) int nextint([int radix]) int String nextline() Scanner skip(pattern pattern j String pattern) Scanner usedelimiter(pattern pattern j String pattern) Scanner useradix(int radix) Scanner ( System.in) scan nextint close Eclipse next next ( delimiter) ( )

202 188 3 Java II 3.58 ScannerExample2.java (next) package section0307; import java.util.scanner; public class ScannerExample2 { Scanner scan = new Scanner( "abc def" ); System.out.println( "scan1 = " + scan.next() ); System.out.println( "scan2 = " + scan.next() ); scan.close(); nextline 3.59 ScannerExample3.java (nextline) package section0307; import java.util.scanner; public class ScannerExample3 { Scanner scan = new Scanner( "abc def" ); System.out.println( "scan1 = " + scan.nextline() ); scan.close(); 1 next() Scanner next() nextline() 3.60 ScannerExample4.java (Scanner ) package section0307; import java.util.scanner; public class ScannerExample4 { // String linecd = System.getProperty("line.separator"); Scanner scan = new Scanner( "abc"+ linecd +"def" ); System.out.println( "scan1 = " + scan.next() ); System.out.println( "scan2 = " + scan.nextline() ); scan.close(); System getproperties() next abc scan1 scan

203 3.7 Scanner 189 nextline next() nextint() nextline() scan2 scan.nextline(); 1 ( ) CSV CSV Comma-Separated Values,.csv Windows CSV Excel Excel CSV Java Scanner Scanner usedelimiter 3.61 DelimiterExample.java ( ) package section0307; import java.util.scanner; public class DelimiterExample { String text = "10 hours, 20 minutes, 30 seconds"; // String values = text.replaceall("[^0-9,]", ""); // Scanner scan = new Scanner(values); scan.usedelimiter( ", " + System.getProperty("line.separator") ); // int hours = scan.nextint(); int minutes = scan.nextint(); int seconds = scan.nextint(); scan.close(); System.out.printf("%2d:%2d:%2d%n", hours, minutes, seconds); String replaceall (Regular Expression) 0-9, ^ [ ] values 10,20,30 usedelimiter ( )

204 190 3 Java II DelimiterExample.java replaceall ( \\s*,\\s* ) Scanner findinline match MathResult group(int) ( ) 3.62 ScannerExample5.java ( ) package section0307; import java.util.scanner; import java.util.regex.matchresult; public class ScannerExample5 { Scanner scan = new Scanner( "Usain Bolt:100m:9.58sec" ); /* * \\w A-Z, a-z, 0-9, _ * * [ ]+ * \\d 0-9 * ( ) match */ scan.findinline( "([\\w ]+):(\\d+)m:([\\d]+.[\\d]+)sec" ); MatchResult result = scan.match(); scan.close(); // group 1 String name = result.group(1); int dist = Integer.valueOf(result.group(2)); double time = Double.valueOf(result.group(3)); System.out.println( name + " " + dist + " m " + time + " "); File Scanner Java Windows C:Yjava (MacOS ) workspace ( ) src bin.metadata

205 3.7 Scanner 191 Eclipse ( ) ( Eclipse ) 3.63 HelloJavaFromFile.java ( Duke) package section0307; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class HelloJavaFromFile { public static void main(string[] args) throws FileNotFoundException { // src section0307 hellojava.txt File file = new File( "src/section0307/hellojava.txt" ); Scanner scan = new Scanner( file ); String message = scan.nextline(); System.out.println( message ); scan.close(); hellojava.txt ( ) Duke 3.64 hellojava.txt ( ) File folder0 folder1 2 file1 folder2 file4 file2 file3 1. file1 file4../file4 2. file4 file1 folder1/file1 3. file1 file2 4. file1 file3 5. file3 file4

206 192 3 Java II Scanner(File) Scanner nextint() Scanner File File Scanner Scanner Java Java 5.0 FileReader BufferedReader InputStreamReader Scanner Scanner ( Scanner ) inputdata.txt /src/section0307 inputdata.txt inputdata.txt ( ) 3.66 FileScanner.java (Scanner ) package section0307; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class FileScanner { public static void main(string[] args) throws FileNotFoundException { Scanner scan = new Scanner( new File( "src/section0307/inputdata.txt" ) ); int x = scan.nextint(); int y = scan.nextint(); int z = x * y; System.out.printf( "%d * %d = %d %n", x, y, z ); double a = scan.nextdouble(); double b = scan.nextdouble(); double c = a * b; System.out.printf( "%f * %f = %f %n", a, b, c ); scan.close(); FileNotFoundException FileNotFoundException IOException (catch ) catch throw (throws FileNotFoundException)

207 Scanner Scanner close() Scanner ( ) nextint() nextlong() nextdouble() Scanner hasnext() hasnext() true next ( ) 3.67 ScannerReadFile.java (hasnext() ) package section0307; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; public class ScannerReadFile { try { Scanner scan = new Scanner( new File( "src/section0307/number.txt" ) ); int count = 0; while( scan.hasnext() ){ String str = scan.next(); count++; System.out.printf( "%2d %s%n", count, str ); scan.close(); catch(filenotfoundexception e) { System.out.println(" "); 3.8 Scanner Java (stream) ( 3.11 ) ( )

208 194 3 Java II キーボード 画面 入力ストリームプログラム出力ストリーム ファイル ファイル 3.11 java.io import ( 3.12 ) Writer Reader Output Input 4 抽象クラス java.io.writer バッファリングすることで書き込みを効率化 java.io.bufferedwriter 3 文字ストリームからバイトストリームへの橋渡し java.io.outputstreamwriter 4 文字ファイルへの書き込み簡易クラス java.io.filewriter 2 抽象クラス : バイト出力ストリームを表現する全てのクラスのスーパークラス java.io.outputstream java.io.filteroutputstream バッファリングされた出力ストリーム java.io.bufferedoutputstream 生のバイトストリームを書き込む際に使用 java.io.fileoutputstream FileInputStream / FileOutputStream FileInput(Output)Stream FileInput(Output)Stream File File FileNotFoundException read write read write IOException close

209 StreamExample01.java (FileOutputStream ) package section0308; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; public class StreamExample01 { // File file = new File( "src/section0308/file01.txt" ); String str = "abcdefg\n "; try { System.out.println( " " ); FileOutputStream fos = new FileOutputStream( file ); // fos.write( str.getbytes() ); fos.close(); // catch ( FileNotFoundException e ) { System.out.println( " " ); catch ( IOException e ) { System.out.println( " " ); // try { System.out.println( " " ); // byte[] bdata = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream( file ); fis.read( bdata ); fis.close(); // // str = new String( bdata ); System.out.println( str ); catch ( FileNotFoundException e ) { System.out.println( " " ); catch ( IOException e ) { System.out.println( " " ); section0308 ( ) jpeg bitmap 3.69 package section0308; StreamExample02.java (FileOutputStream )

210 196 3 Java II import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; public class StreamExample02 { File file = new File("src/section0308/doraemon.bmp"); File file2 = new File("src/section0308/copy.bmp"); byte[] bdata = null; // try { System.out.println(" "); bdata = new byte[(int)file.length()]; FileInputStream fis = new FileInputStream( file ); fis.read( bdata ); fis.close(); catch (FileNotFoundException e) { System.out.println( " "); catch (IOException e) { System.out.println(" "); // try { System.out.println(" "); FileOutputStream fos = new FileOutputStream( file2 ); fos.write( bdata ); fos.close(); catch (FileNotFoundException e) { System.out.println( " "); catch (IOException e) { System.out.println(" "); FileReader / FileWriter Reader / Writer ( Scanner ) FileReader FileWriter Reader / Writer FileWriter write FileReader read read 1 ( ) 3.70 package section0308; import java.io.file; import java.io.filenotfoundexception; StreamExample03.java (FileWriter )

211 import java.io.filereader; import java.io.filewriter; import java.io.ioexception; public class StreamExample03 { File file = new File("src/section0308/file01.txt"); String str = "abcdefg\n "; // try { System.out.println(" "); FileWriter fw = new FileWriter( file ); fw.write( str ); fw.close(); catch (IOException e) { System.out.println(" "); // try { System.out.println(" "); str = ""; FileReader fr = new FileReader( file ); int c; while( (c = fr.read())!=-1 ) { str += (char) c; fr.close(); System.out.println( str ); catch (FileNotFoundException e) { System.out.println( " "); catch (IOException e) { System.out.println(" "); BufferedReader / BufferedWriter abc a b c OS ( ) ( ) FileReader BufferedReader / BufferedWriter BufferedWriter(Reader) FileWriter(Reader) writer newline readline readline null

212 198 3 Java II close 3.71 StreamExample04.java (BufferedWriter ) package section0308; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.filenotfoundexception; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; public class StreamExample04 { File file = new File("src/section0308/file01.txt"); String str = "abcdefg\n "; System.out.println(" "); // try { BufferedWriter bw = new BufferedWriter( new FileWriter( file )); bw.write( str ); bw.newline(); bw.close(); catch (IOException e) { System.out.println(" "); System.out.println(" "); // try { BufferedReader br = new BufferedReader( new FileReader( file )); String line; while( (line = br.readline())!= null ) { System.out.println( line ); br.close(); catch (FileNotFoundException e) { System.out.println( " "); catch (IOException e) { System.out.println(" "); InputStreamReader / OutputStreamWriter InputStreamReader(Writer) Java UNICODE ISO2022JP (JIS ) SJIS ( JIS ) EUC JP ( UNIX ) EUC JP SJIS Eclipse 3.72 StreamExample05.java (OutputStreamWriter )

213 package section0308; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstreamwriter; public class StreamExample05 { File file = new File( "src/section0308/file02.txt" ); String str = "abcdefg\n "; System.out.println( " " ); // EUC try { BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ),"EUC_JP") ); bw.write( str ); bw.newline(); bw.close(); catch ( IOException e ) { System.out.println( " " ); System.out.println( " " ); // "SJIS" try { BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream( file ), "SJIS") ); String line; while( (line = br.readline())!= null ) { System.out.println( line ); br.close(); catch (FileNotFoundException e) { System.out.println( " " ); catch (IOException e) { System.out.println( " " ); Java Excel Mathematica Java Java Excel Java Excel Excel Mathematica Mathematica Java Mathematica

214 200 3 Java II CSV csv CSV Comma Separated Values Excel Excel Mathematica Import CSV 3.73 CSV Example.java (CSV ) package section0308; import java.io.bufferedwriter; import java.io.filewriter; import java.io.ioexception; public class CSV_Example { public static void main(string[] args) throws IOException { // BufferedWriter bw = new BufferedWriter(new FileWriter("src/section0308/curve.csv")); bw.write(", Sin( )/ "); bw.newline(); // [0.1, 30.0] 0.1 for(int i=1; i<=300; i++) { double x = i / 10.0; bw.write( String.valueOf(x) ); bw.write(","); bw.write( String.valueOf(func(x)) ); bw.newline(); bw.close(); System.out.println(" "); static double func(double x) { return Math.sin( x )/x; Eclipse Excel section0308 curve.csv Excel Excel Mathematica Mathematica data = Import[ ] ListPlot[data] [ ] URL 3.9 import Java java.util ArrayList java.util.arraylist list = new java.util.arraylist();

215 3.9 import 201 import ( ) import java.util.arraylist; ArrayList ArrayList list = new ArrayList(); import import import package java.util.arraylist java util ArrayList import (on demand import) java.util 3 import java.util.arraylist; import java.util.linkedlist; import java.util.hashmap; import java.util.*; ( ) import java.awt.canvas; import java.awt.color; import java.awt.event.actionevent; import java.awt.event.mouseaapter; import java.awt.*; import java.awt.event.*; import java.awt import import ( 3.74 ) 3.74 ImportExamaple.java ( ) package section0309; import section0309.sub1.a; public class ImportExamaple { A.print(); // import section0309.sub2.a.print(); //

216 202 3 Java II package section0309.sub1; public class A { public static void print() { System.out.println( "section0309.sub1.a "); package section0309.sub2; public class A { public static void print() { System.out.println( "section0309.sub2.a "); java.lang java.lang Java System.out.println System Object Math String Thread import System.out.println Math.cos java.lang.system.out.println java.lang.math.cos Java 5.0 (static import) java.lang.math Math import 3.75 StaticImportExample.java ( ) package section0309; import static java.lang.math.pi; import static java.lang.math.pow; public class StaticImportExample { double radius = 1.2; System.out.println( " " + radius + " " + pow(radius,2) * PI );

217 ( ) ( ) (abstract class) (concrete class) abstract (abstract method) 3.76 AbstractClass.java ( (?)) package section0310; public class AbstractClass { Shape[] shapes = { new Circle( 1.2 ), // new Square( 1.5, 2.4 ), // new Trapezoid( 1.2, 3.0, 2.5 ) // ; for( Shape shape : shapes ) { shape.printarea(); // abstract class Shape { String name; // Shape( String name ) { // this.name = name; void printarea() { System.out.println( this.name + " " + this.getarea() );

218 204 3 Java II // abstract double getarea(); // class Circle extends Shape { double radius; Circle( double radius ) { super( " " ); this.radius = double getarea() { // return radius * radius * Math.PI; // class Square extends Shape { double height; double width; Square( double height, double width ) { super( " " ); this.height = height; this.width = double getarea() { // return height * width; // class Trapezoid extends Shape { double upper; double lower; double height; Trapezoid( double upper, double lower, double height ) { super( " " ); this.upper = upper; this.lower = lower; this.height = double getarea() { // return ( upper + lower ) * height / 2; Shape Shape 0 Shape getarea() abstract ( abstract double getarea(); ) ( ) abstract int calc( int x, int

219 y ); ( ) Shape double getarea() super(" "); Shape this(); this() super() main shape shape.printarea(); Shape printarea getarea() (Telephone ) (Cellular) (FixedLinePhone) ( ) Telephone 抽象クラス 抽象メソッドは 名前を斜体で Cellular FixedLinePhone Java C ++ Java (interface) ( ) 3.77 InterfaceExample.java ( ) package section0310; public class InterfaceExample { Dog pochi = new Dog( " " ); // Dog peace = new HearingDog( " " ); // Dog hope = new RescureDog( " " ); // Dog lucky = new RescureHearingDog( " " ); // Dog[] mydogs = { pochi, peace, hope, lucky ; for(dog dog : mydogs) { dog.print(); System.out.println();

220 206 3 Java II // Rescurable[] rescurabledogs = RescureDog.selectRescureDog( mydogs ); for(rescurable dog : rescurabledogs) { dog.rescure(); // ( ) class Dog { String name; Dog( String name ) { this.name = name; void print() { System.out.printf( " %s %n", this.name ); // class HearingDog extends Dog implements Hearable { HearingDog( String name ) { super( name void print() { System.out.printf( " %s %n", this.name public void hearing() { System.out.printf( " %s %n", this.name ); // class RescureDog extends Dog implements Rescurable { RescureDog( String name ) { super( name void print() { System.out.printf( " %s %n", this.name public void rescure() { System.out.printf( " %s %n", this.name ); // public static Rescurable[] selectrescuredog( Dog[] dogs) { Rescurable[] selecteddog = new Rescurable[dogs.length]; int count = 0; for(dog dog : dogs) { if( dog instanceof Rescurable ) { selecteddog[count++] = (Rescurable) dog; Rescurable[] rescurabledogs = new Rescurable[count]; System.arraycopy(selectedDog, 0, rescurabledogs, 0, count);

221 return rescurabledogs; // class RescureHearingDog extends Dog implements Hearable, Rescurable { RescureHearingDog( String name ) { super( name void print() { System.out.printf( " %s %n", this.name public void rescure() { System.out.printf( " %s %n", this.name public void hearing() { System.out.printf( " %s %n", this.name ); // interface Hearable { void hearing(); // ( ) // interface Rescurable { void rescure(); // ( ) <<interface>> Hearable ~ hearing( ) : void Dog ~ name : String ~ print( ) : void <<interface>> Rescurable ~ rescue( ) : void HearingDog RescureDog RescureHearingDog 3.13 interface

222 208 3 Java II public package-private public public abstract public static final ( extends ) (extends) (implements) UML ( 3.13 ) implements class implements { RescureHearingDog 2 Hearable Rescurable false 3.78 InterfaceExample2.java ( instanceof) package section0310; public class InterfaceExample2 { B a = new A(); System.out.println("a instanceof A = " + (a instanceof A) ); System.out.println("a instanceof B = " + (a instanceof B) ); System.out.println("a instanceof C = " + (a instanceof C) ); A b = new A(); System.out.println("b instanceof A = " + (b instanceof A) ); System.out.println("b instanceof B = " + (b instanceof B) ); System.out.println("b instanceof C = " + (b instanceof C) ); B c = new B(); System.out.println("c instanceof A = " + (c instanceof A) ); System.out.println("c instanceof B = " + (c instanceof B) ); System.out.println("c instanceof C = " + (c instanceof C) ); C d = new A();

223 System.out.println("d instanceof A = " + (d instanceof A) ); System.out.println("d instanceof B = " + (d instanceof B) ); System.out.println("d instanceof C = " + (d instanceof C) ); class A extends B implements C { public void method() { System.out.println( "yahoo!" ); class B { final int CONST = 100; interface C { void method(); d C d A C method() B CONST ( main System.out.println( d.const ); System.out.println( ((A).d).const ); 100 ) d.method(); yahoo! Java 8 ( ) Java (Java 8 API ) ( ) 3.79 InterfaceExample3.java ( ) package section0310; public class InterfaceExample3 { // TestClass tc = new TestClass(); tc.hello(); // TestClass2 tc2 = new TestClass2(); tc2.hello(); // TestClass3 tc3 = new TestClass3();

224 210 3 Java II tc3.hello(); interface Interface1 { // // default default void hello() { System.out.println( "Hello interface1" ); interface Interface2 { // default void hello() { System.out.println( "Hello interface2" ); class TestClass implements Interface1 { class TestClass2 implements Interface1, Interface2 public void hello() { System.out.println( "Hello TestClass2" ); class TestClass3 implements Interface1, Interface2 { public void hello() { Interface1.super.hello(); static ( static ) 3.11 Java API Collection (generics) (type parameter) (generic type) (type parameter), (type variable) (parameterized type) (raw type) (actual type parameter) (bounded type parameter) (unbounded wildcard type) (upper bounded wildcard type) (lower bounded wildcard type) List<E> <E> List<String> List <String> <E extends Number> List<?> List<? extends Number> List<? super Number>

225 package section0311; public class BoxExample1 { Box1 ibox = new Box1( new Integer(123) ); int n = ((Integer) ibox.get()).intvalue(); System.out.println( n ); BoxExample1.java ( ) // Box1 dbox = new Box1( new Double(12.3) ); // double m = ((Integer) dbox.get()).doublevalue(); // System.out.println( m ); Box1 sbox = new Box1( "Hello" ); String hello = (String) sbox.get(); System.out.println( hello ); class Box1 { Object obj; Box1( Object obj ) { this.obj = obj; Object get() { return obj; Box1 int double String Object Object ( ) int Object Integer (wrapper class) ( ) new Integer(10) 10 Integer ibox String Object Box1 Object Object obj get() Object main int n Integer Integer int intvalue()

226 212 3 Java II double Double ( Object ) Object Double Integer (type safe) 3.81 BoxExample2.java ( ) package section0311; public class BoxExample2 { Box<Integer> ibox = new Box<Integer>( new Integer(123) ); int n = ((Integer) ibox.get()).intvalue(); System.out.println( n ); // Box<Double> dbox = new Box<Double>( new Double(12.3) ); // double m = ((Integer) dbox.get()).doublevalue(); // System.out.println( m ); Box<String> sbox = new Box<String>( "Hello" ); String hello = (String) sbox.get(); System.out.println( hello ); class Box<E> { E e; Box( E e ) { this.e = e; E get() { return e; Box < > 3.82 package section0311; public class BoxExample3 { Box<Integer> ibox = new Box<Integer>( 123 ); int n = ibox.get(); System.out.println( n ); Box<Double> dbox = new Box<Double>( 12.3 ); BoxExample3.java ( )

227 double m = dbox.get(); System.out.println( m ); Box<String> sbox = new Box<String>( "Hello" ); String hello = sbox.get(); System.out.println( hello ); Box<Integer> Integer int Integer ( double Double ) (Autoboxing) ( ) (Auto-Unboxing) ( ) Integer a = 123; int b = a; ( null) NullPointerException class Box<E> { E e; Box( E e ) { this.e = e; E get() { return e; 3.83 Box Box <E> E ( Element ) Box<Integer> E Integer Box<String> E String <E> (Object ) Box Eclipse ( Eclipse ) BoxExample3.java 3.84 BoxExample3.java ( ) package section0311; /* */ import section0311.box; /* */ public class BoxExample3 { /* */ /* 5 */ Box ibox = new Box(Integer.valueOf(123)); /* 6 */ int n = ((Integer)ibox.get()).intValue();

228 214 3 Java II /* 7 */ System.out.println(n); /* */ /* 9 */ Box dbox = new Box(Double.valueOf(12.3D)); /* 10 */ double m = ((Double)dbox.get()).doubleValue(); /* 11 */ System.out.println(m); /* */ /* 13 */ Box sbox = new Box("Hello"); /* 14 */ String hello = (String)sbox.get(); /* 15 */ System.out.println(hello); /* */ /* 17 */ /* */ Box class Box { Object e; Box( Object e ) { this.e = e; Object get() { return e; 3.85 Box Object [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] = Box<int> box = E e = new E() = E[] e = new E[10] Object static static = static ( ) ( ) E T ( Type ) E (Element) T (Type) S K (Key) V (Value) package section0311;

229 public class CreateBoxExample Create<Integer> ci = new Create<Integer>(); Box<Integer> ibox = ci.makebox(100); // Integer i = ci.maket(); // Integer[] iarray = ci.makearray(10); class Create<T> { Box<T> makebox(t e) { return new Box<T>(e); /* T maket() { return new T(null); T[] makearray(int n) { return new T[n]; */ Box ( ) Box<T> makebox(t e) Box makebox(object e) T maket() Object Integer T[] makearray(int n) Object Integer Integer Object Integer Object ( ) ArrayStoreException 3.86 IntegersToObjects.java (Integer Object ) package section0311; import java.util.arrays; public class IntegersToObjects { Integer[] integers = {1, 2, 3; Object[] objects = integers; objects[0] = 1.23; System.out.println( Arrays.toString( objects ) ); String[] strings = {"a", "b", "c"; objects = strings; System.out.println( Arrays.toString( objects ) );

230 216 3 Java II Integer Object Box<Integer> Box<Object> package section0311; public class BoxExample4 { Box<Object> box = new Box<Object>(null); // box = new Box<Integer>( 100 ); // System.out.println( box.get() ); // box = new Box<String>( "Hello" ); System.out.println( box.get() ); Box<Integer> Box<String> Box<Object> (wildcard type) package section0311; public class BoxExample4 { Box<?> box = null; box = new Box<Integer>( 100 ); System.out.println( box.get() ); box = new Box<String>( "Hello" ); System.out.println( box.get() ); Box<?> 3.87 package section0311; public class IntegersToObjects3 { NBox<? extends Number> nbox = null; nbox = new NBox<Integer>( 100 ); System.out.println( nbox.get() ); nbox = new NBox<Double>( ); System.out.println( nbox.get() ); // nbox = new NBox<String>( "abc" ); class NBox<E extends Number> { E e; NBox( E e ) { this.e = e; E get() { return e; IntegersToObjects3.java ( )

231 Number Number ( Number ) Number int n = (Integer) nbox.get(); System.out.println( n ); Generic swap 3.88 GenericMethod.java (Generic method ) package section0311; import java.util.arrays; public class GenericMethod { String[] s = {"a", "b", "c"; swap(s,1,2); System.out.println( Arrays.toString( s ) ); Integer[] i = {1, 2, 3; swap(i,1,2); System.out.println( Arrays.toString( i ) ); Object[] o = {new Integer(10), "a", new Long(0); swap(o,1,2); System.out.println( Arrays.toString( o ) ); static <T> void swap(t[] array, int i, int j) { T temp = array[i]; array[i] = array[j]; array[j] = temp; ( void) swap<string>( s ); ( ) T[] merge(t[], T[]) GenericMethod.java

232 218 3 Java II (wrapper class) ( ) int x; int x byte short int long float double boolean char Byte Short Integer Long Float Double Boolean Character Integer.MAX_VALUE int Integer MAX_VALUE Integer ( ) public Integer static int MAX_VALUE int static int MIN_VALUE int 2 31 Integer(int) Integer Integer(String) Integer static int compare(int, int) int static int max(int, int) int static int min(int, int) int static int parseint(string) static Integer valueof(int) Integer static Integer valueof(string) Integer int compareto(integer) Integer boolean equals(object) Object int intvalue() int String tostring() [0, 1) ArrayList<Double> numbers RandomDoubles.java Math.random()

233 3.12 Java API IntegerExample.java (Integer ) package section0311; public class IntegerExample { Integer value1 = new Integer( 100 ); Integer value2 = new Integer( "100" ); System.out.println( value1 == value2? " ":" " ); System.out.println( value1.compareto( value2 ) == 0? " ":" " ); System.out.println( value1.equals( value2 )? " ":" " ); ArrayListExample.java list.add( 30 ) ArrayList<Integer> list.add( new Integer( 30 ) ) Java 5 Java (autoboxing) (autounboxing) int Integer list.add( new Integer(10) ) list.add( 10 ) OK ( ) int x = list.get(0).intvalue() Integer int x = array.get(0) OK ( ) Java Integer value2 = 100; 3.12 Java API API (Application Programming Interface) OS API OS Java JVM OS API Java OS API Java Java API Java API rt.jar zip Eclipse Java JRE rt.jar Java API

234 220 3 Java II Collection Collection Student[] students = new Student[100]; students.idnosort(); ( Student sort() Student.idNoSort( students ); ) Java API java.util Collection 3.14 <<interface>> Collection <<interface>> List <<interface>> Queue <<interface>> Set Stack ArrayList LinkedList PriorityQueue <<interface>> SortedSet HashSet 実装 : implements 継承 : extends <<interface>> NavigableSet LinkedHashSet TreeSet 3.14 Collection Collection 3 (List ) (Queue ) (Set ) ( ) ArrayList List

235 3.12 Java API ArrayList Collection Map SortedMap ArrayList List ArrayList ArrayList List ( ) Java 3.90 ArrayListExample.java (ArrayList ) package section0311; import java.util.list; import java.util.arraylist; import java.util.comparator; public class ArrayListExample { List<Integer> list = new ArrayList<Integer>(); // list.add( new Integer(10) ); // 10 list.add( new Integer("20") ); // Integer OK

236 222 3 Java II ArrayList<E > (E ) ArrayList() ArrayList(int) boolean add(e) void add(int, E) void clear() Object clone() E get(int) int indexof(object) boolean isempty() boolean remove(object) E remove(int) E set(int, E) int size() void sort(comparator<? super E>) Object[] toarray() 10 true list.add( 30 ); // int list.add( 2, 40 ); // list [2] 40 (30 ) System.out.println( "ArrayList " + list.size() ); System.out.println( list.tostring() ); list.sort( Comparator.naturalOrder() ); // System.out.println( list.tostring() ); list.remove(1); // array [1] ( ) System.out.println( list.tostring() ); ArrayList<Integer> ArrayList List list List ArrayList ArrayList<Point> points = new ArrayList<Point>(100); ArrayList ( ) add Arrays aslist 3.91 package section0312; import java.util.arraylist; import java.util.arrays; public class ArrayListExample2 { ArrayListExample2.java ( ArrayList )

237 3.12 Java API 223 // int Integer Integer[] intarray = { 10, 20, 30, 40 ; ArrayList<Integer> intlist = new ArrayList<Integer> (Arrays.asList( intarray ) ); intlist.add(2, 50); System.out.println( intlist.tostring() ); List<Integer> List<Double> List<Number> list.add( new Double(12.3) ) Comparable Comparator 3.90 Comparator.naturalOrder 2 Java 2 Comparable Comparator java.lang.comparable<t> int compareto(t) compareto(t) 1, 0, Point.java (Comparable ) package section0312; /** * 2 ( ) */ class Point implements Comparable<Point> { /** x */ double x; /** y */ double y; Point( double x, double y ) { this.x = x; this.y = y; /** public String tostring() { return String.format( "(%4.1f,%4.1f)", this.x, this.y ); /** * ( ) */ double dist() {

238 224 3 Java II return Math.sqrt( x*x + y*y ); /** * (-1,0,1 ) <br> * < -1,<br> * = 0,<br> * > 1 public int compareto(point other) { double thisdist = this.dist(); double otherdist = other.dist(); if( thisdist > otherdist ) return 1; else if( thisdist < otherdist ) return -1; else return 0; Point Arrays.sort package section0312; import java.util.arrays; public class ComparableExample { Point[] points = { new Point( 1, 3), new Point( 2, 2), new Point(-2, 1), new Point( 4, 0), new Point( 1,-1), new Point( 0, 3), new Point( 3, 2) ; Arrays.sort( points ); for(point point : points) { System.out.print( point.tostring() + " " ); System.out.println(); Point List Collections.sort package section0312; import java.util.arraylist; import java.util.arrays; import java.util.collections; import java.util.list; public class ComparableExample2 { List<Point> points = new ArrayList<Point>( Arrays.asList( new Point[]{ new Point( 1, 3), new Point( 2, 2), new Point(-2, 1), new Point( 4, 0), new Point( 1,-1), new Point( 0, 3), new Point( 3, 2) ) );

239 3.12 Java API 225 Collections.sort( points ); for(point point : points) { System.out.print( point.tostring() + " " ); System.out.println(); Comparable java.util.comparator<t> compare(t o1, T o2) static package section0312; import java.util.arrays; import java.util.comparator; public class ComparatorExample { Point[] points = { new Point( 1, 3), new Point( 2, 2), new Point(-2, 1), new Point( 4, 0), new Point( 1,-1), new Point( 0, 3), new Point( 3, 2) ; Arrays.sort( points, new PointComparator() ); for(point point : points) { System.out.print( point.tostring() + " " ); System.out.println(); class PointComparator implements Comparator<Point> public int compare(point p, Point q) { double pdist = p.dist(); double qdist = q.dist(); if( pdist > qdist ) return -1; else if( pdist < qdist ) return 1; else return 0; Point ( ) Comparator<Point> PointComparator ArrayListExample.java List sort() sort() Comparator Comparator.naturalOrder() Comparator static naturalorder() Comparable Comparator List Integer Integer Comparable ( Java 8 )

240 226 3 Java II ComparableExample2.java Collections.sort( points ); Comparator.naturalOrder() Comparator.reverseOrder() (String) (double) (double) 3 Person pienuts BodyMeasurement.java Comparator BodyComparator Array.sort( pienuts, new BodyComparator() ) Person[] pienuts = { new Person("Snoopy", 80.0, 15.5), new Person("Charlie", 140.0, 35.5), new Person("Linus", 140.0, 32.0), new Person("Sally", 135.0, 26.5), new Person("Lucy", 142.0, 30.5), new Person("Schroeder", 142.0, 31.0), new Person("Peppermint", 144.0, 33.5), new Person("Marcie", 135.0, 29.5) ; Comparator Java 8 Comparator (^_^;) 3.93 ShapesExample.java ( ) package section0311; import java.util.arraylist; import java.util.comparator; import java.util.list; public class ShapesExample { List<Shape> shapes = new ArrayList<Shape>(); shapes.add( new Circle( 1.2 ) ); shapes.add( new Square( 1.5, 2.4 ) ); shapes.add( new Trapezoid( 1.2, 3.0, 2.5 ) ); // shapes.sort( Comparator.comparing(Shape::getArea) ); for( Shape shape : shapes ) { shape.printarea();

241 3.12 Java API 227 // abstract class Shape { String name; // Shape( String name ) { // this.name = name; abstract double getarea(); // : void printarea() { // System.out.printf( "%s %8.5f%n", this.name, this.getarea() ); // class Circle extends Shape { double radius; Circle( double radius ) { super( " " ); this.radius = double getarea() { // return radius * radius * Math.PI; // : class Square extends Shape { double height; double width; Square( double height, double width ) { super( " " ); this.height = height; this.width = double getarea() { // return height * width; // : class Trapezoid extends Shape { double upper; double lower; double height; Trapezoid( double upper, double lower, double height ) { super( " " ); this.upper = upper; this.lower = lower; this.height = double getarea() { // return ( upper + lower ) * height / 2;

242 228 3 Java II LinkedList ArrayList List LinkedList ArrayList ( ) LinkedList ( ) ( 3.16 ) list list LinkedList ArrayList LinkedList 100 ArrayList 100 LinkedList 100 ArrayList 101 LinkedList ( 3.16 ) 3.94 LinkedListExample.java (LinkedList ) package section0311; import java.util.list; import java.util.linkedlist; import java.util.comparator; public class LinkedListExample { List<Integer> list = new LinkedList<Integer>(); list.add( new Integer(10) ); list.add( new Integer("20") ); list.add( 30 ); list.add( 2, 40 );

243 3.12 Java API 229 System.out.println( "LinkedList " + list.size() ); System.out.println( list.tostring() ); list.sort( Comparator.naturalOrder() ); System.out.println( list.tostring() ); list.remove(1); System.out.println( list.tostring() ); LinkedList ArrayList List ArrayList LinkedList [0] Stack Queue Stackx ( ) Queue ( ) Stack LIFO (Last In First Out: ) Queue FIFO (First In First Out: ) ( 3.17 ) Stack Queue 4 Stack Queue Stack Queue Java Stack List Queue List Queue LinkedList PriorityQueue ( 3.14 ) LinkedList List Queue PriorityQueue Comparator ( ) Stack Queue

244 230 3 Java II Stack<E > Queue<E > E peek() E pop() E push(e) E peek() E poll() boolean offer(e) Stack Queue 3.95 StackQueue.java (Stack Queue ) package section0311; import java.util.linkedlist; import java.util.queue; import java.util.stack; public class StackQueue { Stack<Integer> stack = new Stack<Integer>(); stack.push( 100 ); // stack.push( 200 ); stack.push( 300 ); stack.push( 400 ); System.out.println( "Stack " + stack.capacity() ); System.out.println( " " + stack.size() ); System.out.println( stack.tostring() ); stack.pop(); System.out.println( "Stack " ); System.out.println( stack.tostring() ); stack.push( 500 ); System.out.println( "Stack 500 " ); System.out.println( stack.tostring() ); // LinkedList Queue Queue<Integer> queue = new LinkedList<Integer>(); queue.offer( 100 ); // queue.offer( 200 ); queue.offer( 300 ); queue.offer( 400 ); System.out.println( "Queue " + ((LinkedList<Integer>) queue).size() ); System.out.println( " " + queue.size() ); System.out.println( queue.tostring() ); queue.poll(); System.out.println( "Queue " ); System.out.println( queue.tostring() ); queue.offer( 500 ); System.out.println( "Queue 500 " ); System.out.println( queue.tostring() );

245 3.12 Java API Java LinkedList LinkedList Comparator StudentList() void add(string) void add(int, String, Date, double) void add(student) StudentCell find(int) boolean delete(int) true void print(): ( ) studentslist: package public StudentListExample main StudentList StudentCell StudentList Student ,, 1980,11,26, ,, 1982,1,25, ,, 1983,6,17, ,, 1983,8,30, ,, 1982,12,24, ,, 1978,11,7, ,, 1977,1,11, ,, 1970,11,17, ,, 1972,1,10, ,, 1974,9,2, ,, 1980,11,18, ,, 1979,7,2, ,, 1979,2,20, ,, 1976,5,17, ,, 1971,7,24, ,, 1972,10,9, ,, 1973,12,8, ,, 1977,1,31, , \UTF{5F45, 1974,7,9, students.txt ( ):src/section StudentListExample.java (main )

246 232 3 Java II package section0311; import java.io.filenotfoundexception; public class StudentListExample { public static void main(string[] args) throws FileNotFoundException { StudentList slist = new StudentList(); slist.add( "src/section0312/students.txt" ); slist.add( , " ", new Date(1972,8,18), ); slist.add( , " ", new Date(1972,11,13), ); slist.print(); System.out.println(); // slist.add( , " ", new Date(1972,8,18), ); // slist.delete( ); // " " slist.delete( ); // " " slist.delete( ); // " \UTF{5F45 " slist.delete( ); // " " slist.print(); 3.98 Student.java ( ) package section0311; /** 1.0 */ class Student { /** */ int idno; /** */ String name; /** {@link Date */ Date birthday; /** */ double height; /** * idno name birthday {@link Date height */ Student( int idno, String name, Date birthday, double height ) { this.idno = idno; this.name = name; this.birthday = birthday; this.height = height; /** * csv csvtext csv <br> *,,,,,

247 3.12 Java API 233 */ static Student makestudent( String csvtext ) { String[] term = csvtext.split(",", 0); int idno = Integer.parseInt( term[0].trim() ); String name = term[1].trim(); Date birthday = new Date( Integer.parseInt( term[2].trim() ), Integer.parseInt( term[3].trim() ), Integer.parseInt( term[4].trim() ) ); double height = Double.parseDouble( term[5].trim() ); return new Student( idno, name, birthday, height ); /** * other ${\1,0,1\$ */ public int compareto( Student other ) { if( this.idno < other.idno ) return -1; else if( this.idno == other.idno ) return 0; else return 1; /** * */ void print() { System.out.printf( "%d %-6s (%s) %5.1f%n", idno, name, birthday.tostring(), height ); 3.99 package section0311; /** 1.0 */ class Date { /** */ int year; /** */ int month; /** */ int day; /** * year month day */ Date( int year, int month, int day ) { this.year = year; this.month = month; this.day = day; Date.java ( )

248 234 3 Java II /** * */ public String tostring() { return String.format( "%4d/%02d/%02d", year, month, day ); StudentCell.java ( ) package section0311; /** * {@link StudentList 1.0 */ class StudentCell { /** */ Student student; /** */ StudentCell precell; /** */ StudentCell nextcell; /** * student */ StudentCell( Student student ) { this.student = student; precell = null; nextcell = null; /** other ${\1,0,1\$ */ public int compareto( StudentCell other ) { return (this.student).compareto( other.student ); package section0311; import java.io.file; import java.io.filenotfoundexception; import java.util.scanner; /** 1.0 */ class StudentList { /** */ StudentCell firstcell; // StudentList.java ( )

249 3.12 Java API 235 /** */ StudentCell lastcell; // /** */ StudentList() { this.firstcell = null; this.lastcell = null; /** * filename csv <br> * {@link Student#makeStudent(String) Student.makeStudent FileNotFoundException */ void add( String filename ) throws FileNotFoundException { Scanner scan = new Scanner( new File( filename ) ); while( scan.hasnextline() ) { String csvtext = scan.nextline(); Student newstudent = Student.makeStudent( csvtext ); add( newstudent ); scan.close(); /** * <br> * add(student) idno name birthday Date height */ void add( int idno, String name, Date birthday, double height ) { add( new Student( idno, name, birthday, height ) ); /** * newstudent Student <br> * null */ void add( Student newstudent ) { StudentCell newcell = new StudentCell( newstudent ); // if( firstcell == null && lastcell == null ) { firstcell = newcell; lastcell = newcell; return; // StudentCell nextcell = firstcell; while( nextcell!= null ) { // switch( nextcell.compareto( newcell ) ) { case -1: // nextcell = nextcell.nextcell; break; case 0: // System.out.println( " ");

250 236 3 Java II newcell.student.print(); System.out.println(); return; case 1: // ( ) if( nextcell == firstcell ) { // newcell.nextcell = firstcell; firstcell.precell = newcell; firstcell = newcell; else { // StudentCell precell = nextcell.precell; // precell.nextcell = newcell; newcell.precell = precell; newcell.nextcell = nextcell; nextcell.precell = newcell; return; // newcell.precell = lastcell; lastcell.nextcell = newcell; lastcell = newcell; /** * idno null */ StudentCell find( int idno ) { StudentCell nextcell = firstcell; while( nextcell!= null ) { if( nextcell.student.idno == idno ) return nextcell; if( nextcell.student.idno > idno ) return null; nextcell = nextcell.nextcell; return null; /** * <br> * idno true */ boolean delete( int idno ) { StudentCell findcell = find( idno ); if( findcell == null ) return false; // if( findcell == firstcell ) { // if( findcell == lastcell ) { // firstcell = null; lastcell = null; else { firstcell = firstcell.nextcell; firstcell.precell = null; else if( findcell == lastcell ) { //

251 3.12 Java API 237 lastcell = lastcell.precell; lastcell.nextcell = null; else { // findcell.precell.nextcell = findcell.nextcell; findcell.nextcell.precell = findcell.precell; return true; /** * */ void print() { StudentCell nextcell = firstcell; System.out.println( " " ); while( nextcell!= null ) { nextcell.student.print(); nextcell = nextcell.nextcell; System.out.println(); Java TreeSet TreeMap 3.18 ( ) ( root) ( leaves) (innner node) (Binary tree) (Binary search tree) ( 3.18 ) ( ) BinarySearchTreeExample.java package section0311; /** * <br> *

252 238 3 Java II 1 (-2.1,2.6) (-1.6,-0.5) 2 (-2.5,2.3) (0.5,1.0) 5 (1.6,-3.1) (0.6,-1.1) (2.3,3.2) 7 (3.7,0.8) 8 (5.6,-5.1) (1.0,3.6) 9 (6.6,2.5) 10 (7.3,-3.1) (6.1,0.5) 17 (7.9,0.3) (4.8,-1.7) (3.9,-2.4) 15 (4.3,3.8) (-0.9,5.2) */ public class BinarySearchTreeExample { // y double[][] pointdata = { {-2.1, 2.6, {-2.5, 2.3, {-1.6,-0.5, { 0.5, 1.0, { 1.6,-3.1, { 2.3, 3.2, { 3.7, 0.8, { 5.6,-5.1, { 6.6, 2.5, { 7.3,-3.1, { 1.0, 3.6, { 6.1, 0.5, { 4.8,-1.7, { 0.6,-1.1, { 4.3, 3.8, { 3.9,-2.4, { 7.9, 0.3, {-0.9, 5.2 ; // Node[] nodes = PNode.makeNodeArray( pointdata ); // BinarySearchTree pointtree = new BinarySearchTree( nodes ); // // // pointtree.dpsprint(); // pointtree.bfsprint(); /** * <br> * */

253 3.12 Java API 239 abstract class Node implements Comparable<Node> { /** */ static int sno = 0; /** */ int no; /** */ Node parent; /** */ Node left; /** */ Node right; Node() { this.no = ++sno; this.parent = null; this.left = null; this.right = null; /** * */ class PNode extends Node { /** */ Point point; PNode(Point point ) { super(); this.point = point; /** */ public String tostring() { String sleft = (left==null)?".":string.valueof(left.no); String sright = (right==null)?".":string.valueof(right.no); String sparent = (parent==null)?".":string.valueof(parent.no); return String.format("%2d%s: l=%2s r=%2s p=%2s", this.no, point.tostring(), sleft, sright, sparent); /** * PNode pointdata x y * PNode */ static PNode[] makenodearray( double[][] pointdata ) { PNode[] nodes = new PNode[pointData.length]; for(int i=0; i<pointdata.length; i++) { nodes[i] = new PNode(new Point( pointdata[i][0], pointdata[i][1] )); return nodes; /** * <br> * <br> * */

254 240 3 Java public int compareto(node other) { return this.point.compareto(((pnode)other).point); /** * <br> * */ class BinarySearchTree { /** */ Node root = null; /** * Point points Point */ BinarySearchTree(Node[] nodes) { for(int i=0; i<nodes.length; i++) { add( root, nodes[i] ); /** * currentnode newnode * */ void add( Node currentnode, Node newnode ) { if( currentnode == null ) { // top root = newnode; return; else { int check = currentnode.compareto( newnode ); if( check < 0 ) { if( currentnode.right == null ) { currentnode.right = newnode; newnode.parent = currentnode; else { add( currentnode.right, newnode ); else if( check > 0 ) { if( currentnode.left == null ) { currentnode.left = newnode; newnode.parent = currentnode; else { add( currentnode.left, newnode ); // if( check == 0 ) return; /** */ void dpsprint() { System.out.println( " "); if( root == null ) { System.out.println( " 2 ");

255 System.out.println(); return; dpsprint( root ); System.out.println(); /** * dpsprint() * currentnode Node */ void dpsprint( Node currentnode ) { if( currentnode == null ) return; dpsprint( currentnode.left ); System.out.println( currentnode.tostring() ); dpsprint( currentnode.right ); /** */ void bfsprint() { System.out.println( " "); if( root == null ) { System.out.println( " 2 "); System.out.println(); return; bfsprint( root ); System.out.println(); /** * bfsprint() * currentnode Node */ void bfsprint( Node currentnode ) { if( currentnode == null ) return; System.out.println( currentnode.tostring() ); bfsprint( currentnode.left ); bfsprint( currentnode.right ); 3.13 (nested class) 4

256 242 3 Java II (static) (static member class) static (nonstatic member class) (anonymous class) (local class) 3 (inner class) (enclosing class) (outer class) NestedClassExample.java ( ) package section0312; public class NestClassExample { A a = new A(); a.print(); A.B b = new A.B(3, 4); b.print(); A.B.sprint(); // class A { int ia; static int sa; A() { this.ia = 1; A.sa = 2; void print() { System.out.printf( "A's print() ia=%d, sa=%d, sb=%d%n", ia, sa, B.sb ); // static class B { int ib; static int sb = 0; B(int ib, int sb) { this.ib = ib; B.sb = sb; void print() { System.out.printf( "B's print() sa=%d, ib=%d, sb=%d%n", sa, ib, sb ); static void sprint() { System.out.printf( "B's sprint() sa=%d, sb=%d%n", sa, sb ); B A A A

257 B A main B b = new B( 1 ) static A a.b b = new a.b(); B A b ( static ) (package-pribate publi) ( ) (private protected ) B private (BankAccount) (Permissions) (?) package section0312; public class StaticMemberExample { BankAccount nodaai = new BankAccount( , " " ); nodaai.setpermissions( new BankAccount.Permissions( true, false, false ) ); nodaai.printforclose(); class BankAccount { // int number; // String name; // Permissions permissions; // BankAccount( int number, String name ) { this.number = number; this.name = name; void setpermissions( Permissions permissions ) { this.permissions = permissions; void printforclose() { // System.out.println( name + " " + (permissions.canclose? " ":" ") ); // static static class Permissions { // boolean candeposit; // boolean canwithdraw; // boolean canclose; // Permissions( boolean cand, boolean canw, boolean canc ) { this.candeposit = cand; this.canwithdraw = canw; this.canclose = canc;

258 244 3 Java II static static public, private, protected, package-private, final, abstract static ( ) static (inner class) static static InnerClassExample.java ( ) package section0312b; public class InnerClassExample { A a = new A(); a.print(); A.B ab = a.new B(5, 6); ab.print(); class A { // int ia; static int sa; B b; A() { this.ia = 1; A.sa = 2; this.b = new B(3, 4); void print() { System.out.printf( "A's print() ia=%d, sa=%d%n", ia, sa ); class B { // int ib=3; B( int ib, int sb ) { this.ib = ib; ia = ib; sa = sb; void print() { System.out.printf( "B's print() ia=%d, sa=%d, ib=%d%n", ia, sa, ib ); static A.B ab = new A.B(4); public, private, protected, package-private, final, abstract

259 ( ) Arrays sort AnonymouseClassExample.java ( ) package section0312; import java.util.arrays; import java.util.comparator; public class AnonymouseClassExample { String[] array = {"b", "B", "A", "C", "a", "c"; Arrays.sort( array, new Comparator<String>() { public int compare(string s1, String s2) { return s1.compareto(s2); ); for(string s : array) System.out.println( s ); static 3.14 (parallel computing) 2 (multi-process) (multi-thread) main main main main main main (?)

260 246 3 Java II Java 2 Thread Runnable Java Thread Runnable Thread Thread Thread Thread start run start run run run start ( main ) run run Thread run Thread run ThreadExample01.java (Thread ) package section0314; public class ThreadExample01 { System.out.println( "main thread start..." ); MyThread thread_a = new MyThread(); MyThread thread_b = new MyThread(); thread_a.start(); thread_b.start(); System.out.println( "main thread end..." ); class MyThread extends Thread public void run() { System.out.println( getname() + " thread start..." ); working(); System.out.println( getname() + " thread end..." ); private void working() { int sum = 0; for(int i=1; i<=100000; i++) { sum += i; System.out.println( getname() + ": sum = " + sum );

261 main main thread start... start main thread start... Thread-0 thread start... Thread-1 thread start... main thread end... Thread-0: sum = Thread-0 thread end... Thread-1: sum = Thread-1 thread end... run Thread getname() ( setname(string) ) main 2 run start thread_a.start(); IllegalThreadStateException Exception in thread "main" java.lang.illegalthreadstateexception at java.lang.thread.start(thread.java:705) at threadexamples.threadexample01.main(threadexample01.java:9) start thread B thread A main thread start... main thread end... Thread-1 thread start... Thread-0 thread start... Thread-1: sum = Thread-1 thread end... Thread-0: sum = Thread-0 thread end main ( ) ThreadExample02.java (Thread ) package section0314; public class ThreadExample02 { public static void main(string[] args) throws InterruptedException {

262 248 3 Java II Deposit deposit = new Deposit(); DepositThread thread_a = new DepositThread(" ", deposit, 1000); thread_a.start(); DepositThread thread_b = new DepositThread(" ", deposit, 1000); thread_b.start(); // Thread.sleep(100L); System.out.println( "2 " + deposit.getdeposit() + " " ); class DepositThread extends Thread { Deposit deposit; int money; DepositThread( String name, Deposit deposit, int money ) { super( name ); this.deposit = deposit; this.money = public void run() { System.out.println( getname() + " " + money + " " ); deposit.adddeposit( money ); class Deposit { int deposit; public Deposit() { this.deposit = 0; int getdeposit() { return deposit; void adddeposit( int money ) { this.deposit = this.getdeposit() + money; sleep() : Thread join() : Thread sleep public static void sleep InterruptedException IllegalArgumentException IllegalArgumentException sleep(long) sleep(long, int) ( ) main 100 msec

263 join public final void join InterruptedException join() join(0) join(long) 0 join(long, int) ( ) 2 public static void main(string[] args) throws InterruptedException { Deposit deposit = new Deposit(); DepositThread thread_a = new DepositThread(" ", deposit, 1000); thread_a.start(); DepositThread thread_b = new DepositThread(" ", deposit, 1000); thread_b.start(); // Thread.sleep(100L); thread A.join(); thread B.join(); System.out.println( "2 " + deposit.getdeposit() + " " ); main Runnable Runnable Thread Runnable Runnable run Runnable run Thread start ThreadExample04.java (Runnable ) package section0314; public class ThreadExample04 { System.out.println( "main thread start..." ); Thread thread_a = new Thread( new SubThread("thread_A") ); thread_a.start(); Thread thread_b = new Thread( new SubThread("thread_B") ); thread_b.start(); System.out.println( "main thread end..." ); class SubThread implements Runnable { String name; SubThread( String name ) { this.name = name;

264 250 3 Java II String getname() { return public void run() { System.out.println( getname() + " thread start..." ); working(); System.out.println( getname() + " thread end..." ); private void working() { int sum = 0; for(int i=1; i<=100000; i++) { sum += i; System.out.println( getname() + ": sum = " + sum ); ThreadExample02.java package section0314; public class ThreadExample02 { public static void main(string[] args) throws InterruptedException { for( int i=0; i<100; i++) { Deposit deposit = new Deposit(); DepositThread thread_a = new DepositThread(" ", deposit, 1000); thread_a.start(); DepositThread thread_b = new DepositThread(" ", deposit, 1000); thread_b.start(); // Thread.sleep(100L); thread_a.join(); thread_b.join(); System.out.print( "2 " + deposit.getdeposit() + " " ); if( deposit.getdeposit()!= 2000 ) { System.out.print( " " ); System.out.println(); class DepositThread extends Thread { Deposit deposit; int money; DepositThread( String name, Deposit deposit, int money ) { super( name ); this.deposit = deposit; this.money = public void run() {

265 // System.out.println( getname() + " " + money + " " ); try { Thread.sleep(5L); catch (InterruptedException e) { e.printstacktrace(); deposit.adddeposit( money ); class Deposit { int deposit; public Deposit() { this.deposit = 0; int getdeposit() { return deposit; void adddeposit( int money ) { this.deposit += money; join msec ( 3.19 ) adddeposit() ( ) thread_a adddeposit(1000) deposit 0 円 1000 円 1000 円 2000 円 thread_b adddeposit(1000) 時間の流れ thread_a adddeposit(1000) deposit 0 円 0 円 1000 円 1000 円 thread_b adddeposit(1000) 時間の流れ 3.19 ( 1000 ) adddeposit

266 252 3 Java II synchronized adddeposit synchronized void adddeposit( int money ) { this.deposit += money; BankBooks.java ( ) package section0314; public class BankBooks { BankBook bankbook1 = new BankBook( " 1", ); // BankBook bankbook2 = new BankBook( " 2", ); // // 1000 BankingThread banking1 = new BankingThread( bankbook1, bankbook2, 1000 ); // 5000 BankingThread banking2 = new BankingThread( bankbook2, bankbook1, 5000 ); // banking1.start(); banking2.start(); // bankbook1.print(); bankbook2.print(); System.out.printf( " %n%s : %d %n%s : %d %n", bankbook1.name,14000,bankbook2.name,16000 ); // class BankingThread extends Thread { BankBook book1; // BankBook book2; // int money; // BankingThread( BankBook book1, BankBook book2, int money ) { this.book1 = book1; this.book2 = book2; this.money = money; public void run() { book1.setdeposit(book1.getdeposit() - money); // book2.setdeposit(book2.getdeposit() + money); // // class BankBook { String name; // int deposit; // BankBook( String name, int money ) {

267 3.15 JavaFX ( ) 253 this.name = name; this.deposit = money; // int getdeposit() { return this.deposit; // void setdeposit(int money) { this.deposit = money; // void print() { System.out.printf( "%s : %d %n", this.name, this.deposit ); 3.15 JavaFX ( ) Java 1996 Java 1.0 GUI (Graphical User Interface) AWT (Abstract Window Toolkit) GUI CUI (Character-based User Interface) Java OS write once, run anywhere ( ) AWT 1998 Java 2 GUI Swing Swing AWT OS ( OS GUI ) Java Flash Sun Microsystems Flash JavaFX 2007 JavaFX Script 2011 Oracle JavaFX Script Java Swing JavaFX Java 7 GUI JavaFX (^_^;) Eclipse JavaFX e(fx)clipse: Eclipse JavaFX JavaFX Scene Builder: GUI e(fx)clipse JavaFX Java 7u2 JDK JavaFX Eclipse Scene Builder Eclipse ( ) e(fx)clipse Eclipse

268 254 3 Java II e(fx)clipse - install ( 3.20 ) Eclipse JavaFX javafx e(fx)clipse Scene Builder Scene Builder JavaFX GUI Oracle Scene Builder ( ) Java SE 8u40 Scene Builder GLUON Scene Builder OpenJDK OpenJFX ( ) GLUON GLUON Download Now Scene Builder ( 3.21 ) Gluon Eclipse Scene Builder Eclipse (MacOS Eclipse ) JavaFX SceneBuilder SceneBuilder.exe (MacOS SceneBuilder.app) ( 3.22 )

269 3.15 JavaFX ( ) Scene Builder 3.22 Eclipse Scene Builder JavaFX JavaFX JavaFX section0315 JavaFX Java... JavFX JavaFX Java JavaFX UI FXML JavaFXExample01.java Eclipse JavaFX JavaFX Eclipse JavaFX SDK JavaFX SDK JRE package section0315; import javafx.application.application; import javafx.event.actionevent; import javafx.event.eventhandler; JavaFXExample01.java ( JavaFX)

270 256 3 Java II import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.layout.stackpane; import javafx.stage.stage; public class JavaFXSample01 extends Application { launch( args public void start(stage primarystage) { Button button = new Button(); button.settext( "Say 'Hello World'" ); button.setonaction(new EventHandler<ActionEvent>() public void handle( ActionEvent event ) { System.out.println( "Hello World!" ); ); StackPane root = new StackPane(); root.getchildren().add( button ); Scene scene = new Scene( root, 300, 250 ); primarystage.settitle( "Hello World!" ); primarystage.setscene( scene ); primarystage.show(); 3.23 JavaFX JavaFXExample01 javafx.application.application JavaFX main Application Application void init() abstract void start(stage) throws Exception Stage void stop() static void launch(string...) JavaFX main main

271 3.15 JavaFX ( ) 257 start JavaFX main main main main launch Application init JavaFX ( ) init start start JavaFX (Stage) (Scene) (SceneGraph) Application start start primarystage JavaFX Stage (components) Scene primarystage.setscene( scene ) Stage Scene (container) Stage show() ( start ) show() void hide() void close() start Exception try catch throws ( throws ) main main Eclipse start start stop stop Stage Label Button ( ) 6 VBox ( ) HBox

272 258 3 Java II AnchorPane FlowPane BorderPane GridPane JavaFX JavaFXExample02.java (JavaFX ) package section0315; import javafx.application.application; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.layout.vbox; import javafx.stage.stage; public class JavaFXExample02 extends Application { print( Thread.currentThread(), "main() " ); launch(args); print( Thread.currentThread(), "main() " ); static void print( Thread thread, String message ) { System.out.printf( "%s: %s%n", thread.getname(), message public void init() throws Exception { print( Thread.currentThread(), "init() " public void start(stage primarystage) throws Exception { print( Thread.currentThread(), "start() " ); primarystage.settitle( "JavaFX " ); Label label = new Label(" "); VBox vbox = new VBox(); vbox.setalignment( Pos.CENTER ); vbox.getchildren().add( label ); primarystage.setscene( new Scene( vbox, 480, 240 ) ); primarystage.show(); print( Thread.currentThread(), "start() " public void stop() throws Exception { print( Thread.currentThread(), "stop() " ); main: main() JavaFX-Launcher: init() JavaFX Application Thread: start()

273 3.15 JavaFX ( ) 259 JavaFX Application Thread: start() JavaFX Application Thread: stop() main: main() main launch() JavaFX-Launcher main JavaFX-Launcher init GUI JavaFX Application Thread start GUI start show ( ) stop stop main Stage start Stage Stage Stage JavaFXExample03.java ( ) package section0315; import javafx.application.application; import javafx.stage.stage; public class JavaFXExample03 extends Application { public void start(stage primarystage) throws Exception { primarystage.settitle( " " ); // (= true) primarystage.setresizable( true ); // primarystage.setwidth( 480 ); primarystage.setheight( 240 ); // 640, 360 primarystage.setmaxwidth( 640 ); primarystage.setminwidth( 360 ); // primarystage.show(); ( ) false (Windows MacOS ) ( Stage (public final ) is

274 260 3 Java II JavaFXExample04.java ( ) package section0315; import javafx.application.application; import javafx.scene.scene; import javafx.scene.layout.flowpane; import javafx.scene.paint.color; import javafx.stage.stage; public class JavaFXExample04 extends Application { public void start(stage primarystage) throws Exception { primarystage.settitle( " " ); primarystage.setwidth( 640 ); primarystage.setheight( 480 ); primarystage.show(); Stage newstage = new Stage(); newstage.settitle( " " ); newstage.setwidth( 320 ); newstage.setheight( 240 ); newstage.setalwaysontop(true); newstage.setresizable(false); newstage.show(); Stage newstage2 = new Stage(); newstage2.settitle( " " ); newstage2.setscene( new Scene(new FlowPane(), 480, 360, Color.PINK ) ); newstage2.show(); 3 ( 3.24 ) alwaysontop false true fullscreenexitkey fullscreen false true iconified false true maxwidth, maxheight Double.MAX_VALUE minwidth, minheight 0 width, height 0 resizable true false title opacity scene null x, y Stage

275 (Scene) ( ) setscene Scene Stage Scene Stage setscene Scene Scene GUI GUI ( VBox ) ( ) setscene Java int double 2 Java 5 (enumerated type) ( ) C C ++ enum

276 262 3 Java II (enumerate) enum package section0316; public class PlayCardExample01 { // Card1 card = new Card1( Card1.HEARTS, 8 ); System.out.println( card.tostring() ); // class Card1 { int suit; // int number; // static final int CLUBS = 0; static final int DIAMONDS = 1; static final int HEARTS = 2; static final int SPADES = 3; Card1( int suit, int number ) { this.suit = suit; this.number = number; public String tostring() { String suitname = null; switch( this.suit ) { case CLUBS: suitname = " "; break; case DIAMONDS: suitname = " "; break; case HEARTS: suitname = " "; break; case SPADES: suitname = " "; break; default: throw new IllegalArgumentException(); return suitname + " " + this.number; PlayCardExample01.java ( ) int (Card1 card = new Card1( 5, 8 ); ) 4 0, 1, 2, 3 4 int 4 int bin (static final int CLUBS = 4;)

277 PlayCardExample02.java ( ) package section0316; public class PlayCardExample02 { Card2 card = new Card2( Card2.Suit.HEARTS, 8 ); System.out.println( card.tostring() ); class Card2 { Suit suit; int number; Card2( Suit suit, int number ) { this.suit = suit; this.number = number; public String tostring() { return this.suit.suitname + ":" + this.number; // static class Suit { private final String suitname; static final Suit CLUBS = new Suit(" "); static final Suit DIAMONDS = new Suit(" "); static final Suit HEARTS = new Suit(" "); static final Suit SPADES = new Suit(" "); private Suit( String suitname ) { // this.suitname = suitname; Card2 Suit Card2 Suit Suit private Suit 4 Java 5 enum enum java.lang.enum enum Suit Card3 ( enum?) ( ) PlayCardExample new.java (enum ) package section0316; public class PlayCardExample3 { Card3 card = new Card3( Card3.Suit.HEARTS, 8 ); System.out.println( card.tostring() ); // Card3.Suit.suitPrint(); class Card3 { int number; Suit suit; Card3( Suit suit, int number ) { this.suit = suit;

278 264 3 Java II this.number = number; public String tostring() { return this.suit.tostring() + ":" + this.number; // ( ) enum Suit { // ( ) CLUBS(" "), DIAMONDS(" "), HEARTS(" "), SPADES(" "); // // private final String name; // private Suit( String name ) { this.name = name; // static void suitprint() { for( Suit suit : Suit.values()) { System.out.printf( "[%d] %s(%s)%n", suit.ordinal(), suit.name(), suit.name ); class { enum { ( ) ( ) ordinal() name() ordinary() name() name enum enum package section0316; public enum RGBColor { RED( " ", 0xff0000 ), GREEN( " ", 0x00ff00 ), BLUE( " ", 0x0000ff ); String jname; int value; RGBColor( String jname, int value ) { this.jname = jname; RGBColor.java ( )

279 this.value = public String tostring() { return jname + "(" + String.format("%06x", Value) + ")"; EnumImportExample.java ( ) package section0316; import static section0316.rgbcolor.*; public class EnumImportExample { RGBColor c = GREEN; // RGBColor.GREEN System.out.println( " " + c.tostring() ); 3.17 (annotation) Java 5 ( Eclipse) (Eclipse java.lang.annotation.annotation java.lang ( ) Javadoc DeprecatedExample.java ( : ) package section0317; public class DeprecatedExample { System.out.println( plus(1, 2) ); System.out.println( add(1, 2) ); // plus add plus

280 266 3 Java public static int plus(int a, int b) { return a + b; public static int add(int a, int b) { return a + b; (plus add) Eclipse ( ) public class A { public void print() { /* */ class B extends A public void print( int x ) { /* (Warning) alength Eclipse a alength int[] a = { 1, 2, 3 int alength = a.length; 3.18 System.out.printf int x = 100; int y = 200; System.out.printf( "x = %d%n", x );

281 System.out.printf( "%d + %d = %d%n", x, y, (x+y) ); printf printf Java 5 (variable arity parameter) (arity ) VariableArityExample.java ( ) package section0318; public class VariableArityExample { System.out.println( "sum(1, 2) = " + sum( 1, 2 ) ); System.out.println( "sum(1, 2, 3) = " + sum( 1, 2, 3 ) ); // {11, 12, 13, 14, 15 [2] check( 2, 11, 12, 13, 14, 15 ); static int sum( int... x ) { int sum = 0; for(int i : x) { sum += i; return sum; static void check( int n, int... x ) { System.out.printf( "x[%d] = %d%n", n, x[n] ); int... x int 3 sum( int... x, int... y ) ( x y ) sum( int x, int... y ) sum( int... x, int y ).length ( x.length 0 sum() check( 2 ); ArrayIndexOutOfBoundsException

282 III

283 Apx.2 A A ( A.1 ) A , ( A.2 ) 16 ( A.3 ) A F ( ) 0 F 16

284 A.2 Apx.3 A.2 A.3 16 A x (x hexadecimal:16 ) 0x7a 0x7A bit( ) 8 bits 1 1 byte( ) A

285 Apx.4 A A ) ) ) ) ) ) ) ₂ A A A

286 A.3 Apx.5 n 2 b 1 b 2 b 3 b n x 0 x 2x + b i, (i = 1, 2,..., n) x 10 A A A A3 8 A p p 1 (= p 1 ) p 2 1 (= p 2 ) A

287 Apx.6 A A ( A.8 ) = = = = A ( A.9 ) A A

288 A.4 2 Apx.7 A A A.4 2 n 10 2 A.11 2 p log p A n p log p ( ) [ ] < log 2 10 <

289 Apx.8 A A (= 2 1 ) 1 bit 2 ( A.12 ) 2.5 ( ) A A bits 64 bits Windows OS 64 bits 32 bits 32 bits Emulator ( ) 32 bits 1 bit 0 1 ) 16 bits 16 bits 16 bits 0 bits = = = ( A.13 ) A bits 2 )

290 A.7 2 Apx.9 正の最大整数 A bits A ( ) 16 bits A ( 100) 17 bits 16 bits bits 0 1 A bits bits

291 Apx.10 A A (Fixed Decimal Point Number) (Floating Decimal Point Number) 0.123E-2 E (Exponential) Java = = (Mantissa) (Exponent) bits ( A.15 ) A bits A.9 IEEE 754 IEEE 754 IEEE (Institude of Electrical and Elctrinics Engineers) IEEE bits 64 bits bits ( ) IEEE (^^;) ( ) 127 A.9.1 IEEE IEEE 754 (1 bit) (8 bits) (23 bits) 32 bits ( A.16 ) bit

292 A.9 IEEE 754 Apx ( ) 1 ( ) (127) = 133 = A.16 IEEE 754 A.9.1. IEEE bits ( ) A IEEE bits 1 bit 11 bits 52 bits IEEE bits 0.1 ( A.9.1 ) NaN Not a Number 0 / / / / NaN 0 / A.9.1 IEEE 754

293 Apx.12 A A.10 6 bits 1 bit 2 bits 3 bits 2 ( A.17 ) A IEEE bits A IEEE 754 Java Float.MAX_VALUE, Float.MIN_VALUE

294 Apx.13 B Eclipse B.1 Eclipse Eclipse Eclipse Java Eclipse Java Eclipse Eclipse Neon Apple MacOS Windows All in One 64bits MacOS B.2 Pleiades Eclipse Java Pleiades Windows Pleiades zip MacOS Pleiades dmg JDK JRE Eclipse Eclipse 4.6 Neon Pleiades ( B.2 ) ( B.2 ) OS 32bits 64bits Full Edition Standard Edition 36 Windows OS 32bits 64bits 64 64bits OS bit ( Full Edition, Java ) JRE JDK ( B.2 ) Windows / MacOS

295 付録 B Eclipse のインストール Apx.14 図 B.1 Pleiades のダウンロードの開始ページ 図 B.3 図 B.2 Pleiades の Edition の選択 Pleiades のダウンロード開始 (Windows) ファイル名は Windows 用 pleiades java-win-64bit-jre_ MacOS 用 pleiades java-mac-jre_ dmg でした B.3 Java API ドキュメントのダウンロード Eclipse では プログラムの作成中に Java で用意されているクラスやメソッドのドキュメント (Java API documents) をネットを介して見ることができるのですが そのドキュメントを事前に各自のパソコンにダウンロードして おけば ネットに接続していない環境でも利用出来るようになり便利です そこで ドキュメントをダウンロードする ため Oracle のダウンロードページに移動します URL からわかるように マニュアルの改変で URL が変わる可能性があるので その場合は Google などでキーワー ド Java API ドキュメント と検索して下さい 日本語版ドキュメントのダウンロードはページの下のほうにアーカ イブ ファイルへのリンクがありますので それをクリックします (図 B.4 参照) 最新バージョンの日本語版ドキュメントをダウンロードすることにします この文章を書いている時点でダウンロー

296 B.4 Eclipse Apx.15 B.4 API B.5 8.zip ( B.5 ) Java Eclipse B.4 Eclipse Eclipse B.4.1 ( ) Eclipse Java Eclipse Windows C:Y pleiades Eclipse java MacOS pleiades.app Eclipse java pleiades (MacOS pleiades.app) B.4.2 Windows pleiades-***.zip MacOS pleiades-***.dmg

297 Apx.16 B Eclipse Windows pleiades ( B.6 ) pleiades C:Y ( B.7 ) C:Ypleiades C:YpleiadesYeclipseYclipse.exe ( B.8 ) MacOS Eclipse ( B.9 ) Eclipse Eclipse B.6 Pleiades (1) B.7 Pleiades (2) B.8 Eclipse (Windows) B.9 Pleiades (Mac)

298 B.5 Java API Apx.17 B.5 Java API zip E Windows Eclipse C:Ypleiades ( B.10, B.11 ) C:YpleiadesYE YapiYindex.html ( B.12 ) MacOS Pleiades 1 ( ) 8.zip 8.zip E54527_01 Applications\E \api\index.html zip dmg Eclipse B.10 API (1) B.11 API (2) B.6 Eclipse B.6.1 workspace Windows C:YpleiadesYeclipseYeclipse.exe Eclipse ( B.8 ) Eclipse eclipse.exe (K) ( B.13 ) Eclipse ( B.14 ) MacOS Eclipse LaunchPad Windows

299 Apx.18 B Eclipse B.12 API B.13 Eclipse B.14 Eclipse Windows workspace C:Yjava ( B.15 ) OK Eclipse MacOS workspace Windows

300 B.6 Eclipse Apx.19 B.15 ( B.16 ) (H) (W) B.16 Eclipse (W) (V) ( B.17 B.18 ) B.17 B.18

301 Apx.20 B Eclipse B.6.2 (F) (N) Java Java (P) ( B.19 B.20 ) JRE Java JavaSE-1.8 Java Eclipse Java B.19 Java (1) B.20 Java (2) (N) B.6.3 API Windows MacOS Eclipse Java JRE JRE(J) java 8 (MacOS Java SE 8) (E) ( B.21 ) JRE JRE JRE Ctrl+A (MacOS Command+A) Javadoc ( B.22 ) Javadoc URL

302 B.6 Eclipse Apx.21 B.21 Javadoc (1) B.22 Javadoc (2) B.23 Javadoc (3) B.24 Javadoc (4) Windows C:YpleiadesYE Yapi MacOS E ( B.23 ) ( B.24 )

303 Apx.22 B Eclipse B.6.4 Eclipse Eclipse 6 Java Windows OS MS ( ) ( ) ( ) MS (MacOS Eclipse ) Java Java... OK ( B.25 ) B.25 Font

304 B.6 Eclipse Apx OK Eclipse Eclipse MacOS Eclipse eclipse.ini MacBookAir Eclipse.app Contents/Eclipse/eclipse.ini -Dorg.eclipse.swt.internal.carbon.smallFonts eclipse MacOS Eclipse (Windows -clean ) Applications/Eclipse.app/Contents/MacOS/eclipse -clean Eclipse Windows MacOS Eclipse ( B.26 ) 2 3 ( ) B.26 Eclipse

305 Apx.24 B Eclipse Windows MacOS Eclipse Java B.27 // ${todo ( B.27 ) B.27 Eclipse 2 ( B.28 ) 80 Windows MacOS Eclipse Java / Eclipse

306 B.7 Eclipse Apx.25 B.28 / OK B.7 Eclipse Eclipse -clean Eclipse Windows Eclipse eclipse.exe (C:YpleiadesYeclipse) eclipse.exe -clean.cmd ( B.29 ) MacOS Eclipse -clean /Appications/Eclipse.app/Contents/MacOS/eclipse -clean Eclipse C:Yjava c:ypleiades C:Yjava Eclipse

307 Apx.26 B Eclipse B.29 eclipse.exe -clean.cmd... C:Yjava

308 Apx.27 C javadoc C.1 Javadoc Java API ( C.1 ) Javadoc (Java ) Javadoc Sun Microsystems Java HTML API C.1 API C.2 package sectionc01; // public class Circle { C.1 Circle.java ( )

309 Apx.28 C javadoc // private double radius; // private Point center; // public Circle( Point center, double r ) { this.center = center; this.radius = r; // public Circle( double x, double y, double r ) { this.center = new Point(x, y); this.radius = r; // public double area() { return Math.PI * Math.pow( this.radius, 2 ); // public double circumference() { return 2 * Math.PI * this.radius; // true public boolean includes( Point p ) { if( p.distance( this.center ) <= this.radius ) { return true; else { return false; C.2 Point.java ( ) package sectionc01; // public class Point { // private double x; // private double y; // Point( double x, double y ) { this.x = x; this.y = y; // p public double distance( Point p ) { return Math.sqrt( Math.pow( this.x - p.x, 2 ) + Math.pow( this.y - p.y, 2 ) ); javadoc (/** */ ) Eclipse

310 C.2 Apx.29 / //\s*(.+) /** $1 */ // \s 0 * (.+) /** */ (^^;) Javadoc section01 Javadoc Javadoc sectionc01 2 Circle.java Point.java Javadoc Private ( doc ) URL URL index.html Web Javadoc Javadoc Javadoc HTML HTML ( <br> ) Javadoc C.3 Circle.java ( ) package sectionc01; /** 1.0 */ public class Circle { /** */ private double radius; /** */ private Point center; /** center <br>null r <br> */ public Circle(Point center, double r) { this.center = center; this.radius = r; /** x y r <br> */ public Circle(double x, double y, double r) { this.center = new Point(x, y); this.radius = r; /**

311 Apx.30 C javadoc */ public double area() { return Math.PI * Math.pow( this.radius, 2 ); /** */ public double circumference() { return 2 * Math.PI * this.radius; /** true <br> * p <br>null true false */ public boolean includes( Point p ) { if( p.distance( this.center ) <= this.radius ) { return true; else { return false; C.4 Point.java ( ) package sectionc01; /** 1.0 */ public class Point { /** */ private double x; /** */ private double y; /** x y */ Point( double x, double y ) { this.x = x; this.y = y; /** p other <br>null */ double distance( Point other ) { return Math.sqrt( Math.pow( this.x - other.x, 2 ) + Math.pow( this.y - other.y, 2 ) ); section01

312 C.3 Javadoc C.3 Javadoc Java /** Javadoc @version {@link Sample01#getX() getx Java 8 API

313 Apx.32 D debug D.1 (debugging: ) Eclipse ( D.1 ) D.1 Debug mode D.2 ( D.2 ) D.2.1 Java

314 D.2 Apx.33 D.2 Debug perspective D.2.2 D.2.3 D.2.4 D.2.5

新・明解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入門編

解きながら学ぶJava入門編 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

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

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 tutimura@mist.i.u-tokyo.ac.jp kaneko@ipl.t.u-tokyo.ac.jp 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プログラミング -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

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

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2  2 B R b I 4 003 4 30 1 ASCII ( ) 0 17 0 NUL 16 DLE SP 0 @ P 3 48 64 80 96 11 p 1 SOH 17 DC1! 1 A Q a 33 49 65 81 97 113 q STX 18 DC " B R b 34 50 66 8 98 114 r 3 ETX 19 DC3 # 3 C S c 35 51 67 83 99 115 s 4 EOT

More information

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

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

More information

¥ƥ­¥¹¥ȥ¨¥ǥ£¥¿¤λȤ¤˽

¥ƥ­¥¹¥ȥ¨¥ǥ£¥¿¤λȤ¤˽ : 2010 2 14 1 MS Word.doc (MS Word 2003 ).docx (MS Word 2007 ) Word Windows.txt MS Word Word Word Word Excel Word 1 Word Word Word MS Word MS Word MS Word Word Windows MS Word MS Word Word Windows.txt

More information

JavaプログラミングⅠ

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

More information

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D II 8 2003 11 12 1 6 ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 Daisuke 8 =>. 73 Daisuke 35 Hiroshi 64 Ichiro 87 Junko

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

JavaプログラミングⅠ

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

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

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

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 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

10

10 2: http://www7.bpe.es.osaka-u.ac.jp/~kota/classes/jse.html kota@fbs.osaka-u.ac.jp 10 : 0 1 2 n 2 n 0 1 1 0 1 0 0 1 (2) = 105 1 = 8 1 2 8 = 256 0 9 105 i 106 j 256 2 1 #include int main(void)

More information

Bar-Code_GUIDE

Bar-Code_GUIDE JPN 1 2 1 1-1 1-2 2 2-1 2-2 2-3 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 ' K ' X 5 T 3 O 0 1 2 3 4 K : N 01 23 1 2 3 4 5 6 7 8 9 10

More information

バーコードプリントガイド

バーコードプリントガイド CR-HIT/HIW/MEO/MEQ/MER/MET JPN 1 2 1 1-1 1-2 2 2-1 2-2 2-3 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 ' K ' X 5 T 3 O 0 1 2 3 4 K : N 01

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 (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

1: JX-model XML File Package Import Class Intf Ctor Method SInit Field Param Local ExtdOpt ImplOpt ThrwOpt Members QName Type Stmt Label Expr ident li

1: JX-model XML File Package Import Class Intf Ctor Method SInit Field Param Local ExtdOpt ImplOpt ThrwOpt Members QName Type Stmt Label Expr ident li Sapid JX-model ver. 1.3.13 2003 2 27 1 JX-model Java XML JX-model JX-model Java (Java 2 ver. 1.4) 20 7 JX-model 1 ^ $ Child nodes JX-model / ( ) JX-model @ @id @sort 1.1 File File JX-model XML /Package,

More information

untitled

untitled Java 1 1 Java 1.1 Java 1.2 Java JavaScript 2 2.1 2.2 2.3 Java VM 3 3.1 3.2 3.3 3.4 4 Java 4.1 Java 4.2 if else 4.3 switch case 4.4 for 4.5 while 4.6 do-while 4.7 break, continue, return 4.8 try-catch-finally

More information

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B 2 p.1 2 Java Java JDK Sun Microsystems Oracle JDK javac Java java JVM IDESun Microsystems Oracle NetBeans, IBM 1 Eclipse 2, JetBrains IntelliJ IDEA IDE GUI JDK Java 2.1 Hello World! 2.1.1 Java 2.1.1 GUI

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

Java 2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q 2.

Java 2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q 2. Java 2 p.1 2 Java Java JDK Sun Microsystems Oracle JDK javac Java java JVM appletviewer IDE Sun Microsystems NetBeans, IBM 1 Eclipse 2 IDE GUI JDK Java 2.1 Hello World! 2.1.1 Java 2.1.1 Hello World Emacs

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 は現在 事務処理計算用プログラミング言語として開発された 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

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 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

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B 2 p.1 2 Java Java JDK Sun Microsystems Oracle JDK javac Java java JVM appletviewer IDESun Microsystems NetBeans, IBM 1 Eclipse 2 IDE GUI JDK Java 2.1 Hello World! 2.1.1 Java 2.1.1 Hello World Emacs Hello0.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

PPS40511.PDF

PPS40511.PDF IBM Infoprint 1000J IBM PAGES ( CODE128 / QR CODE / PDF417 ) Rev. 1.1 2004/06/17-1 - 1. (ESX40) 3 2. ESX42 7 Code128 8 13 PDF417 15 IBM Infoprint 1000J (IP1316J, IP1336J, IP1356J) IBM PAGES IBM PC CODE128,

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

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B 2 p.1 2 Java Java JDK Sun Microsystems Oracle JDK javac Java java JVM appletviewer IDESun Microsystems NetBeans, IBM 1 Eclipse 2 IDE GUI JDK Java 2.1 Hello World! 2.1.1 Java 2.1.1 Hello World Emacs Hello0.java

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

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

I. (i) Java? (A). 2Apples (B). Vitamin-C (C). Peach21 (D). Pine_Apple (ii) Java? (A). Java (B). Java (C). Java (D). JavaScript Java JavaScript Java (i

I. (i) Java? (A). 2Apples (B). Vitamin-C (C). Peach21 (D). Pine_Apple (ii) Java? (A). Java (B). Java (C). Java (D). JavaScript Java JavaScript Java (i 12 7 27 10:30 12:00 I. I VI II. III. IV. ( a d) V. VI. 80 100 60 : this==null, T == N A ActionListener A addactionlistener C class D actionperformed E ActionEvent G getsource I implements J JApplet K KeyListener

More information

JavaプログラミングⅠ

JavaプログラミングⅠ Java プログラミング Ⅱ 10 回目への出力 ストリーム ストリームとは入力または出力の流れをもつデータの連鎖ですストリームは異なる機器からのデータを統一的に扱う考え方です 文字ストリーム 文字ベースの入出力をサポートします - Unicode(Java 内部 ) とローカルコード (Java 外部 ) の変換が行われ プログラマはその違いを意識しなくてすみます バイトストリームバイナリデータの入出力をサポートします

More information

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B 2 p.1 2 Java Java JDK Sun Microsystems JDK javac Java java JVM appletviewer IDESun Microsystems NetBeans, IBM 1 Eclipse 2 IDE GUI JDK Java 2.1 Hello World! 2.1.1 Java 2.1.1 Hello World Emacs Hello0.java

More information

System.out.println("char : " + (int)character.min_value + "~" + (int)character.max_value); System.out.println("float : " + Float.MIN_VALUE + "~" + Flo

System.out.println(char :  + (int)character.min_value + ~ + (int)character.max_value); System.out.println(float :  + Float.MIN_VALUE + ~ + Flo 変数と演算子 2 前回は標準出力を行う方法として println print printf について学習した その際 変数についても少し触れたが 今回はより詳しく解説していく また 演算子や標準入力 乱数の生成についても解説する 変数プログラムでデータを取り扱う場合には対象となるデータを保存する必要がでてくる このデータを保存する場所のことを 変数 と呼び 変数を説明する上ではよく データを入れておく箱

More information

Q&A集

Q&A集 & ver.2 EWEB-3C-N080 PreSerV for Web MapDataManager & i 1... 1 1.1... 1 1.2... 2 1.3... 6 1.4 MDM. 7 1.5 ( )... 9 1.6 ( )...12 1.7...14 1.8...15 1.9...16 1.10...17 1.11...18 1.12 19 1.13...20 1.14...21

More information

1/8 ページ Java 基礎文法最速マスター Java Javaの文法一覧です 他の言語をある程度知っている人はこれを読めばJavaの基礎をマスターしてJavaを書くことができるようになっています 簡易リファレンスとしても利用できると思いますので これは足りないと思うものがあれば教えてください 1. 基礎 class の作成プログラムはclassに記述します たとえばSampleという名前のclassを作る場合

More information

2 p.2 2 Java > javac Hello0.java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.j

2 p.2 2 Java > javac Hello0.java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.j 2 p.1 2 Java Java JDK Sun Microsystems Oracle JDK javac Java java JVM appletviewer IDESun Microsystems Oracle NetBeans, IBM 1 Eclipse 2, JetBrains IntelliJ IDEA IDE GUI JDK Java 2.1 Hello World! 2.1.1

More information

untitled

untitled 2004845 PKIUTF8String Part1: UTF8String UTF8String PKI UTF8String UTF8String 2 (1) ( ) A, ü, [ ] [ ] ASCII JIS X2013 Unicode(ISO 10646) ( )( ) Unicode A U+0041 U+007F 3 (2) u ü ( )( ) ( )(

More information

Java プログラミング Ⅰ 3 回目変数 変数 変 数 一時的に値を記憶させておく機能型 ( データ型 ) と識別子をもつ 2 型 ( データ型 ) 変数の種類型に応じて記憶できる値の種類や範囲が決まる 型 値の種類 値の範囲 boolean 真偽値 true / false char 2バイト文

Java プログラミング Ⅰ 3 回目変数 変数 変 数 一時的に値を記憶させておく機能型 ( データ型 ) と識別子をもつ 2 型 ( データ型 ) 変数の種類型に応じて記憶できる値の種類や範囲が決まる 型 値の種類 値の範囲 boolean 真偽値 true / false char 2バイト文 Java プログラミング Ⅰ 3 回目変数 変数 変 数 一時的に値を記憶させておく機能型 ( データ型 ) と識別子をもつ 2 型 ( データ型 ) 変数の種類型に応じて記憶できる値の種類や範囲が決まる 型 値の種類 値の範囲 boolean 真偽値 true / false char 2バイト文字 0x0000 ~ 0xffff byte 1バイト整数 - 2 8 ~ 2 8-1 short 2バイト整数

More information

1 Java Java GUI , 2 2 jlabel1 jlabel2 jlabel3 jtextfield1 jtextfield2 jtextfield3 jbutton1 jtextfield1 jtextfield2 jtextfield3

1 Java Java GUI , 2 2 jlabel1 jlabel2 jlabel3 jtextfield1 jtextfield2 jtextfield3 jbutton1 jtextfield1 jtextfield2 jtextfield3 1 2 2 1 2 2.1.................................................... 2 2.2.................................................... 2 2.3........................................ 2 2.4....................................................

More information

PR300 電力モニタ 通信インタフェース (RS-485通信,Ethernet通信)

PR300 電力モニタ 通信インタフェース (RS-485通信,Ethernet通信) User s Manual 1 2 3 1 2 3 Ethernet 1 2 3 4 Ethernet (ST-NO) (PCLK1) (PCLK2) (COMM) (M ASC) (M RTU) (M TCP) (RS-485) (B-RT) (PR201) (NONE) (PRI) (EVEN) (ODD) (STP) (DLN) (RS-485) (Ethernet) (IP-1)

More information

I HTML HashMap (i) (ii) :.java import java.net.*; import java.io.*; import java.util.hashmap; public class SimpleStopWatch { public static voi

I HTML HashMap (i) (ii) :.java import java.net.*; import java.io.*; import java.util.hashmap; public class SimpleStopWatch { public static voi II Java 10 2 12 10:30 12:00 I. I III II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K KeyListener J JApplet L addmouselistener M MouseListener

More information

2016 年度 JAVA 講座第六週目 目次 パッケージ... 2 パッケージの作成... 2 パッケージの使用方法... 3 異なるパッケージ同名クラスの宣言... 4 パッケージの側面から見たアクセス修飾子... 4 ラッパークラス... 5 ラッパークラス利用法:キャスト... 5 ラッパーク

2016 年度 JAVA 講座第六週目 目次 パッケージ... 2 パッケージの作成... 2 パッケージの使用方法... 3 異なるパッケージ同名クラスの宣言... 4 パッケージの側面から見たアクセス修飾子... 4 ラッパークラス... 5 ラッパークラス利用法:キャスト... 5 ラッパーク 2016 年度 JAVA 講座第六週目 目次 パッケージ... 2 パッケージの作成... 2 パッケージの使用方法... 3 異なるパッケージ同名クラスの宣言... 4 パッケージの側面から見たアクセス修飾子... 4 ラッパークラス... 5 ラッパークラス利用法:キャスト... 5 ラッパークラス利用法:ArrayList... 5 例外:Exception... 6 ぬるぽ... 6 例外処理:try-catch-finaly...

More information

(Java/FX ) Java CD Java version Java VC++ Python Ruby Java Java Eclipse Java Java 3 Java for Everyone 2 10 Java Midi Java JavaFX Shape Canvas C

(Java/FX ) Java CD Java version Java VC++ Python Ruby Java Java Eclipse Java Java 3 Java for Everyone 2 10 Java Midi Java JavaFX Shape Canvas C (Java/FX ) Java CD Java version 10.0.1 Java VC++ Python Ruby Java Java Eclipse Java Java 3 Java for Everyone 2 10 Java Midi Java JavaFX Shape Canvas Canvas Eclipse Eclipse M... 1 javafx e(fx)clipse 3.0.0

More information

II 1 p.1 1 Servlet 1.1 Web Web WWW HTML CGI Common Gateway Interface Web HTML Web Web CGI CGI CGI Perl, PHP C JavaScript Web CGI HTML 1.2 Servlet Java

II 1 p.1 1 Servlet 1.1 Web Web WWW HTML CGI Common Gateway Interface Web HTML Web Web CGI CGI CGI Perl, PHP C JavaScript Web CGI HTML 1.2 Servlet Java II 1 p.1 1 Servlet 1.1 Web Web WWW HTML CGI Common Gateway Interface Web HTML Web Web CGI CGI CGI Perl, PHP C JavaScript Web CGI HTML 1.2 Servlet Java Servlet Servlet CGI Web CGI Java Java JVM Java CGI

More information

http://www.impressjapan.jp/ Copyright 2014 Socius Japan, Inc. All rights reserved. Java SE 7 Java SE 7 OCJ-P Bronze SE 7 Java Java SE 7 Bronze OCJ-P Silver SE 7 Java Java SE 7 Programmer I OCJ-P Gold

More information

Thread

Thread 14 2013 7 16 14.1....................................... 14 1 14.2 Thread................................... 14 1 14.3............................. 14 5 14.4....................................... 14 10

More information

Java講座

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

More information

1 1 1............................ 1 2............................ 1 3 Java........................ 2 4........................ 3 2 4 1................

1 1 1............................ 1 2............................ 1 3 Java........................ 2 4........................ 3 2 4 1................ Java 1 1 1............................ 1 2............................ 1 3 Java........................ 2 4........................ 3 2 4 1......................... 4 2.......................... 4 3....................

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

スライド 1

スライド 1 Web プログラミング 2 7. JSP と Servlet による Web プログラミング概要 ( 復習 )Web アプリケーションの実現方式 : 授業で扱う範囲 SSI (Server Side Include) C-Shellなど JSP (Java Server Pages) PHP など Web ブラウザ Internet Done Web サーバ Done JavaApplet JavaScript

More information

Web 1 p.2 1 Servlet Servlet Web Web Web Apache Web Servlet JSP Web Apache Tomcat Jetty Apache Tomcat, Jetty Java JDK, Eclipse

Web 1 p.2 1 Servlet Servlet Web Web Web Apache Web Servlet JSP Web Apache Tomcat Jetty Apache Tomcat, Jetty Java JDK, Eclipse Web 1 p.1 1 Servlet 1.1 Web Web WWW HTML CGI Common Gateway Interface Web HTML Web Web CGI CGI CGI Perl, PHP C Java Applet JavaScript Web CGI HTML 1.2 Servlet Java Servlet Servlet CGI Web CGI 1 Java Java

More information

基礎情報処理 I (文字型)

基礎情報処理 I (文字型) プログラミング 1 ( 文字型 ) program character1; a,b,c: char; writeln('1 文字づつ3 文字入力してください :'); readln(a); readln(b); readln(c); write(a); write(b); write(c); writeln; a,b,c:char; a:='a'; b:='b'; c:='c'; write(a);

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

Make the Future Java FY13 PPT Template

Make the Future Java FY13 PPT Template 1 - 目指せ脱初心者!! - クイズで覚える Java プログラミング 日本オラクル株式会社オラクルユニバーシティ #jdt2014_e3 2 以下の事項は 弊社の一般的な製品の方向性に関する概要を説明するものです また 情報提供を唯一の目的とするものであり いかなる契約にも組み込むことはできません 以下の事項は マテリアルやコード 機能を提供することをコミットメント ( 確約 ) するものではないため

More information

1: Preference Display 1 package sample. pref ; 2 3 import android. app. Activity ; 4 import android. content. Intent ; 5 import android. content. Shar

1: Preference Display 1 package sample. pref ; 2 3 import android. app. Activity ; 4 import android. content. Intent ; 5 import android. content. Shar Android 2 1 (Activity) (layout strings.xml) XML Activity (Intent manifest) Android Eclipse XML Preference, DataBase, File 3 2 Preference Preference Preference URL:[http://www.aichi-pu.ac.jp/ist/lab/yamamoto/android/android-tutorial/tutorial02/tutorial02.pdf]

More information

コーディング基準.PDF

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

More information

BlueJ 2.0.1 BlueJ 2.0.x Michael Kölling Mærsk Institute University of Southern Denmark Toin University of Yokohama Alberto Palacios Pawlovsky 17 4 4 3 1 5 1.1 BlueJ.....................................

More information

Microsoft Word - java a.doc

Microsoft Word - java a.doc 4 入出力の基本として ディスプレイへの文字出力と キーボードからの文字入力の方法を学びます 入出力とは何か 標準出力 標準入力 43 4.1. 入出力とは プログラムと外部機器の間でデータをやりとりすることをいいます プログラムから出て行く方向が 出力 プログラムに入って来る方向が 入力 です 出力 外部機器 プログラム 入力 外部機器 外部機器はさまざまな種類があります 出力を行うには ディスプレイ

More information

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 条件判断文 3 switch 文 switch 文式が case の値と一致した場合 そこから直後の break; までを処理し どれにも一致しない場合 default; から直後の break; までを処理する 但し 式や値 1

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 条件判断文 3 switch 文 switch 文式が case の値と一致した場合 そこから直後の break; までを処理し どれにも一致しない場合 default; から直後の break; までを処理する 但し 式や値 1 Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 条件判断文 3 switch 文 switch 文式が case の値と一致した場合 そこから直後の までを処理し どれにも一致しない場合 default; から直後の までを処理する 但し 式や値 1 値 2は整数または文字である switch( 式 ) case 値 1: // コロン : です セミコロン ; と間違えないように!!

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演習(2) -- 簡単なプログラム --

Java演習(2)   -- 簡単なプログラム -- Java public class Hello Hello (class) (field)... (method)... Java main Hello World(Hello.java) public class Hello { public static void main(string[ ] args) { public() (package) Hello World(Hello.java)

More information

Quick Sort 計算機アルゴリズム特論 :2017 年度 只木進一

Quick Sort 計算機アルゴリズム特論 :2017 年度 只木進一 Quick Sort 計算機アルゴリズム特論 :2017 年度 只木進一 2 基本的考え方 リスト ( あるいは配列 )SS の中の ある要素 xx(pivot) を選択 xx より小さい要素からなる部分リスト SS 1 xx より大きい要素からなる部分リスト SS 2 xx は SS 1 または SS 2 に含まれる 長さが 1 になるまで繰り返す pivot xx の選び方として 中央の要素を選択すると効率が良い

More information

MS240 JISⅡ半角カナ文字データを含むカード読み取り設定例

MS240 JISⅡ半角カナ文字データを含むカード読み取り設定例 MS240 磁気ストライプストライプカードリーカードリーダ JISⅡ 半角カナカナ文字文字データデータを含むカードカード読み取り設定例 ユニテック ジャパン株式会社 Ver1.0 目次 1. JIS-II 半角カタカナをデータに含む磁気カードについて...3 1.1. JIS-II カード規格とは...3 1.2. 半角カタカナデータを含む JIS-II コードの仕組み...3 1.3 半角カタカナデータを含む

More information

第 1 章 Java 言語について ( オブジェクト指向, 変数の扱い方, 繰り返し条件と条件分岐 ) Java 言語の概要とオブジェクト指向, 変数の扱い方, 繰り返し条件と条件分岐について理解し, プログラム作成を行う 1.1 Java 言語の概要 JAVA は Sun Microsystems

第 1 章 Java 言語について ( オブジェクト指向, 変数の扱い方, 繰り返し条件と条件分岐 ) Java 言語の概要とオブジェクト指向, 変数の扱い方, 繰り返し条件と条件分岐について理解し, プログラム作成を行う 1.1 Java 言語の概要 JAVA は Sun Microsystems 第 1 章 Java 言語について ( オブジェクト指向, 変数の扱い方, 繰り返し条件と条件分岐 ) Java 言語の概要とオブジェクト指向, 変数の扱い方, 繰り返し条件と条件分岐について理解し, プログラム作成を行う 1.1 Java 言語の概要 JAVA は Sun Microsystems が開発したプログラミング言語であり, たとえば, 一般ユーザが使うアプリケーションを始めとして, ネットワークを介したコミュニケーションツールや

More information

H02_ROM_ indd

H02_ROM_ indd ESC/P ESC/P IBM 5557-H02 ESC/P ESC/P IBM 5557-H02 ESC/P VP5074 IBM 5579/5577/5573 ESC/P ESC/P ESC/P ESC/P IBM 5557-H02 ESC/P ESC/P ESC/P / CD ESC/P ESC/P 289 IBM 5557-H02 / ESC R ESC t ESC X ESC / ESC

More information

ohp02.dvi

ohp02.dvi 172 2017.7.16 1 ? X A B A X B ( )? IBMPL/I FACOM PL1 ( ) X 2 ( ) 3 2-0 ( ) ( ) ( ) (12) ( ) (112) 31) 281 26 1 4 (syntax) (semantics) ( ) 5 BNF BNF(Backus Normal Form) Joun Backus (grammer) English grammer

More information

パソコン機能ガイド

パソコン機能ガイド PART12 ii iii iv v 1 2 3 4 5 vi vii viii ix P A R T 1 x P A R T 2 xi P A R T 3 xii xiii P A R T 1 2 3 1 4 5 1 6 1 1 2 7 1 2 8 1 9 10 1 11 12 1 13 1 2 3 4 14 1 15 1 2 3 16 4 1 1 2 3 17 18 1 19 20 1 1

More information

パソコン機能ガイド

パソコン機能ガイド PART2 iii ii iv v 1 2 3 4 5 vi vii viii ix P A R T 1 x P A R T 2 xi P A R T 3 xii xiii P A R T 1 2 1 3 4 1 5 6 1 2 1 1 2 7 8 9 1 10 1 11 12 1 13 1 2 3 14 4 1 1 2 3 15 16 1 17 1 18 1 1 2 19 20 1 21 1 22

More information

Microsoft PowerPoint ppt

Microsoft PowerPoint ppt 独習 Java ( 第 3 版 ) 6.7 変数の修飾子 6.8 コンストラクタの修飾子 6.9 メソッドの修飾子 6.10 Object クラスと Class クラス 6.7 変数の修飾子 (1/3) 変数宣言の直前に指定できる修飾子 全部で 7 種類ある キーワード final private protected public static transient volatile 意味定数として使える変数同じクラスのコードからしかアクセスできない変数サブクラスまたは同じパッケージ内のコードからしかアクセスできない変数他のクラスからアクセスできる変数インスタンス変数ではない変数クラスの永続的な状態の一部ではない変数不意に値が変更されることがある変数

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

tkk0408nari

tkk0408nari SQLStatement Class Sql Database SQL Structured Query Language( ) ISO JIS http://www.techscore.com/tech/sql/02_02.html Database sql Perl Java SQL ( ) create table tu_data ( id integer not null, -- id aid

More information

デジタル表現論・第4回

デジタル表現論・第4回 デジタル表現論 第 4 回 劉雪峰 ( リュウシュウフォン ) 2016 年 5 月 2 日 劉 雪峰 ( リュウシュウフォン ) デジタル表現論 第 4 回 2016 年 5 月 2 日 1 / 14 本日の目標 Java プログラミングの基礎 出力の復習 メソッドの定義と使用 劉 雪峰 ( リュウシュウフォン ) デジタル表現論 第 4 回 2016 年 5 月 2 日 2 / 14 出力 Systemoutprint()

More information

VB.NETコーディング標準

VB.NETコーディング標準 (C) Copyright 2002 Java ( ) VB.NET C# AS-IS extremeprogramming-jp@objectclub.esm.co.jp bata@gold.ocn.ne.jp Copyright (c) 2000,2001 Eiwa System Management, Inc. Object Club Kenji Hiranabe02/09/26 Copyright

More information

ALG ppt

ALG ppt 2012 6 21 (sakai.keiichi@kochi-tech.ac.jp) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2012/index.html 1 l l O(1) l l l 2 (123 ) l l l l () l H(k) = k mod n (k:, n: ) l l 3 4 public class MyHashtable

More information

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU 1. 1.1. 1.2. 1 PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU 2. 2.1. 2 1 2 C a b N: PC BC c 3C ac b 3 4 a F7 b Y c 6 5 a ctrl+f5) 4 2.2. main 2.3. main 2.4. 3 4 5 6 7 printf printf

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

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

A-GAGE High - Resolution MINI ARRAY Instruction Manual Printed in Japan J20005M

A-GAGE High - Resolution MINI ARRAY Instruction Manual     Printed in Japan J20005M A-GAGE High - Resolution MINI ARRAY Instruction Manual E-mail : mail@bannerengineering.co.jp http://www.bannerengineering.com Printed in Japan J20005M4 page 2 page 3 page 4 page 5 page 6 page 7 page 8

More information

: : : TSTank 2

: : : TSTank 2 Java (8) 2008-05-20 Lesson6 Lesson5 Java 1 Lesson 6: TSTank1, TSTank2, TSTank3 java 2 car1 car2 Car car1 = new Car(); Car car2 = new Car(); car1.setcolor(red); car2.setcolor(blue); car2.changeengine(jet);

More information

Javaハンドブック

Javaハンドブック Java ハンドブック (C) Copyright 2002-2004 ( 株 ) 永和システムマネジメント オブジェクト倶楽部天野勝 m-amano@esm.co.jp 初版 2002 年 11 月 1 日第 2 版 2002 年 11 月 21 日第 3 版 2004 年 11 月 19 日 オリジナル :http://www.objectclub.jp/technicaldoc/java/javahandbook.doc

More information

Programming-C-9.key

Programming-C-9.key プログラミングC 第9回 例外 スレッド 白石路雄 2 finally try{ ( 例外が発生するかもしれない処理 ) catch(exception のクラス名 e){ ( 例外が発生した時の処理 ) finally{ ( 例外の発生の有無に関わらず 必ず行う処理 ) 3 Integer.parseInt() NumberFormatException

More information

untitled

untitled 2011 6 20 (sakai.keiichi@kochi-tech.ac.jp) http://www.info.kochi-tech.ac.jp/k1sakai/lecture/alg/2011/index.html tech.ac.jp/k1sakai/lecture/alg/2011/index.html html 1 O(1) O(1) 2 (123) () H(k) = k mod n

More information

PowerPoint プレゼンテーション

PowerPoint プレゼンテーション 32786~32767 2147483648~2147483647 9223372036854775808~9223372036854775807 ±10 38 ~10 38 ±10 308 ~10 308 public static void main(string[] args) { int a; double b; String s; a = 42; b = 3.1415926535; s =

More information

. IDE JIVE[1][] Eclipse Java ( 1) Java Platform Debugger Architecture [5] 3. Eclipse GUI JIVE 3.1 Eclipse ( ) 1 JIVE Java [3] IDE c 016 Information Pr

. IDE JIVE[1][] Eclipse Java ( 1) Java Platform Debugger Architecture [5] 3. Eclipse GUI JIVE 3.1 Eclipse ( ) 1 JIVE Java [3] IDE c 016 Information Pr Eclipse 1,a) 1,b) 1,c) ( IDE) IDE Graphical User Interface( GUI) GUI GUI IDE View Eclipse Development of Eclipse Plug-in to present an Object Diagram to Debug Environment Kubota Yoshihiko 1,a) Yamazaki

More information

II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K

II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K II Java 09 2 13 10:30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K KeyListener J JApplet L addmouselistener M MouseListener

More information

1... 1 2... 1 1... 1 2... 2 3... 2 4... 4 5... 4 6... 4 7... 22 8... 22 3... 22 1... 22 2... 23 3... 23 4... 24 5... 24 6... 25 7... 31 8... 32 9... 3

1... 1 2... 1 1... 1 2... 2 3... 2 4... 4 5... 4 6... 4 7... 22 8... 22 3... 22 1... 22 2... 23 3... 23 4... 24 5... 24 6... 25 7... 31 8... 32 9... 3 3 2620149 3 6 3 2 198812 21/ 198812 21 1 3 4 5 JISJIS X 0208 : 1997 JIS 4 JIS X 0213:2004 http://www.pref.hiroshima.lg.jp/site/monjokan/ 1... 1 2... 1 1... 1 2... 2 3... 2 4... 4 5... 4 6... 4 7... 22

More information

Exam : 1z1-809-JPN Title : Java SE 8 Programmer II Vendor : Oracle Version : DEMO Get Latest & Valid 1z1-809-JPN Exam's Question and Answers 1 from Ac

Exam : 1z1-809-JPN Title : Java SE 8 Programmer II Vendor : Oracle Version : DEMO Get Latest & Valid 1z1-809-JPN Exam's Question and Answers 1 from Ac Actual4Test http://www.actual4test.com Actual4test - actual test exam dumps-pass for IT exams Exam : 1z1-809-JPN Title : Java SE 8 Programmer II Vendor : Oracle Version : DEMO Get Latest & Valid 1z1-809-JPN

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