2110101 : computer programmingsomchai/2110101/lectures/pdf/10… · ˚ s*˙ ˆ ˝ !˙ ubˇ ˝1# ˆ...

112
!" !" # $%&"'(&)# $)* +, !! "# !"#$%&'(&)'*+ $$$ #$%# -# $)&.& .& ) $ /0(0,&.& 0# $-. 1& 2&-2 .& /"&.&/"0, .& ,"' /03"&),0/'.& 0, 1,

Upload: others

Post on 26-Feb-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

������������������ ��������������

������������������������

����������������� ����������������������

��������������� ����������� �������������

���� ���!���"����������

� ���������� ���� ������������ ���� ���� !����������"�������� ���

����������� ������ ��

��������������� ����������� �������������

�����������

� �#$������%�&��" �'(&���)#$�)�*����� ������������ ��������� ����������� �������� ���������� ������ �������

� ��+����,� ����� �

� ��������� ��� ��� ����

� ����������!�!��� ����������������������������� �� �����

� "#�� �� !"#$%������ &������'(�&����)������'� *+�������������

� $$$

��������������� ����������� �������������

� #$��%#�

� -#$�)����&�.&����.&�)�$�� /0(0,&�.&���0 #$��-.1&�2&-��2� �.&�/"���&�.&�/"��0,� �.&���,�" '�� /03��"&)�,��0��/�'�.&���0��,�1,

Page 2: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �������������

������&��'�����������()���� �*�+����*��,

� �'���&4������,��,-�) �������,�1�����5� '�*61,&4��,���,�.&�)�$�+,��,��75� ��*������*61,&4�),���,������8+,9 :�#�5� ��,/;,<����&4+��/;��1��.&� ���" ��#� �0��7�75� ���=����&4/(����(�� �>,�),/(��(��5

?@@ABCD&EFG&H& ��I,I��7�������� ���7��)��J������*61,)�� �0����2&KL& "#�,?@@ABCD&EFG&H& ��I,I��7�������� ���7��)��J������*61,)�� �0����2&KL& "#�,

��������������� ����������� �������������

-./01�2�34+5

%&'()*)+,-./)�� �012�����)34564)7����8#7

%&&()*)+,-./)9:;<)���)=$'')5)6$>&)7����)��$

��������������� ����������� �������������

/66/01�/�2�37�5

01)>?@44)2���)����5���5���)A)>5%45@$6)8#7�

2���)6)7��)�B)CDCEFG)6$%>H)���)CIJKDL;9)MFNC('H)02�� �����O�����)%4)�P

��������������� ����������� �������������

0889:�1;<8=>:?@

,-./�0�122%!�3 ,-45�0�122%!�333 ,-46�0�122%!�33!

,-46�0�7$8#

,-..�0�122%!�33

,-49�0�:#;�,<4= ,-4/�0�:#;�2%>8 ,-4.�0�:#;�33

,-4-�0�:#;�2?@A#B%! <55,�0�C?D!@:#;E>B!

������

<55<�0�$:#;

Page 3: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �������������

����������"�������������

� MFANGFAB& �#$�����%�&��" �'(&���)#$�)�*����� D@OPGFAB& �#$�)�$�+��&MFANGFAB&�.&���,��*�1,��,�7$�.&��,"/��

��������������� ����������� ��������������

AB?CDB?:

� QBRPAFS&TA@UBDDVRW&XRVP&4QTX5� ����������������!Q 7R��7�S?)7����?)���������� �����?)����T)����UTV)�

� ?BY@AZ� ���������������!�!������

� [\]� ����������������������)W��X��)��Y�� ��S)����S)Z�����!������ ��S)����)$$$[

ECFECF

:!"?@G:!"?@G 3HI3HI

��������������� ����������� ��������������

1:E>?B9�F?;G:@@HEI�JEH>

� �.&���,����"�.&�)�$��7$�.&��,"+��� �.&���,/"��" '�&4��,���,�.&�)�$�+,��,��75� ��(�����.&���,*��)0�,�#$,2� ��"�.&�)�$�

� ��O��2��\������ ����0�2����!����T)�� ���B������� �

� �7$���� -KLD])^DKL;NC?)-KLD])/D]DFEK?)-KLD])_LFEKJ.`a?.ab)cDEK?)_NK_<IF9D?)-da)^EeDF^/?)$$$

��������������� ����������� ��������������

1FJ

YVUA@^A@UBDD@A

Page 4: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ��������������

1FJ

<;9EfIgIh--

��������������� ����������� ��������������

�K���)��

��������������� ����������� ��������������

L:<;?M

� �'(��"�.&�)�$����.&���,*��&QTX� �'(*����).&���(������%�� �(0� �>,

� CI;K)CDCEFG� ���������� ��� � ����� ���������������������������������� �� ��!"#$%&�"''())�&(&%!*�

� iD9EKMIFG)iLEFIJD� �������+�� � ,�������� ���+-���./�0��1�������������2,����������� 3"!$�$4)56�7/8�9 6�/:/8�9 6�&(&%!*�);4'56�111

��������������� ����������� ��������������

L:<;?M

Page 5: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ��������������

/E8=>NO=>8=>�P:QHG:@

� �������(*����� ��Y�� ��S)����S)��Y�������S)����)��������S)Z���� �Z!�Z8�)�!������������SZ!�)�!�����������7�

� ������)0��#��)"�*����� ��X��)�!������ ��S)Z����)�T)�Z��

��������������� ����������� ��������������

R�*�S�*��T�'&�*�!�(U

� _VP&`&�,0�� �'�)�"��,)��)����&4_VRFAZ&NVWVP5� _ZPB&`&L&_VPD� G@AN&`&�.&�,�,&_ZPBD&�7$ �#$������%�/"�)�"��� QTX&H&abb?cde&fghicd� jk?&H&fhl?m� cFANNVDn&H&Lbime&ofbbA^Y� TAVRPBA&H&lbbN^Ve&Kb^^Y� p@qRN&H&rrgKscd&Kl`_VP&DPBAB@� ?@NBY&H&hls_^D

��������������� ����������� ��������������

����V���"��K $�����R���

j aI;KkFICDj a;K;h9EC<NLDFj ^DFiEKI])/EC<NLDFj ,DLeEFl)/EC<NLDFj ^DFiEKI])b;J;LI]).ii;iLIKL

��������������� ����������� ��������������

W:9HBXH9H>M

���� ������ �����������

�������� �������������������

�����!

�������"���� !#�$��!

���� %�������&'( )!��$��!

*���+,���� -�!)�$��!

�. /���0�+"��,���� --1!2�$��!

Page 6: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ��������������

����������������Y��2.-1Z-15

� QTX&H&abb?cd� ?BY@AZ

J KLM1:��/9:N�O,66:PQR

J PL�F%A@#�L:1�O,55:NH8R�<5SN

J L$8=!AA!�T@$U!�0�,�99:N

J ELVMI:�0�W5X

� [\]J YS1�;#@T�0�1SC��,/:N

J :?Z$A?@�0�,W[���4WPQ�#A�455\/55��5�<4""

J K?>ZT�;#@T�0�CE3��1>T$?�E?T!;�-.

J 71N�;#@T�0�]X,5H,55

J :?T!"�0�Y�-5�W/^BH8

>4?444)���

��������������� ����������� ��������������

[;\>DB?:

� ��"�.&�)�$��0��2&-6$�)�$�+��&MFANGFAB&�.&���,��*�1,��,�7$�.&��,"/��

� � �'(&D@OPGFAB&����/��+,&DBU@RNFAZDP@AFWB&4MFANNVDne&Qt`j]?e&tut`j]?5

� �((,.&�&4�#�&S@FN5&D@OPGFAB&�7$��������+,YFVR&YBY@AZ& �#$�+����,

��������������� ����������� ��������������

[;\>DB?:

� pZDPBY&D@OPGFAB� m<DFIL;KJ)iGiLDC)Wn;KMEei)c^?)o;KN5?)aI9)m_?)$$$[� _GiLDC)LEE]i)WIKL;g;FNi?)p;<qNKp;<?)rI9lN<qFDiLEFD?)$$$[� bDgD]E<CDKL)LEE]i)W9EC<;]DF?)MDrNJJDF?)$$$[� $$$

� k^^SVUFPV@R&D@OPGFAB� mkk;9D)WDCI;]?)eEFM)<FE9DiiEF?)i<FDIMi:DDL?)$$$[� ,DLeEFl)I<<];9IL;EKi)WrFEeiDF?)9:IL?)$$$[� +KLDFLI;KCDKL)WCEg;Di?)CNi;9i?)JICDi?)$$$[� $$$

��������������� ����������� ��������������

AB?CDB?:�]�[;\>DB?:

cFANGFAB

pZDPBY&p@OPGFAB

vBPG@An

k^^SVUFPV@R&p@OPGFAB

Page 7: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ��������������

^:X�_?;D@:?

��������������� ����������� ��������������

^:X�_;B?C

��������������� ����������� ��������������

[;\>DB?:

� ���w,�&D@OPGFAB&/"�"����� *7�,I�������� ���&4U@Y^qPBA&^A@WAFY5� Z������!��1#�!T)�����Us���B�����X�t�!��� ��7��S������������!��� ��7��S02�� ���7����T)�2��

� � �

� � �

� �

� �

��

0011101010101001010010101010101001001001

2@?_@#""!@8

;?"2>A!@�2@?_@#"8"#;`$Z!�;?T!8

public double sqrt(double a) { double x = 1.0;

for (int i = 0; i < 20; i++) { x = 0.5 * (x + a / x); } return x;}

��������������� ����������� ��������������

"�����������`���@;\>DB?:

� �.&��,"���������&4� !��5� �� ��������� !��� ����((*�1,��,��J7���/*� !��� �.&�I�����*�1,��,��J7�7$����((� �")�(���=������� ��(������/*I���

Page 8: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ��������������

����S*��

� ���������� !���u�B����1#�����

� �� ������ �2�������

j ��01�Y���T)��������j ��01�Y������7���v)���������Y�� ��S

� �����"Sj ����!���u�B����������X��

��������������� ����������� ��������������

����S*��

� ����((� ����T)��������)�������01� �������7���v)�����01� �����"S!T)���Q ������7�

� ���������"S��������X��

���� ������ ��

∑=

=�

��

��������������� ����������� ��������������

����S*��

import jlab.JLabIO;public class Stat { public static void main(final String[] args) { int n = JLabIO.readInt("Enter the number of data : "); if (n > 0) { double sum = 0.0; for (int i = 0; i < n; i++) { sum += JLabIO.readDouble("data #" + i + " = "); } System.out.println("-------------------"); System.out.println("mean = " + sum / n); System.out.println("-------------------"); } }}

import jlab.JLabIO;public class Stat { public static void main(final String[] args) { int n = JLabIO.readInt("Enter the number of data : "); if (n > 0) { double sum = 0.0; for (int i = 0; i < n; i++) { sum += JLabIO.readDouble("data #" + i + " = "); } System.out.println("-------------------"); System.out.println("mean = " + sum / n); System.out.println("-------------------"); } }}

������������������

���� � ������ �

Enter the number of data : 5data #0 = 2data #1 = 3data #2 = 4.6data #3 = 8data #4 = 4-------------------mean = 4.32-------------------

Enter the number of data : 5data #0 = 2data #1 = 3data #2 = 4.6data #3 = 8data #4 = 4-------------------mean = 4.32-------------------

00110111000111100111001111000111110001110011111111

00110111000111100111001111000111110001110011111111

��������������������

��������������� ����������� ��������������

�������� ���

� BNVP&H& *7�,I���"�����8������ ���� U@Y^VSB&H&���I����7$ *7�,+�� �>,��) �#$��� BxBUqPB&H&)�$���,��) �#$���7$/"�� NB_qW&H&���7$%�"���"*��I���

� ���+��&[RPBWAFPBN&^A@WAFYYVRWBRyVA@RYBRP&D@OPGFAB& �>, �#$��#�+,����w,�-�;�z����(( ('" )'�).&���(�����{BNVP`U@Y^VSB`BxBUqPB`NB_qW|

Page 9: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ��������������

a6BX

Page 10: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

������������������ ��������������

������������� ���

����������������� ����������������������

��������������� ����������� �����������

������

� �������������� �� ��!"��"�� �� ��#�$���"�� �� �� �%�"�� �&�'�" �� �($�� �'�&�� ���)*���" ���)*�+��� ��!"��"���)*��� �

��������������� ����������� �����������

��� ��� ��

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

�������������� ������������������������� ���� ���������� !

����������������"#�� ��$��������"�"���%&�'(���)����*+����,"�����

��������������,"+�����-�����.�(������,�#����$ �����,�#�"+��*+�������/)#��

��������������� ����������� �����������

��� �������������� !"##

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

�����������+����� ���01233���*#��01233�$)#�451167

�����/�������01233�451167����.�������/8��9���:��/"�� �������public class Hello1

Page 11: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

$!"##������������%&'()*

��;5<=6>��������"+������,������

�����/�������;5<=6>����.�������/8��9���:��/"���"��;5<=6>

��������������� ����������� �����������

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

+&'()*�����������,-����.�/0

������/���,"�����-"#��?�, ��� ���@��A35;B0616CD

���System.out.print��-��� ����.�������/8��,��E.�%

���println ,���������)��print�����*+����,"������ ��

��������������� ����������� �����������

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

��� �1 1�.�/0��0��

JLab>javac Hello1.java

JLab>java Hello1

Hello 2110101

JLab>

����������

������ �����

������������

�����������

��������������� ����������� �����������

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

%"23�%&'()*

��F2G2�12HC0=5I�E������$ �;2BC�;5<=6>����01233

���"��"+����,������E��&#�,�#�;2BC�;5<=6>����01233��"+�

Page 12: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

// 2110101 : Computer Programming// First Java Program

public class Hello1 {

// main method begins Java application execution public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }} // end of class Hello1

$)%%&3'

��J6;;5C<���&#�� ���KK��A�"����&L���������)#�D

������������!������06;;5C<��� ����������)#����-�

���"��06;;5C<�,&+����������8�"�,�������� ���)���&�

��������������� ����������� ������������

public class Hello1 { public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }}

public class Hello1 { public static void main(final String[] args) { System.out.print("Hello "); System.out.println("2110101"); }}

�����4����� ���5���6��06��7

public class Hello1{public static voidmain(final String[] args){System.out.print("Hello ");System.out.println ("2110101"); } }

public class Hello1{public static voidmain(final String[] args){System.out.print("Hello ");System.out.println ("2110101"); } }

� ��$"&�'�,���,��*���,��"'�* �����-!"���+&�, �($�&�'�� �",��,�"* ���,�"�,���-!"

-����������+,���������)��"�������������������������"��M

��������������� ����������� ������������

.������8���9:���;

********************************************************** p u b l i c c l a s s H e l l o 1 { ** ** PUBLIC STATIC VOID main(final String[] args) { ** ** System.out.print("Hello "); ** ** System.out.println("2110101"); ** ** } ** ** } **********************************************************

********************************************************** p u b l i c c l a s s H e l l o 1 { ** ** PUBLIC STATIC VOID main(final String[] args) { ** ** System.out.print("Hello "); ** ** System.out.println("2110101"); ** ** } ** ** } **********************************************************

��������������� ����������� ������������

���5,�.4��</�5���6��06���=>��4�

// 2110101 : Computer Programmingpublic class Hello1 { public static void main(final String[] args) { System.out.print("Hello "); System.out.print("2110101"); System.out.print('A'); }}

// 2110101 : Computer Programmingpublic class Hello1 { public static void main(final String[] args) { System.out.print("Hello "); System.out.print("2110101"); System.out.print('A'); }}

� ������������ ��� ��������������������������������� ��������������� !��"#������#��$�%&'���������� ��������#(�&)�����*+���!���&,���-$���.� ��$+�����#(�&)�����*+���!���&,���-$���/� ���������,��0

Page 13: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

���5,�.4��</�.�0�������:���1��

// 2110101 : Computer Programmingpublic Class Hello1 { public static void main(fina1 String[] args) { System.out.print("Hello); System.out.print("2110101"); Systen.out.print('A'); }}

// 2110101 : Computer Programmingpublic Class Hello1 { public static void main(fina1 String[] args) { System.out.print("Hello); System.out.print("2110101"); Systen.out.print('A'); }}

� �Class�� ���N��class A� ���N��0��"��/8�D� �fina1�� ���N��final A� ���N��"��/�/8������$��/��7D� �"Hello); � ���N��"Hello"); A�"��O��?�, �������D� �Systen�� ���N��System�A� ���N��;D

����-�-���N�����-�������-����P���������%)#��������"���/.�(��"+����-��E-��/�

��������������� ����������� ������������

��������.�/0��0��9����1016�0

� �+���$�.��� ����)*���" �/"�)*�+��*������+�)*�#�$��"���,��

public class PrintStars { public static void main(final String[] args) { System.out.println("*******"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("*******"); }}

public class PrintStars { public static void main(final String[] args) { System.out.println("*******"); System.out.println("* *"); System.out.println("* *"); System.out.println("* *"); System.out.println("*******"); }} JLab>javac PrintStars.java

JLab>java PrintStars******** ** ** ********JLab>

��������������� ����������� ������������

��� .�0 ���������

import javax.swing.JOptionPane;

public class HelloDialog { public static void main(final String[] args) { JOptionPane.showMessageDialog(null, ”Hello"); System.exit(0); }}

import javax.swing.JOptionPane;

public class HelloDialog { public static void main(final String[] args) { JOptionPane.showMessageDialog(null, ”Hello"); System.exit(0); }}

������$ �/"��01233�����Q����E����javax.swing.JOptionPane

�����,"��import ��������������+� �����$ �JOptionPane

��� ���&��System.exit(0) �%)#�� ���������R�,������

��������������� ����������� ������������

�����6�0���.�/0����?�

���� !

"

# g.setColor(Color.red); g.fillOval(x, y, w, h);

g.setColor(Color.red); g.fillOval(x, y, w, h);

���� !

"

# g.setColor(Color.red); g.drawOval(x, y, w, h);

g.setColor(Color.red); g.drawOval(x, y, w, h);

Page 14: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

�������9�@��

public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(20, 20, 100, 100); g.setColor(Color.blue); g.fillOval(40, 40, 60, 60); }

public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(20, 20, 100, 100); g.setColor(Color.blue); g.fillOval(40, 40, 60, 60); }

���������������������������

�����

����

����

����

����

����

����

���

���

public void paint(Graphics g) { g.setColor(Color.blue); g.fillOval(40, 40, 60, 60); g.setColor(Color.red); g.fillOval(20, 20, 100, 100); }

public void paint(Graphics g) { g.setColor(Color.blue); g.fillOval(40, 40, 60, 60); g.setColor(Color.red); g.fillOval(20, 20, 100, 100); }

���������������������������

�����

����

����

����

����

����

����

���

��� ��������������� ����������� ������������

�����6�0��� �������?�

import java.awt.*;import java.awt.event.*;import java.applet.*;

public class Monster extends Applet {

// main method public static void main(String[] args) { Frame frame = new Frame("JLab"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.add(new Monster(), BorderLayout.CENTER); frame.setSize(210, 210); frame.setVisible(true); }

import java.awt.*;import java.awt.event.*;import java.applet.*;

public class Monster extends Applet {

// main method public static void main(String[] args) { Frame frame = new Frame("JLab"); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.add(new Monster(), BorderLayout.CENTER); frame.setSize(210, 210); frame.setVisible(true); }

��������������� ����������� ������������

�����6�0��� �������?��A�6�B

public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(0, 0, 200, 100); g.setColor(Color.yellow); g.fillOval(20, 20, 160, 160); g.setColor(Color.green); g.fillOval(40, 40, 120, 120); g.setColor(Color.blue); g.fillOval(60, 60, 80, 80); g.setColor(Color.white); g.fillOval(80, 80, 40, 40); }

} // end of Monster class

public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(0, 0, 200, 100); g.setColor(Color.yellow); g.fillOval(20, 20, 160, 160); g.setColor(Color.green); g.fillOval(40, 40, 120, 120); g.setColor(Color.blue); g.fillOval(60, 60, 80, 80); g.setColor(Color.white); g.fillOval(80, 80, 40, 40); }

} // end of Monster class

��������������� ����������� ������������

��� �����:1������C=0���

public class Add1to10 { public static void main(String [] args) { System.out.println(55); }}

public class Add1to10 { public static void main(String [] args) { System.out.println(55); }}

public class Add1to10 { public static void main(String [] args) { System.out.println(1+2+3+4+5+6+7+8+9+10); }}

public class Add1to10 { public static void main(String [] args) { System.out.println(1+2+3+4+5+6+7+8+9+10); }}

public class Add1to10 { public static void main(String [] args) { double x = 1+2+3+4+5+6+7+8+9+10; System.out.println(x); }}

public class Add1to10 { public static void main(String [] args) { double x = 1+2+3+4+5+6+7+8+9+10; System.out.println(x); }}

Page 15: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

��� ������6��D14/���0.4/9�����

import jlab.JLabIO;

public class Mean { public static void main(String [] args) { double x = 0.0; x += JLabIO.readDouble("data #1 : "); x += JLabIO.readDouble("data #2 : "); x += JLabIO.readDouble("data #3 : "); x += JLabIO.readDouble("data #4 : "); System.out.println("mean = " + x / 4.0 ); }}

import jlab.JLabIO;

public class Mean { public static void main(String [] args) { double x = 0.0; x += JLabIO.readDouble("data #1 : "); x += JLabIO.readDouble("data #2 : "); x += JLabIO.readDouble("data #3 : "); x += JLabIO.readDouble("data #4 : "); System.out.println("mean = " + x / 4.0 ); }}

JLab>javac Mean.javaJLab>java Meandata #1 : 10data #2 : 12data #3 : 14data #4 : 14mean = 12.5JLab>

��������������� ����������� ������������

�������� ���

� 0123*4* �%�"�� �+'����5������ ���� 67892:0*4* ���� ��%$ �%�"&�' �/"��# �($��� 0;06<30*4*#�$���"��# �($���%$.+'� ���&�'*=>30?@A301*9@7?@A882>?0>B2@7>80>3*C7D3EA@0* �/" �($��(�&"����F"�G�H�I �� �� �J+ #J�#)*���������K0123L67892:0L0;06<30L10M<?N

� #)*��������"%! ���&�'*OPAM* �/"G�H�I ��&"���,����F"��� �

��������������� ����������� ������������

EF"G

��������������� ����������� ������������

EF"G������.���0 HI���� ���

Page 16: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

EF"G������.���0 HI���� ���

��������������� ����������� ������������

EF"G��������4����� ���

��������������� ����������� ������������

EF"G������ �1��� ���

��������������� ����������� ������������

EF"G������ ���4/:���;

Page 17: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

EF"G������.�/0��� ���5����0��

��������������� ����������� ������������

EF"G���������=�

��������������� ����������� ������������

EF"G������.���0��� ��� �����HJ�

��������������� ����������� ������������

EF"G������.���0��� ��� �����HJ�

Page 18: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

EF"G��������4����� ���

��������������� ����������� ������������

E!"G������ �1��� ���

��������������� ����������� ������������

EF"G������.�/0��� �����0��

��������������� ����������� ������������

.�-�

� �� �*OABA*�����+'��*6:ACC� ���)*���"���*OABA*A99:26A327>*�� �$�%$*8A2>� �� �%�"�� �&�'�,�"�,��Q� ���+�)*�#�$��,��Q*�'���R+�'��+'��*C08267:7>*S� ���)*���"���)*��%���)*�#�$����)*�+������"���,��� ���)*��� � �/"�����0123L67892:0L0;06<30L10M<?

Page 19: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

������������������ ��������������

������������������ ��� �������� �������!"#$�#

����������������� ����������������������

��������������� ����������� �����������

%�����

� �������������� �!�"#�$%&�� %' ��(����

� ���������� ��������� �

� ���&%� ���������� ������������

� ���%)�*�����#+##,##-##.##/� !���!�

��������������� ����������� �����������

���&'�(���(��������� ���

� 0������(����1�� �������������� !"#������$�%&�'���!���

� �"#������ ������� !��!�� !�����������(����)��������*+,�-���$�!���./ �0��!�����

� %' ��(����� �%�����!����&0�!��#%� !� ���1���(����-������1 02 &�-�$%�3���"#�!�������0������

��������������� ����������� �����������

)�*���+������(��������� ���

� 0 2�*2��!� 0 �"#���� &� 3�4!�43�4�! ��0 04�#5���� (��!6��#788/9

public class Hello1 {

Public static void main(String [] a) {

system.out.println('Hello 2110101')

}

} }

public class Hello1 {

Public static void main(String [] a) {

system.out.println('Hello 2110101')

}

} }

���������� ��������������������������� !��

�"�#�$�

�"���%&

�&�� ' '�()

Page 20: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

���,�-� ���������,�-����.���

� :;<=>?@A#B#���C���C!#DE#���#5 � !#@>F=GHI;F#HJF#;IA;F#A=HKLGF#MMM9

� >N;?OK>?A#B#���P�%)�*�����������������&�'���������C���C!#D8#��P�5 � !#+F#,F#-F#.F#/F#+QF#MMM9

� IHR?O?<#B#������"#�����(!�4S�T $���S�� ���S��04� %U!�V�!#5 � !#AW?KF#L>AFA>?KF#LI>X;F##Y;KZH[;\>X;F#Y;K]>LOI;FY;K^>?@_XAKOXL;F#MMM9

*+,

-,

��������������� ����������� �����������

�����������

� %' ���VC!`�!� ���4�����&0

. �������%�/byte, short, int, long

. �����0��/float1double

. �"&�0�/char

. �00&�/boolean

� ���$((�- ��� �����5!

� %' ��#LIOAA� ��$((���6������(1��-��$((

. �2��String1Text1Rectangle1333

� ���$((����- ��� ��5!

��������������� ����������� �����������

�����������������/0���,���

� '**S�� !VC�����! ������"#��� �� ��'�– byte�)7��(+,8�short�)9��(+,8�int�):��(+,8long�)4��(+,8�float�):��(+,8�double )4��(+,8char�)9��(+,

� '**%'��3��� ���%'���P����4�2��50�������������%��66int7��2 �&����66long8

� L>[NHI;?#�����*���6������(��$%&�04�� 3�� (��!*����4%'����(����S��(����� �!�V�!� �! (��S�$%&�04�� ��

��������������� ����������� �����������

�����.��#�#��1��234567689

� �"#�!�! �a#50 ���4��!��9������7;9<8�=>94?8�;8��7;8�@>=<?9

� ����(!�4#(bC!��*� ��(���"#�!�!���S��– byte )A794��"B���79=,– short )A<9>=4��"B���<9=>=,– int )A97:=:4<>:4��"B���97:=:4<>:=,– long )A9����"B��9���A�7,

$4�����0%S��#int$4�����0%S��#int

Page 21: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

�����.��#�#.����286:;�4<=>689

� �"#�!�!������4��!��04��2��-+93+1:3-;-<=1+3+1>-+3?1@+3::::

� ���� �a*�"#�!�!���&**#JI>OKHXY#N>HXK-+++++++�&%6+3-A-+�1+3+++++-9�&%6+3-9A-+��

� ����(!�4#(bC!��*����' ���4(���"#�!�!��� �a*– float�����(�����*�>A?����$ ��� %���C50�� ��������

)�������������������������� ����������������������������,– double ��(�����*�7DA7=����$ ��� %���C50�� ��������

)���������������������������� ������������������������������,

$4�����0%S��#double$4�����0%S��#double

��������������� ����������� ������������

��)������?.��#�#��1��.��#�#.���#@�.����A

� #float# �a*(����S!� ������������ �#int#��c� #int# �a*(����04����c#������ %U!0%04�S!� ��� #float# �a*04�*����� � �!�C!#5 ��' �a*�"#�!�!��� �(������4��!��04��"#���49

� ����� ��� �int���(3���97:=:4<>:=��5!� �float���(3���97:=:4<>:=�����5!�"!���5����5� !�����(��E��9F7:=:4<>D�G�7;�

����&0�!��#%-���6��������%H%�5!��������-��������5������5�$%�I��+5$��+�����(���0������J5�������&0�!��#%-���6��������%H%�5!��������-��������5������5�$%�I��+5$��+�����(���0������J5���

��������������� ����������� ������������

������������2BC:8:B5689

� (����&**#char#�V�������('�!b������ ���('#5LGO?OLK;?9#�V�

� �����'��)��&�'����K�,�����LML8�LNL8�LOL8�LPL8�LQL8�LRL8�L�L8�L�L

� ���%��;A?�)��&�'����K�,�����L7L8�L9L8�LSL8�LTL

� 1�/%��'*+- ��'�)��&�'����K�,�����LUL8�LVL8�LWL8�LXL

� � �13�(3C��C���*+�����'\n' ���B��(��0�5� ����'\t' ���YMN

��������������� ����������� ������������

�����,�*�������2String9

� #String �V��"#�4�*(��#char#�"#�!�!��!�����(bC!0%� �"engineering"� �"[email protected]"

� �"������������"

� �"Hello How are you?"� �""

"1004"#0 �V�!��*#788D"1004"#0 �V�!��*#788D

""#�������#&� #''#0 �""#�������#&� #''#0 �

Page 22: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

�����������2>DD;6:49

� #boolean� �04�&� ���� ��V�#true#&�'#false � �!�C!� 7#0 04�&�!#true###8#0 04�&�!#false

��������������� ����������� ������������

���� ���2E:83:>;69

� ���&% %U!��� �a*(�����������(��$%&�� ���&%��������V��&�'%' ��(�����"#���*� ����%'������&%� �!����'!"#��S��

public class Zombie {

public static void main(final String[] args ) { int counter = 0; double radius = 5.225; boolean success = false; String name = "somchai";

... }}

public class Zombie {

public static void main(final String[] args ) { int counter = 0; double radius = 5.225; boolean success = false; String name = "somchai";

... }}

��������������� ����������� ������������

��� ��

� ���&% %U!��� �a*(������������ ��!"#�(������� �a*S!���&%�S��#0 �"#�S��(������� �a*S!���&%��T���

� ��!"#�(����S� �a*S� ���&%# %U!��&�!���(���� 4�#5'**0 �a*�"#� !�(���� 4�0��9

int numCircles = 27, k;

System.out.println( numCircles );System.out.println( numCircles );

k = 35;numCircles = k;

dEint numCircles

eeint k

fgint numCircles

fgint k

��������������� ����������� ������������

��������&��� ��

int counter = 0; double radius = 5.225; boolean success = false;

char korKai = '�';

String name = "somchai";

int counter = 0; double radius = 5.225; boolean success = false;

char korKai = '�';

String name = "somchai";

50��BC� ��D� 2EF��"��50 ����0�F�� ����� ��D�C(F�&%6#��"��50

int i = 100, j = 200; double x0, y0, z0; double x1, y1 = 4.5, z1 = 7.8;

int i = 100, j = 200; double x0, y0, z0; double x1, y1 = 4.5, z1 = 7.8;

50�&�G600C" �����H�"��0E��7��#������0�F�� �&%7

Page 23: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

����*@����������&��� ��?-F�G

int counter = 0.75; double radius = "15.25"; boolean success = 1;

int counter = 0.75; double radius = "15.25"; boolean success = 1;

integer counter; doublE radius = "15.25"; string title = ”WWW";

integer counter; doublE radius = "15.25"; string title = ”WWW";

#������0�F�� �C(F�� 50��BC

��H��50��BC� ��D�C(F7��0D "&

int boolean; double public = 12.7; String 12X = ”WWW";

int boolean; double public = 12.7; String 12X = ”WWW";

�"I�2EF��"��50�� &J

int counter int counter

7��7 5K C �H600C" �H)

��������������� ����������� ������������

�H�����"�I!-�

� �V��%'��*4���������1#��� �(#���#h#�V�#i#�a04�� �V�����(bC!��!4������ �(� �V�����c#04�0 %U!0� ������1���S�T 0 �V�!��� �a�� ����0 2"C#���*�"#����!(����1������ ����0 2"C#���*�V���V�!c#(��$%&�#4�2��2EF���LMM8

�"��H���ND&

int17 butterCup Public int2String day_of_weekint17 butterCup Public int2String day_of_week

�"��H�����

7zean I.love.you public ohOH! ed-edd-n-eddy7zean I.love.you public ohOH! ed-edd-n-eddy

��������������� ����������� ������������

���,��#J#��(�.���

abstract assert boolean break bytecase catch char class constcontinue default do double elseextends false final finally floatfor goto if implements importinstanceof int interface long nativenew null package private protectedpublic return short static strictfpsuper switch synchronized this throwthrows transient true try voidvolatile while

!������3���1����������E�������$��

��������������� ����������� ������������

K����#?*�#�*�J#�����"�I!-�

� ���&%#B#(bC!��!4���������1��� �a�� LIOAA#B#(bC!��!4���������1���S�T � ���� �������S��#i#&�'#h� �V��������� 6���V��%'��*4����"#������"#�# (��!&**#j������`j

numStudents dayOfWeek imageBuffer

tvChannel customerID initialVelocity

Page 24: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

������ �������!"#$�#���.��#�#��1�

� *��#�*#���#��#&�' �1 ��V��������5#+##,##-##.##/#9

int i=1, j=2, k;

k = i + j;

i = k * j;

j = i / 2;

k = i % 2;

i = (j + k) * 3;

-

int i int j int k

9 O

- 9 :

* 9 :

* : :

* : +

= : +

����������������� �

����!��

�"���#$��

����

%!�&'�(�)��"

��������������� ����������� ������������

������ �������!"#$�#���.��#�#.���

� *��#�*#���#��#&�' �1 ��V��������5#+##,##-##.##/#9

double x=1.0, y=2.0;

x = y + 5.0;

y = x / 2.0;

x = y % 3.0;

y = (x * 3.0) + 2.0;

x = -0.5 - y;

-3+

double x double y

93+

P3+ 93+

P3+ :3<

+3< P3+

+3< :3<

>;3+ :3<

����������������� �

����!��

�"���#$��

����

%!�&'�(�)��"

��������������� ����������� ������������

���J%��@���� ���2LMM374=6459

� S�� �V������#Q

���&%#Q#!���!�#k���&%#Q#!���!�#k

������������ ��� ������������������������ �!"�#�$�%��"&��'�(�)� �������� ���

x = (-b + Math.sqrt(b*b - 4*a*c)) / (2.0*a);x = (-b + Math.sqrt(b*b - 4*a*c)) / (2.0*a);

*�+

��,

���

double a

double b

double c

����-,

double x

����

�� −+−

������

��������������� ����������� ������������

#��.#��2NOP86MM3D49

� � ������#���&%#lm�����!#5�V�#[;KG>@9#�V�� �����!(��� ������#���&%#lm�����!#!���!�� ��4������%)�*�����#&�' �V�������� �a*%n4 %n4#������6�"#�!��� �04�

(-b + Math.sqrt(b*b - 4.0*a*c)) / (2.0*a)(-b + Math.sqrt(b*b - 4.0*a*c)) / (2.0*a)

.����$)/����(012!'�)!���34$'567'!���!�������(01)���8��34$' ������%)�1 ��� )1

���

� +�

����

�� −+−

((9.0 * c) / 5.0) + 32.0

(((9.0) * (c)) / (5.0)) + (32.0)

9.0*c/5.0+32.0

((9.0 * c) / 5.0) + 32.0

(((9.0) * (c)) / (5.0)) + (32.0)

9.0*c/5.0+32.0

Page 25: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

����*@��

i = j;i = j; ����%!�&'�(�)��"�j�/�59!5�(�)��"�i

i = i + j;i = i + j; ����%!�&'�(�)��"�i�����j�1��) ����7)����:��) �#$/�7/�59!5�(�)��"�i

int i=1, j=2;

i = i + j;

j = j + j + j;

- 9

: 9

: *

int i int j

��������������� ����������� ������������

����*@��

x = -x;

f = ((9.0 * c) / 5.0) + 32.0;

c = 5 * (f - 32.0) / 9.0;

d = Math.sqrt(dx*dx + dy*dy);

x = -x;

f = ((9.0 * c) / 5.0) + 32.0;

c = 5 * (f - 32.0) / 9.0;

d = Math.sqrt(dx*dx + dy*dy);

���

� +← ��

�� −←

����� −← ��

�� ��� ∆+∆←

����� ∆∆+∆∆← ��

��������������� ����������� ������������

�H�����.��+����G� ������#�+

� �"#����S!�� �a*�"#�� �!� S!�������%)�*������������#�"#����"#�4�*4��!�C

� ����������!�Z[Y\]^� _`Mab����@� c��d��e� @���� f

� �"#����2���0%(��#5����*�0���]g[aMY]a������5 h����0�����9

��������������� ����������� ������������

����1 ���%#G���G� ������#�+

x = ((9.0 * c) / 5.0) + 32.0x = ((9.0 * c) / 5.0) + 32.0

*

/

+

Page 26: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

������ ���������%#G���G� ������#�+

x = (-b + Math.sqrt(b*b - 4.0*a*c)) / (2.0*a)x = (-b + Math.sqrt(b*b - 4.0*a*c)) / (2.0*a)

* *

*

-

*-

Math.sqrt()

+

/

��������������� ����������� ������������

����*@��F�GQ

int i, k = 9;

double x = 1.0;

k = i; // i is uninitialized

i = x; // assign double to int

i = 3k; // no operator

3.0 * x + 5.78; // not a complete statement

i = 3--k; // should be i = 3 - (-k)

i = (k + 5; // incomplete parentheses

int i, k = 9;

double x = 1.0;

k = i; // i is uninitialized

i = x; // assign double to int

i = 3k; // no operator

3.0 * x + 5.78; // not a complete statement

i = 3--k; // should be i = 3 - (-k)

i = (k + 5; // incomplete parentheses

��������������� ����������� ������������

��� ��� ���%#@�*���G?*#��R#��&�

� o;WpH?;[;XK� �J��$�����(�C� ������E����5���$%!�$�%���E�����

� qXOI<AHA� ��(�C��G���E���������� �H���0��$�i�- �-+

� G����5�����3���0����(��������������������������

� $15��C� ������E�����0�� ��5!0����&�-

� r;AHYX� �����H�����

� _[NI;[;XKOKH>X� �����J��$���

�������

��� ≈π

��������������� ����������� ������������

F����#���� ���� �����G,�

6&�0C�����

�0�F�&�0C�����

0"6���4�LQ�LR8�&%6#���"��50A

<P39=*SA

import jlab.JLabIO;

public class Radian2Degree {

public static void main(String[] args) { double x, d; x = JLabIO.readDouble("input radian : "); d = 57.296 * x; System.out.println(d); }}

import jlab.JLabIO;

public class Radian2Degree {

public static void main(String[] args) { double x, d; x = JLabIO.readDouble("input radian : "); d = 57.296 * x; System.out.println(d); }} JLab>java Radian2Degree

input radian : 3171.888JLab>

Page 27: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

��� ��� ���%#@�*���G?*#��R#��&�

import jlab.JLabIO;

public class Radian2Degree {

public static void main(String[] args) { double x, d; x = JLabIO.readDouble("input radian : "); d = 180.0 * x / Math.PI; System.out.println( x + " radians = " + d + " degrees"); }}

import jlab.JLabIO;

public class Radian2Degree {

public static void main(String[] args) { double x, d; x = JLabIO.readDouble("input radian : "); d = 180.0 * x / Math.PI; System.out.println( x + " radians = " + d + " degrees"); }} JLab>java Radian2Degree

input radian : 33.0 radians = 171.88733853924697 degreesJLab>

πj ��������3��������� !��!j �Math.PI = :3-;-<=9*<:<?=P=:9:?;*

��������������� ����������� ������������

��� ���%��S�.������@��S�T���� �#�#

� o;WpH?;[;XK� J��$��� �3���!�������������3���!��0���<��$((����

� qXOI<AHA� ��(3��3���!��0���)k�]\Zl,�<������$�i�- �-+

� $15�3��3������0�������������������������0����&�-

� r;AHYX� �����H�����

� _[NI;[;XKOKH>X� �����J��$���

���

���

���

++

��

��

��

��������������� ����������� ������������

F����#���� ���� �����G,�

6&�0C�����

�0�F�&�0C�����

0"6���

�����

import jlab.JLabIO;

public class ParallelResistance {

public static void main(String[] args) { double r1, r2, r3, r; r1 = JLabIO.readDouble("r1 (Kohms) = "); r2 = JLabIO.readDouble("r2 (Kohms) = "); r3 = JLabIO.readDouble("r3 (Kohms) = "); r = 1.0 / (1.0/r1 + 1.0/r2 + 1.0/r3); System.out.println("R (parallel) = " + r + " Kohms"); }}

import jlab.JLabIO;

public class ParallelResistance {

public static void main(String[] args) { double r1, r2, r3, r; r1 = JLabIO.readDouble("r1 (Kohms) = "); r2 = JLabIO.readDouble("r2 (Kohms) = "); r3 = JLabIO.readDouble("r3 (Kohms) = "); r = 1.0 / (1.0/r1 + 1.0/r2 + 1.0/r3); System.out.println("R (parallel) = " + r + " Kohms"); }} JLab>java ParallelResistance

r1 (Kohms) = 22r2 (Kohms) = 33r3 (Kohms) = 100R (parallel) = 11.660777385159012 KohmsJLab>

���

���

���

++

��������������� ����������� ������������

��� ���%�������,����������,��

� o;WpH?;[;XK� �J��$��� �������1����

� qXOI<AHA� ��(��������� ��M�N�$%��m�H���0��$�i�- �-+

� 3�����*���1��������1#�

� $15����0���1��0�� ��5!0����&�-

� r;AHYX� �����H�����

� _[NI;[;XKOKH>X� �����J��$���

�� =++ �����

�����

�� −±−=

Page 28: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

F����#

6&�0C�����

�0�F�&�0C�����

0"6���LT����

A-1A9

�����

��

� −+−=

�����

��

� −−−=

��������������� ����������� ������������

��� ��� �����G,�

import jlab.JLabIO;

public class QRoot { public static void main(String[] args) { double a, b, c, x1, x2;

a = JLabIO.readDouble("a = "); b = JLabIO.readDouble("b = "); c = JLabIO.readDouble("c = ");

double t = Math.sqrt(b * b - 4.0 * a * c); x1 = (-b + t) / (2.0 * a); x2 = (-b - t) / 2.0 / a; System.out.println("x1 = " + x1); System.out.println("x2 = " + x2); }}

import jlab.JLabIO;

public class QRoot { public static void main(String[] args) { double a, b, c, x1, x2;

a = JLabIO.readDouble("a = "); b = JLabIO.readDouble("b = "); c = JLabIO.readDouble("c = ");

double t = Math.sqrt(b * b - 4.0 * a * c); x1 = (-b + t) / (2.0 * a); x2 = (-b - t) / 2.0 / a; System.out.println("x1 = " + x1); System.out.println("x2 = " + x2); }}

JLab>java QRoota = 1b = 4c = 3x1 = -1.0x2 = -3.0JLab>

�����

�� −±−=

Page 29: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

������������������ ��������������

��������������������������� !���"�#$%��&�

����������������� ����������������������

��������������� ����������� �����������

'��$%�

� ��������������� ������������ �� �!�"�#�$�%&�'��(��)* �� �� %(�)�$������%���+��)�) �,!�"�+��)�)���� �� ��( �)�" ��%&�'�

��������������� ����������� �����������

��(��$�!���)

�������������������������������� ����������������������������������� ���

���������� �����

���������������������� ������������������������� ���

x = x - 5.0;

x = 3.0 * x;

x = x % (y + z * a);

x = x / (2.0 * x);

x = x - 5.0;

x = 3.0 * x;

x = x % (y + z * a);

x = x / (2.0 * x);

x -= 5.0;

x *= 3.0;

x %= (y + z * a);

x /= (2.0 * x);

x -= 5.0;

x *= 3.0;

x %= (y + z * a);

x /= (2.0 * x);

������������������������������������������ ����������� �����������

��� ���'��*��+�� !

import jlab.JLabIO;

public class Mean { public static void main(String [] args) { double x = 0.0; x += JLabIO.readDouble("data #1 : "); x += JLabIO.readDouble("data #2 : "); x += JLabIO.readDouble("data #3 : "); x += JLabIO.readDouble("data #4 : "); x += JLabIO.readDouble("data #5 : "); System.out.println("mean = " + (x / 5.0) ); }}

import jlab.JLabIO;

public class Mean { public static void main(String [] args) { double x = 0.0; x += JLabIO.readDouble("data #1 : "); x += JLabIO.readDouble("data #2 : "); x += JLabIO.readDouble("data #3 : "); x += JLabIO.readDouble("data #4 : "); x += JLabIO.readDouble("data #5 : "); System.out.println("mean = " + (x / 5.0) ); }} JLab>java Mean

data #1 : 12data #2 : 35.5data #3 : 16.8data #4 : 19.0data #5 : 2.2mean = 17.1JLab>

Page 30: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

�����������������������,�������-� �",��.�/�*�0

� �� �� �$��(�"�)* ���(-��!��– a++�����������a��� ��������������������a�����������– ++a������������a���������������������������� ��

int i=1, j=2, k;

i++;

++j;

j = i++;

k = ++j + i++;

k = j++ + ++i;

int i int j

� �

� �

� �

� �

� �

������������� ���

�����

���������

���

������������

int k

��������������� ����������� �����������

�����������������������,�������-� �",��.�/�*�0

� ���#�$��(�"�)* ���(-��!��– a--�����������a��� �����������������a������– --a���������a����������������������� ��

int i=10, j=4, k;

i--;

--j;

j = --i;

k = --j - --i;

k = j-- - --i;

�!

int i int j

" �

" �

# #

� �

������������� ���

�����

���������

���

������������

int k

!

��������������� ����������� �����������

����!*�.....

double x=10.0, y=20.0, z;

z = x++ + ++y;

z = x-- - --y;

z = x++-++y;

z = x--+--y;

z = x+++++y;

z = x-----y;

(x++)++;

double x=10.0, y=20.0, z;

z = x++ + ++y;

z = x-- - --y;

z = x++-++y;

z = x--+--y;

z = x+++++y;

z = x-----y;

(x++)++;

$%&'()*+�,-.��

$%&'()*+���

/0��,-.,��

��������������� ����������� �����������

����$�!��*��.���� ��1�,�������-�

� �&��.$(�/0112� 3045��&���6#�&��#&���3��7��8�9!�� �,��7�:�;$<� =�)-���>��&� %(�) � #&���?�9�� �&)�$��?<� =�)!�#�>��&�� � #&���?� =�)-�����>��&�� � #&���?@

int base10 = 2545; int base8 = 04761; int base16 = 0x9F1;

int base10 = 2545; int base8 = 04761; int base16 = 0x9F1;

long base10 = 2545L; long base8 = 04761L; long base16 = 0x9F1L;

long base10 = 2545L; long base8 = 04761L; long base16 = 0x9F1L;

1���

Page 31: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

����$�!��*��.���� ��1�,�����,��.

2������*��34��4,��2������*��34��4,��

� A302B��&���6#�&��#&���A��7��C� D0EF3G��6#�&��#&���D��7��H��7�.$(�,.#&� %(�):)'�!���$���( (���'�#&���I?����+�����.#&� �$)

� !"#######$#��������%�• 123E7 567���/������8��!�9• 12.3E8 567���/����:��8��!�9• 0.0123E11 567���/���:���8��!��9; :::

� &#$###'()��������%�• -567E-6 567���/��<��#�8��!��9• -5.67E-4 567���/��<�:�#�8��!��9• -0.567E-3 567���/��<!:��#�8��!��9; :::

�3=� >�0��3�������?�@0�������4-�3=� >�0��3�������?�@0�������4-

��������������� ����������� ������������

������� !���"�#$�.$%��&�

� �����J� �����&���� ��( �)%&�'��" ���)* �.�����(��" ���)* �� �$)� ���*�+,�� ��������-.���/-.���0���������0/�1,� ���234567��0/88,��0.8���9����/�0/�1,�.8,���0���� ����0����*�������-:�����0�.���-�-���0;�<�����=>����� ;�,�����*����0������?���=>��-�-���0�@�,��� $$$

int dx, dy, halfDist; ... halfDist = 0.5 * Math.sqrt(dx*dx + dy*dy);

int dx, dy, halfDist; ... halfDist = 0.5 * Math.sqrt(dx*dx + dy*dy);

∆+∆=

�� ���

��������������� ����������� ������������

2345�67589:;<

� ��:�&�$�K�#�" ��������!����!��.$K$�)

public class TypeChecking { public static void main(String[] args) { double x = 1.0; short k = 100000; float f = 1.2; }}

public class TypeChecking { public static void main(String[] args) { double x = 1.0; short k = 100000; float f = 1.2; }} JLab>javac TypeChecking

����� �!"#$%&'()(*+*�,-../01�,-..221��� 345�3�����6�6����������7�6487�9��#$:�����7������; <=:>�!<?@A$*.2����� �!"#$%&'()(*2*�,-../01�,-..221��� 345�3�����6�6����������7�6487�9��B<@C?������7������D?<(:>�!<?@A$*.2JLab>

$%&'()AB(%C�*++%+

��������������� ����������� ������������

������� !���"�#$�.$%��&�

� /2LB�0MGN2B0N�>�:-$�7 ��" ��:�$���:)�� �,�.�&�)&�)���)��( �&����:�& ��( �)

public class TypeCasting { public static void main(String[] args) { int j = 3; System.out.println("int : " + (1 / j)); System.out.println("float : " + (1.0F / (float) j)); System.out.println("double : " + (1.0 / (double) j )); }}

public class TypeCasting { public static void main(String[] args) { int j = 3; System.out.println("int : " + (1 / j)); System.out.println("float : " + (1.0F / (float) j)); System.out.println("double : " + (1.0 / (double) j )); }} JLab>java TypeCasting

int : 0float : 0.33333334double : 0.3333333333333333JLab>

3

int j

1.0 / (double) j

3.01.0

D 0.3333333333333333

Page 32: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

$%��&���,=&>'�!?�������� !���"�#$%��&�

� ��( �)����+��)�)��� �O)�+��)�) �,� �P�"Q'���#��J�� ��( �).� �O)�" ���( ���$��%&�'�.#&!����$�%&�'����-';����!��!���R

public class LossFromTypeCasting { public static void main(String[] args) { int i = (int) 12.999; int k = 196620; // 0x3000B short tiny = (short) k; System.out.println("i = " + i); System.out.println("tiny = " + tiny); }}

public class LossFromTypeCasting { public static void main(String[] args) { int i = (int) 12.999; int k = 196620; // 0x3000B short tiny = (short) k; System.out.println("i = " + i); System.out.println("tiny = " + tiny); }} JLab>java LossFromTypeCasting

i = 12tiny = 11JLab>$(==<E#$%��=#A#:#)��!<$)�=;#<$$(==<E#$%��=#A#:#)��!<$)�=;#<$

//�/F�//�/G//�/F�//�/G

//�/G//�/G

��������������� ����������� ������������

@ABBCD:;<�EB:F:G:H5�6C;H5BI:C;

• byte �?���char• short �?���byte�E���char• char �?���byte�E���short• int �?���byteF�shortF�E���char• long �?���byteF�shortF�charF�E���int• float �?���byteF�shortF�charF�intF�E���long• double �?���byteF�shortF�charF�intF�longF�E���float

��������������� ����������� ������������

�"��.��� �.���)�������.��

� /2LB�0MGN2B0N��"Q'��+���$�)�S�T�U�V�W!�$�����9�<�!�"�E42NX�S�T

public class PrecedenceOfCasting { public static void main(String[] args) { double x = 1.9, y = 10.9; System.out.println("(int) x * y = " + ((int) x * y)); System.out.println("(int) y * x = " + ((int) y * x)); System.out.println("(int) (x * y) = " + ((int) (x * y))); System.out.println("(int) (y * x) = " + ((int) (y * x))); }}

public class PrecedenceOfCasting { public static void main(String[] args) { double x = 1.9, y = 10.9; System.out.println("(int) x * y = " + ((int) x * y)); System.out.println("(int) y * x = " + ((int) y * x)); System.out.println("(int) (x * y) = " + ((int) (x * y))); System.out.println("(int) (y * x) = " + ((int) (y * x))); }} JLab>java PrecedenceOfCasting

(int) x * y = 10.9(int) y * x = 19.0(int) (y * x) = 20(int) (x * y) = 20JLab>

��������������� ����������� ������������

������� !���"�#$�.$%��&��!*�.���������

� 7 �:�&�$�������!��( ���$��%&�'�.#&��&����$�� S�T�U�V��7��W�:))���)���%&�'��$���" ����)

G H��- =��-I3E�7���?��double�@1�3 ���� �����?��double�����G ,-.��.���>��H��- =��-I3E�7���?��float�@1�3 ���� �����?��float�����G ,-.��.���>��H��- =��-I3E�7���?��long�@1�3 ���� �����?��long�����G ,-.��.���>���3 ���=��-I3/�>�JI.�?��int

public class AutoConversion { public static void main(String[] args) { int i = 7, j = 8; double x = 0.5F * i + j; System.out.println("x = " + x); }}

public class AutoConversion { public static void main(String[] args) { int i = 7, j = 8; double x = 0.5F * i + j; System.out.println("x = " + x); }} JLab>java AutoConversion

x = 11.5JLab>

E#B�$#$%��=#A#:#)��!<$)�=;#<$E#B�$#$%��=#A#:#)��!<$)�=;#<$

Page 33: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

J:K5;:;<�EB:F:G:H5�6C;H5BI:C;

• byte �?��shortF�intF�longF�floatF�E���double• short �?��intF�longF�floatF�E���double• char �?��intF�longF�floatF�E���double• int �?��longF�floatF�E���double• long �?��float�E���double• float �?��double

public class Widening { public static void main(String[] args) { int big = 1234567890; float approx = big; System.out.println("big = " + big); System.out.println("approx = " + approx); }}

public class Widening { public static void main(String[] args) { int big = 1234567890; float approx = big; System.out.println("big = " + big); System.out.println("approx = " + approx); }}

�� ����HI7I���7��J���� ������345I��87��-���K���������int�I�L��long������floatK���������long������double

�� ����HI7I���7��J���� ������345I��87��-���K���������int�I�L��long������floatK���������long������double

JLab>java Wideningbig = 1234567890approx = 1.23456794E9JLab>

��������������� ����������� ������������

���L��

double x = (1/2 + 1/2);double x = (1/2 + 1/2); M�6��������H�N

float y = 1.0/2.0;float y = 1.0/2.0; ��6��������H�N

double z = 4d + 5;double z = 4d + 5; O�6��������H�N

double u = 0.0 + 1/2;double u = 0.0 + 1/2; @�6��������H�N

double v = (0.0 + 1)/2;double v = (0.0 + 1)/2; )�6��������H�N

double f = 212.0;double c = (5/9) * (f - 32);

double f = 212.0;double c = (5/9) * (f - 32); !�6��������H�N

��������������� ����������� ������������

��� �.�%�.�&%

public class Imprecision1 { public static void main(String[] args) { double x = 20.1; double y = x + 16.8; System.out.println(y); }}

public class Imprecision1 { public static void main(String[] args) { double x = 20.1; double y = x + 16.8; System.out.println(y); }}

JLab>java Imprecision136.900000000000006JLab>

public class Imprecision2 { public static void main(String[] args) { double x = 1.0E-200; System.out.println(x * x / x); System.out.println(x * (x / x)); }}

public class Imprecision2 { public static void main(String[] args) { double x = 1.0E-200; System.out.println(x * x / x); System.out.println(x * (x / x)); }}

JLab>java Imprecision20.01.0E-200JLab>

K)%AB(CL�'%(CB�- =���@0�����

30�������J0����M��>�N0��J�O

��������������� ����������� ������������

��� �.�%�.�&%

public class DivByZero { public static void main(String[] args) { int i = 0; int j = 5 / i;

System.out.println(j); }}

public class DivByZero { public static void main(String[] args) { int i = 0; int j = 5 / i;

System.out.println(j); }} JLab>java DivByZero

java.lang.ArithmeticException: / by zeroat DivByZero.main(DivByZero.java:6)

Exception in thread "main"JLab>

@0�������4-2�@���,-.- J.��(CK(C(BP�����>���E�����QI��R@1/0��2E�����J��-S��T3��=M1/0������5� ���.������*8$*'B(%C9

Page 34: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

��� �.�%�.�&%

public class DivByZero { public static void main(String[] args) { double i = 0.0; double j = 5.0 / i; System.out.println(j); System.out.println(0.0/i); }}

public class DivByZero { public static void main(String[] args) { double i = 0.0; double j = 5.0 / i; System.out.println(j); System.out.println(0.0/i); }} JLab>java DivByZero

InfinityNaNJLab>

K)%AB(CL�'%(CB�2�@���- J.��(CK(C(BP�31�UAU�5U%B�A�UV&W*+9�����>���E�����QI��R@1,-.�����*8$*'B(%C

Page 35: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

������������������ ��������������

������� ���� !��"#$%�$

����������������� ����������������������

��������������� ����������� �����������

&'�()�

� ������� �� char� ������� �� String

� ��������� ��������String������������

� !"#$"%& '#()* + ,)*()*– System.out.print�System.out.println� ����������� !�"#$%

��������������� ����������� �����������

()��*�����+�'�(���,char-

� �����-.. char �/����������012����� ����� 3456764*&78 �/�

� &��'��()*+,-�(�&.��/0 1.,2�2�2"2�232�242�252�2�2�262

� &�� �6789*+,-�(�&.��/0 1.,2:2�2;2�2<2�2=2

� ��>���(?@AB C(*+,-�(�&.��/0 1.,2D2�2E2�2F2�2G2

� )H������IJ'IK�)?@ 1.,'\n' &��6LM,�))�N+HJ.'\t' &����"

��������������� ����������� �����������

��� $�'�(��.)�/�&'0

� ���� �9.������:��; <�����=����>��?�0 3�@2 @���:� A#B4"C&8 �0�< DE .�� ��������012���> �:0� 2�2*)H��77O:

�0�2"2*)H��77O;

�0�232*)H��77P:

�0�

242*)H��77P;�0�252*)H��7Q39

�0�2G2*)H��7RST

�0�

2�2*)H��7R7:�0�262*)H��7R7;

�0

� ���.>���<��F�3B#*&7#6*B"#6%BG6*B"# �@�0�:��:� BDH# 8

Page 36: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

����(1/$�2��3�'� 44�char

� �:������-.. char �@�0���=0 �/2����� I� ������� �F.����� ����=�� &$46(& $&J)&#4&

3 �2<��� K 8 �:0� '\n' &��6LM,�))�N+HJ.� '\t’ &����"� '\\' ��>���(?@U*"�VW�X���0� '\'' ��>���(?@'*�YZ[X�\]���0� '\"' ��>���(?@"*��]"X�\]���0

��������������� ����������� �����������

��5�2�&�"�$�'4�6�6

� �L �0�0 �9 M� +1�����)K)�J��_� 1B��?B&C��&)@*��`$abbb0

� ����� IMI� +1�c�'��))�H�.��'IK�)?@��N���d'-�AH)c'K)�J��

_� 1B�'��6)�'c�,/� ��)K)�J��_� 1B��?B&C��&)@���'��6)�d�eJ.eN_�

'�.�����BN*2;2�2Q2eJ.eN2S20� f�d�,��eKK)�J��_� 1B��?B&C��&)@�g&'� K����, Kh,

d��,�, &gJ ����.',

00 02short

00 32char

6'��� �&iJ�d��,�, &gJ;�&.eJ.J�V����V���2�;2

��������������� ����������� �����������

()��*�����+0�/�'�(���,String-

� String �@�0 �2<��� S ���=�N:� �����-.. String �9.��������O<�����; ���� �:������-.. String �@�0���=0 �/2����� P� �@�0�:������-.. String ���.��<O:O<�� @���L �0�0������:� ������� @�� "" �:� #)%% $*7B#Q

public class StringSample { public static void main(String [] args) { String s1 = "This is a string literal"; String s2 = "This is an illegal string literal"; }}

public class StringSample { public static void main(String [] args) { String s1 = "This is a string literal"; String s2 = "This is an illegal string literal"; }}

��������������� ����������� �����������

�'��7�4'������8��'4�String

� String @����R�.�������� <@���/� S� S �/����:�>�������>��>�� ���<�����0� /2������-.. String S ��.�����=< �����0�T0

��U�� ��@2�0 �V0 String �:�0 -����1� Spublic class StringConcatenation { public static void main(String [] args) { String s1 = "Approx. value of PI"; String s2 = "is"; System.out.println(s1 + s2); System.out.println(s1 + " " + s2); System.out.println(s1 + " " + s2 + " " + Math.PI); }}

public class StringConcatenation { public static void main(String [] args) { String s1 = "Approx. value of PI"; String s2 = "is"; System.out.println(s1 + s2); System.out.println(s1 + " " + s2); System.out.println(s1 + " " + s2 + " " + Math.PI); }}

JLab>java StringConcatenationApprox. value of PIisApprox. value of PI isApprox. value of PI is 3.141592653589793JLab>

Page 37: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

()�������'3������.'4����8

"1 + 2 = " + 1 + 2.0;"1 + 2 = " + 1 + 2.0;

S

toString

"1"

S

"1 + 2 = 1"

toString

"2.0"

"1 + 2 = 12.0"

��������������� ����������� ������������

()�������'3������.'4����8

"1 + 2 = " + (1 + 2.0);"1 + 2 = " + (1 + 2.0);

"1 + 2 = 3.0"

S

toString

"3.0"

S3.0

�����������

1.0

��������������� ����������� ������������

String�5�29:2����+()��*��"#$%�$9$;���

� String �V0 4%6$$ ��?�0=0����� @ W&*5"C =�� @��=������� ��X@ @��=�� W&*5"C ��� String

� 1c�'&���K)�� Kh,j��YZ[� &�JN�� �)c�'�HJ��dIN� &�JN��1c�'������� &�JN��*0� -��+,�� �g�&'�+�.'�e)6LM,�����������+1

String s1 = "Java JA"; String s2 = s1.toUpperCase(); // "JAVA JA" char c = s2.charAt(2); // 'V'

String s1 = "Java JA"; String s2 = s1.toUpperCase(); // "JAVA JA" char c = s2.charAt(2); // 'V'

��������������� ����������� ������������

4�3�<=�>?@A�(�3�String

� %&#Q*538 �/0 int Y12�-�0���������>�������0@T� *"A((&7!6$&38 �/0 String Y12�@�:� �/�0>��

�����0@T -�:������F����ZF������ ��@2�0 �V0���=�N:� *7BW38 �/� String Y12�@�:� �/�0>�������0@T -�:��<

$(64&$ �@2���:���Y���-�����������

public class StringAPI { public static void main(String [] args) { String s1 = " Moo Mak "; System.out.println(s1.length()); System.out.println("\"" + s1.toUpperCase()+"\""); System.out.println("\"" + s1.trim() + "\""); }}

public class StringAPI { public static void main(String [] args) { String s1 = " Moo Mak "; System.out.println(s1.length()); System.out.println("\"" + s1.toUpperCase()+"\""); System.out.println("\"" + s1.trim() + "\""); }}

10" MOO MAK ""MOO MAK"

Page 38: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

4�3�<=�>?@A�(�3�String ,�2�-

� $)[$*7B#Q3 int b\ int e 8� )��int�'�d��,�,*�JJ&B+H1c�' Kh,"����0� �c,String �� Kh,���'��6)��.'� )B�J&�M��&.&����" Kh,

d��,�,��"&��� &���H,.�k���IN6'����'��6)��c'&���H,.�7

public class Substring { public static void main(String [] args) { String s1 = "Scorpian"; System.out.println(s1.substring(1,5)); }}

public class Substring { public static void main(String [] args) { String s1 = "Scorpian"; System.out.println(s1.substring(1,5)); }} JLab>java Substring

corpJLab>S c o r p i a n

0 1 2 3 4 5 6 7

��������������� ����������� ������������

B=�>?@A��2�3C�(�3�String

� String @ W&*5"C$ ����=��=��� ���������� �@�<�����:�0 5&%( ]B%& ��� ^6_6 `a'� =0 ^b6[ �<��c dD -���=���..���L ��:� e*7B#QcharAt compareTo concatcontentEquals copyValueOf endsWithequals getBytes getCharshashCode indexOf internlastIndexOf length matchesregionMatches replace replaceAllreplaceFirst split startsWithsubSequence substring toCharArraytoLowerCase toString toUpperCasetrim valueOf

��������������� ����������� ������������

DEFE�GH��I=JK�L�J=

� ������<=��-���:�0 5&%( ]B%& =�� �V0� ����@.���=��=������ �L �O:O<��< ������0-��

�:�0=�� �V0 /2�������=��� ��=��f g�=0�R�.�����

��������������� ����������� ������������

��� ��� 0.3:"M��."�$

� h&J)B7&W&#*� ��N�1c�' Nc',d��HJ�� �6 Nc',��eN)��

� `#6%i$B$� )��HJ�� �66'� Nc',_.�,���Kl,ABJA@� ��N�1c�' Nc',��d'-�A� 6'��� �&i Nc',��1c�'����IN�c'"SEPTEMBER"

� j&$BQ#� �)�����'��6)����9m:;n:7T&���))dI6c�' Nc',

)���&.'��,eK Nc',��9&��'��()

J A N U A R Y F E B R U A R Y M A R C H ...

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 1 1 1 1 1 1 1 1 1 1 2 2 2 2

Page 39: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

N'33�$��� 0.3:"M��."�$

������������� ��������������������

������������

���������������

����� ���������!�"��#$%��

�����&"�&����$�"��#�'��()*+,�-.�

(*+� �����

��������������� ����������� ������������

��� ��������:"M��."�$

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); System.out.println(month.substring(9*(m-1),9*m)); }}

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); System.out.println(month.substring(9*(m-1),9*m)); }}

JLab>java MonthEnter month number : 2FEBRUARYJLab>

J A N U A R Y F E B R U A R Y M A R C H ...

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 1 1 1 1 1 1 1 1 1 1 2 2 2 2

��������������� ����������� ������������

���0'M3�O?�A?J=�� !��"#$%�$9$;���

� -><���������=�� ei$*&Wk")*k(7B#*3 8\ ei$*&Wk")*k(7B#*%#3 8

� �.��������-�l0����=�� ei$*&WkB# -�:��X@=������:���� �9�0���

// read a string from standard input BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); String s = ""; try { s = reader.readLine(); } catch (IOException e) { e.printStackTrace(); }

������:�0O:�� /2��O: �V0O

��������������� ����������� ������������

���0'M3�O?�A?J=���KP��1M�19&)�Q���9$�DRES

� ^b6[',k7&6C'#*38 m ��.�L �0�0 �9� ^b6[',k7&6Cj")[%&38 m ��.�L �0�0���� ^b6[',k7&6Ce*7B#Q38 m ��.>�������� ���=0�� �9.��=>:�/�O:=>: String �9O<�

U��=>: ��-><����������:�0�.������ ������ ��.��< import jlab.JLabIO;

O���@2.��<-�; ���n�-�

Page 40: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

���0'M3�O?�A?J=���KP��1M�19&)�Q���9$�DRES

import jlab.JLabIO;public class Month { public static void TestJLabIO(String [] args) { String name = JLabIO.readString("Name : "); int age = JLabIO.readInt("Age : "); double tax = JLabIO.readDouble("Income Tax : "); System.out.println(name + ", age = " + age + ", tax = " + tax); }}

import jlab.JLabIO;public class Month { public static void TestJLabIO(String [] args) { String name = JLabIO.readString("Name : "); int age = JLabIO.readInt("Age : "); double tax = JLabIO.readDouble("Income Tax : "); System.out.println(name + ", age = " + age + ", tax = " + tax); }} JLab>java TestJLabIO

Name : SomchaiAge : 41Income Tax : I won't tell youinvalid real number, try again15000.50Somchai, age = 41, tax = 15000.5JLab>

/012�34�����

��������������� ����������� ������������

TE��O?@=�������%�$�UGVW�X

� �V0��?�0 [67 4"C& ����.<������ �� Do ���� ���������>�<�/���� ���� �F 345&4p CBQB*8 Y12�U��

��2 �/2�=�����>�.���U������ DM ������Y���� �L ��0<=�� ��� ��������>�< �V0 ���L �<�.�@2 q

�������������

��������

���

������

���

×+−= ∑∑

≤≤≤≤ �

����

))56+6768656(,67)96:6;6<6=69,,�>�(:

)+9*)(:?+9,,?+9�>�=

��������������� ����������� ������������

��� ���&��O>=OY�@�����(�3�UGVW�X

� h&J)B7&W&#*� ��N�V��VW�Y[Y�6'�&�� �6:;H���+,)���R3o8:Q

� `#6%i$B$� )��&�� �6:;H������Kl,ABJA@� +1�p&)��)H�V��VW�Y[Y�6'�R3o8:Q� ��N�V��VW�Y[Y���d'-�A

� j&$BQ#� 6��,_����,

� 'W(%&W&#*6*B"#� 6��,qK)��)J

��������������� ����������� ������������

N'33�$

������������

���������������

������������!��@

)+9*)A?+9,,?+9

A�>�7B)@�6@

�6@

�6@

�6@

�6@

��,

A�6>�)@�6@

�6@

�6@

6@

6@

��,

Page 41: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

��� ���&��O>=OY�@�����(�3�UGVW�X

import jlab.JLabIO;public class EAN13CheckDigit { public static void main(String[] args) { int s = 0; String d = JLabIO.readString("Enter 12 digits : "); s += Character.digit(d.charAt(1), 10); s += Character.digit(d.charAt(3), 10); s += Character.digit(d.charAt(5), 10); s += Character.digit(d.charAt(7), 10); s += Character.digit(d.charAt(9), 10); s += Character.digit(d.charAt(11), 10); s *= 3; s += Character.digit(d.charAt(0), 10); s += Character.digit(d.charAt(2), 10); s += Character.digit(d.charAt(4), 10); s += Character.digit(d.charAt(6), 10); s += Character.digit(d.charAt(8), 10); s += Character.digit(d.charAt(10), 10); System.out.println("Check Digit is " + (10-(s%10))%10); }}

import jlab.JLabIO;public class EAN13CheckDigit { public static void main(String[] args) { int s = 0; String d = JLabIO.readString("Enter 12 digits : "); s += Character.digit(d.charAt(1), 10); s += Character.digit(d.charAt(3), 10); s += Character.digit(d.charAt(5), 10); s += Character.digit(d.charAt(7), 10); s += Character.digit(d.charAt(9), 10); s += Character.digit(d.charAt(11), 10); s *= 3; s += Character.digit(d.charAt(0), 10); s += Character.digit(d.charAt(2), 10); s += Character.digit(d.charAt(4), 10); s += Character.digit(d.charAt(6), 10); s += Character.digit(d.charAt(8), 10); s += Character.digit(d.charAt(10), 10); System.out.println("Check Digit is " + (10-(s%10))%10); }}

Page 42: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

������������������ ��������������

�������������� ���!��"� �"#���$!�%"�

����������������� ����������������������

��������������� ����������� �����������

&'�()�

� ���������� � ���������� !"#$"� %&&#"'(�")*+"$$�&(

� ������������������ �������������� �����������������

� ,"$-".�� � /-'-"0"(-�1�2#&34� $5�-36!3'$"� �� 78�9 �8�9����:�:���;�<����

��������������� ����������� �����������

��� ���&����*+,�-

� =">?�+"0"(-� ������������������������� !"��#$%��!&�!�������'(�

� @('#A$�$� "')�������"*!����#�!�+,�-*.-/� ���Math.sqrt����"0�"��#$%��!� �� !"��#$%��!#$%0�1 �#�!��2�-

� B"$�C(� 3&$4��'!!��

� D0*#"0"(-'-�&(� 3&$4�5+"��".

��������������� ����������� �����������

.'--�/���� ���� 01���2,�3

����������

��� ���������

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); System.out.println("sqrt(x) = " + Math.sqrt(x) ); }}

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); System.out.println("sqrt(x) = " + Math.sqrt(x) ); }}

JLab>java Sqrtx = 1.44sqrt(x) = 1.2JLab>

JLab>java Sqrtx = 1.44sqrt(x) = 1.2JLab>

��������������������������

������� �!�"

Page 43: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

���2,�3

� �<E�< ��FGHI:JK�LMKN�K7O�:����:�:��F�9

��# $���%�&��������� �!�"��'(#(���) �)(���*��+���,���-������!./����.0123 "�(��)4�'56���7) +��8�9:�:9���# $��;���$<�=>6?�-?�� �,�$��,���@?���(���*��+�:�:��(����?A�������*?�# $(���*��+

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); System.out.println("sqrt(x) = " + Math.sqrt(x) ); }}

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); System.out.println("sqrt(x) = " + Math.sqrt(x) ); }} JLab>java Sqrt

x = -8sqrt(x) = NaNJLab>

JLab>java Sqrtx = -8sqrt(x) = NaNJLab>

��������������� ����������� �����������

��� ���&����*+,�-�453'3��'3��6-7

� =">?�+"0"(-� ������������������������� !"��#$%��!&�!�������'(�6��3+7�������1.�8* �)

� @('#A$�$� "')�������"*!����#�!�+,�-*.-/� 6��8* �)�0��� !9��:�;<��=�:�>���?�;@A��9� 6��3+7�)��0"B�C�0��� !"��#$%��!#$%0�1 �#�!��2�-

� B"$�C(� 3&$4��'!!��

� D0*#"0"(-'-�&(� 3&$4�5+"��".

��������������� ����������� �����������

.'--�/

��� ���������

����������

B�C�3

� 03��%�&�D

��������������������������

����������� ����������������

������� �!�"

��E�&�D

B�C�3

� 03

��������������� ����������� �����������

���������������"�%��8���

do this

Boolean expr.

B�C�3

� 03

if ( Boolean expr ) { do this ; }

if ( Boolean expr ) { do this ; }

Page 44: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� �����������

��� ���� 01���2,�3

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); if (x < 0) { System.out.println("Re-run with non-negative number"); } if (x >= 0) { System.out.println("sqrt(x) = " + Math.sqrt(x) ); } }}

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); if (x < 0) { System.out.println("Re-run with non-negative number"); } if (x >= 0) { System.out.println("sqrt(x) = " + Math.sqrt(x) ); } }} JLab>java Sqrt

x = -8Re-run with non-negative numberJLab>

JLab>java Sqrtx = -8Re-run with non-negative numberJLab>

JLab>java Sqrtx = 1.21sqrt(x) = 1.1JLab>

JLab>java Sqrtx = 1.21sqrt(x) = 1.1JLab>

@?$������F��$��G))4�?$��8�����G)�;��G)*H ��I�!�7',���)����8���=�?$�����4(6�"

��������������� ����������� ������������

.'--�/

��� ���������

����������

� 03��%�&�D

��������������������������

����������� ����������������

������� �!�"

B�C�3

��������������� ����������� ������������

��������������!��"��"�%��8���

if ( Boolean expr ) { do this if true ; } else { do this if false ; }

if ( Boolean expr ) { do this if true ; } else { do this if false ; }

do this if true

Boolean expr.B�C�3 � 03

do this if false

��������������� ����������� ������������

��� ���� 01���2,�3

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); if (x < 0) { System.out.println("Re-run with non-negative number"); } else { System.out.println("sqrt(x) = " + Math.sqrt(x) ); } }}

import jlab.JLabIO;

public class Sqrt { public static void main(String [] args) { double x = JLabIO.readDouble("x = "); if (x < 0) { System.out.println("Re-run with non-negative number"); } else { System.out.println("sqrt(x) = " + Math.sqrt(x) ); } }}

JLab>java Sqrtx = -8Re-run with non-negative numberJLab>

JLab>java Sqrtx = -8Re-run with non-negative numberJLab>

JLab>java Sqrtx = 1.21sqrt(x) = 1.1JLab>

JLab>java Sqrtx = 1.21sqrt(x) = 1.1JLab>

Page 45: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

/��9/�3:0*/�4;����%��<=>?�""���7

� EFKL����P� 7Q:7< ���boolean� �K��������N�K

� �����������������D8'�3+"$4)3#$4)&��.���))-B(�E��3���.���������4����3#���'�

� @��=��#$%FB�F��+"G32#boolean� A�������������D8'�3�B%�.F��.�'.-'�H/�*-��/)��$�3���������

� �boolean 7�8��: 7Q:�String EFK �R�:�I:public class TrueFalse { public static void main(String [] args) { System.out.println( true + " " + false ); }}

public class TrueFalse { public static void main(String [] args) { System.out.println( true + " " + false ); }} JLab>java TrueFalse

true falseJLab>

JLab>java TrueFalsetrue falseJLab>

��������������� ����������� ������������

@��%����%��A>�?%��?"

� ��� 78�9 �8�9GK�M�7< ���SI:T�:�EFK;�RIJ���4����K LJ.������KIMJ���4����0"B�3#���')K LMJ.������0"B�3#���')KMMJ3#���')K NMJ1.�3#���')K

� GK�M��8�:���� 78�9 �8�9�K�� 7Q:7< �� F8����:�S�N�K������;�<��P8�� 7�8��:7< ��GK�M�� �R: F8����9:���:�������������

� EFKL����P� 7Q:7< ���boolean

( (n % 2) == 1 ) ,;?(����8• true 5?���n ��8�����() • false 5?� n ��8�����(6�

( (n % 2) == 1 ) ,;?(����8• true 5?���n ��8�����() • false 5?� n ��8�����(6�

n = 1 ,���*�#$ n == 1 n = 1 ,���*�#$ n == 1

��������������� ����������� ������������

;����%��A>�?%��?"

� N�K �S�������&&�;�:�'(.� N�K �S�������||�;�:�&+� N�K �S�������!���;�:�(&-� '(.��&+�� 7Q:���7U�9����� �S�� �S��:���:�9M�8:���

:���:��EFK 7Q::���:�9M�8:N�R� �0�A��GB3+7��*-��/)��$�– A && B3+7�true3.B%�#'(!A��GB3+7�true#'(!F��– A || B3+7�true3.B%�A0"B�B3+7�true�'�0�O%!8'�

� (&-� 7Q:���7U�9����� �S�� 7�8��:L�G��:���:�9M�8:�8������:FN�K������� 7Q: �V���S���� �V� 7Q:���

��������������� ����������� ������������

��� ��� ,2-BC+��2C�/

� �9��� �G FS�:�W8�R�EFK��I�;�R�X�JH��XYZ� ;�F��S�� FS�:����� �G�8�EFK�9

� N�K�0�;�:��� �G FS�:�8�EFK�9� �����R����I�;�R������[����8��\W0!XZ�JH��\0!X��V

�S��S�� FS�:� JK�LMKN�K7O�: FS�:�8��Y��V���9�����R��GK���R��:8I

J A N U A R Y F E B R U A R Y M A R C H ...

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 1 1 1 1 1 1 1 1 1 1 2 2 2 2

Page 46: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

��� ��� ,2-BC+��2C�/

� �<E�< ��FGHI:JK�LMKN�K7O�: �G FS�:�8��X]]

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); System.out.println(month.substring(9*(m-1),9*m)); }}

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); System.out.println(month.substring(9*(m-1),9*m)); }}

JLab>java MonthEnter month number : 100java.lang.StringIndexOutOfBoundsException: String index outof range: 900

at java.lang.String.substring(String.java:1473)at Month.main(Month.java:10)

Exception in thread "main"JLab>

JLab>java MonthEnter month number : 100java.lang.StringIndexOutOfBoundsException: String index outof range: 900

at java.lang.String.substring(String.java:1473)at Month.main(Month.java:10)

Exception in thread "main"JLab>

3�* StringIndexOutOfBoundsException

��������������� ����������� ������������

������9,�3�-C+�/D(

��?������1/.������-# $�;#$��4*�;

����������

��� ���������

����7@�;#$��������������1

��?������1/.������-# $�;#$��4*�;

����������

��� ���������

����7@�;#$��������������1

����+�$+@$1/.�����4�������) J!1KL"�5H�J1KL

LM1MLN�D� 03

����+�$+@$1/.�����4�������) J!1KL"�5H�J1KL

B�C�3

��������������� ����������� ������������

��� ��� ,2-BC+��2C�/

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); if ((1 <= m) && (m <= 12)) { System.out.println(month.substring(9*(m-1),9*m)); } }}

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); if ((1 <= m) && (m <= 12)) { System.out.println(month.substring(9*(m-1),9*m)); } }}

LM1MLNLM1MLN (1 <= m) && (m <= 12)(1 <= m) && (m <= 12)

��������������� ����������� ������������

�-C+�/D(&/E+-��(*F/D2)&0�F 33

P JQIM@KRRJ@IMQSK

P JCI@KRRJ@IQTK

P JQSLM@KRRJQIM@K

P J@MMQKUUJ@MMSKUUJ@MMTKUUJ@MMVKUUJ@MMWKUUJ@MMXKUUJ@MMYKUUJ@MMZKUUJ@MM[KUUJ@MQCKUUJ@MMQQKUUJ@MMQSK

P NJNJQIM@KUUNJ@IMQSKK

P NJJ@IQKUUJ@LQSKK

Page 47: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

����-����9��-

P JQIM@KRRJ@IMQSKFB�NJJ@IQKUUJ@LQSK\

(1<=m) (m<=12) (1<=m)&&(m<=12)

true true true true false false false true false false false false

(1<=m) (m<=12) (1<=m)&&(m<=12)

true true true true false false false true false false false false

(m<1) (m>12) (m<1)||(m>12) !((m<1)||(m>12)

false false false true false true true false true false true false true true true false

(m<1) (m>12) (m<1)||(m>12) !((m<1)||(m>12)

false false false true false true true false true false true false true true true false

��������������� ����������� ������������

�G�����9��H�0��2'3����I�3'�����

� ���N:�� �V9� ?('+A�^��_��^^��!!��`� -A*"�3'$-�(C� a��b��c� 2�('+A�^��_� d��e��df��ef� ff��`f� 11� gg� f��af��bf��cf��^f��_f

int k; ... boolean b = ! k==0; // wrong ...

if ( 3<=k+1 && k<=5 || k>=9 ) { // k=2,3,4,5, 9,10,11,... }

int k; ... boolean b = ! k==0; // wrong ...

if ( 3<=k+1 && k<=5 || k>=9 ) { // k=2,3,4,5, 9,10,11,... }

��������������� ����������� ������������

��� ��� ,2-9��/�/�'/(�-�2C�/��J�J

� =">?�+"0"(-� 0����3 B���]-]&�!+#$%�0�.�.$SZ0"B�S[�'�

� @('#A$�$� "')3�&+J3+7�F]_]K����#�!�+,�-*.-/� �����"# ��)+�#*��"#*�3-B%��� !�������$���3 B���]-]

� B"$�C(� 3&$4��'!!��

� D0*#"0"(-'-�&(� 3&$4�5+"��".

��������������� ����������� ������������

���2,�3�K���,6��/

� FS�:������:P�G��7h�A�W�i�iZ�8�Y\���: S��� 3�&+JF]_]K8��!0�" ��4V�!8'��8�8��!0�" ��4QCC1.��!8'�4�3����"a$#$%0�" ��4VCC�!8'�O �P�N&&N����=���)�NQ����!*��;?�+�R�,��7���"O �P�N&&R����=���)�NJ����!*��;?�+�R�7�����7'*��;?�+�L&&�,��7"O �P�NL&&����=���)�NQ����!*��;?�+�L&&�7���"O �P�N&&&����=���)�NJ����!*��;?�+�R&&�7���"

� �0�bFB�3�&+JF]_]KO !S�T�R"�UU�&��������;�$����*��;?�+�R�7����DO !S�T�L&&"�VU�&��������;�$����*��;?�+�L&&�,��7����DO !S�T�R&&"�UU�&��������;�$����*��;?�+�R&&�7����D

if ( (y%4 == 4 && y%100 != 0) || (y%400 == 0) )

if ( (y%4 == 4 && y%100 != 0) || (y%400 == 0) )

Page 48: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

.'--�/

��� ���������

����������

� 03!STR&&�UU�&"�*�#$!!STRUU&"��7'�!STL&&�VU�&""�

����7@�P������������S

NQ���

B�C�3

NJ���

��������������� ����������� ������������

��� ��� ,2-9��/�/�'/(�-�2C�/��J�J

import jlab.JLabIO;public class Feb { public static void main(String [] args) { int y = JLabIO.readInt("year = "); if ((y%4==0 && y%100!=0) || (y%400==0)) { System.out.println("Feb. has " + 29 + " days."); } else { System.out.println("Feb. has " + 28 + " days."); } }}

import jlab.JLabIO;public class Feb { public static void main(String [] args) { int y = JLabIO.readInt("year = "); if ((y%4==0 && y%100!=0) || (y%400==0)) { System.out.println("Feb. has " + 29 + " days."); } else { System.out.println("Feb. has " + 28 + " days."); } }} int y = JLabIO.readInt("year = ");

int numDays; if ((y%4==0 && y%100!=0) || (y%400==0)) { numDays = 29; } else { numDays = 28; } System.out.println("Feb. has " + numDays + " days.");

int y = JLabIO.readInt("year = "); int numDays; if ((y%4==0 && y%100!=0) || (y%400==0)) { numDays = 29; } else { numDays = 28; } System.out.println("Feb. has " + numDays + " days.");

��������������� ����������� ������������

L�"��M�N�

if ( A ) { if ( B ) { if ( C ) { D ; } } }

if ( A ) { if ( B ) { if ( C ) { D ; } } }

true

true

true�

��������������� ����������� ������������

L�"��M�N�

if ( A ) { if ( B ) { E ; if ( C ) { D ; } } }

if ( A ) { if ( B ) { E ; if ( C ) { D ; } } }

true

true

true

Page 49: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

L�"��M�N�

true

true

true

��

if ( A ) { if ( B ) { E ; if ( C ) { D ; } } else { G ; if ( H ) { J ; } } } else { F ; }

if ( A ) { if ( B ) { E ; if ( C ) { D ; } } else { G ; if ( H ) { J ; } } } else { F ; }

true

��������������� ����������� ������������

.'--�/ ,2-BC+��2C�/

����������

��� ���������

L�M�1�D

B�C�3

� 03

1�M�LN�D

B�C�3

� 03

��?������1/.������-# $�;#$��4*�;

����7@�;#$��������������1

����+�$+@$1/.�����4�������) J!1KL"�5H�J1KL

LM1MLN�D

��������������� ����������� ������������

��� ��� ,2-BC+��2C�/

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); if (1 <= m) { if (m <= 12) { System.out.println(month.substring(9*(m-1),9*m)); } } }}

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = ""; month += "JANUARY FEBRUARY MARCH APRIL "; month += "MAY JUNE JULY AUGUST "; month += "SEPTEMBEROCTOBER NOVEMBER DECEMBER "; int m = JLabIO.readInt("Enter month number : "); if (1 <= m) { if (m <= 12) { System.out.println(month.substring(9*(m-1),9*m)); } } }}

��������������� ����������� ������������

�$�?�!��?�O����P

��VU&�D

B�C�3

� 03

SW��X�Y�D

B�C�3

� 03

ZUL

if (x != 0) { if (y/x > 5) { z = 1; } }

if (x != 0) { if (y/x > 5) { z = 1; } }

if (x != 0 && y/x > 5) { z = 1; }

if (x != 0 && y/x > 5) { z = 1; }

if (y/x > 5 && x != 0) { z = 1; }

if (y/x > 5 && x != 0) { z = 1; }

5?����UU�&��',���)���(����G�SW� (����G�SW����$�;�$����VU�&

[�/ �K\] \0]�].^�!,��(�;*�;�=+�+��*���7�;"O����G)�&&���'�7����'����3�_ ���@��5�;,��5?�=��3�_ ��) ��8�B�C�3O����G)�||���'�7����'����3�_ ���@��5�;,��5?�=��3�_ ��) ��8�� 03

Page 50: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

��%��8���� 01�;���Q

if (Boolean Expression) a statement ;

if (Boolean Expression) a statement ;

� %#&34��S����RG��9��F�����������9FK���j��k

if (Boolean Expression){ statements ;}

if (Boolean Expression){ statements ;}

if (Boolean Expression) a statement ;else a statement ;

if (Boolean Expression) a statement ;else a statement ;

if (Boolean Expression){ statements ;} else { statements ;}

if (Boolean Expression){ statements ;} else { statements ;}

�cd����@�� �c:��d�d����@��

��������������� ����������� ������������

�(*F/D2)&0�F 33

if (x < 0) { x = -x; }

if (x < 0) x = -x;

if (x < 0) x = -x;

if (x < 0) { done = true; } else { done = false; }

if (x < 0) done = true; else done = false;

done = (x < 0);

��������������� ����������� ������������

��� ��� ,2-BC+��2C�/

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = "Illegal month number"; int m = JLabIO.readInt("Enter month number : "); if (m == 1) month = "JANUARY"; if (m == 2) month = "FEBRUARY"; if (m == 3) month = "MARCH"; if (m == 4) month = "APRIL"; if (m == 5) month = "MAY"; if (m == 6) month = "JUNE"; if (m == 7) month = "JULY"; if (m == 8) month = "AUGUST"; if (m == 9) month = "SEPTEMBER"; if (m == 10) month = "OCTOBER"; if (m == 11) month = "NOVEMBER"; if (m == 12) month = "DECEMBER"; System.out.println(month); }}

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = "Illegal month number"; int m = JLabIO.readInt("Enter month number : "); if (m == 1) month = "JANUARY"; if (m == 2) month = "FEBRUARY"; if (m == 3) month = "MARCH"; if (m == 4) month = "APRIL"; if (m == 5) month = "MAY"; if (m == 6) month = "JUNE"; if (m == 7) month = "JULY"; if (m == 8) month = "AUGUST"; if (m == 9) month = "SEPTEMBER"; if (m == 10) month = "OCTOBER"; if (m == 11) month = "NOVEMBER"; if (m == 12) month = "DECEMBER"; System.out.println(month); }}

��������������� ����������� ������������

��� ��� ,2-BC+��2C�/

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = "Illegal month number"; int m = JLabIO.readInt("Enter month number : "); if (m == 1) month = "JANUARY"; else if (m == 2) month = "FEBRUARY"; else if (m == 3) month = "MARCH"; else if (m == 4) month = "APRIL"; else if (m == 5) month = "MAY"; else if (m == 6) month = "JUNE"; else if (m == 7) month = "JULY"; else if (m == 8) month = "AUGUST"; else if (m == 9) month = "SEPTEMBER"; else if (m == 10) month = "OCTOBER"; else if (m == 11) month = "NOVEMBER"; else if (m == 12) month = "DECEMBER"; System.out.println(month); }}

import jlab.JLabIO;

public class Month { public static void main(String [] args) { String month = "Illegal month number"; int m = JLabIO.readInt("Enter month number : "); if (m == 1) month = "JANUARY"; else if (m == 2) month = "FEBRUARY"; else if (m == 3) month = "MARCH"; else if (m == 4) month = "APRIL"; else if (m == 5) month = "MAY"; else if (m == 6) month = "JUNE"; else if (m == 7) month = "JULY"; else if (m == 8) month = "AUGUST"; else if (m == 9) month = "SEPTEMBER"; else if (m == 10) month = "OCTOBER"; else if (m == 11) month = "NOVEMBER"; else if (m == 12) month = "DECEMBER"; System.out.println(month); }}

Page 51: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

"#���$�!��%"�

import jlab.JLabIO;public class Month { public static void main(String [] args) { int m = JLabIO.readInt("Enter month number : "); switch (m) { case 1 : System.out.println("JANUARY"); break; case 2 : System.out.println("FEBRUARY"); break; case 3 : System.out.println("MARCH"); break; case 4 : System.out.println("APRIL"); break; case 5 : System.out.println("MAY"); break; case 6 : System.out.println("JUNE"); break; case 7 : System.out.println("JULY"); break; case 8 : System.out.println("AUGUST"); break; case 9 : System.out.println("SEPTEMBER"); break; case 10 : System.out.println("OCTOBER"); break; case 11 : System.out.println("NOVEMBER"); break; case 12 : System.out.println("DECEMBER"); break; default : System.out.println("Illegal month number"); } }}

import jlab.JLabIO;public class Month { public static void main(String [] args) { int m = JLabIO.readInt("Enter month number : "); switch (m) { case 1 : System.out.println("JANUARY"); break; case 2 : System.out.println("FEBRUARY"); break; case 3 : System.out.println("MARCH"); break; case 4 : System.out.println("APRIL"); break; case 5 : System.out.println("MAY"); break; case 6 : System.out.println("JUNE"); break; case 7 : System.out.println("JULY"); break; case 8 : System.out.println("AUGUST"); break; case 9 : System.out.println("SEPTEMBER"); break; case 10 : System.out.println("OCTOBER"); break; case 11 : System.out.println("NOVEMBER"); break; case 12 : System.out.println("DECEMBER"); break; default : System.out.println("Illegal month number"); } }}

��������������� ����������� ������������

"#���$!�%"�

P ���!3�e)0�'!switch8��!3+7��f��#$%1 ���3+7�������38e.P 8�.0�'!case8��!3+7�F��F!8'��))������38e.P #��4� &�!case6��.$break�G�"G5 ������A���gP 6��#��4� &�!case1.�.$break��"#��!���G10�8���!1+#��!����case6' �!1+

P 6��1.�8"!case� 3�4��"#��!���G1+#$%default switch (m) { case 1 : case 2 : System.out.println("m = 1 or 2"); break; case 4 : System.out.println("m = 4"); break; default : System.out.println("m != 1, 2, and 4"); }

switch (m) { case 1 : case 2 : System.out.println("m = 1 or 2"); break; case 4 : System.out.println("m = 4"); break; default : System.out.println("m != 1, 2, and 4"); }

��������������� ����������� ������������

"#���$!�%"�

3�_ 3��]/.

L N R `3B�0C�

��������������� ����������� ������������

,�+-*+�3�R�.�2�'/3R�F

if (0 <= x <= 10)

if x == 0

if (x >= 0 && <= 10)

if (x == 0 || == 1)if (x = 0)

if (x => 0)

if (0 <= x <= 10)

if x == 0

if (x >= 0 && <= 10)

if (x == 0 || == 1)if (x = 0)

if (x => 0)

if (0 <= x && x <= 10)

if (x == 0)

if (x >= 0 && x <= 10)

if (x == 0 || x == 1)if (x == 0)

if (x >= 0)

if (0 <= x && x <= 10)

if (x == 0)

if (x >= 0 && x <= 10)

if (x == 0 || x == 1)if (x == 0)

if (x >= 0)

if (x != 0 || x != 5)

if (x < 3 && x > 9)

if (done && !done)

if (done || !done)

if (x != 0 || x != 5)

if (x < 3 && x > 9)

if (done && !done)

if (done || !done)

if (x == 0) if (y == 0) z = 0;

else if (a == 1)

z = 1;

if (x == 0) if (y == 0) z = 0;

else if (a == 1)

z = 1;

if (x == 0) { if (y == 0) z = 0;

} else if (a == 1)

z = 1;

if (x == 0) { if (y == 0) z = 0;

} else if (a == 1)

z = 1;

3&$4�1+1"�F��

Page 52: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

�FR�SB)�TT���C+����*F3�*F3����%���P�>����

� �� �V9����:�:���FK���float�;�<�double�8GK�������F S������;R:����

� ������:���H�������N�KL����P�8������F ��S��:� ER��:�������:�:�����F��9�R� �R���:�S�ER

public class Imprecision { public static void main(String [] args) { double a = 49.0;

System.out.println( 1.0 == (1.0/a * a) ); System.out.println( 1.0 - (1.0/a * a) ); }}

public class Imprecision { public static void main(String [] args) { double a = 49.0;

System.out.println( 1.0 == (1.0/a * a) ); System.out.println( 1.0 - (1.0/a * a) ); }} JLab>java Imprecision

false1.1102230246251565E-16JLab>

��������������� ����������� ������������

������*F3�*F39��/�/9��-�=�TT�U�V

( ) ε≤−

��

��

����

� ��I��R��l�E�K�8��X]����JK�LR�:���F��9GK��G��:8I��V;�F��R��);�<�A�8�R�N��K �8����:��

public class Epsilon { public static void main(String [] args) { double EPSILON = 1.0E-14; double a = 49.0; double x = 1.0, y = (1.0/a * a); boolean eq = (Math.abs(x-y) / Math.max(Math.abs(x), Math.abs(y))) <= EPSILON; System.out.println( eq ); }}

public class Epsilon { public static void main(String [] args) { double EPSILON = 1.0E-14; double a = 49.0; double x = 1.0, y = (1.0/a * a); boolean eq = (Math.abs(x-y) / Math.max(Math.abs(x), Math.abs(y))) <= EPSILON; System.out.println( eq ); }}

JLab>java EpsilontrueJLab>

��������������� ����������� ������������

���SB)��TT��W��X���C+����*F3�*F3�char

� N�K��ff��d��e� �S�� 78�9 �8�9�char�EFK������F�9������[�W#")�3&C+'*6�3�&+."+�(CZ

� �� 78�9 �8�9���G<�S��� 78�9 �8�9�����m(�3&."��8�N�K;�:���G<:�I:

0 1 2 3 4 5 6 7 8 9 A B C D E F002 ! " # $ % & ' ( ) * + , - . /003 0 1 2 3 4 5 6 7 8 9 : ; < = > ?004 @ A B C D E F G H I J K L M N O005 P Q R S T U V W X Y Z [ \ ] ^ _006 ` a b c d e f g h i j k l m n o007 p q r s t u v w x y z { | } ~

��������������� ����������� ������������

�FR�SB)��TT��W��X���C+����*F3�*F3�String

� �String�ERN�R7< ���SI:T�:� ��K��N�K�0"-6&.$��equals�;�<�compareTo

N:�� 78�9 �8�9

s1.equals(s2) =

true 5?� s1 �*�#$��� s2

false 5?� s1 ,���*�#$��� s2

s1.compareTo(s2)

< 0 5?� s1 ����$ s2 ���7���;�����$��a�

= 0 5?� s1 �*�#$��� s2

> 0 5?� s1 ��*7� s2 ���7���;�����$��a�

Page 53: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

��������������� ����������� ������������

������*F3�*F3�String

public class StringCompare { public static void main(String [] args) { System.out.println("Computer".equals("Computer")); System.out.println("Computer".compareTo("Computer")); System.out.println("Computer".compareTo("Engineer")); System.out.println("Computer".compareTo("Comp")); System.out.println("Computer".compareTo("Computing")); }}

public class StringCompare { public static void main(String [] args) { System.out.println("Computer".equals("Computer")); System.out.println("Computer".compareTo("Computer")); System.out.println("Computer".compareTo("Engineer")); System.out.println("Computer".compareTo("Comp")); System.out.println("Computer".compareTo("Computing")); }} JLab>java StringCompare

true0-24-4JLab>

Page 54: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 : �������������� �����

Iteration : while, do-while, for

����������������� ��

������������������

2110101 ���������������� ��� 15/7/2002 2

�����

• ������� do-while

• ������� while

• ������� for

• ������!#�����%%���&�%

• ������� break ��' continue

2110101 ���������������� ��� 15/7/2002 3

���������������

import jlab.JLabIO;

public class Mean {public static void main(String [] args) {

double s = 0.0;s += JLabIO.readDouble("data #1 : ");s += JLabIO.readDouble("data #2 : ");s += JLabIO.readDouble("data #3 : ");s += JLabIO.readDouble("data #4 : ");s += JLabIO.readDouble("data #5 : ");System.out.println("mean = " + (s / 5.0) );

}}

import jlab.JLabIO;

public class Mean {public static void main(String [] args) {

double s = 0.0;s += JLabIO.readDouble("data #1 : ");s += JLabIO.readDouble("data #2 : ");s += JLabIO.readDouble("data #3 : ");s += JLabIO.readDouble("data #4 : ");s += JLabIO.readDouble("data #5 : ");System.out.println("mean = " + (s / 5.0) );

}} JLab>java Mean

data #1 : 12data #2 : 35.5data #3 : 16.8data #4 : 19.0data #5 : 2.2mean = 17.1JLab>

����������� ����

�������� 50 ������

������������������

2110101 ���������������� ��� 15/7/2002 4

������������� do-while

double s = 0.0;int n = 0;

do {s += JLabIO.readDouble();

n++;

} while( n < 50 );

System.out.println( s/50 );

double s = 0.0;int n = 0;

do {s += JLabIO.readDouble();

n++;

} while( n < 50 );

System.out.println( s/50 );

������������

� �������

false

true

�� ����������������� s

s / 50

s = 0, n = 0

n < 50 ?

n++

Page 55: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 5

������������� while

double s = 0.0;int n = 0;

while ( n < 50 ) {

s += JLabIO.readDouble();

n++;

}

System.out.println( s/50 );

double s = 0.0;int n = 0;

while ( n < 50 ) {

s += JLabIO.readDouble();

n++;

}

System.out.println( s/50 );

������������

� �������

false

true

�� ����������������� s

s / 50

s = 0, n = 0

n < 50 ?

n++

2110101 ���������������� ��� 15/7/2002 6

������� do-while ��! while

do {do this

} while ( Boolean expr );

do {do this

} while ( Boolean expr );

do this

Boolean expr.

false

true

while ( Boolean expr ) {do this

}

while ( Boolean expr ) {do this

}

do this

Boolean expr.false

true

2110101 ���������������� ��� 15/7/2002 7

��������������"��

• Requirement

– ��������������!" #��#�$����%������������������

• Analysis

– ��&��������'����%��#(�*'�*

– !"����!��������%�����

– #�$����%�����%����$%����,�*

• Design

– �����������

• Implementation

– �����-(�#���

+=+

k

kk

x

a

xx

2

1

1

2110101 ���������������� ��� 15/7/2002 8

�����!�#�������"��

• ��������������� �� a

• �� xk ����������� �� √a

• �� �� xk+1

= (xk + a/x

k)/2 ��������������

��� √a ������ xk (�����!��)

a = 4.0x0 = 1.0x1 = 2.5x2 = 2.05x3 = 2.000609756097561x4 = 2.0000000929222947x5 = 2.000000000000002x6 = 2.0

a = 100.0x0 = 1.0x1 = 50.5x2 = 26.24009900990099x3 = 15.025530119986813x4 = 10.840434673026925x5 = 10.032578510960604x6 = 10.000052895642693x7 = 10.000000000139897x8 = 10.0

Page 56: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 9

���$!��!�#�%�"�������� ?

• ���� xk+1

= (xk + a/x

k)/2 k = 1,2,3,...

�����#�$ x0 = 1

• ������ xk+1

�%& xk ��������$��%����

• x '�� y ('&& double) ��������$��%������*��

(�� ε ������%& 10-14

)

( )ε≤

yx

yx

,max

2110101 ���������������� ��� 15/7/2002 10

�����������������"��

������������

� �������

false

true

�� ������������ a

y

y = 1.0

x = y

y = (x + a/x)/2

( )ε>

yx

yx

,max

2110101 ���������������� ��� 15/7/2002 11

��������������"��

import jlab.JLabIO;public class Sqrt {public static void main(String[] args) {double x, y = 1.0;double a = JLabIO.readDouble("a = ");do {x = y;y = (x + a / x) / 2.0;

} while ((Math.abs(x - y) /Math.max(Math.abs(x), Math.abs(y)))>= 1E-14);

System.out.println("sqrt(" + a + ") = " + y);}

}

import jlab.JLabIO;public class Sqrt {public static void main(String[] args) {double x, y = 1.0;double a = JLabIO.readDouble("a = ");do {x = y;y = (x + a / x) / 2.0;

} while ((Math.abs(x - y) /Math.max(Math.abs(x), Math.abs(y)))>= 1E-14);

System.out.println("sqrt(" + a + ") = " + y);}

} JLab>java Sqrtx = 9E200sqrt(9.0E200) = 3.0E100JLab>

JLab>java Sqrtx = 9E200sqrt(9.0E200) = 3.0E100JLab>��������� 339 ��

2110101 ���������������� ��� 15/7/2002 12

�������&������(��)(�*��

• ��� (file) ������ �������������������������

(�!��"�#$��� harddisk)

• �%�&����'������"�

• Text file ��������������� ��%�)� �$�����"!+

�%�&����"!�"�!����"��,-�����"!���

4531001321 3

4531003621 3

4531004221 4

4531005921 3

4531006521 3

4531101421 4

4531102021 4

4531102221 0

4531001321 3

4531003621 3

4531004221 4

4531005921 3

4531006521 3

4531101421 4

4531102021 4

4531102221 0

C:\DATA\SCORE.TXT

Page 57: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 13

���+, JLabIO ����&����

import java.io.*;import jlab.JLabIO;

// open fileBufferedReader in = JLabIO.openFile("c:\dat.txt");...// read the next line from fileString txt = in.readLine();if (txt == null) // end of file...// close the filein.close();

import java.io.*;import jlab.JLabIO;

// open fileBufferedReader in = JLabIO.openFile("c:\dat.txt");...// read the next line from fileString txt = in.readLine();if (txt == null) // end of file...// close the filein.close();

2110101 ���������������� ��� 15/7/2002 14

�������".� text file ���$�/��

import jlab.JLabIO;import java.io.*;

public class DumpFile1 {public static void main(String[] args)

throws IOException {

String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);String txt;do {txt = in.readLine();if (txt != null) System.out.println(txt);

} while ( txt != null );in.close();

}}

import jlab.JLabIO;import java.io.*;

public class DumpFile1 {public static void main(String[] args)

throws IOException {

String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);String txt;do {txt = in.readLine();if (txt != null) System.out.println(txt);

} while ( txt != null );in.close();

}}

2110101 ���������������� ��� 15/7/2002 15

�������".� text file ���$�/��

import jlab.JLabIO;import java.io.*;

public class DumpFile2 {public static void main(String[] args)

throws IOException {

String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);String txt;while ( (txt = in.readLine()) != null ) {System.out.println(txt);

}in.close();

}}

import jlab.JLabIO;import java.io.*;

public class DumpFile2 {public static void main(String[] args)

throws IOException {

String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);String txt;while ( (txt = in.readLine()) != null ) {System.out.println(txt);

}in.close();

}}

2110101 ���������������� ��� 15/7/2002 16

���%���� throws IOException ��� main ?

• ��������)!��.)!/��!�&������������� 0,�

��������"���������#!����$�&���

(IOException)

– ����������������� (��������������)

– ����������!������ ������������"!���#$��� (���

������ diskette ����%��&��#�������)

• �/��������� ��#�������.)!/��!�����-

• ��������.)!/��!��� IOException ���&��

(%"��$�������������#����"�.)!'��)

IOException ������� class ����*��%���++��,�

Page 58: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 17

���������������

• Requirement

– -�.��/!���������!&$�-��%����

• Analysis

– ��%���+0������������"��"

– �������!��2+�3��,�����+��&�!�-�#���3��,�

– %� Double.parseDouble(s) ��!������ s ��� double

– �����3��,�����! �!�.��/!���-�������4�"

• Design

– �����&����

• Implementation

– ����5������

2110101 ���������������� ��� 15/7/2002 18

������������������

false

������������

� �������

true

fn = �������

n, sum / n

��������������

�����! �� txt

�"� txt �"����������$����� sum

�$���%��� n ��� 1

�"'��� fn, n = 0, sum = 0

2110101 ���������������� ��� 15/7/2002 19

���������������

import jlab.JLabIO;import java.io.*;public class Average1 {public static void main(String[] args)

throws IOException {String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);int n = 0;double sum = 0.0;while ((txt = in.readLine()) != null) {sum += Double.parseDouble(txt);n++;

}in.close();System.out.println("n = " + n +

" avg = " + sum / n);}

}

import jlab.JLabIO;import java.io.*;public class Average1 {public static void main(String[] args)

throws IOException {String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);int n = 0;double sum = 0.0;while ((txt = in.readLine()) != null) {sum += Double.parseDouble(txt);n++;

}in.close();System.out.println("n = " + n +

" avg = " + sum / n);}

}

2110101 ���������������� ��� 15/7/2002 20

�����������"0.

• Requirement

– -�.�����0��������!&$�-��%����

• Analysis

– ��%���+0������������"��"

– �������!��2+�3��,�����+��&�!�-�#���3��,�

– ����.�����0��-�������4�"

• Design

– �����&����

• Implementation

– ����5������

Page 59: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 21

��������"0.

x 12 34 8 9 0 44 9

max -∞ 12 34 34 34 34 44 44

�����!�����!�.� max

!3��&+�������!����������

2110101 ���������������� ��� 15/7/2002 22

��������������"0.

false

������������

� �������

true

fn = �������

max

��������������

�����! �� txt

�"� txt �"��������� x

�"'��� fn, max = -Infinity

x > max

max = xtrue

false

2110101 ���������������� ��� 15/7/2002 23

�����������"0.

import jlab.JLabIO;import java.io.*;public class MaxValue {public static void main(String[] args)

throws IOException {

String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);double x, max = Double.NEGATIVE_INFINITY;while ((txt = in.readLine()) != null) {x = Double.parseDouble(txt);if (x > max) max = x;

}in.close();System.out.println("max = " + max);

}}

import jlab.JLabIO;import java.io.*;public class MaxValue {public static void main(String[] args)

throws IOException {

String fn = JLabIO.readString("file name : ");BufferedReader in = JLabIO.openFile(fn);double x, max = Double.NEGATIVE_INFINITY;while ((txt = in.readLine()) != null) {x = Double.parseDouble(txt);if (x > max) max = x;

}in.close();System.out.println("max = " + max);

}}

2110101 ���������������� ��� 15/7/2002 24

���+, while ��������� EAN-13

import jlab.JLabIO;public class EAN13CheckDigit {

public static void main(String[] args) {int s = 0;String d = JLabIO.readString("Enter 12 digits : ");s += Character.digit(d.charAt(1), 10);s += Character.digit(d.charAt(3), 10);s += Character.digit(d.charAt(5), 10);s += Character.digit(d.charAt(7), 10);s += Character.digit(d.charAt(9), 10);s += Character.digit(d.charAt(11), 10);s *= 3;s += Character.digit(d.charAt(0), 10);s += Character.digit(d.charAt(2), 10);s += Character.digit(d.charAt(4), 10);s += Character.digit(d.charAt(6), 10);s += Character.digit(d.charAt(8), 10);s += Character.digit(d.charAt(10), 10);System.out.println("Check Digit is " + (10-(s%10))%10);

}}

import jlab.JLabIO;public class EAN13CheckDigit {

public static void main(String[] args) {int s = 0;String d = JLabIO.readString("Enter 12 digits : ");s += Character.digit(d.charAt(1), 10);s += Character.digit(d.charAt(3), 10);s += Character.digit(d.charAt(5), 10);s += Character.digit(d.charAt(7), 10);s += Character.digit(d.charAt(9), 10);s += Character.digit(d.charAt(11), 10);s *= 3;s += Character.digit(d.charAt(0), 10);s += Character.digit(d.charAt(2), 10);s += Character.digit(d.charAt(4), 10);s += Character.digit(d.charAt(6), 10);s += Character.digit(d.charAt(8), 10);s += Character.digit(d.charAt(10), 10);System.out.println("Check Digit is " + (10-(s%10))%10);

}}

Page 60: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 25

���+, while ��������� EAN-13

import jlab.JLabIO;public class EAN13CheckDigit {

public static void main(String[] args) {int s = 0, k;String d = JLabIO.readString("Enter 12 digits : ");

k = 1;while (k<12) {

s += Character.digit(d.charAt(k), 10);k += 2;

}s *= 3;

k = 0;while (k<12) {

s += Character.digit(d.charAt(k), 10);k += 2;

}System.out.println("Check Digit is " + (10-(s%10))%10);

}}

import jlab.JLabIO;public class EAN13CheckDigit {

public static void main(String[] args) {int s = 0, k;String d = JLabIO.readString("Enter 12 digits : ");

k = 1;while (k<12) {

s += Character.digit(d.charAt(k), 10);k += 2;

}s *= 3;

k = 0;while (k<12) {

s += Character.digit(d.charAt(k), 10);k += 2;

}System.out.println("Check Digit is " + (10-(s%10))%10);

}}

2110101 ���������������� ��� 15/7/2002 26

���+, while ��������� EAN-13

import jlab.JLabIO;public class EAN13CheckDigit {

public static void main(String[] args) {int s = 0, k;String d = JLabIO.readString("Enter 12 digits : ");

k = 0;while (k<12) {

if ( (k % 2) == 0 ) {s += Character.digit(d.charAt(k), 10);

} else {s += 3 * Character.digit(d.charAt(k), 10);

}k++;

}

System.out.println("Check Digit is " + (10-(s%10))%10);}

}

import jlab.JLabIO;public class EAN13CheckDigit {

public static void main(String[] args) {int s = 0, k;String d = JLabIO.readString("Enter 12 digits : ");

k = 0;while (k<12) {

if ( (k % 2) == 0 ) {s += Character.digit(d.charAt(k), 10);

} else {s += 3 * Character.digit(d.charAt(k), 10);

}k++;

}

System.out.println("Check Digit is " + (10-(s%10))%10);}

}

2110101 ���������������� ��� 15/7/2002 27

while ��! do-while

• ���������!�� while �$����$� do-while #!

• ���������!�� do while �$����$� while #!

• �$�������������������������������� ������

• while : %������������#��$���)� �,��&���

– �������3�%� block

• do-while ���#$�����+ %������������#��"��$���)�

– ����������3�%� block -�#��.�&$�

2110101 ���������������� ��� 15/7/2002 28

�������������$�������(�����������.

• �%"��$�����"�%�����������

i = 0;while (i < n) {

// do something here

i++;}

i = 0;while (i < n) {

// do something here

i++;}

i = 1;while (i <= n) {

// do something here

i++;}

i = 1;while (i <= n) {

// do something here

i++;}

i = n;while (i > 0) {

// do something here

i--;}

i = n;while (i > 0) {

// do something here

i--;}

i = n-1;while (i >= 0) {

// do something here

i--;}

i = n-1;while (i >= 0) {

// do something here

i--;}

Page 61: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 29

�����!���

i = 1;while (i < n) {

// do something here

i++;}

i = 1;while (i < n) {

// do something here

i++;}

• ����!��,���� ����)���,���� ������%)! loop

i = 0;while (i <= n) {

// do something here

i++;}

i = 0;while (i <= n) {

// do something here

i++;}

i = n;while (i > 1) {

// do something here

i--;}

i = n;while (i > 1) {

// do something here

i--;}

i = 0;while (i != n) {// The test above can// cause infinite// loop.i++;

}

i = 0;while (i != n) {// The test above can// cause infinite// loop.i++;

}

2110101 ���������������� ��� 15/7/2002 30

������� for

• ���������$���������"-�%��������! (counting

loop) �$�������"�/�������

• %"�3�4��,������"� for �/����� counting loop

i = 1;while (i <= n) {// do something herei++;

}

i = 1;while (i <= n) {// do something herei++;

}

for (i = 1; i <= n; i++) {// do something here

}

for (i = 1; i <= n; i++) {// do something here

}

i = n;while (i > 0) {// do something herei--;

}

i = n;while (i > 0) {// do something herei--;

}

for (i = n; i > 0; i--) {// do something here

}

for (i = n; i > 0; i--) {// do something here

}

2110101 ���������������� ��� 15/7/2002 31

������� for

for ( initialization ; condition ; update ) {do this

}

for ( initialization ; condition ; update ) {do this

}

update

conditionfalse

true

initialization

do this

2110101 ���������������� ��� 15/7/2002 32

comma

for ( i = 0, j = n-1; i < j; i++, j--) {do this

}

for ( i = 0, j = n-1; i < j; i++, j--) {do this

}

i++, j--

i < jfalse

true

i = 0, j = n-1

do this

Page 62: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 33

�������������� for

for (i = 1; i <= n; i++) {System.out.println(i);

}

for (i = 1; i <= n; i++) {System.out.println(i);

}

for (i = 1; i <= n; i += 2) {System.out.println(i);

}

for (i = 1; i <= n; i += 2) {System.out.println(i);

}

n = JLabIO.readInt("n = ");for (i = 2; (i < n) && (n % i != 0); i++) {}if (i < n) {System.out.println( n + " = " + i + "x" + (n/i) );

} else {System.out.println( n + " is prime.");

}

n = JLabIO.readInt("n = ");for (i = 2; (i < n) && (n % i != 0); i++) {}if (i < n) {System.out.println( n + " = " + i + "x" + (n/i) );

} else {System.out.println( n + " is prime.");

}

2110101 ���������������� ��� 15/7/2002 34

��2�� block ��)� ��2�� statement

for ( initialization ; condition ; update ) {statements

}

for ( initialization ; condition ; update ) {statements

}

for ( initialization ; condition ; update )statement ;

for ( initialization ; condition ; update )statement ;

do {statements

} while ( Boolean expr );

do {statements

} while ( Boolean expr );

while ( Boolean expr ) {statements

}

while ( Boolean expr ) {statements

}

dostatement ;

while ( Boolean expr );

dostatement ;

while ( Boolean expr );

while ( Boolean expr )statement ;

while ( Boolean expr )statement ;

2110101 ���������������� ��� 15/7/2002 35

���+, for � .���30��!"�

double x = 0, y = 1;double a = JLabIO.readDouble("a = ");while ((Math.abs(x - y) /

Math.max(Math.abs(x), Math.abs(y)))>= 1E-14) {

x = y;y = (x + a / x) / 2.0;

}System.out.println("sqrt(" + a + ") = " + y);

double x = 0, y = 1;double a = JLabIO.readDouble("a = ");while ((Math.abs(x - y) /

Math.max(Math.abs(x), Math.abs(y)))>= 1E-14) {

x = y;y = (x + a / x) / 2.0;

}System.out.println("sqrt(" + a + ") = " + y);

double x = 0, y = 1;double a = JLabIO.readDouble("a = ");for ( ;

(Math.abs(x - y) /Math.max(Math.abs(x), Math.abs(y))) >= 1E-14);

x = y, y = (x + a / x) / 2.0 ) ;System.out.println("sqrt(" + a + ") = " + y);

double x = 0, y = 1;double a = JLabIO.readDouble("a = ");for ( ;

(Math.abs(x - y) /Math.max(Math.abs(x), Math.abs(y))) >= 1E-14);x = y, y = (x + a / x) / 2.0 ) ;

System.out.println("sqrt(" + a + ") = " + y);

2110101 ���������������� ��� 15/7/2002 36

��"��� break ��)�����$������

while ((t = in.readLine()) != null) {if ("END".equals(t)) break;x = Double.parseDouble(t);if (x > max) max = x;

}

while ((t = in.readLine()) != null) {if ("END".equals(t)) break;x = Double.parseDouble(t);if (x > max) max = x;

}

��%�-�.�����0�����3��,����2+

%��������!���+��&���.3�,�

END

false

true

break

. . .

. . .

Page 63: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 37

��"��� continue ��)������%�������

false

true

continue

while ((t = in.readLine()) != null) {x = Double.parseDouble(t);if (x <= 0) continue;if (x > max) max = x;

}

while ((t = in.readLine()) != null) {x = Double.parseDouble(t);if (x <= 0) continue;if (x > max) max = x;

}

��%�-�.�����0�����/"���3��,�

��2�+,� ���2+%��������!

. . .

. . .

2110101 ���������������� ��� 15/7/2002 38

Nested Loops

• 3����������,��&��������� #!

for �� while while �� while

do-while ��

while

do-while ��

do-while

break ��( continue )�*������' �!�(������ �����+�

2110101 ���������������� ��� 15/7/2002 39

Mandelbrot Set

• �����!"����������

�')�0�� dn

�����5#!���

dn = (d

n-1)2 +c �!��

d0 = 0+i0 = 0 ��&

c �$��������')�0��

• c �$����')����

Mandelbrot set 6����!"�

��� |dn| �����������%"�

��� x �������������� ��� y ��������������,)�$

��''�����"%���������� Mandelbrot set

2110101 ���������������� ��� 15/7/2002 40

��������!4#$������, �5��

• | (x+iy) | = (x2 + y

2)1/2

• (x+iy)+(p+iq) = (x+p) + i(y+q)

• (x+iy)(p+iq) = (xp-yq) + i(xq+yp)

�&,���������!�����!����.� r

c = (0.2+i0.2) �(-'

0.344, 0.351, 0.327, 0.305, 0.304, 0.312, 0.316, 0.316, 0.314,

0.313, 0.313, 0.313, 0.314, 0.314, 0.314, ...(converge)

c = (1.0+i1.0) �(-'

3.162, 9.899, 97.01, 9409, 8.853E7, 7.837E15, 6.142E31,

3.773E63, 1.424E127, ... (diverge to infinity)

Page 64: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 41

�������".� Mandelbrot set

public void paint(Graphics g) {double xnew, xnew, xold, xold, x, y;double stepX = 3.0 / getSize().width;double stepY = 3.0 / getSize().height;int n;for (y = -1.5; y <= 1.5; y += stepY) {

for (x = -2.0; x <= 1.0; x += stepX) {for (xold = x, yold = y, n = 0; (n < 256); n++) {

xnew = xold * xold - yold * yold + x;ynew = 2 * xold * yold + y;if (Math.sqrt(xnew * xnew + ynew * ynew) > 2) break;xold = xnew;yold = ynew;

}if (n >= 256) {

g.drawRect((int)((x + 2) / stepX),(int)((y + 1.5) / stepY), 1, 1);

}}

}}

public void paint(Graphics g) {double xnew, xnew, xold, xold, x, y;double stepX = 3.0 / getSize().width;double stepY = 3.0 / getSize().height;int n;for (y = -1.5; y <= 1.5; y += stepY) {

for (x = -2.0; x <= 1.0; x += stepX) {for (xold = x, yold = y, n = 0; (n < 256); n++) {

xnew = xold * xold - yold * yold + x;ynew = 2 * xold * yold + y;if (Math.sqrt(xnew * xnew + ynew * ynew) > 2) break;xold = xnew;yold = ynew;

}if (n >= 256) {

g.drawRect((int)((x + 2) / stepX),(int)((y + 1.5) / stepY), 1, 1);

}}

}}

2110101 ���������������� ��� 15/7/2002 42

Visualizing Mandelbrot Set

-2.0 1.0

-1.5

1.5

-0.734 -0.726

0.215

0.226

Page 65: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 : �������������� �����

Methods

����������������� ��

������������������

2110101 ���������������� ��� 15/7/2002 2

�����

• �������� methods

• Methods ���� ��� class Math

• ����������� method

• �� !��"�

• Overloading

2110101 ���������������� ��� 15/7/2002 3

��������������

• �%�����&�'�����

– ������

– debug ���

– ��� ��� ���������

• ��(�� �����&�'���)�*�

– ��������������������

� ����� ���������

– ������������!��� ����

class Human {

}

class Human {

}

void main(...) {

}

void main(...) {

}

2110101 ���������������� ��� 15/7/2002 4

��������������

• ���'�

– ��" ����� ��������

– ��������" ��������!���

#���� ��������

• !��(�+��� ���+!,��

subroutine, subprogram,

�-� method

• '��� method !�+��!)"+

��� ������ .+��.

class Human {

}

class Human {

}

main(...) {born();grown();sick();dead();

}

main(...) {born();grown();sick();dead();

}

born(...) {

}

born(...) {

}

grown(...) {

}

grown(...) {

}

sick(...) {

}

sick(...) {

}

dead(...) {

}

dead(...) {

}

Page 66: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 5

��������������������

• ��./0�&�'� ���!) �� �!�+*�

• ��./0������1�(�)��������!)!"�"�*. (Java API)

• +1�����!)!��4�'�*� ('5 .java)

• +1�����!)!��4�"�"� ('5 .class)

• �!�+�1�(�)� �-)� �-)�������!)!��4"�"�*.

• /��1� �+ �0� �!�+"�

– �����$������� (readability)

– �����$������� (maintainability)

– �����$����������� $%$�����&��� �'$����#�

(reuseability)

2110101 ���������������� ��� 15/7/2002 6

������� method �������

System.out.println("Hello World");System.out.println("Hello World");

int x = JLabIO.readInt("x = ");int x = JLabIO.readInt("x = ");

double r = Math.random();double r = Math.random();

�� "Hello World" ����� System.out.println(�� ������������)

�� "x = " ����� JLabIO.readInt (�� ������������

����������������������������������) ���! "����#���������

����&���� ����!�������� x

!����'!� Math.random ���'! "����#�������������&����

(*+����������������,��-�� 0.0 .+� 1.0) ����!�������� x

2110101 ���������������� ��� 15/7/2002 7

Methods ���� ���!�" Math

• abs( x ) : absolute value

• acos( x ) : arc cosine

• asin( x ) : arc sine

• atan( x ) : arc tangent

• cos( x ) : trigonometric cosine

• sin( x ) : trigonometric sine

• tan( x ) : trigonometric tangent

• exp( x ) : ex

• log( x ) : natural logarithm (base e)

• pow( x, y ) : xy

2110101 ���������������� ��� 15/7/2002 8

Methods ���� ���!�" Math (��)

• ceil( x ) : ceiling

• floor( x ) : floor

• max( x, y ) : the greater of x and y

• min( x , y ) : the smaller of x and y

• random() : a random number in [0.0, 1.0)

• round( x ) : the closest integer to x

• sqrt( x ) : positive square root of x

• toDegrees( x ) : convert angle in radian to degree

• toRadians( x ) : convert angle in degree to radian

Page 67: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 9

������� Java API Help File

public static double sin(double a)

Returns the trigonometric sine of an angle. Special cases:

• If the argument is NaN or an infinity, then the result is

NaN.

• If the argument is zero, then the result is a zero with the

same sign as the argument.

A result must be within 1 ulp of the correctly rounded

result. Results must be semi-monotonic.

Parameters:

a - an angle, in radians.

Returns:

the sine of the argument.

ulp - unit of the last place

2110101 ���������������� ��� 15/7/2002 10

��������#�$����"���������

• Requirement

– ����� (� ���*�) �+��������������

• Analysis

– ���������-�'��.����� (0, 0)

– ����������-�� �0.�0 "'��+�����������-�' ���

(uX, u

Y) �+ (v

X, v

Y)

– �1����������#&�

– �'����������'���-�2�0

( )( )

++

+

=−

2222

1

cos

YXYX

YYXX

vvuu

vuvu

θ

���������

(uX, u

Y)

(vX, v

Y)

θ

(0, 0)

2110101 ���������������� ��� 15/7/2002 11

�������� � �� ������������

( )( )

++

+

=2222

)cos(

YXYX

YYXX

vvuu

vuvu

θ

cosine = (ux * vx + uy * vy) /(Math.sqrt(ux * ux + uy * uy) *Math.sqrt(vx * vx + vy * vy));

cosine = (ux * vx + uy * vy) /(Math.sqrt(ux * ux + uy * uy) *Math.sqrt(vx * vx + vy * vy));

cosine = (ux * vx + uy * vy) /(length(ux, uy) * length(vx, vy));

public static double length(double x, double y) {return Math.sqrt(x * x + y * y);

}

cosine = (ux * vx + uy * vy) /(length(ux, uy) * length(vx, vy));

public static double length(double x, double y) {return Math.sqrt(x * x + y * y);

}

2110101 ���������������� ��� 15/7/2002 12

��������������������������

import jlab.JLabIO;public class A {public static void main(String[] args) {double ux = JLabIO.readDouble("ux = ");double uy = JLabIO.readDouble("uy = ");double vx = JLabIO.readDouble("vx = ");double vy = JLabIO.readDouble("vy = ");

double cosine = (ux * vx + uy * vy) /(length(ux, uy) * length(vx, vy));

double degree = Math.toDegrees(Math.acos(cosine));System.out.println(JLabIO.format(degree, 5, 2));

}

public static double length(double x, double y) {return Math.sqrt(x * x + y * y);

}}

import jlab.JLabIO;public class A {public static void main(String[] args) {double ux = JLabIO.readDouble("ux = ");double uy = JLabIO.readDouble("uy = ");double vx = JLabIO.readDouble("vx = ");double vy = JLabIO.readDouble("vy = ");

double cosine = (ux * vx + uy * vy) /(length(ux, uy) * length(vx, vy));

double degree = Math.toDegrees(Math.acos(cosine));System.out.println(JLabIO.format(degree, 5, 2));

}

public static double length(double x, double y) {return Math.sqrt(x * x + y * y);

}}

Page 68: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 13

���������!��������

public static double length(double x, double y) {double len;len = Math.sqrt(x * x + y * y);return len;

}

public static double length(double x, double y) {double len;len = Math.sqrt(x * x + y * y);return len;

}

• ��������

– �����������������������

– ������� ������������������ ����������������!���#�

• �������

– ���$%�&�����'��

– �''��(�����������

– )����(��*�����'�

2110101 ���������������� ��� 15/7/2002 14

���������!��������

public static double length(double x, double y) {double len;len = Math.sqrt(x * x + y * y);return len;

}

public static double length(double x, double y) {double len;len = Math.sqrt(x * x + y * y);return len;

}

���� method

������ ��

�������������

���������

parameters

����������!�

��� method

return "�#���$���#����%�������� method #�'

(�����"�#�� �����������������������)

����*�� method

2110101 ���������������� ��� 15/7/2002 15

��������� : ����"�!��# !��$ �%&

• �� ��������������� (int, double, boolean, …)

• �� ��������������� (String, Point, …)

• ����� method ��!"����$����% '�'��)��� void

public static int min(int a, int b)public static int min(int a, int b)

public static String toString(int i, int radix)public static String toString(int i, int radix)

int m = min(a, 0);

String s = toString(a, 16);

public static void gc()public static void gc()

gc();

2110101 ���������������� ��� 15/7/2002 16

��������� : '()�

• *����+,�!�',*-������*�������

– ������&'%�&���� %�&��� %�& $ ,�� _ ��#�

– ,��-.�%��&'%�&���

– #�����/(��������)&�'&�������

– %�&����%�&�,0#��,����%�&����

– %��#�1.(���)(*�&������/&

• ./��!�"�,�!�������0����/�����*

• ��*��+,�!������'����*�1/�

fillCircle setEnabled turnLeft println start runfillCircle setEnabled turnLeft println start run

Page 69: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 17

��������� : ����!��%��*����

• ��/*����+ parameters

– parameter ���%�&��*(,���������������&���

– �%�� parameter ���'��,��������$%�&��

(�%#�%�����'�&' semi-colon)

– �%�� parameter )����&'/���) (comma)

– '���� parameters �'��'��&�����

(5#��� parameter ��6 ��#�%���*��#��&�����)

public static void turnLeft(double angle)public static void turnLeft(double angle)

public static double pow(double a, double b)public static double pow(double a, double b)

public static String readString()public static String readString()

2110101 ���������������� ��� 15/7/2002 18

���������

public static double readDouble(String msg) {

double x = 0; // local var. declarationboolean success = false; // local var. declaration

do {String s; // local var. declarations = readString(msg);try {

x = Double.parseDouble(s);success = true;

} catch (NumberFormatException e) {System.err.println("invalid real number, try again");success = false;

}} while (!success);

return x; // return to caller

}

public static double readDouble(String msg) {

double x = 0; // local var. declarationboolean success = false; // local var. declaration

do {String s; // local var. declarations = readString(msg);try {

x = Double.parseDouble(s);success = true;

} catch (NumberFormatException e) {System.err.println("invalid real number, try again");success = false;

}} while (!success);

return x; // return to caller

}

2110101 ���������������� ��� 15/7/2002 19

��������� : ���(����+���#$#��,��

• ���!�*���)�+�����)���!+ return *�2 ���*���)�+��

*���"���$����/* method

• �)����� method ��!"���*�����$����%

– ,���)(& return ���'�&' ; ��'

– 5)(*����'*����� method #��� return ��/��*������

return ,��������'*��

public static void printError(String msg) {if ( msg == null ) return;System.err.println( msg );

}

public static void printError(String msg) {if ( msg == null ) return;System.err.println( msg );

}

2110101 ���������������� ��� 15/7/2002 20

��������� : ���(����+���#$#��,��

• �)����� method ��!��*�����$����%

– ,���)(& return %��%��&'�7�/�

– )����7�/����)����������! %�������������'&���

������$#&���,�& method ,��������*�5/��(

promotion �,#�

– )����#��)%�&���'&

public static long clip(long a) {if ( a > 0 )return a;

elsereturn 0; \\ 0 is promoted to 0L

}

public static long clip(long a) {if ( a > 0 )return a;

elsereturn 0; \\ 0 is promoted to 0L

}

Page 70: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 21

��������� : Local Variables

• Local variables ����������!',��/'������

• �� *�3'� block ' ',"�4�� '� block ����

���� block ��!5���/�,���'�6 ��+ block ����

• �� *�3��!27' ',"���+27������/'� block

public static void test(int a) {int z = 7;{ int x = z; System.out.println( x ); }System.out.println( x ); // wrong{System.out.println( x ); // wrongint x = z; System.out.println( x );{ int y = z; System.out.println( y ); }{ int x = z; System.out.println( x ); } // wrong

}}

public static void test(int a) {int z = 7;{ int x = z; System.out.println( x ); }System.out.println( x ); // wrong{System.out.println( x ); // wrongint x = z; System.out.println( x );{ int y = z; System.out.println( y ); }{ int x = z; System.out.println( x ); } // wrong

}}

2110101 ���������������� ��� 15/7/2002 22

��������� : Local Variables

• ��*���+'���7*����+��!�)�+���0+27��!�� *�3

(���*��,8�'#�#��%7�)�7��%�)

• ��*�)���/�����+2� block ��!��*�� *�3

• ������+6 '������1���*���� local variables

import jlab.JLabIO;public class A {

public static void main(String[] args) {int s = 0, n = JLabIO.readInt("n = ");for (int i = 0; i <= n; i++) {

int i2 = i * i;s += i2;int i3 = i2 * i;s += i3;

}System.out.println("s = " + s );

}}

import jlab.JLabIO;public class A {

public static void main(String[] args) {int s = 0, n = JLabIO.readInt("n = ");for (int i = 0; i <= n; i++) {

int i2 = i * i;s += i2;int i3 = i2 * i;s += i3;

}System.out.println("s = " + s );

}}

i3

i2 s, ni

2110101 ���������������� ��� 15/7/2002 23

�*)��,)���$*����

public static printError(String msg) {System.err.println( msg );

}

public static printError(String msg) {System.err.println( msg );

}

public static int abs(int a) {if ( a < 0 ) a = -a;

}

public static int abs(int a) {if ( a < 0 ) a = -a;

}

public static float max(float x, y) {if ( x > y ) return x; else return y;

}

public static float max(float x, y) {if ( x > y ) return x; else return y;

}

public static double length(double dx, double dy) {double dx = Math.abs(dx);return Math.sqrt( dx*dx + dy*dy );

}

public static double length(double dx, double dy) {double dx = Math.abs(dx);return Math.sqrt( dx*dx + dy*dy );

}

public static float getBlackColor() {return 0.0;

}

public static float getBlackColor() {return 0.0;

}

2110101 ���������������� ��� 15/7/2002 24

����,��-'������

• *���+�����"� (arguments)

• *�����$���!"����� (returned value)

• "���������+"� ����"���$������ *�"

public static int power(int x , int y ) {int p = 1;for (int i=1; i<=y; i++)p *= x;

return p;}

public static int power(int x , int y ) {int p = 1;for (int i=1; i<=y; i++)p *= x;

return p;}

int a, b;...b = power( a+2 , 3 );...

int a, b;...b = power( a+2 , 3 );...

Page 71: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 25

����,��-'������ : ����!��#

• 2)�����������!�+�+�+*����!�� *�3"���!��������

• �1!+��!�+"��+����� �����!�������!�����������"

(��2)���� � ��2 �)� argument promotion '�)

x = max( (float) (a/3.0), 1f );y = max( Float.parseFloat(str), 1 ); // 1 -> 1.0f

x = max( (float) (a/3.0), 1f );y = max( Float.parseFloat(str), 1 ); // 1 -> 1.0f

x = max( (float) (a/2) ); // wrong number of argsy = max( "1.0", 1 ); // can't promote "1.0" to 1f

x = max( (float) (a/2) ); // wrong number of argsy = max( "1.0", 1 ); // can't promote "1.0" to 1f

���

���

public static float max(float x, float y) {. . .

}

public static float max(float x, float y) {. . .

}

2110101 ���������������� ��� 15/7/2002 26

����,��-'������ : $ �,)�(�� ��

• '�*�9���!�������*������� *��+�������50!+��

�� �����!�������+�������!�����"

public static float max(float x, float y) {. . .

}

public static float max(float x, float y) {. . .

}

double x = max( 1.0f, (int) (x/3) );int y = (int) max( 1, 2 );float z = max( max(x, y), -10.5f ) );

double x = max( 1.0f, (int) (x/3) );int y = (int) max( 1, 2 );float z = max( max(x, y), -10.5f ) );

long x = max( 4.0f, 2.3f ); // float to longfloat y = max( 4.0, 1.0 ); // double to floatmax( 2.0f, 1.0f ); // max returns float

long x = max( 4.0f, 2.3f ); // float to longfloat y = max( 4.0, 1.0 ); // double to floatmax( 2.0f, 1.0f ); // max returns float

���

���

2110101 ���������������� ��� 15/7/2002 27

������ : Integration

• ��+������!'*��:������6

• ��������!��+�� ��� �����$����

• ', midpoint rule �� ��9������!'*��:�+���

n

ab

h

=

∑∫=

+

n

i

ii

b

a

xx

hfdxxf

1

1

2

)(

x0

x1x2

xn-1

xn

f(x)

2110101 ���������������� ��� 15/7/2002 28

Integration using Midpoint Rule

public class Integration {public static void main(String[] args) {

System.out.println(simpson(0, 10, 1000));}public static double midpoint(double a, double b, int n) {

double h, s1, x;h = (b - a) / n;s1 = 0.0;x = a + h / 2.0;for (int i = 1; i <= n; i++) {

s1 += h*f(x);x += h;

}return s1;

}public static double f(double x) {

return 2 * x * x;}

}

public class Integration {public static void main(String[] args) {

System.out.println(simpson(0, 10, 1000));}public static double midpoint(double a, double b, int n) {

double h, s1, x;h = (b - a) / n;s1 = 0.0;x = a + h / 2.0;for (int i = 1; i <= n; i++) {

s1 += h*f(x);x += h;

}return s1;

}public static double f(double x) {

return 2 * x * x;}

}

∑∫=

+

n

i

ii

b

a

xx

hfdxxf

1

1

2

)(

Page 72: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 29

������� : Integration

x0

x1x2

xn-1

xn

f(x)

• ��������� �����������

• ����������� ����� ����������

• �! Simpson's rule �����"������ ����#$���

n

ab

h

=

+

++

++≈ ∑∑∫

=

=

)()(2)(4)(

3

)(

2

,...6,4,2

1

,...5,3,1

bfihafihafaf

h

dxxf

n

i

n

i

b

a

2110101 ���������������� ��� 15/7/2002 30

Integration using Simpson's Rule

public class Integration {public static void main(String[] args) {

System.out.println(simpson(0, 10, 1000));}public static double simpson(double a, double b, int n) {

double h, s1, s2, x;h = (b - a) / n;s1 = s2 = 0.0;x = a;for (int i = 1; i <= n; i++) {

x += h;if (i % 2 == 1) // is odd ?

s1 += f(x);else

s2 += f(x);}return (h / 3 * (f(a) + 4 * s1 + 2 * s2 + f(b)));

}public static double f(double x) {

return 2 * x * x;}

}

public class Integration {public static void main(String[] args) {

System.out.println(simpson(0, 10, 1000));}public static double simpson(double a, double b, int n) {

double h, s1, s2, x;h = (b - a) / n;s1 = s2 = 0.0;x = a;for (int i = 1; i <= n; i++) {

x += h;if (i % 2 == 1) // is odd ?

s1 += f(x);else

s2 += f(x);}return (h / 3 * (f(a) + 4 * s1 + 2 * s2 + f(b)));

}public static double f(double x) {

return 2 * x * x;}

}

+

++

++ ∑∑

=

=

)()(2)(4)(

3

2

,...6,4,2

1

,...5,3,1

bfihafihafaf

hn

i

n

i

2110101 ���������������� ��� 15/7/2002 31

������� : ������������������������

• t=0 : ���������� �����������

• t=1 : ������� �����!���� �!�

• ����"�#

• $�! �!������"�#

)1()1(4)()1(5.05.0 tt

eeetI

−−−−

−−=

∫= dttItQ )()(

C

tQ

tV

)(

)( =

V(t)

2110101 ���������������� ��� 15/7/2002 32

Voltage Across a Capacitor

public class CapacitorVoltage {public static void main(String[] args) {

double c = 0.05; // capacitancedouble v, q; // voltage and chargedouble a = 1.0, b = 10.0; // 1 to 10 seconds

for (double t = a; t <= b; t++) {q = simpson(a, t, 1000);v = q / c;System.out.println(t + "\t" + q + "\t" + v);

}}public static double simpson(double a, double b, int n) {

...}public static double f(double x) {

return 4 * (1 - Math.exp(-0.5))* Math.exp(-0.5 * (t - 1))* (1 - Math.exp(-t));

}}

public class CapacitorVoltage {public static void main(String[] args) {

double c = 0.05; // capacitancedouble v, q; // voltage and chargedouble a = 1.0, b = 10.0; // 1 to 10 seconds

for (double t = a; t <= b; t++) {q = simpson(a, t, 1000);v = q / c;System.out.println(t + "\t" + q + "\t" + v);

}}public static double simpson(double a, double b, int n) {

...}public static double f(double x) {

return 4 * (1 - Math.exp(-0.5))* Math.exp(-0.5 * (t - 1))* (1 - Math.exp(-t));

}}

Page 73: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 33

Method Overloading

• ����������� � � � �� �� �����������������

• ���������������������� �!�"�

public static int max(int x, int y) {if ( x > y ) return x; else return y;

}public static double max(double x, double y) {

if ( x > y ) return x; else return y;}public static double max(double x, double y, double z) {

if ( x > y && x > z ) return x;else if ( y > x && y > z ) return y;else return z;

}

public static int max(int x, int y) {if ( x > y ) return x; else return y;

}public static double max(double x, double y) {

if ( x > y ) return x; else return y;}public static double max(double x, double y, double z) {

if ( x > y && x > z ) return x;else if ( y > x && y > z ) return y;else return z;

}

2110101 ���������������� ��� 15/7/2002 34

Method Overloading

• ��! � �� overload ��������� �� �� ������

������ #�$���%�&��'(���)����������

• )* ���������������&�+� � ������ ��'���$���%�

&��'( �������*����������"�

public static int max(int x, int y) {if ( x > y ) return x; else return y;

}

public static double max(int x, int y) {if ( x > y ) return (double) x; else return (double) y;

}

public static int max(int a, int b) {if ( x > y ) return a; else return b;

}

public static int max(int x, int y) {if ( x > y ) return x; else return y;

}

public static double max(int x, int y) {if ( x > y ) return (double) x; else return (double) y;

}

public static int max(int a, int b) {if ( x > y ) return a; else return b;

}

Page 74: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 : �������������� �����

Arrays

����������������� ��

������������������

2110101 ���������������� ��� 15/7/2002 2

�����

• �� �

– ��������� ������ ����

• ������ ��� ����

• ��� !�"�

• �� #���$���%!�"�

• �� ��������

2110101 ���������������� ��� 15/7/2002 3

������������ � �������

• #&'!�"� ��%��� �� #� + �*&

• �*& #��,�&� 0 -.� 4 (�$� � ��)

• ����"��# �� #� 2� 0 �#3� 1 �#3� ... 4 �#3�

4531001321 3

4531003621 3

4531004221 4

4531005921 3

4531006521 3

4531101421 4

4531102021 4

4531102221 0

����� ��������� ��

0 1

1 0

2 0

3 4

4 3

2110101 ���������������� ��� 15/7/2002 4

��������������� � �������

������������

� �������

false

true�� �������

sc0, sc1, sc2

sc3, sc4

�� sc0, sc1, sc2

sc3, sc4 ��� 0

������

not EOF

s = ��������

s = ?

sc0++ sc1++ sc2++ sc3++ sc4++

0 1 2 3 4

s = ��������

1

1

Page 75: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 5

������������ � �������

import jlab.JLabIO;import java.io.*;public class Frequency1 {

public static void main(String[] args) throws IOException {String txt, t;int sc, score0, score1, score2, score3, score4;BufferedReader in = JLabIO.openFile(JLabIO.readString(">"));score0 = score1 = score2 = score3 = score4 = 0;while ((txt = in.readLine()) != null) {

t = txt.substring(10, txt.length());switch ( Integer.parseInt(t.trim()) ) {

case 0 : score0++; break;case 1 : score1++; break;case 2 : score2++; break;case 3 : score3++; break;case 4 : score4++; break;default : System.out.println("data error -> " + txt);

}}in.close();// Five more lines to display score frequencies...

import jlab.JLabIO;import java.io.*;public class Frequency1 {

public static void main(String[] args) throws IOException {String txt, t;int sc, score0, score1, score2, score3, score4;BufferedReader in = JLabIO.openFile(JLabIO.readString(">"));score0 = score1 = score2 = score3 = score4 = 0;while ((txt = in.readLine()) != null) {

t = txt.substring(10, txt.length());switch ( Integer.parseInt(t.trim()) ) {

case 0 : score0++; break;case 1 : score1++; break;case 2 : score2++; break;case 3 : score3++; break;case 4 : score4++; break;default : System.out.println("data error -> " + txt);

}}in.close();// Five more lines to display score frequencies...

2110101 ���������������� ��� 15/7/2002 6

������!"#���

0 1 2 3 4

1score

score[0]

0

score[1]

0

score[2]

4

score[3]

3

score[4]

1

score0

0

score1

0

score2

4

score3

3

score4

��!��� 5 ��! 5 ������!��� 5 ��! 5 ����

���"���# $��%&��' ���!��&(���� 1 �&(� 1 ����

)�*��&��"���# �"���*�&$����

���"���# $��%&��' ���!��&(���� 1 �&(� 1 ����

)�*��&��"���# �"���*�&$����

2110101 ���������������� ��� 15/7/2002 7

������������ � ������� (������!)

import jlab.JLabIO;import java.io.*;public class Frequency2 {

public static void main(String[] args) throws IOException {String txt, t;int sc;int [] score = new int[5]; // declare + allocate arrayBufferedReader in = JLabIO.openFile(JLabIO.readString(">"));

for (int i=0; i<5; i++) score[i] = 0; // unnecessary

while ((txt = in.readLine()) != null) {t = txt.substring(10, txt.length());sc = Integer.parseInt(t.trim());score[sc]++;

}

for (int i=0; i<5; i++) {System.out.println( i + " -> " + score[i] );

}}

}

import jlab.JLabIO;import java.io.*;public class Frequency2 {

public static void main(String[] args) throws IOException {String txt, t;int sc;int [] score = new int[5]; // declare + allocate arrayBufferedReader in = JLabIO.openFile(JLabIO.readString(">"));

for (int i=0; i<5; i++) score[i] = 0; // unnecessary

while ((txt = in.readLine()) != null) {t = txt.substring(10, txt.length());sc = Integer.parseInt(t.trim());score[sc]++;

}

for (int i=0; i<5; i++) {System.out.println( i + " -> " + score[i] );

}}

}

2110101 ���������������� ��� 15/7/2002 8

���%��"�� (Arrays)

• �� ��7��#3 ��%����!��!�"�8* �� �#����

• �� � ��%!�"���,��������9 �� 28� � ������$�

�7� $�!�"�� ��%� "���" �#3��� #����

• &��*�� �#�73��$���%

• ����� �!���*%��$�&� �!��!�"�� �� �

0 1 2 3 4 5 6 7 8 9 10 11

23 2 3 14 5 99 9 8 7 44 23 3a

System.out.println( a[6] + a[9] );

Page 76: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 9

��������&�%������������!

• �� ���� � ���

– ��������������

– ����������

int [] data;int [] data;

�����+��!�������!� data �",!��������*$� int

data = new int[10];data = new int[10];

.������*$� int ���!� 10 �� �&!�� data �����!���

.���� ��������*�"�,�.��$/2�

int [] data = new int[10];int [] data = new int[10];

�����+��!����&�.������*3�������&*�#,�

2110101 ���������������� ��� 15/7/2002 10

����������!����������!

• ��8*������&8�� � 8 ������#3 ��% "�#3��""

(reference) !���� ��#3�*���

int [] a

0

0

0

0

[1]

[0]

[2]

[3]

new int[4];=

2110101 ���������������� ��� 15/7/2002 11

�������� : ��������&+�������

• ��8*������&8�� � 22� 8 ���������� �

• ! ��!���� �-"��$�� ��� �������� � 2��

�� 8*������&8

• ! ��!���� � 8 ��!�� ��� ��2�

( data = new int[ 3*n + 1]; )

• �� ��#3���!., 2���- ��3�7���! ��2�

• *%%�*��,��� �3� !��!�"�� �� �<�����< ���

(�����" ���%�� �!���$� � &�* ���� false ��%

�� �!�� boolean)

2110101 ���������������� ��� 15/7/2002 12

�������� : ������� �����!

• �� �&-�� .3� ���#���&8�������������2�

int [] a

0

0

0

[0]

[3]

[1]

[2]

new int[4];=

int [] b = a;

a ��� b �������� ��� ��������������� �� ����

������� a[i] ��� b[i] �������������������� ����

a ��� b �������� ��� ��������������� �� ����

������� a[i] ��� b[i] �������������������� ����

0

Page 77: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 13

������'" ������" ��!

int[] data;

data = new int[4];

data[2] = 9;

data = new int[3];

int[] data;

data = new int[4];

data[2] = 9;

data = new int[3];

data

0 0 0 0 0

data

0 0 9 0 0

data

0 0 0 0

0 0 9 0 0

data

2110101 ���������������� ��� 15/7/2002 14

�����&+����+��)���� *��

int[] data = new int[4];data[0] = 23;data[1] = 3;data[2] = 21;data[3] = 47;

int[] data = new int[4];data[0] = 23;data[1] = 3;data[2] = 21;data[3] = 47;

int[] data = {23, 3, 21, 47};int[] data = {23, 3, 21, 47};

• �� initializer list *%��� �3�

• 2����$�� ��$� � ����#3�*���

• �$� � ����#3��� ����%�$� � !�"�� list

int[] data;data = {23, 3, 21, 47};

int[] data;data = {23, 3, 21, 47}; �� �� �����������������!�!�

2110101 ���������������� ��� 15/7/2002 15

�������,%������!

• ����� �!��� ( #���� index) !���� � 8

���������!�"�� �� � � data[2] 8 ��

������!�"�����#3 index 2 � �� � data

• index !���� � �3�#3 0 ��

• -��� �#! �� n ��� index ��� 8 �$� � ��

#��2���,�&� 0 -.� n-1

public class Array1 {public static void main(String [] args) {

int [] data = new int[100];int sum = 0;for(int i = 0; i < 100; i++)

sum += data[i];System.out.println( sum );

}}

public class Array1 {public static void main(String [] args) {

int [] data = new int[100];int sum = 0;for(int i = 0; i < 100; i++)

sum += data[i];System.out.println( sum );

}}

i = 0; i < 100i = 0; i < 100

2110101 ���������������� ��� 15/7/2002 16

������� ���,%���� "-

• -��� �#! �� n ���

index ��� 8 �$� � ��#��2���,�&� 0 -.� n-1

• index 2���$� � �� - compile error

• index 8 �$� � ���#3��" �����

– ���� ArrayIndexOutOfBoundsException ������

public class Array3 {public static void main(String [] args) {int [] data = new int[100];System.out.println( data[100] );

}}

public class Array3 {public static void main(String [] args) {int [] data = new int[100];System.out.println( data[100] );

}} JLab>java Array3

java.lang.ArrayIndexOutOfBoundsExceptionat Array3.main(Array3.java:4)

Exception in thread "main"JLab>

JLab>java Array3java.lang.ArrayIndexOutOfBoundsException

at Array3.main(Array3.java:4)Exception in thread "main"JLab>

Page 78: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 17

��������"��.*��".��

import jlab.JLabIO;public class Month {public static void main(String [] args) {String month[] =

{"JANUARY", "FEBRUARY", "MARCH", "APRIL","MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER","OCTOBER", "NOVEMBER", "DECEMBER"};

int m = JLabIO.readInt("Enter month number : ");if (1<=m && m<=12) System.out.println(month[m-1]);

}}

import jlab.JLabIO;public class Month {public static void main(String [] args) {String month[] =

{"JANUARY", "FEBRUARY", "MARCH", "APRIL","MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER","OCTOBER", "NOVEMBER", "DECEMBER"};

int m = JLabIO.readInt("Enter month number : ");if (1<=m && m<=12) System.out.println(month[m-1]);

}}

0 1 2 3 4 5 6 7 8 9 10 11

"JANUARY"

"FEBRUARY"

"MARCH"

"APRIL"

"MAY"

"JUNE"

"JULY"

"AUGUST"

"SEPTEMBER"

"OCTOBER"

"NOVEMBER"

"DECEMBER"

2110101 ���������������� ��� 15/7/2002 18

���������!'�!������"�.*�

• ���73��� �&� ��,��� � (2# [ ] )

• ������ ��*22���!�"�!����,��� �28 &�

�*���$�&� � (reference) !���� �28&�

public static double mean(double[] x) {...

}

public static double mean(double[] x) {...

}

public static void main(String[] args) {double [] data = new double[4];...double a = mean( data );

}

public static void main(String[] args) {double [] data = new double[4];...double a = mean( data );

}

1 23 9 -19 5

data

x

2110101 ���������������� ��� 15/7/2002 19

���!���,���"�������!

• �� .length �������73��� �

public class Array2 {public static void main(String [] args) {int [] data = new int[100];System.out.println( sum(data) );

}

public static int sum(int [] d) {int sum = 0;for(int i = 0; i < d.length; i++)sum += d[i];

return sum;}

}

public class Array2 {public static void main(String [] args) {int [] data = new int[100];System.out.println( sum(data) );

}

public static int sum(int [] d) {int sum = 0;for(int i = 0; i < d.length; i++)sum += d[i];

return sum;}

} ���������� ���� String ������

.length() ��������������

���������� ���� String ������

.length() ��������������

2110101 ���������������� ��� 15/7/2002 20

������������#*������/����-

import jlab.JLabIO;import java.io.*;public class CountChars {

public static void main(String[] args) throws IOException {String txt;int[] freq = new int[26];BufferedReader in = JLabIO.openFile(JLabIO.readString(">"));while ((txt = in.readLine()) != null) {

int txtLen = txt.length();for (int i = 0; i < txtLen; i++) {

char c = Character.toUpperCase(txt.charAt(i));if ('A' <= c && c <= 'Z') freq[c - 'A']++;

}}for (int i = 0; i < freq.length; i++) {

if (freq[i] > 0)System.out.println((char)('A' + i) + " --> " + freq[i]);

}in.close();

}...

import jlab.JLabIO;import java.io.*;public class CountChars {

public static void main(String[] args) throws IOException {String txt;int[] freq = new int[26];BufferedReader in = JLabIO.openFile(JLabIO.readString(">"));while ((txt = in.readLine()) != null) {

int txtLen = txt.length();for (int i = 0; i < txtLen; i++) {

char c = Character.toUpperCase(txt.charAt(i));if ('A' <= c && c <= 'Z') freq[c - 'A']++;

}}for (int i = 0; i < freq.length; i++) {

if (freq[i] > 0)System.out.println((char)('A' + i) + " --> " + freq[i]);

}in.close();

}...

���� � char ������� ��!��"

#�!$��� � (unsigned integer)

���� � char ������� ��!��"

#�!$��� � (unsigned integer)

Page 79: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 21

����"����0%#*! ���#*!����

public static double mean(double[] x) {double sum = 0.0, m = 0.0;if (x.length > 0) {for (int i = 0; i < x.length; i++)sum += x[i];

m = sum / x.length;}return m;

}

public static double mean(double[] x) {double sum = 0.0, m = 0.0;if (x.length > 0) {for (int i = 0; i < x.length; i++)sum += x[i];

m = sum / x.length;}return m;

}public static double stddev(double[] x) {double dev = 0;double sumsq, m = mean(x);if (x.length > 1) {for (int i = 0; i < x.length; i++)sumsq += (x[i] - m) * (x[i] - m);

dev = sum / (x.length - 1);}return dev;

}

public static double stddev(double[] x) {double dev = 0;double sumsq, m = mean(x);if (x.length > 1) {for (int i = 0; i < x.length; i++)sumsq += (x[i] - m) * (x[i] - m);

dev = sum / (x.length - 1);}return dev;

}

=

=

n

i

ix

n

x

1

1

( )

1

1

2

=

=

n

xx

s

n

i

i

2110101 ���������������� ��� 15/7/2002 22

��������� �����

• Requirement

– ������������ �������������

• Analysis

– ��� data ���������������������

– ��� key �������������������� ��������

– �� index ���!�"���� data ��������� ������ key

$�%�&����' key �� data �! �� � -1

– �(�)*��� �$�������� (sequential search)

0 1 2 3 4 5 6

23 2 3 14 5 99 9data 5key

public static int seqSearch( int [] data, int key )public static int seqSearch( int [] data, int key )

����� seqSearch(data, key) ��� 4

2110101 ���������������� ��� 15/7/2002 23

��������� �����

0 1 2 3 4 5 6

23 2 3 14 5 99 9data

public static int sequentialSearch( int [] data, int key )public static int sequentialSearch( int [] data, int key )

i data[i] data[i] == key

0 23 false

1 2 false

2 3 false

3 14 false

4 5 true

return i

5key

i data[i] data[i] == key

0 23 false

1 2 false

2 3 false

3 14 false

4 5 false

5 99 false

6 9 false

7

return -1

8key

2110101 ���������������� ��� 15/7/2002 24

��������������� �����

seqSearch

(data, key)

return i

i = 0

(i < data.length)

&& (data[i] != key)

i ++

i >= data.length

i = -1

true

false

true

Page 80: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 25

�������������� �����

public class TestSeqSearch {

public static int seqSearch(int[] data, int key) {int i = 0;while (i < data.length && data[i] != key) {i++;

}if (i >= data.length) i = -1;return i;

}

// main is used for testingpublic static void main(String[] args) {int[] data = {23, 2, 3, 14, 5, 99, 9};System.out.println(seqSearch(data, 5));System.out.println(seqSearch(data, 8));

}}

public class TestSeqSearch {

public static int seqSearch(int[] data, int key) {int i = 0;while (i < data.length && data[i] != key) {i++;

}if (i >= data.length) i = -1;return i;

}

// main is used for testingpublic static void main(String[] args) {int[] data = {23, 2, 3, 14, 5, 99, 9};System.out.println(seqSearch(data, 5));System.out.println(seqSearch(data, 8));

}}

2110101 ���������������� ��� 15/7/2002 26

��������� ����� (�������)

0 1 2 3 4 5 6

23 2 3 14 5 99 9data

public static int sequentialSearch( int [] data, int key )public static int sequentialSearch( int [] data, int key )

i data[i] data[i] == key

6 9 false

5 99 false

4 5 true

return i

5key

i data[i] data[i] == key

6 9 false

5 99 false

4 8 false

3 14 false

2 3 false

1 2 false

0 23 false

-1

return i

8key

2110101 ���������������� ��� 15/7/2002 27

��������������� ����� (�������)

seqSearch

(data, key)

return i

i = data.length – 1

( i >=0 ) &&

(data[i] != key)

i – –

true

false

2110101 ���������������� ��� 15/7/2002 28

�������������� ����� (�������)

public class TestSeqSearch {

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (i >= 0 && data[i] != key) {i--;

}return i;

}

// main is used for testingpublic static void main(String[] args) {int[] data = {23, 2, 3, 14, 5, 99, 9};System.out.println(seqSearch(data, 5));System.out.println(seqSearch(data, 8));

}}

public class TestSeqSearch {

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (i >= 0 && data[i] != key) {i--;

}return i;

}

// main is used for testingpublic static void main(String[] args) {int[] data = {23, 2, 3, 14, 5, 99, 9};System.out.println(seqSearch(data, 5));System.out.println(seqSearch(data, 8));

}}

Page 81: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 29

������������

public static int seqSearch(int[] data, int key) {int i = data.length;while (i >= 0 && data[i] != key) i--;return i;

}

public static int seqSearch(int[] data, int key) {int i = data.length;while (i >= 0 && data[i] != key) i--;return i;

}

public static int seqSearch(int[] data, int key) {int i;for (i = data.length - 1;

i >= 0 && data[i] != key;i--) ;

return i;}

public static int seqSearch(int[] data, int key) {int i;for (i = data.length - 1;

i >= 0 && data[i] != key;i--) ;

return i;}

2110101 ���������������� ��� 15/7/2002 30

����������

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (data[i] != key && i >= 0) i--;return i;

}

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (data[i] != key && i >= 0) i--;return i;

}

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (i-- >= 0 && data[i] != key) ;return i;

}

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (i-- >= 0 && data[i] != key) ;return i;

}

������� i >= 0 ��� �������� data[i] != key

i ����� ���������� data[i]

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (i >= 0 && data[i--] != key) ;return i;

}

public static int seqSearch(int[] data, int key) {int i = data.length - 1;while (i >= 0 && data[i--] != key) ;return i;

} i ����� ��������������������

return i ����!!"�

2110101 ���������������� ��� 15/7/2002 31

�����������������������

• Requirement

– ������������ ����� ��������������������

• Analysis

– ��� data �����������!���"�����

– ��� index ��������������#����"�����% �������

– �&"'(��� �����������)�����

0 1 2 3 4 5 6

23 2 3 14 5 99 9data

public static int max( int [] data )public static int max( int [] data )

����� max(data) ��� 5

2110101 ���������������� ��� 15/7/2002 32

����������������������������

public class TestMax {public static int max(int[] data) {return max(data, data.length);

}// find the max among the first k elements in the array

public static int max(int[] data, int k) {int m = -1;if (k <= data.length && k > 1) {m = 0;for (int i = 1; i < k; i++)if (data[m] < data[i]) m = i;

}return m;

}public static void main(String[] args) {

int[] data = {23, 2, 3, 14, 5, 99, 9};System.out.println(max(data));System.out.println(max(new int[0])); // zero size array

}

}

public class TestMax {public static int max(int[] data) {return max(data, data.length);

}// find the max among the first k elements in the array

public static int max(int[] data, int k) {int m = -1;if (k <= data.length && k > 1) {m = 0;for (int i = 1; i < k; i++)if (data[m] < data[i]) m = i;

}return m;

}public static void main(String[] args) {

int[] data = {23, 2, 3, 14, 5, 99, 9};System.out.println(max(data));System.out.println(max(new int[0])); // zero size array

}

}

Page 82: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 33

������������� ���

• Requirement

– �����������������)��������*)�������

• Analysis

– ��� data �����������!���"�����

– !��)��������*)�� data � �����)�����!����#���

data[0] <= data[1] <= data[2] <= ...

– �&"'(��������)���������)�� (selection sort)

0 1 2 3 4 5 6

23 2 3 14 5 99 9data

public static void selectionSort( int [] data )public static void selectionSort( int [] data )

2 3 5 9 14 23 99data

���

����

2110101 ���������������� ��� 15/7/2002 34

������������� � ��!��

23 2 3 14 5 99 9

23 2 3 14 5 999

23 2 3 14 5 999

232 3 14 5 999

232 3 14 5 999

232 3 145 999

232 3 145 999

232 3 145 999

232 3 145 999

2323 145 999

232 3 145 999

2323 145 999

• ���������������"�

– � �� � ������������

– � ���� ����������

• �"�-��

– ���� ������������

– ��������� �����

• )���)��

– � ������������

� �� ��!"���������

• ��'�!�#����"��"�

������ + �)�� #�!�

�.����"�-��� )����"

����" �����!

2110101 ���������������� ��� 15/7/2002 35

������������������� � ��!��

selectionSort

(data)

return

k = data.length

k > 1

true

false

i = max(data,k)

swap data[i] and data[k-1]

k – –

2110101 ���������������� ��� 15/7/2002 36

������������������ � ��!��

public class TestSort {public static void selectionSort(int[] data) {

int i, t;for (int k = data.length; k > 1; k--) {

i = max(data, k);t = data[i];data[i] = data[k - 1];data[k - 1] = t;

}}public static void main(String[] args) {

int[] data = {23, 2, 3, 14, 5, 99, 9};selectionSort(data);printArray(data);

}public static void printArray(int[] data) {

for (int i = 0; i < data.length; i++)System.out.print(data[i] + " ");

System.out.println();}public static int max(int[] data, int k) {

...

public class TestSort {public static void selectionSort(int[] data) {

int i, t;for (int k = data.length; k > 1; k--) {

i = max(data, k);t = data[i];data[i] = data[k - 1];data[k - 1] = t;

}}public static void main(String[] args) {

int[] data = {23, 2, 3, 14, 5, 99, 9};selectionSort(data);printArray(data);

}public static void printArray(int[] data) {

for (int i = 0; i < data.length; i++)System.out.print(data[i] + " ");

System.out.println();}public static int max(int[] data, int k) {

...

Page 83: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 37

Arrays.binarySearch ��� Java API

• ������� (��������� ������ ���������)

int binarySearch(byte[] a, byte key)int binarySearch(short[] a, short key)int binarySearch(int[] a, int key)int binarySearch(long[] a, long key)int binarySearch(float[] a, float key)int binarySearch(double[] a, double key)int binarySearch(char[] a, char key)

int binarySearch(byte[] a, byte key)int binarySearch(short[] a, short key)int binarySearch(int[] a, int key)int binarySearch(long[] a, long key)int binarySearch(float[] a, float key)int binarySearch(double[] a, double key)int binarySearch(char[] a, char key)

import java.util.Arrays;

public class TestSort {public static void main(String[] args) {

int[] data = {2, 3, 5, 9, 14, 23, 99};System.out.println(Arrays.binarySearch(data, 5));System.out.println(Arrays.binarySearch(data, 8));

}}

import java.util.Arrays;

public class TestSort {public static void main(String[] args) {

int[] data = {2, 3, 5, 9, 14, 23, 99};System.out.println(Arrays.binarySearch(data, 5));System.out.println(Arrays.binarySearch(data, 8));

}}

2110101 ���������������� ��� 15/7/2002 38

Arrays.equals ��� Java API

• �������� ������������������!�

boolean equals(boolean[] a, boolean[] b)boolean equals(byte[] a, byte[] b)boolean equals(short[] a, short[] b)boolean equals(int[] a, int[] b)boolean equals(long[] a, long [] b)boolean equals(float[] a, float [] b)boolean equals(double[] a, double [] b)boolean equals(char[] a, char [] b)

boolean equals(boolean[] a, boolean[] b)boolean equals(byte[] a, byte[] b)boolean equals(short[] a, short[] b)boolean equals(int[] a, int[] b)boolean equals(long[] a, long [] b)boolean equals(float[] a, float [] b)boolean equals(double[] a, double [] b)boolean equals(char[] a, char [] b)

2110101 ���������������� ��� 15/7/2002 39

Arrays.fill ��� Java API

• ��"����"#����$����% ��������#������

void fill(boolean[] a, boolean val)void fill(byte[] a, byte val)void fill(short[] a, short val)void fill(int[] a, int val)void fill(long[] a, long val)void fill(float[] a, float val)void fill(double[] a, double val)void fill(char[] a, char val)

void fill(boolean[] a, boolean val)void fill(byte[] a, byte val)void fill(short[] a, short val)void fill(int[] a, int val)void fill(long[] a, long val)void fill(float[] a, float val)void fill(double[] a, double val)void fill(char[] a, char val)

void fill(boolean[] a, int from, int to, boolean val)void fill(byte[] a, int from, int to, byte val)void fill(short[] a, int from, int to, short val)void fill(int[] a, int from, int to, int val)void fill(long[] a, int from, int to, long val)void fill(float[] a, int from, int to, float val)void fill(double[] a, int from, int to, double val)void fill(char[] a, int from, int to, char val)

void fill(boolean[] a, int from, int to, boolean val)void fill(byte[] a, int from, int to, byte val)void fill(short[] a, int from, int to, short val)void fill(int[] a, int from, int to, int val)void fill(long[] a, int from, int to, long val)void fill(float[] a, int from, int to, float val)void fill(double[] a, int from, int to, double val)void fill(char[] a, int from, int to, char val)

2110101 ���������������� ��� 15/7/2002 40

Arrays.sort ��� Java API

• ��� �����������

void sort(boolean[] a)void sort(byte[] a)void sort(short[] a)void sort(int[] a)void sort(long[] a)void sort(float[] a)void sort(double[] a)void sort(char[] a)

void sort(boolean[] a)void sort(byte[] a)void sort(short[] a)void sort(int[] a)void sort(long[] a)void sort(float[] a)void sort(double[] a)void sort(char[] a)

void sort(boolean[] a, int from, int to)void sort(byte[] a, int from, int to)void sort(short[] a, int from, int to)void sort(int[] a, int from, int to)void sort(long[] a, int from, int to)void sort(float[] a, int from, int to)void sort(double[] a, int from, int to)void sort(char[] a, int from, int to)

void sort(boolean[] a, int from, int to)void sort(byte[] a, int from, int to)void sort(short[] a, int from, int to)void sort(int[] a, int from, int to)void sort(long[] a, int from, int to)void sort(float[] a, int from, int to)void sort(double[] a, int from, int to)void sort(char[] a, int from, int to)

Page 84: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 41

System.arraycopy ��� Java API

• copy ���������� !& �������� ��'#�

public class ArrayCopy {public static void main(String[] args) {int[] a = {23, 2, 3, 14, 5, 99, 9};int[] b = new int[4];System.arraycopy(a, 2, b, 0, 3);...

}}

public class ArrayCopy {public static void main(String[] args) {int[] a = {23, 2, 3, 14, 5, 99, 9};int[] b = new int[4];System.arraycopy(a, 2, b, 0, 3);...

}}

0 1 2 3 4 5 6

23 2 3 14 5 99 9a

3 14 5 0b

src dest lengthsrcPos destPos

2110101 ���������������� ��� 15/7/2002 42

���������#�# (Multidimensional Array)

0 1 2 3 4 5 6

0 0 3 0 0 0 0a

0 1 2 3 4

0 0 0 0 0

b

0 0 0 4 0

0 0 0 0 0

0

1

2

int [] a = new int[7];a[2] = 3;

int [] a = new int[7];a[2] = 3;

int [][] b = new int[3][5];b[1][3] = 4;

int [][] b = new int[3][5];b[1][3] = 4;

���������

(vector)

�����

(matrix)

2110101 ���������������� ��� 15/7/2002 43

Initializer List

0 1 2 3 4

1 2 3 4 5

b

5 4 3 2 1

10 20 30 40 50

0

1

2

int [][] b = { {1, 2, 3, 4, 5},{5, 4, 3, 2, 1},{10, 20, 30, 40, 50} };

int [][] b = { {1, 2, 3, 4, 5},{5, 4, 3, 2, 1},{10, 20, 30, 40, 50} };

2110101 ���������������� ��� 15/7/2002 44

�������������

int [][] b = new int[3][5];int [][] b = new int[3][5];

int [][] b;b = new int[3][];b[0] = new int[5];b[1] = new int[5];b[2] = new int[5];

int [][] b;b = new int[3][];b[0] = new int[5];b[1] = new int[5];b[2] = new int[5];

0 1 2 3 4

0 0 0 0 0

b

0 0 0 0 0

0 0 0 0 0

0

1

2

0 1 2 3 4

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0

1

2

b

Page 85: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 45

�����������$���������#�#

public class TestPrintArray2 {public static void main(String[] args) {int[][] b = {{1, 2, 3, 4, 5},

{5, 4, 3, 2, 1},{10, 20, 30, 40, 50}};

printArray2(b);}public static void printArray2(int[][] a) {for (int i = 0; i < a.length; i++) {for (int j = 0; j < a[i].length; j++)System.out.print(a[i][j] + "\t ");

System.out.println();}

}}

public class TestPrintArray2 {public static void main(String[] args) {int[][] b = {{1, 2, 3, 4, 5},

{5, 4, 3, 2, 1},{10, 20, 30, 40, 50}};

printArray2(b);}public static void printArray2(int[][] a) {for (int i = 0; i < a.length; i++) {for (int j = 0; j < a[i].length; j++)System.out.print(a[i][j] + "\t ");

System.out.println();}

}}

JLab>java TestPrintArray21 2 3 4 55 4 3 2 110 20 30 40 50JLab>

JLab>java TestPrintArray21 2 3 4 55 4 3 2 110 20 30 40 50JLab>

2110101 ���������������� ��� 15/7/2002 46

��� %����#�&

• Requirement

– ���������� �� ���������

• Analysis

– �� a ��� b ����������������� int

– ����������������!����"� ����� a �� b

cij = a

ij + b

ij

2110101 ���������������� ��� 15/7/2002 47

����������� ��� %����#�&

public class TestAddMatrices {public static void main(String[] args) {int[][] b = {{1, 2, 3, 4, 5},

{5, 4, 3, 2, 1},{10, 20, 30, 40, 50}};

printArray2(addMatrices(b, b));}public static int[][] addMatrices(int[][] a,

int[][] b) {int[][] c = new int[a.length][a[0].length];for (int i = 0; i < a.length; i++)for (int j = 0; j < a[i].length; j++)c[i][j] = a[i][j] + b[i][j];

return c;}public static void printArray2(int[][] a) {...

}

public class TestAddMatrices {public static void main(String[] args) {int[][] b = {{1, 2, 3, 4, 5},

{5, 4, 3, 2, 1},{10, 20, 30, 40, 50}};

printArray2(addMatrices(b, b));}public static int[][] addMatrices(int[][] a,

int[][] b) {int[][] c = new int[a.length][a[0].length];for (int i = 0; i < a.length; i++)for (int j = 0; j < a[i].length; j++)c[i][j] = a[i][j] + b[i][j];

return c;}public static void printArray2(int[][] a) {...

}

2110101 ���������������� ��� 15/7/2002 48

����'���#�&

• Requirement

– ���������� ���#$�������

• Analysis

– �� a ��� b ����������������� int

– ����������������!����"��#$��� a �� b

– % a �'��� [p x q] ��� b �'��� [q x r]

"��#$��� a x b (�������������� [p x r]

∑=

=

q

k

kjikijbac

1

Page 86: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 15/7/2002 49

����������� ����'���#�&

public static int[][] mulMatrices(int[][] a,int[][] b) {

int[][] c = new int[a.length][b[0].length];for (int i = 0; i < a.length; i++) {for (int j = 0; j < b[i].length; j++) {c[i][j] = 0;for (int k = 0; k < b.length; k++) {c[i][j] += a[i][k] * b[k][j];

}}

}return c;

}

public static int[][] mulMatrices(int[][] a,int[][] b) {

int[][] c = new int[a.length][b[0].length];for (int i = 0; i < a.length; i++) {for (int j = 0; j < b[i].length; j++) {c[i][j] = 0;for (int k = 0; k < b.length; k++) {c[i][j] += a[i][k] * b[k][j];

}}

}return c;

}

∑=

=

q

k

kjikijbac

1

2110101 ���������������� ��� 15/7/2002 50

*���%�+- ���.�

import jlab.JLabIO;public class TestMulMatrices {public static void main(String[] args) {int[][] f1 = {{0, 1}, {1, 1}};int[][] fn = {{0, 1}, {1, 1}};int n = JLabIO.readInt("Fibonacci n = ");for (int i = 1; i < n; i++)fn = mulMatrices(f1, fn);

System.out.println("f(" + n + ") = "+ fn[0][1]);

}

public static int[][] mulMatrices(int[][] a,int[][] b) {

...}

}

import jlab.JLabIO;public class TestMulMatrices {public static void main(String[] args) {int[][] f1 = {{0, 1}, {1, 1}};int[][] fn = {{0, 1}, {1, 1}};int n = JLabIO.readInt("Fibonacci n = ");for (int i = 1; i < n; i++)fn = mulMatrices(f1, fn);

System.out.println("f(" + n + ") = "+ fn[0][1]);

}

public static int[][] mulMatrices(int[][] a,int[][] b) {

...}

}

n

nn

nn

ff

ff

=

+

11

10

1

1

Page 87: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 : �������������� �����

Classes & Objects

����������������� ��

������������������

2110101 ���������������� ��� 7/8/2002 2

�����

• ���������������

• ������� ����� !�����"������� �#��

• Instance variables

• Class methods !�� object methods

• Constructor methods

• ������� : ���� Complex

– Newton-Raphson

– Mandelbrot Set

2110101 ���������������� ��� 7/8/2002 3

��������������

class Robot {

}

class Robot {

}

data membersdata members

constructor methodsconstructor methods

class & object

methods

class & object

methods

2110101 ���������������� ��� 7/8/2002 4

�������� ����

• �"�%�% ���&�% z = x + iy

– x �����������

– iy ������������ ��

• y �����������

• i = √-1 (imaginary unit)

• conjugate

• absolute square :

• '� z1 = x

1 + iy

1!�� z

2 = x

2 + iy

2

• z1 + z

2= (x

1+x

2) + i(y

1+y

2)

• () ���*� ���'% (++� �-.���� /�0� ...

iyxz −=

222

))(( yxiyxiyxzzz +=−+==

1−=

πi

e

Page 88: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 5

���������� ����

• �������������� �������

• ������������ �� �� ��������

• ���� class ������� Complex

• ���� class = ���������� �����!���"#������

• ���������� ���$� ��� = ���������� %�

��� z = x + iy

public class Complex {public double x;public double y;

}

public class Complex {public double x;public double y;

}

fields

(data members)

2110101 ���������������� ��� 7/8/2002 6

�������������������

Complex z;z = new Complex();

Complex z;z = new Complex();

public class Complex {public double x;public double y;

}

public class Complex {public double x;public double y;

}

Complex z;���������������������� z �����������������������������!�

�������� Complex

z = new Complex();

�������������������������!��������� Complex

0.0 0.0

x yz

��"�����#���

������������$%�� new

2110101 ���������������� ��� 7/8/2002 7

���������

public class Complex {

public double x ;

public double y ;}

public class Complex {

public double x ;

public double y ;}

class &��� Complex

object '���� ('��� instance)

���#��� Complex0.0 0.0

x yz

����������� object

��� class Complex

fields ('��� data

members) ��� class

object reference

instance variables

2110101 ���������������� ��� 7/8/2002 8

�������������������

- ����'����*��

- ��+����#����,�"

- ����'����*��

- ��+����#����,�"

�!����$�� ��$�������$������!��

class ���"�"��#������ object #�����%������,!���������

object ��� class �$�"���� �

#��������� ����%����"�����"$

/�"0����'�������

Page 89: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 9

� ��!"

2110101 ���������������� ��� 7/8/2002 10

new ����$%�&�������� objects

Complex z1 = new Complex();Complex z2 = new Complex();Point p1 = new Point();Rectangle r = new Rectangle();

Complex z1 = new Complex();Complex z2 = new Complex();Point p1 = new Point();Rectangle r = new Rectangle();

z1 z2 p1 r

• new ��%�� object ����� class !�����&��

• �� ���'����(���'����� �� �� object ���

2110101 ���������������� ��� 7/8/2002 11

���

• Object !��)"�%�� �$* �������'�����&�� ��

• ��*�!��%"+��#�

• ���������� "�������" ���*�!����#��*�������

Complex z1 = new Complex(); // #1...z1 = new Complex(); // #2

Complex z1 = new Complex(); // #1...z1 = new Complex(); // #2

z1

#1

z1

#1 #2

2110101 ���������������� ��� 7/8/2002 12

��������������� object

• %�� -&��� new

• !�#��-&���������'�����&���� �� )$

Complex [] za = new Complex[4];for(int i=0; i< za.length; i++) {za[i] = new Complex();

}za[2] = null;za[3] = new Complex();

Complex [] za = new Complex[4];for(int i=0; i< za.length; i++) {za[i] = new Complex();

}za[2] = null;za[3] = new Complex();

0 1 2 3za 0 1 2 3za

�"�

null ���#�#����1��2��

object reference ������$����������

null ���#�#����1��2��

object reference ������$����������

�"�

Page 90: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 13

���( Instance Variables

• instance variables (���'������ . ������

object !���&)"�%�� �$*

• �������� ��������"� object . variableName

public class Complex {public double x;public double y;

}

public class Complex {public double x;public double y;

}4.0 9.8

x yz

Complex z = new Complex();

z.x = 4;z.y = 9.8;

Complex z = new Complex();

z.x = 4;z.y = 9.8;

2110101 ���������������� ��� 7/8/2002 14

���()�����+$���� instance variable

public class Complex {public double x;public double y;

}

public class Complex {public double x;public double y;

}

public class Complex {public double x = 1.0;public double y = 0.0;

}

public class Complex {public double x = 1.0;public double y = 0.0;

}

��������� object '����,!��������� %������0'#� 0 �� false ���

instance variables ���3 ��� object ����������/�%+����

�� boolean ����+�$��

0�#��������������$ �&��$�"�����������#��������

��������/�"0������$

2110101 ���������������� ��� 7/8/2002 15

Methods

• ������(#�%�/���������#'�01��� (#�%

– fields ������������ object

– methods ��������������������������� object

• (#�% Complex

– fields :

• x ��� y ����������������� ���

– methods :

• �� absolute

• �� congujate

• �����������������������������

• sort ������� array ��� int ��������!�� "�����$

2110101 ���������������� ��� 7/8/2002 16

Class Methods vs. Object Methods

• ��������!��&��2"�����������&%� ��3�

– class methods className .methodName(...)

– object methods objectName .methodName(...)

double a = Complex.abs(z1);Complex z2 = Complex.conjugate(z1);Complex z3 = COmplex.add(z2, z1);

double a = Complex.abs(z1);Complex z2 = Complex.conjugate(z1);Complex z3 = COmplex.add(z2, z1);

double a = z1.abs();Complex z2 = z1.conjugate();Complex z3 = z2.add(z1);

double a = z1.abs();Complex z2 = z1.conjugate();Complex z3 = z2.add(z1);

Page 91: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 17

����!�

• �(��� class methods ���#�

– Math.sqrt(x)

– JLabIO.readInt( )

– Array.equals(a, b)

– public static void main( String [] args )

– ...

• �(��� object methods ���#�

– str.toUpperCase()

– in.readLine()

– System.out.println( msg )

– ...

2110101 ���������������� ��� 7/8/2002 18

Class Methods

public class Complex {public double x;public double y;

public static double abs( Complex z ) {return Math.sqrt( z.x * z.x + z.y * z.y );

}}

public class Complex {public double x;public double y;

public static double abs( Complex z ) {return Math.sqrt( z.x * z.x + z.y * z.y );

}}

public class Test {public static void main( String [] args ) {double a = JLabIO.readDouble("real part : ");double b = JLabIO.readDouble("imag part : ");Complex z = new Complex();z.x = a;z.y = b;System.out.println( Complex.abs(z) );

}}

public class Test {public static void main( String [] args ) {double a = JLabIO.readDouble("real part : ");double b = JLabIO.readDouble("imag part : ");Complex z = new Complex();z.x = a;z.y = b;System.out.println( Complex.abs(z) );

}}

�$�$%��� Complex�$�$%��� Complex

��� object ����� method ��� Complex��� object ����� method ��� Complex

2110101 ���������������� ��� 7/8/2002 19

Object Methods

public class Complex {public double x;public double y;

public static double abs( ) {return Math.sqrt(this.x*this.x + this.y*this.y);

}}

public class Complex {public double x;public double y;

public static double abs( ) {return Math.sqrt(this.x*this.x + this.y*this.y);

}}

public class Test {public static void main( String [] args ) {double a = JLabIO.readDouble("real part : ");double b = JLabIO.readDouble("imag part : ");Complex z = new Complex();z.x = a;z.y = b;System.out.println( z.abs() );

}}

public class Test {public static void main( String [] args ) {double a = JLabIO.readDouble("real part : ");double b = JLabIO.readDouble("imag part : ");Complex z = new Complex();z.x = a;z.y = b;System.out.println( z.abs() );

}}

this �����&'� object ���&��

��$�����������������(�!

this �����&'� object ���&��

��$�����������������(�!

)!%��� static ��*� �(

���$&'� object method

)!%��� static ��*� �(

���$&'� object method

2110101 ���������������� ��� 7/8/2002 20

Class Methods vs Object Methods

• class method ��(��� "static" ���'�!���� method

• object method ����(��� "static" ���'�

• ����� class methods ������(#�%�

• ����� object methods ��������������

• (�)�� : ���!'&�&�� #� �*������� class method

– System.getProperties( )

– Math.random( )

– JLabIO.format( x, 5, 2 )

– a.compareTo(b)

– rectangle.setSize( w, h )

– Math.PI

�� )*��+���$����

������$�������

,-���"!!��'*�

Page 92: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 21

������� Object Methods ��� Complex

public class Complex {public double x, y; // data members

public double abs( ) {return Math.sqrt(this.x*this.x + this.y*this.y);

}public Complex conjugate() {Complex zbar = new Complex();zbar.x = this.x;zbar.y = -(this.y);return zbar;

}public Complex add( Complex z ) {Complex sumz = new Complex();sumz.x = this.x + z.x;sumz.y = this.y + z.y;return sumz;

}}

public class Complex {public double x, y; // data members

public double abs( ) {return Math.sqrt(this.x*this.x + this.y*this.y);

}public Complex conjugate() {Complex zbar = new Complex();zbar.x = this.x;zbar.y = -(this.y);return zbar;

}public Complex add( Complex z ) {Complex sumz = new Complex();sumz.x = this.x + z.x;sumz.y = this.y + z.y;return sumz;

}}

iyxz −=

z1 + z

2= (x

1+x

2) + i(y

1+y

2)

z1 + z

2= (x

1+x

2) + i(y

1+y

2)

2110101 ���������������� ��� 7/8/2002 22

Object Method �!+���$! : toString

• toString ������# ���"#���� object (instance

variables) ���� String !��(�������

• -&�!'�������/���!����%& 2#�&%�&�� �/���

�� System.out.println( z ) ������� z.toString()

-&��'�-�'��

public class Complex {public double x, y;...public String toString() {return "(" + this.x + ", i" + this.y + ")";

}...

}

public class Complex {public double x, y;...public String toString() {return "(" + this.x + ", i" + this.y + ")";

}...

}

2110101 ���������������� ��� 7/8/2002 23

Object Method �!+���$! : equals

• !&%�� obj1 == obj2 ����(����� obj1 �#�

obj2 �� object �&����'������

• !&%�� obj1.equals( obj2 ) ����(�����

object obj1 "�!��'�" object obj2 ������

• (��������� ��� "�!��'" �� object '*�$*

�'�2"������ class

public class Complex {...public boolean equals( Complex z ) {Complex z = new Complex();System.out.println( z );System.out.println( z.toString() );

}}

public class Complex {...public boolean equals( Complex z ) {Complex z = new Complex();System.out.println( z );System.out.println( z.toString() );

}}

public class Complex {public double x, y;...public boolean equals( Complex z ) {return this.x == z.x && this.y == z.y;

}...

}

public class Complex {public double x, y;...public boolean equals( Complex z ) {return this.x == z.x && this.y == z.y;

}...

}equals ���.��������/��������*

2110101 ���������������� ��� 7/8/2002 24

��������

• ������� instance variable object.variable

• ������� object method object.method( ... )

• �� �'�� !����� �#��������

public class Complex {public double x, y;...public String toString() {return "(" + this.x + ", i" + this.y + ")";

}...

}

public class Complex {public double x, y;...public String toString() {return "(" + this.x + ", i" + this.y + ")";

}...

}Complex z1 = new Complex();...double p = z1.x;String s = z1.toString();double q = z1.x(); // WRONGString t = z1.toString; // WRONGSystem.out.println( "what am I ?" );

Complex z1 = new Complex();...double p = z1.x;String s = z1.toString();double q = z1.x(); // WRONGString t = z1.toString; // WRONGSystem.out.println( "what am I ?" );

Page 93: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 25

����!�

• String �#� array �� object

• String ����!��& length()

• array �� instance variable ������ length

String s = "Hi Ho Hi Ho;int a = new int[100];

System.out.println( s.length() );System.out.println( a.length );

String s = "Hi Ho Hi Ho;int a = new int[100];

System.out.println( s.length() );System.out.println( a.length );

2110101 ���������������� ��� 7/8/2002 26

Constructor Methods

• ����!��&/��60�� (#�% �$� �����������-&�

�'�-�'���#' new (#�%'*

• �����/����'* (����������'� instance variables

�� object ����$� �/�� )"�%��

• ����!��&/��60

– ��������������� class

– �������� return type ��������

public class Complex {public double x, y;

public Complex( ) {this.x = 0.0;this.y = 0.0;

}

public class Complex {public double x, y;

public Complex( ) {this.x = 0.0;this.y = 0.0;

}

2110101 ���������������� ��� 7/8/2002 27

Constructor Methods

• Overload �&&��������&�������� /���

������!������� �'

public class Complex {public double x, y;

public Complex( ) {this.x = 0.0;this.y = 0.0;

}public Complex( double x1, double y1 ) {this.x = x1;this.y = y1;

}public Complex( Complex z ) {this.x = z.x;this.y = z.y;

}

public class Complex {public double x, y;

public Complex( ) {this.x = 0.0;this.y = 0.0;

}public Complex( double x1, double y1 ) {this.x = x1;this.y = y1;

}public Complex( Complex z ) {this.x = z.x;this.y = z.y;

}

Complex z1 = new Complex();Complex z2 = new Complex(2, 3.5);Complex z3 = new Complex( z2 );

Complex z1 = new Complex();Complex z2 = new Complex(2, 3.5);Complex z3 = new Complex( z2 );

2110101 ���������������� ��� 7/8/2002 28

Constructor Methods

• Constructor ������'�� �& -&��� this

public class Complex {public double x, y;

public Complex( ) {Complex(0, 0); // WRONG

}public Complex( double x1, double y1 ) {this.x = x1;this.y = y1;

}

public class Complex {public double x, y;

public Complex( ) {Complex(0, 0); // WRONG

}public Complex( double x1, double y1 ) {this.x = x1;this.y = y1;

}

public class Complex {public double x, y;

public Complex( ) {this(0, 0); // CORRECT

}public Complex( double x1, double y1 ) {this.x = x1;this.y = y1;

}

public class Complex {public double x, y;

public Complex( ) {this(0, 0); // CORRECT

}public Complex( double x1, double y1 ) {this.x = x1;this.y = y1;

}

Page 94: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 29

��%+����,

• Utility class (��(#�%!������ class methods

(�� Math.*, Arrays.*, JLabIO.* )

• (#�%�������� ����!��& main �������(#�%!��

2"�����&������� �-&���

• )�(#�%���� constructor �������������������

����'�/���������

• �'* ����(#�%�� noun �'* ������!��&�� verb

• �'& this. !�* ���& public class Complex {public double x, y;...public boolean equals( Complex z ) {

return x == z.x && y == z.y;}

public class Complex {public double x, y;...public boolean equals( Complex z ) {

return x == z.x && y == z.y;}

2110101 ���������������� ��� 7/8/2002 30

������� : )������� Newton-Raphson

• �� ���������� %���� f(x) = 0

• ���&�7/�����!����������

• ��3��� Newton-Raphson

– ��������������� f(x)

– �� xk

���������������! "�#�����$��%���

����������! (�&��&!%�) xk+1

����&'

)(

)(

1

k

k

kk

xf

xf

xx

−=+

x0

x1

x2

f(x)

x0

2110101 ���������������� ��� 7/8/2002 31

Class NewtonRaphson

public class NewtonRaphson {public static double f (double x) {

return x * x + 4 * x + 4;}public static double df (double x) {

return 2 * x + 4;}public static void solve (double xnew, double tol, int imax) {

double xold;int i;for (i = 0; i < imax; i++) {

xold = xnew;xnew = xold - f(xold) / df(xold);;System.out.println("x = " + xnew);if (Math.abs(xnew - xold) < tol) break;

}if (i >= imax) System.out.println("not converge");

}public static void main(String[] args) {

solve(0, 1e-10, 100);}

}

public class NewtonRaphson {public static double f (double x) {

return x * x + 4 * x + 4;}public static double df (double x) {

return 2 * x + 4;}public static void solve (double xnew, double tol, int imax) {

double xold;int i;for (i = 0; i < imax; i++) {

xold = xnew;xnew = xold - f(xold) / df(xold);;System.out.println("x = " + xnew);if (Math.abs(xnew - xold) < tol) break;

}if (i >= imax) System.out.println("not converge");

}public static void main(String[] args) {

solve(0, 1e-10, 100);}

}

)(

)(

1

k

k

kk

xf

xf

xx

−=+

44)(2

++= xxxf

42)( +=′ xxf

2110101 ���������������� ��� 7/8/2002 32

����� (��������� ������)

• �������� Complex � �

– data members : x ��� y

– constructor

– object methods :

add, subtract, multiply, divide, abs, toString

• �������� NewtonRaphsonComplex

Page 95: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 33

Class Complex

public class Complex {public double x, y;

public Complex(double x1, double y1) {x = x1; y = y1;

}public String toString() {

if (y >= 0) return "(" + x + " + i" + y + ")";else return "(" + x + " - i" + (-y) + ")";

}public Complex multiply(Complex z) {

return new Complex(x * z.x - y * z.y, x * z.y + y * z.x);}public Complex divide(Complex z) {

double abs2 = z.x * z.x + z.y * z.y;return new Complex((x * z.x + y * z.y) / abs2,

(z.x * y - z.y * x) / abs2);}public double abs( ) {

return Math.sqrt(x * x + y * y);}...

public class Complex {public double x, y;

public Complex(double x1, double y1) {x = x1; y = y1;

}public String toString() {

if (y >= 0) return "(" + x + " + i" + y + ")";else return "(" + x + " - i" + (-y) + ")";

}public Complex multiply(Complex z) {

return new Complex(x * z.x - y * z.y, x * z.y + y * z.x);}public Complex divide(Complex z) {

double abs2 = z.x * z.x + z.y * z.y;return new Complex((x * z.x + y * z.y) / abs2,

(z.x * y - z.y * x) / abs2);}public double abs( ) {

return Math.sqrt(x * x + y * y);}...

��������� add �� subtract ��

2110101 ���������������� ��� 7/8/2002 34

Class NewtonRaphsonComplex

public class NewtonRaphsonComplex {

public static Complex f(Complex x) {Complex two = new Complex(2, 0);Complex c1 = x.multiply(x);Complex c2 = x.multiply(two);return c1.add(c2.add(two));

}

public static Complex df(Complex x) {Complex two = new Complex(2, 0);Complex c2 = x.multiply(two);return c2.add(two);

}

public class NewtonRaphsonComplex {

public static Complex f(Complex x) {Complex two = new Complex(2, 0);Complex c1 = x.multiply(x);Complex c2 = x.multiply(two);return c1.add(c2.add(two));

}

public static Complex df(Complex x) {Complex two = new Complex(2, 0);Complex c2 = x.multiply(two);return c2.add(two);

}

22)(2

++= xxxf

22)( +=′ xxf

������...

2110101 ���������������� ��� 7/8/2002 35

Class NewtonRaphsonComplex

public static void solve(Complex xnew, double tol, int imax) {Complex xold, temp;int i;for (i = 0; i < imax; i++) {

xold = xnew;temp = f(xold).divide(df(xold));xnew = xold.subtract(temp);System.out.println("x = " + xnew);temp = xnew.subtract(xold);if (temp.abs() < tol) break;

}if (i >= imax) System.out.println("not converge");

}

public static void main(String[] args) {solve(new Complex(1, 3), 1e-10, 100);

}}

public static void solve(Complex xnew, double tol, int imax) {Complex xold, temp;int i;for (i = 0; i < imax; i++) {

xold = xnew;temp = f(xold).divide(df(xold));xnew = xold.subtract(temp);System.out.println("x = " + xnew);temp = xnew.subtract(xold);if (temp.abs() < tol) break;

}if (i >= imax) System.out.println("not converge");

}

public static void main(String[] args) {solve(new Complex(1, 3), 1e-10, 100);

}}

)(

)(

1

k

k

kk

xf

xf

xx

−=+

...������

2110101 ���������������� ��� 7/8/2002 36

Edit + Compile + Run

Page 96: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 37

������

Complex c1 = new Complex(5,0);Complex c2 = new Complex(4,0);Complex c3 = new Complex(3,0);

Complex c4 = c1.add(c2).subtract(c3);Complex c5 = c1.multiply(c2).add(c3);Complex c6 = c1.add(c2).multiply(c3);Complex c7 = c1.add( c2.multiply(c3) );

Complex c1 = new Complex(5,0);Complex c2 = new Complex(4,0);Complex c3 = new Complex(3,0);

Complex c4 = c1.add(c2).subtract(c3);Complex c5 = c1.multiply(c2).add(c3);Complex c6 = c1.add(c2).multiply(c3);Complex c7 = c1.add( c2.multiply(c3) );

c4, c5, c6, c7 = ?c4, c5, c6, c7 = ?

public Complex {...public Complex sqr( ) {return ;

}...

}

public Complex {...public Complex sqr( ) {return ;

}...

} ������������ ��������������

Complex ��� �������� multiply

������������ ��������������

Complex ��� �������� multiply

2110101 ���������������� ��� 7/8/2002 38

Mandelbrot Set (������)

• ��������� �����

������� dn

�������� ��

dn = (d

n-1)2 +c ������

d0 = 0+i0 = 0 ���

c ��� ������������

• c ���"#������

Mandelbrot set $������

�� |dn| �%��������'��

� � x ��������������� � � y ������������������

���������!"�#������ ��� Mandelbrot set

2110101 ���������������� ��� 7/8/2002 39

�������� Mandelbrot set

public void paint(Graphics g) {int n;Complex c, d;double stepX = 3.0 / getSize().width;double stepY = 3.0 / getSize().height;for (double y = -1.5; y <= 1.5; y += stepY) {

for (double x = -2.0; x <= 1.0; x += stepX) {c = new Complex(x, y);d = new Complex(0, 0);n = 0;while (d.abs() < 2 && n++ < 256) {

d = d.multiply(d).add(c);}if (n >= 256) {

g.drawRect((int)((x + 2) / stepX),(int)((y + 1.5) / stepY), 1, 1);

}}

}}

public void paint(Graphics g) {int n;Complex c, d;double stepX = 3.0 / getSize().width;double stepY = 3.0 / getSize().height;for (double y = -1.5; y <= 1.5; y += stepY) {

for (double x = -2.0; x <= 1.0; x += stepX) {c = new Complex(x, y);d = new Complex(0, 0);n = 0;while (d.abs() < 2 && n++ < 256) {

d = d.multiply(d).add(c);}if (n >= 256) {

g.drawRect((int)((x + 2) / stepX),(int)((y + 1.5) / stepY), 1, 1);

}}

}}

|dn

| < 2|d

n

| < 2

dn

= (dn-1

)2

+ cdn

= (dn-1

)2

+ c

2110101 ���������������� ��� 7/8/2002 40

������� : ���������� "Rational"

• Rational ������"��(��'))���

– �.�. 3/4, 15/7, 8/1

• #�"�� fields : numerator ��� denominator

– �.�. 15/7 �� numerator ��� 15 � denominator ��� 7

• #��#�(�������*

– add, subtract, multiply, divide, toString, � equals

• ��)��(���)%� ��� 4/8 ��)��(��� 1/2

public class Rational {public int numerator;public int denominator;...

}

public class Rational {public int numerator;public int denominator;...

}

Page 97: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 : �������������� �����

Composition & Inheritance

����������������� ��

������������������

2110101 ���������������� ��� 7/8/2002 2

�����

• Composition

• Inheritance

– subclass ��� superclass

– Overriding vs. Overloading

– class Object

– Inheritance Hierarchy

– upcasting ��� downcasting

• Composition vs. Inheritance

2110101 ���������������� ��� 7/8/2002 3

�������������������

• ������

– ����������� ��� �

– ����������������������

• ����� j2sdk help file (�� ~ 3000 ��)

– ��������������� ��������������

• ����� ������� �� (JNL, JSci, JLAPACK, JEdit, Lucene, ...)

– �������� �������!

• �����������!"

– copy & change source code ("���#)

– composition

– inheritance

avoid reinventing the wheelavoid reinventing the wheel

2110101 ���������������� ��� 7/8/2002 4

����������

• well defined

• carefully tested

• well documented

• portable

• highly rated

• open source

ReusabilityReusability

Rapid

Application

Development

Rapid

Application

Development

Page 98: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 5

Composition

• #�����#���"%&'��(��� fields �"% &�

objects +�����#�,%�

• +"�� methods +�����#�� -���� fields .�'

methods +�����&'��(������� &�&'-���

• ���' �"��+�� methods �� &� #,�� "���"

�"%���+���"%"��/�&'��������0�#�%�&'��12��3

2110101 ���������������� ��� 7/8/2002 6

����!� "���������������!�"

• ����0����# DrawableRect ("#"���) 6�%�" �7��

– equals( DrawableRect r ) : !����� r ������ this ?

– draw( Graphics g ) : ��! this ?

• +"������ (�� ,%��)

• �����#��2��+������ Rectangle, Color

• Rectangle "#����:

– fields :

• x, y, width, height

– methods :

• add, contains, equals, getWidth, getHeight, grow,

intersection, intersects, move, toString, union, ...

2110101 ���������������� ��� 7/8/2002 7

Composition

rect color

dr

object �����

Rectangle

object ���

�� Color

object �����

DrawableRect

• #�����#��&'��(��� fields

– �������������� Rectangle

– �������������� Color

2110101 ���������������� ��� 7/8/2002 8

Composition

import java.awt.*;public class DrawableRect {

public Rectangle rect;public Color color;

public DrawableRect(int x, int y, int w, int h, Color c) {this.rect = new Rectangle(x, y, w, h);this.color = c;

}public boolean equals(DrawableRect r) {

if (r == null) return false;return this.rect.equals(r.rect) &&

this.color.equals(r.color);}public void draw(Graphics g) {

g.setColor(this.color);g.fillRect(this.rect.x, this.rect.y,

this.rect.width, this.rect.height);}

}

import java.awt.*;public class DrawableRect {

public Rectangle rect;public Color color;

public DrawableRect(int x, int y, int w, int h, Color c) {this.rect = new Rectangle(x, y, w, h);this.color = c;

}public boolean equals(DrawableRect r) {

if (r == null) return false;return this.rect.equals(r.rect) &&

this.color.equals(r.color);}public void draw(Graphics g) {

g.setColor(this.color);g.fillRect(this.rect.x, this.rect.y,

this.rect.width, this.rect.height);}

}

�������������������

�� Rectangle �� Color

Page 99: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 9

Inheritance

• +"�����#�� (B) 6�%� "+���" ���#(������

���# �� (A) �"%"��/ (class B extends A)

• "�����# B �� "�(��" ������# A

– B ����% fields ��� A ("! �'�"��'�*)

– B ����% methods ��� A ("! �'�"��'�*)

( ���� constructors)

– ��� � B ���+� subclass ��� A

– ��� � A ���+� superclass ��� B

– ������-!�� B �+��������+���#�.*�/0��� A

• ���������� B ��� A

• ������������ A �������������� B

• �!������"� class Cat extends Mammal

A

B

2110101 ���������������� ��� 7/8/2002 10

Inheritance : �""����#�$

import java.awt.*;public class R1 extends Rectangle {public R1() {super();

}}

import java.awt.*;public class R1 extends Rectangle {public R1() {super();

}}

• +"�����#�� �"��.�(����+�������"����#

• �,�� Rectangle ���&'�� (�� �� constructors)

• R1 " fields .�' methods ���3 ���"% Rectangle "

• 0" constructor �'0 �� '((�' �� constructor

.((0�(���� ����

import java.awt.*;public class R1 extends Rectangle {}

import java.awt.*;public class R1 extends Rectangle {}

����� constructor ��� superclass

2110101 ���������������� ��� 7/8/2002 11

Inheritance : �""��������

• �����������#��+���������# ���"%"

• ��% fields �"%��� &�

• +"�� constructors �"%������(���

• ��% methods ��3 �"% superclass 0"

• &�"%�� methods ��+�� superclass ��"�;��

��� ( "���� overriding method)

• ����0���<�����( methods +�� superclass

�"% subclass 0����0�

2110101 ���������������� ��� 7/8/2002 12

Inheritance

import java.awt.*;

public class DrawableRect extends Rectangle {public Color color;

public DrawableRect(int x, int y, int w, int h, Color c) {super(x, y, w, h);this.color = c;

}

public boolean equals(Object r) {if ( !super.equals(r) ) return false;if ( this.getClass() != r.getClass() ) return false;return this.color.equals(((DrawableRect) r).color);

}

public void draw(Graphics g) {g.setColor(this.color);g.fillRect(super.x, super.y, super.width, super.height);

}}

import java.awt.*;

public class DrawableRect extends Rectangle {public Color color;

public DrawableRect(int x, int y, int w, int h, Color c) {super(x, y, w, h);this.color = c;

}

public boolean equals(Object r) {if ( !super.equals(r) ) return false;if ( this.getClass() != r.getClass() ) return false;return this.color.equals(((DrawableRect) r).color);

}

public void draw(Graphics g) {g.setColor(this.color);g.fillRect(super.x, super.y, super.width, super.height);

}}

�# $� field �%�

�# $� method �%�

���$�� method �� �

����� constructor

Page 100: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 13

����% fields ��� superclass

• ������ fields +�� superclass 0�-���� super.

�������,%� field

• �,��'�� this. �������,%� field +�� superclass

�70� ( ��' "�(��" �.��)

• �,��'��.��,%� field �70=�� ( ��'��� this. 0�)

public void draw(Graphics g) {g.setColor(this.color);g.fillRect(super.x, super.y, super.width, super.height);

}

public void draw(Graphics g) {g.setColor(this.color);g.fillRect(super.x, super.y, super.width, super.height);

}public void draw(Graphics g) {

g.setColor(this.color);g.fillRect(this.x, this.y, this.width, this.height);

}

public void draw(Graphics g) {g.setColor(this.color);g.fillRect(this.x, this.y, this.width, this.height);

}public void draw(Graphics g) {

g.setColor(this.color);g.fillRect(x, y, width, height);

}

public void draw(Graphics g) {g.setColor(this.color);g.fillRect(x, y, width, height);

}

�����������

�� ��

2110101 ���������������� ��� 7/8/2002 14

����% methods ��� superclass

• ������ methods �� +�� superclass 0�-����

super. �������,%� method

• �,��'�� this. �������,%� method (�"%00��/�

overriden) +�� superclass �70� ( ��' "�(

��" �.��)

public boolean equals(Object r) {if ( !super.equals(r) ) return false;if ( this.getClass() != r.getClass() ) return false;return this.color.equals(((DrawableRect) r).color);

}

public boolean equals(Object r) {if ( !super.equals(r) ) return false;if ( this.getClass() != r.getClass() ) return false;return this.color.equals(((DrawableRect) r).color);

}

���� equals ��� superclass ��� ����������� Rectangle ��

�������������� (������!"# this.equals ���� ���!.....)

2110101 ���������������� ��� 7/8/2002 15

�""&����

• +"�� �7�� toString ����( DrawableRect

• +�.�'��� : ���� toString +�� superclass

public class DrawableRect extends Rectangle {...public String toString() {

}}

public class DrawableRect extends Rectangle {...public String toString() {

}}

2110101 ���������������� ��� 7/8/2002 16

Overriding !����( Overloading

• Overriding &��� +"�� �7���� subclass 6�%�"�,%�

.�'�������� ��&��>��/.���� superclass

( &��� "�(���" +�� ���"%"��/)

• Overloading &��� +"�� �7���� 6�%�"�,%�6?��

.��������� ��0 �,����(+�� ��

( &��� " ��%��'" ����(�,%� �7��������������

.(( +�?���(�������� ���"%0��()

public class A {...public int f() {

...}

}

public class A {...public int f() {

...}

}

public class B extends A {public int f() { // overriding

...}public int f(int x) { // overloading...

}

public class B extends A {public int f() { // overriding

...}public int f(int x) { // overloading...

}

Page 101: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 17

��� Object

• ����"���#��2���,%��� Object (+��?�� �,%�

Object �"? &��,%����# +�?������-���<)

• ���# Object &� "(�(��1" +��������#������

• �� +"�����#���"%00� extends ������#�� �,�

0��� extends ������# Object

String

Object

Component Exception... Complex

... ...

2110101 ���������������� ��� 7/8/2002 18

Inheritance Hierarchy

FreshyEng101

EngStudent

CPEngStudent CEStudent EEStudentFreshyEng

Student

ArtStudent CommStudent

2110101 ���������������� ��� 7/8/2002 19

������� Inheritance �( Help File

java.lang.Object|+-JSci.maths.Matrix

|+-JSci.maths.ComplexMatrix

|+-JSci.maths.ComplexSquareMatrix

|+-JSci.maths.ComplexTridiagonalMatrix

|+-JSci.maths.ComplexDiagonalMatrix

2110101 ���������������� ��� 7/8/2002 20

�""&����

• +"�� inheritance hierarchy +�����#��0&�"?

– Quadrilateral, Trapesoid, Parallelogram, Rectangle,

��� square

– Vehicle, Car, Truck, Sedan, Coupe, PrickupTruck,

SportUtilityVehicle, Minivan, Bicycle, ��� Motocycle

– Archosaurs, Thecodonts, Pterosaurs, Dinosaurs,

Crocodilians, Saurischians, ��� Ornithischians

Page 102: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 21

Casting

• �������� B extends A

– B �'�.1�*������� ����� A

– B "�+�" A (����+���#�.*�/0��� A)

• Upcasting

– ��������3 cast ���������� B ���+� A -!

– upcast -!����

• Downcasting

– ���������� B ���� 34� upcast ���+� A

�����334� cast ��'������+� B -!

– 3� downcast -�-!����*! ClassCastException

A

B

2110101 ���������������� ��� 7/8/2002 22

��) equals ��� DrawableRect

• equals �(��( ���+�����# Object �����?�

– equals ��5 override equals ��� Rectangle

• �� super.equals(r)

– �.���!������+� Rectangle ���������'�����-�

• �� this.getClass() ��( r.getClass()

– �.����+�� ��� ��� r �+�����+���6�!� ��'� this ����-�

– 3��+�����+���6�!� ��'���-! downcast ��� Object

��-! �.�����-!� *� color : ((DrawableRect) r).color

public boolean equals(Object r) {if ( !super.equals(r) ) return false;if ( this.getClass() != r.getClass() ) return false;return this.color.equals(((DrawableRect) r).color);

}

public boolean equals(Object r) {if ( !super.equals(r) ) return false;if ( this.getClass() != r.getClass() ) return false;return this.color.equals(((DrawableRect) r).color);

}

2110101 ���������������� ��� 7/8/2002 23

�""&����

• +"�����# Square -�������# Rectangle .((

– Composition

– Inheritance

• Fields :

– x, y ����.*�'!��������������� ��'���'�

– length �������� ��!��

• Methods :

– boolean contains( int x, int y )

– void translate( int dx, int dy )

– int getLength( )

– int setLength( )

– boolean equals( Object s )

– String toString( )

�������%������

�����&��������

JDK help file ���

Rectangle

2110101 ���������������� ��� 7/8/2002 24

Composition vs. Inheritance

• �� composition ,%� ������#(���+�����#�,%�

� &�#��&'��( �,%����.((+����

– ��� �3 "��" �� ������� �� ������ +���4

– !'��'5����� Car +�����!� fields �������������

���� Wheel, Engine, Window, Door �+���

• �� inheritance ,%� ���������#��6�%� &�

��"�� �1+�����# �� �����������#(��� ��

#����<+�����# �� "�� &�"%���,� ��% ��

���#(�����(��

– ��� �*�*��*/�7 ��8�7 "�+�" �*�*���8�7 ������-����

� ���.*����*������#�'��-+

– !'��'5����� EngCUStudent extends CUStudent

Page 103: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 : �������������� �����

����������� (���)

����������������� ��

������������������

2110101 ���������������� ��� 7/8/2002 2

�����

• packages, import

• private, public, final, static

• Error & Exception Handling (Optional)

2110101 ���������������� ��� 7/8/2002 3

Package

• �������������������!���� ��#��$%

&$�' �� &��������� �(�&���'�(%'

• ����� �)����!���+ ����, �$%,�' ���$��-$���

��'�(%�/#0�,�����,

• ��� �)����!���+ 1'2�&�� package

• ���!�$%��21' package �$����' �(���21'

directory �$����'

jlab

JLabIO.class

JLabPanel.class

�������������� �

package jlab

���� package �������������������� package ����������������

2110101 ���������������� ��� 7/8/2002 4

Package

• Package $�0������#' : $ subdirectory ,�

• �������(%� package 1'�������

somchai

j101

Lab1.class somchai.j101.Lab1

Lab2.class somchai.j101.Lab2

j211

Lab1.class somchai.j211.Lab1

Lab2.class somchai.j211.Lab2

Demo.class somchai.j211.Demo

����������� package somchai.j101;

public class Lab1 {...

package somchai.j101;

public class Lab1 {...

package somchai.j211;

public class Lab1 {...

package somchai.j211;

public class Lab1 {...

public class Other {...somchai.j101.Lab1 lab101;somchai.j211.Lab1 lab211;...

public class Other {...somchai.j101.Lab1 lab101;somchai.j211.Lab1 lab211;...

��������������������!

Page 104: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 5

������� Package �� JLab

2110101 ���������������� ��� 7/8/2002 6

import

• ��1����!�$%��2'�� package &����� ��

– �������� class �� ��� package �������

– ���� import �� �����������������!"���

• import ���"#�$�������

• import ����%�%���$����

import jlab.JLabIO;

public class Happy {public void A() {

int x = JLabIO.readInt();...

}...

import jlab.JLabIO;

public class Happy {public void A() {

int x = JLabIO.readInt();...

}...

import jlab.*;

public class Happy {public void A() {

int x = JLabIO.readInt();...

}...

import jlab.*;

public class Happy {public void A() {

int x = JLabIO.readInt();...

}...

public class Happy {public void A() {

int x = jlab.JLabIO.readInt();...

}...

public class Happy {public void A() {

int x = jlab.JLabIO.readInt();...

}...

2110101 ���������������� ��� 7/8/2002 7

����������� fields �!" methods

• 1� public �0����

– �#"�%� : �������& ��!"��"

• 1� private �0����

– #�#" : ��!"�����'���������(���� �����

public class Square {public Rectangle rect;

public class Square {public Rectangle rect;

public class Square {private Rectangle rect;

public class Square {private Rectangle rect;

...Square s = new Square( 10, 10, 100 );s.rect.width = 70;...

...Square s = new Square( 10, 10, 100 );s.rect.width = 70;...

��"& ���� rect '� �� rect '��()#�

���������

�������������������������� ��������

�� !"��� Square � %���%&

2110101 ���������������� ��� 7/8/2002 8

����"���

• 1����+ fields �' private

• $ public method 1������(� ��$%�'��&�� field ,�

public class Square {private Rectangle rect;

public Square(int x, int y, int len) {this.rect = new Rectangle(x, y, len, len);

}public int getLength( ) {return this.rect.width;

}public void setLength(int len) {if (len > 0)this.rect.width = this.rect.height = len;

}...

public class Square {private Rectangle rect;

public Square(int x, int y, int len) {this.rect = new Rectangle(x, y, len, len);

}public int getLength( ) {return this.rect.width;

}public void setLength(int len) {if (len > 0)this.rect.width = this.rect.height = len;

}...

*���+�� ��#�"�,%,$���)"�����-*"��"�'������*���+�� ��#�"�,%,$���)"�����-*"��"�'������

Page 105: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 9

#��$�������

• '����� private ��� public ������$

– protected �)*�+������������� package �"������

��� subclass � ����� ���� protected ���������

– !�+����!�,���� �)*�+������������� package

�"������� �����

package mars;public class A {protected int count;...

package mars;public class A {protected int count;...

package earth;public class B extends mars.A {

public void retire( ) {super.count = 5; // OKC c = new C();c.number = 7; // fail...

package earth;public class B extends mars.A {

public void retire( ) {super.count = 5; // OKC c = new C();c.number = 7; // fail...

package venus;public class C {protected int number;...

package venus;public class C {protected int number;...

2110101 ���������������� ��� 7/8/2002 10

final

• �� final �'� field

– !�+������#������#����!" (constant)

• �� final �'� method

– !�+����� subclass override !"

• �� final �'� class

– !�+������� extends !"

����������� final field ���������."����%/�$����

final String COMPUTER = "cp";final String ELECTRICAL = "ee";final String CIVIL = "ce";

...

����������� final field ���������."����%/�$����

final String COMPUTER = "cp";final String ELECTRICAL = "ee";final String CIVIL = "ce";

...

2110101 ���������������� ��� 7/8/2002 11

static

• �� static �'� method �0�1� �' class method

• �� static �'� field �0�1� �' class data

– ����.�!�!"�/#�(,��+����(�+ �����

– �+�/#�(,������(��� "������(1�������� ����.��"���

public class Undergrad {public static final int FRESHY = 1;public static final int SOPHOMORE = 2;public static final int JUNIOR = 3;public static final int SENIOR = 4;private int status;public void setStatus( int s ) {

switch( s ) {...this.status = s;...

}}

public class Undergrad {public static final int FRESHY = 1;public static final int SOPHOMORE = 2;public static final int JUNIOR = 3;public static final int SENIOR = 4;private int status;public void setStatus( int s ) {

switch( s ) {...this.status = s;...

}}

Undergrad s = new Undergrad();s.setStatus( Undergrad.FRESHY );...

Undergrad s = new Undergrad();s.setStatus( Undergrad.FRESHY );...

2110101 ���������������� ��� 7/8/2002 12

Error �!" Exception

• ���0���'&������$����! ������4������

�(���!�%�4������

• ���4��������� ����$%

– '����+�����(��� �� ���'������ ."��������+

2����(���

• %�����,0%��, ����� index �����"���������,��'�, ...

– '������ ."��%/�������

• ��� ������*" '��*'*�����%��� (�����'������, ...)

Page 106: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 13

Exceptions �$��� %�&'�*���"��*���

• ���0���'$����!4������ (Exception)

// ArithmeticExceptionint a = 4, b = 0;int c = a / b;

// ArithmeticExceptionint a = 4, b = 0;int c = a / b;

// StackOverflowErrorvoid Jeng() {Jeng();

}

// StackOverflowErrorvoid Jeng() {Jeng();

}

// ArrayIndexOutOfBoundsExceptionint [] a = int[40];a[40] = 20;

// ArrayIndexOutOfBoundsExceptionint [] a = int[40];a[40] = 20;

// ArrayStoreExceptionObject [] a = new String[5];a[0] = new Rectangle();

// ArrayStoreExceptionObject [] a = new String[5];a[0] = new Rectangle();

// ClassCastExceptionObject a = new Rectangle();System.out.println((String) a);

// ClassCastExceptionObject a = new Rectangle();System.out.println((String) a);

// NegativeArraySizeExceptionint n = -100;Vector [] a = new Vector[n];

// NegativeArraySizeExceptionint n = -100;Vector [] a = new Vector[n];

// FileNotFoundExceptionBufferedReader in = JLabIO.openFile( "C:\\DATA.IN" );String txt = in.readLine();

// FileNotFoundExceptionBufferedReader in = JLabIO.openFile( "C:\\DATA.IN" );String txt = in.readLine();

2110101 ���������������� ��� 7/8/2002 14

�����$#���+�%

,��0

,��0

�0'�

�0'�

1

2

4

5

�0'�

3

2110101 ���������������� ��� 7/8/2002 15

, %��� �!�-*�%���

,��0

,��0

1

2

4

5

�0'�'�

3

�0'�'�

�0'�'�

2110101 ���������������� ��� 7/8/2002 16

, %��� ���*�%���

,��0

,��0

�0'�'�

�0'�

1

2

4

8

�0'�'��0������

5

�0'�

3

�0'�

7

6

Page 107: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 17

Robustness

• 5� ������4������ ���,$!�'1�&�������$%

����!5�'�����$% ���&6#'

– ���(���"������34�������%4"#�+4�('��

– �����4��� ,����

• ����$%�&)�����������������4�������$%���

���&6#'1����$%!�� ����0�1���������0���'��

,�,�

• ������� : ����� ���8&�2� ����,��

– � � ��(���%/���� "file not found" �����4��� ,����

– �.��%/����������5���� ������#"�5����

2110101 ���������������� ��� 7/8/2002 18

Throwing Exceptions

• (%� ������4���������1' �)�� �����������

1� �)���$% $���1����

– �����(�+������ ��� �����%4"#�+4 ����4"�1��

– ��� "8��" ��(�+������%/������ ��

• �$%��� �)������������� �)��'$#���$�� "��'"

!�%�4�������� ��1����

public double [] qRoot( double a, double b, double c )throws IllegalArgumentException {

double t = b * b - 4 * a * c;if ( t < 0 ) {

IllegalArgumentException e =new IllegalArgumentException("imaginary root");

throw e;}...

}

public double [] qRoot( double a, double b, double c )throws IllegalArgumentException {

double t = b * b - 4 * a * c;if ( t < 0 ) {

IllegalArgumentException e =new IllegalArgumentException("imaginary root");

throw e;}...

}a

acbb

2

4

2

−+−

2110101 ���������������� ��� 7/8/2002 19

Exception Classes

• ����1����! �(%� &$�'�������4������

• �(%����!��!(%�������56��� ��&�����4��

– IOException, OutOfMemoryError, ...

package java.lang;

public class IllegalArgumentException extends RuntimeException {public IllegalArgumentException() {

super();}public IllegalArgumentException(String s) {

super(s); // s is detail exception message}

}

package java.lang;

public class IllegalArgumentException extends RuntimeException {public IllegalArgumentException() {

super();}public IllegalArgumentException(String s) {

super(s); // s is detail exception message}

}

��� IllegalArgumentException ��������� ������������

���4��*����"5����������'�"����� ��'��"������'�������'�

2110101 ���������������� ��� 7/8/2002 20

Catching Exception

• (%�1���,�$%��� "��'" !�%�4������

• �)������ "��" !�%�4���������

• 1��0�!�%� try-catch

try {

statementstatement

...statement

} catch( Exception1 varName ) {statement...statement

}

try {

statementstatement

...statement

} catch( Exception1 varName ) {statement...statement

}

6�����������-�����,�����

��� Exception1 �"�0�,#

�"#-��'*�0������ catch block

����*"�������������������,�����6 �

-����������,���������4��*���

Page 108: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 21

$-%�!�# catch blocks

try {

statement...

statement

} catch( Exception1 varName ) {statement

...statement

} catch( Exception2 varName ) {statement

...statement

} catch( Exception3 varName ) {statement

...statement

}

try {

statement...

statement

} catch( Exception1 varName ) {statement

...statement

} catch( Exception2 varName ) {statement

...statement

} catch( Exception3 varName ) {statement

...statement

}

�"�0�,#�"#-��'*

�0������ catch block

�� �������������,�����

6 �-���"�������

��� catch block ��

2110101 ���������������� ��� 7/8/2002 22

����#��

• �)�� dumpFile �����' IOException object

public void dumpFile(String fn) throws IOException {BufferedReader in = JLabIO.openFile(fn);String txt;while ( (txt = in.readLine()) != null ) {

System.out.println(txt);}in.close();

}

public void dumpFile(String fn) throws IOException {BufferedReader in = JLabIO.openFile(fn);String txt;while ( (txt = in.readLine()) != null ) {

System.out.println(txt);}in.close();

}

do {try {

String fn = JLabIO.readString("file = ");dumpFile(fn);success = true;

} catch( IOException e ) {System.err.println(e.getMessage() + " : try again");success = false;

}} while ( !success );

do {try {

String fn = JLabIO.readString("file = ");dumpFile(fn);success = true;

} catch( IOException e ) {System.err.println(e.getMessage() + " : try again");success = false;

}} while ( !success );

2110101 ���������������� ��� 7/8/2002 23

#��$������*��* ��$���

• 5�, "��" �)��� "��'" ��

• ���!&�����4��������#����� extends ����

���!��;�' 3 �� ���(�

– Error, Exception ��� RuntimeException

• �0����&�� catch block $������

• ,��� catch ���4�������$% extends ���

RuntimeException �),�

• ...

Page 109: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 : �������������� �����

������������������� �

����������������� ��

������������������

2110101 ���������������� ��� 7/8/2002 2

�����

• Recurrences

• Iterative vs. Recursive

• Examples

2110101 ���������������� ��� 7/8/2002 3

��������������� � (Recurrences)

• �� ���������������������� � !����"�#

0, 1, 2, 3, 4, ... an

= an-1

+ 1 ����� n > 0, a0 = 0

3, 5, 7, 9, ... an

= an-1

+ 2 ����� n > 0, a0 = 3

0, 1, 3, 6, 10, 15, ... an

= an-1

+ n ����� n > 0, a0 = 0

0, 1, 1, 2, 3, 5, 8,... an

= an-1

+ an-2

����� n > 1,

a0 = 0, a

1 = 1

2110101 ���������������� ��� 7/8/2002 4

0, 1, 3, 6, 10, 15, ... (Iterative)

• ����$%&��������' n

• )�� an

= (0+1+2+...+n)

import jlab.JLabIO;

public class Sum2N {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {int s = 0;for (int i = 0; i <= n; i++)s += i;

return s;}

}

import jlab.JLabIO;

public class Sum2N {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {int s = 0;for (int i = 0; i <= n; i++)s += i;

return s;}

}

Page 110: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 5

0, 1, 3, 6, 10, 15, ... (Recursive)

• ����$%&��������' n

• )�� an

= an-1

+ n ,'� n > 0, a0 = 0

import jlab.JLabIO;

public class Sum2N {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {if (n <= 0)return 0;

elsereturn a(n-1) + n;

}}

import jlab.JLabIO;

public class Sum2N {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {if (n <= 0)return 0;

elsereturn a(n-1) + n;

}}

0 if n ≤ 0

an-1

+ n otherwise

an

=

�������������

(recursive program)

�������������

(recursive program)

2110101 ���������������� ��� 7/8/2002 6

0, 1, 1, 2, 3, 5, 8, 13, ... (Iterative)

• ����$%&��������' n

import jlab.JLabIO;

public class Fibo {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {int p = 0, q = 1, t = n;for (int i = 2; i <= n; i++) {t = p + q;p = q;q = t;

}return t;

}}

import jlab.JLabIO;

public class Fibo {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {int p = 0, q = 1, t = n;for (int i = 2; i <= n; i++) {t = p + q;p = q;q = t;

}return t;

}}

2110101 ���������������� ��� 7/8/2002 7

0, 1, 1, 2, 3, 5, 8, 13, ... (Recursive)

• ����$%&��������' n

• )�� an

= an-1

+ an-2

,'� n > 1, a0 = 0 , a

0 = 1

import jlab.JLabIO;

public class Fibo {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {if (n < 2) return nreturn a(n-1) + a(n-2);

}}

import jlab.JLabIO;

public class Fibo {public static void main(String[] args) {int n = JLabIO.readInt("n = ");System.out.println(a(n));

}static int a(int n) {if (n < 2) return nreturn a(n-1) + a(n-2);

}}

0 if n = 0

1 if n = 1

an-1

+ an-2

otherwise

an

=

2110101 ���������������� ��� 7/8/2002 8

������������� ����� (Iterative)

• ����� d[0] + d[1] + d[2] + ... d[d.length-1]

public static int sum(int [] d) {int s = 0;for (int i = 0; i < d.length; i++)s += d[i];

return s;}

public static int sum(int [] d) {int s = 0;for (int i = 0; i < d.length; i++)s += d[i];

return s;}

Page 111: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 9

������������� ����� (Recursive)

• ����� d[0] + d[1] + d[2] + ... d[d.length-1]

• �����"!� sn= d[0] + d[1] + ... + d[n-1]

• &�"��� sn-1

= d[0] + d[1] + ... + d[n-2]

• �-."�� sn = s

n-1 + d[n-1] ,'� n > 0, s

0 = 0

• �� ���� %�

s(d,n)= s(d,n-1) + d[n-1] ,'� n > 0, s(0) = 0

public static int sum(int[] d) {return sum(d, d.length);

}private static int sum(int[] d, int n) {if (n <= 0) return 0;return sum(d, n - 1) + d[n - 1];

}

public static int sum(int[] d) {return sum(d, d.length);

}private static int sum(int[] d, int n) {if (n <= 0) return 0;return sum(d, n - 1) + d[n - 1];

}

2110101 ���������������� ��� 7/8/2002 10

������������������ �����

• �����"!� mn= max(d[0], d[1], ..., d[n-1])

• &�"��� mn-1

= max(d[0], d[1], ..., d[n-2])

• ." mn = max(m

n-1, d[n-1]) ,'� n > 0, s

1 = d[0]

• �� ���� m(d,n)= max(m(d,n-1), d[n-1]) n > 0,

s(1) = d[0]

public static int max(int[] d) {return max(d, d.length);

}private static int max(int[] d, int n) {if (n == 1) return d[0];int m = max(d, n - 1);return (m > d[n-1] ? m : d[n-1]);

}

public static int max(int[] d) {return max(d, d.length);

}private static int max(int[] d, int n) {if (n == 1) return d[0];int m = max(d, n - 1);return (m > d[n-1] ? m : d[n-1]);

}

2110101 ���������������� ��� 7/8/2002 11

�������������� ������������� �����

public static int maxI(int[] d) {return maxI(d, d.length);

}private static int maxI(int[] d, int n) {if (n == 1) return 0;int i = maxI(d, n - 1);return (d[i] > d[n-1] ? i : n-1);

}

public static int maxI(int[] d) {return maxI(d, d.length);

}private static int maxI(int[] d, int n) {if (n == 1) return 0;int i = maxI(d, n - 1);return (d[i] > d[n-1] ? i : n-1);

}

public static int max(int[] d) {return max(d, d.length);

}private static int max(int[] d, int n) {if (n == 1) return d[0];int m = max(d, n - 1);return (m > d[n-1] ? m : d[n-1]);

}

public static int max(int[] d) {return max(d, d.length);

}private static int max(int[] d, int n) {if (n == 1) return d[0];int m = max(d, n - 1);return (m > d[n-1] ? m : d[n-1]);

}

���������� ����������

��� index i ��

d[i] ��������

��� index i ��

d[i] ��������

2110101 ���������������� ��� 7/8/2002 12

��������������������

23 2 3 14 5 99 9

23 2 3 14 5 999

23 2 3 14 5 999

232 3 14 5 999

232 3 14 5 999

232 3 145 999

232 3 145 999

232 3 145 999

232 3 145 999

2323 145 999

232 3 145 999

2323 145 999

• ����������������

– ������ �����������

– ����� �������

• ������

– ��!������������

– ���"��"!���� ��

• ��������

– ������ ��#��!��

�������$�����#��!��

– �����������%� �"������

��!&��

Page 112: 2110101 : Computer Programmingsomchai/2110101/Lectures/pdf/10… · ˚ S*˙ ˆ ˝ !˙ uBˇ ˝1# ˆ ˚ ˚2˙˝ j 01 Y ˆ T) ˆˆ j 01 Y ˆ 7˙ ˝v)˙ ˆ˜ ˝˚Yˆ˛ ˛S ˛"S j ˚ ˝!˙

2110101 ���������������� ��� 7/8/2002 13

��������������������

public static void selectionSort(int[] data) {int i, t;for (int k = data.length; k > 1; k--) {

i = maxI(data, k);t = data[i];data[i] = data[k - 1];data[k - 1] = t;

}}

public static void selectionSort(int[] data) {int i, t;for (int k = data.length; k > 1; k--) {

i = maxI(data, k);t = data[i];data[i] = data[k - 1];data[k - 1] = t;

}}

public static void selectionSort(int[] data) {selectionSort( data, data.length );

}private static void selectionSort(int[] data, int n) {

int i, t;if (n < 2) return;i = maxI(data, n);t = data[i];data[i] = data[n-1];data[n-1] = t;selectionSort( data, n-1 );

}

public static void selectionSort(int[] data) {selectionSort( data, data.length );

}private static void selectionSort(int[] data, int n) {

int i, t;if (n < 2) return;i = maxI(data, n);t = data[i];data[i] = data[n-1];data[n-1] = t;selectionSort( data, n-1 );

}

IterativeIterative

RecursiveRecursive

2110101 ���������������� ��� 7/8/2002 14

������ �� aP

mod m

• ap mod m %�����������'!�#��!��� �������#

• ���������' 1 : 220 % 31 = ?

– ������ 220 �� 1048576 ������� % 31 �� 1

• ���������' 2 : 2100 % 31 = ?

– ������ 2101 �� 2535301200456458802993406410752

������� % 31 �� 2

– ��������� : !" 220 % 31 !����#$�"�

• 2101

% 31 = 2 x (220

)5 % 31

• '(�����)���� (ab % m) = (a % m)(b % m)

• 2101

% 31 = 2 x (220

)5 % 31 = (2 % 31)(2

20 % 31)

5 = 2

2110101 ���������������� ��� 7/8/2002 15

������ �� aP

mod m

1 if p = 0

(ap/2

% m)2 % m if p is even

2(a(p-1)/2

% m)2 % m if p is odd

ap % m =

public static int powerMod(int a, int p, int m) {if (p == 0) return 1;if (p == 1) return a % m;int k = powerMod(a, p / 2, m);k = (k * k) % m;if ((p % 2) == 1) k = (2 * k) % m; // p is oddreturn k;

}

public static int powerMod(int a, int p, int m) {if (p == 0) return 1;if (p == 1) return a % m;int k = powerMod(a, p / 2, m);k = (k * k) % m;if ((p % 2) == 1) k = (2 * k) % m; // p is oddreturn k;

}

2110101 ���������������� ��� 7/8/2002 16

�������������� ��!�

• $%&���'��� ������ ��

• ��������&#� %����0

– ����%&��'�� ������(��������

– �����&)� �����*����� $��+���+��-��*����(���

• ������� (?) ��� (?) &������������