149
! " # # $ % " $& $’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ! #’# () ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ! #’ * ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ! #’’# * $ ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ + #’’ , - ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ . #’’/ $ - ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ # #’/ - ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ # #’/’# * 0 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ # #’/’ $ $ ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ## #’ 1 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #/ #’’# 1 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #/ #’! - 1 2 ) ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ # #’!’# $ 2 )’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ # #’+ 3 4 " ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #! 56 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #+ ’# () ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #+ ’ 3 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #+ 7 " ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #+ ’/ 8 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #9 - 8 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #9 ’ " 8 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #9 ’! 0 7 " :3 ; ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #. ’+ < % <5 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ #. ’9 "’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ’. 3 4 " ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ / $ % " ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ + /’# ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ + /’ 0 ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ + /’/ ) * ) ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ 9 /’ 4 = ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ 9 /’! =")’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ . /’+ 6’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ . /’9 $ ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ > /’. ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ / /’.’# ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ /# /’.’ * ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ / /’.’/ $’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ // /’.’ 1 ?’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ / /’.’! @ 1 ? ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ / /’.’+ @ * ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ / /’> A ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ /! /’>’# $" ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ /+ /’>’ 0 B ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ /+ /’# C 2 ) ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ /9 /’## 3 4 " ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ /. D ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ’# -’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ’#’# @ - ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ ’#’ @ ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ # ’ 3 A ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’ # ’’# $)A ’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

  • Upload
    vancong

  • View
    226

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

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

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

#'#� �(��)���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ! #'�� �* ��������������������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' !

#'�'#� �* ��������������������������$�� ������ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + #'�'�� ��,��������������������-�������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' . #'�'/� �$���������-�������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�

#'/� ���-������������������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #� #'/'#� �* � ���������������������0������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #� #'/'�� �$�� ��������������$�� ������ '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ##

#'�� �1 ����������������������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/ #'�'#� �������������1 ����������������������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/

#'!� �-�1 ��������2 �)�''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #� #'!'#� �$�� �������������������2 �)�''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�

#'+� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #! �� 5�����6���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #+

�'#� �(��)���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #+ �'�� �����3 �''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #+

7 ���"��� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #+ �'/� ����8 ����'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #9

-������������8 ����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #9 �'�� ����������"�����������������8 ����� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #9 �'!� ��0��������7 ���"�����:����3 ��; '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #. �'+� �<�������% <5 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #. �'9� ������������"�����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �� �'.� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ��

/� $��������% "������������������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �+ /'#� ������������������������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �+ /'�� ������0������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �+ /'/� �����)����* ����)���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �9 /'�� ��4���������=������ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �9 /'!� �=��")��'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �. /'+� ������������������6����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �. /'9� �$��������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �> /'.� �� ���������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /�

/'.'#� ��������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /# /'.'�� �* ��� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /� /'.'/� �$���������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' // /'.'�� �1 ?���'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /� /'.'!� �@ ���������1 ?���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /� /'.'+� �@ ���������* ��������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /�

/'>� ��������A �� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /! /'>'#� �$�����"��� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /+ /'>'�� ��������0�����B����������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /+

/'#�� �C �� �����������2 �)� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /9 /'##� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' /.

�� ��D ��������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �� �'#� ��������������-�������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ��

�'#'#� �@ ������������-������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �� �'#'�� �@ ����������������������������������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �#

�'�� ��3 ����A ��'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �# �'�'#� �$��)���A ������� � '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ��

Page 2: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

4.3 -�$������Math���Random '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �/ �'/'#� �% �����������$������ '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �� �'/'�� �$������Math '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �� �'/'/� �$������Random''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ��

�'�� �������������4���������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �+ �'�'#� ���4����������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �+ �'�'�� ���������������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �9 �'�'/� �$����� Le ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �9 �'�'�� �$������String''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' �>

�'!� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' !� !� �������A ������$������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' !�

!'#� ���������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' !� !'#'#� ������������ ���E�if''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' !� !'#'�� �������������-�������)�E�if-else''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' !� !'#'/� ����������(� �� ��E�switch - case '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' !>

!'�� �* � ����� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' +� !'�'#� ��������������* � ������while ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' +/ !'�'�� ��������������* � ������do-while ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' +! !'�'/� ��������������* � ������for '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' +9

!'/� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 9# +� ��� ��������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 9>

+'#� �������������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' .� +'�� �5���A ��''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' .# +'/� ����8 ������ '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' .� +'�� ����������������8 ������ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' .� +'!� �=������������=��")��'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' .+ +'+� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ..

9� $���������@ �F�����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' .> 9'#� �@ �F����� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' .> 9'�� �$������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >� 9'/� �-������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >� 9'�� �(,�����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >� 9'!� ����������������$������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ># 9'+� �* �0��������@ �F�����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ># 9'9� �(,�����������������������8 �������:��)���; '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >/ 9'.� �$����������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >! 9'>� ��0��������<�)���$������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >9 9'#�� �=��")������$�����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >. 9'##� �=��")����������8 ��� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >> 9'#�� �@ �@ ����������-��� * �0����������this '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >> 9'#/� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #��

.� * �����)����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�/ .'#� ��0��A ��'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�/ .'�� ��3 ���4���� �"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�!

>� �������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�+ >'#� �(��)���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�+ >'�� ��������C ���������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�.

>'�'#� �=��")����� ���������� �������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�. >'�'�� �������� �������6������������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�. >'�'/� �-�����������������������3 �������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ##�

>'/� �������������������������� ��8 �����������,����� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ##� >'�� �#'��G��������(������������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ##/ >'!� ������������@ �F�����'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ##� >'+� �$������Vector ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #��

Page 3: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/�

>'+'#� ����������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�� >'+'�� �-�������,����������������Vector'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #��

>'9� ��3 ���4������"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�� #�� -�������������������B���D ��� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�!

#�'#� �����������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�! #�'�� �����������D ������ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�!

#�'�'#� ��� ���'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�! #�'�'�� �$�����������''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�9

#�'/� ���������% �"��'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�. #�'�� ��3 ���4���� �"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/�

##� -������������@ ��������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/� ##'#� �����������'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/� ##'�� �% ��������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #// ##'/� ��H������� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/! ##'�� �@ ��������� ����������� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/9 ##'!� �-��������I��J ����''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #/. ##'+� ��3 ���4���� �"���� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #��

#�� -��3 ����G�$������1 �'F�)�''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�# #/� -��3 �����G�7 ����"�� '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�9 #�� -��3 ������ �% ������0� ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #�>

Page 4: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

� �E�-���������������������������������K���� ���D ���������������������0������������������������K� ���0�)�����F� �����������"���D ���,�� ����������������3 �� ����������������� ���4)�����0����� �����������'�$������������F���"���0������ ���)���D ��� ������K�����H��������D ����������������������� ��0�������� �������H���0��������������'�-�����0������ ��0�����������"�����,������������� �������H�����0�������������������K� �����)��� ���)���� �����������������������������"���D ��� ���)������������������������������������������ )���������H�����������'����������������K� ������,����� ������������������ ����� ��0�������� F"�����������K������������� ���� �����������������������������������������������)����������B���#���,����/B����K�)�����������������6����������� ���)�������������������0��&���� ��������������������� �����D �����6����'�

Page 5: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

1 - Conceitos Básicos de Ciências da Computação

1.1 - Motivação

<��� ���������� ��������� ������ ��� ��������� ��� ���)����� �� ������ ������� ��� ������������� ��� � ��� ������� ��������'���@ � 0������������ ���� ��� ��������� �� ����� ��� ���F������ ��� ��������K� ��� �������� 04����K� �� ��� � � ����K�������A ��� D ��� ��� ��� ��������� ��)��� �3 ������'� ����� �6�� ��� D ��� ��� ��������� 0�������� ���H��������� ��������,�������������������� ��������'�-� �����������,���L����M��������������� ��������������������������D �&����������A ���:�����������;��� �6�����������)����������������'�

1.2 - Resolução de Problemas

@ � ��F���)�� ��� ��� ����������K� D ������ �����)�� ��� �������K� ,� �� ��� �����)��� ��� ������� ��������'� -���K� �� ����� 0����������� ��� ������� ��� ��� �������� ,� �� ��0����� ��� ��� ��������'� $������������� ��� D ��� ��� ����� �����K� ��� ������ ���� )�6��K� �� ��� ��� �0�������� ���� ������ ��� ����3 �� ��� ��� ���8 ����� ��� ��������� ��� ��������������6���K����������� ���������������A ��� ������ ����������������'��C ����������� ����������0���������������F�����0�������������A ���������0��������������4����K�D ����3 ��������� �����������������������������)������ �������'�������D ������ ������������������������������� ������� �������K�,�������"����������� ����������� �������'����������K��� ����������)���������������������������� �������������A ��� �����������'�I���������0�6�����"���������� ��������,�� ��������������������&����� �������E��

� �����������K�����,�D ��������������������������� �������N��� �������0�������K�D ����� ��0������ ������������"���� ���������0������������������������������0���N�� �������0���K������F�K���D ������ ����������������������������'�

�(������������������� ��������������������������������0��������� ������������"������� �������'�-� ������������� ���������,����� ����0�K� ������ D ��������H"������������� 0���� ���� �������� ��������,����������0������K� ���� ����� ����� ���� �� ������ ��� �����A ��� D ��� �� �����)��'� C ���� ���������� ���������� ��� ���������H�������� ��H��D ���������K�������������������������������3 ����)��N����� �������?�K������ ������������K��������)�������D �������������A ���������������������D ������H�������������������������0��D ����������������'��� � ��������=������� ���D ������������������0�������)����� ��������������0����������������'�-��������� ������� D ���,�������"��� ������� ������������ ����0���� ������������������ ���������"������������K�D ����������� �������������O��� � ���������E������������� � �� �������E� ��������������0��������������:-�������;��� #'��������������������� #'#�% ����������������������� ������,���������������N�� #'��-�����������)���� #'/�2 ��������0���H�������0�������N�� #'��$��������������������0���������������������'� ��������������� �'#�-D ��������0���������� ������������F����

Page 6: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+�

� �'��$����������0��������� �����D ��������������F����6���� � �������E����������-0���� ���� ,� �?� ��� ����������� ��� ��� ��������� D ��� ������� � ����� �������,����� ��� � �� ��� �����4��� ���������)��� ��������P�-D ��)����� �������������������)��� D ���������3 ��������������������������� ���������� D ��� ��������������D �� ������������� �������������� ��������������� ��������'�

1.2.1 - Resolução de Problemas num Computador

I���������������������������� ���������������0����������� ���������,�� ���������3 �� �������6��������A ���D �������� ���������F���� �6�����3 ������E��

� * ���������0���������0��������� 7 ��������0�������� ���� �����������6������ @ ����A �������,������ @ ����A ����?������ �����H���������)"�������A ���:��������;�� * � ���������� ��������A ���:�� �����;�

�C �������������)��)��0�������������E�

�� �����)��� ������ ��� �����0�����A ��� ������"���� ���� �����)��� �� �������K� 0�6���� �� ��� ������� D ���

�����������3 ����")��� ������� �������� �����������4����:D ���D ���� ������D ������3 ���������,����������������������;�� ����� ��� ������ ���F����� ��� ������ ��)�� ����6�� ��� ��� ��� ������� ����������� :��� ���� ��� �������

����������K� ���� ������� D ���������� �),������� ������ �0�������� ����� ��� ������� ��� ��H�� ������K���������O;�

� Produzir resultados correctos, qualquer que seja o conjunto de dados legítimos utilizado. Exemplos: Exemplo 1: Elaborar um algoritmo que calcule a área total das paredes de uma caixa de secção rectangular a partir das suas dimensões. �� ���������E���� ������K��������K��������� �� �������E�$�����������"������������������������������������"���������3 ��� �������E�"���������3 ������� �����E��1 &���� �������1 &���������1 &���������-���Q �����R���� �������S���������-���Q �������#�R���� �������S��������-���Q ���������R���������S��������-���Q ��3 ��R���S�-���Q �����T���S�-���Q �������#�T���S�-���Q ���������������)��-���Q ��3 ����3 �� ����E��C �����F��)�����0�����K��0����������0������������������� ����������������:��� ����U�V!��G��W K�V!��U���� ����U�V�!��G�!W ������ ����X�V�!��G�#�W ;�������������� �������������&������������� ��������� ��������

Page 7: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9�

+W '�����)��)�������������� ���K�� ?��������6��������������� ��K����������K��� ��������� ���8 �������������K�D ������,�D ��������������������0���)���������� ����'����� �����E��

1 &���� ����1 &�� �Q ������������ �Q �������,� � ������������ �����Q �Q �����R���� ����T���� ����S��K�+�������� ������ ����U�!���������� � ���������R��K���������� � ������ ����U��!�����������������R��K�!�� � ���������������R��K#�5�Q ��������Q �Q �����R���� ����G���� ����S����������5�Q ��������)�������Q �Q �������3 �� ���/E��$������������������� �������#����� ������������� ���)������� �����E��-����������� �����@ �<� ���������� ���#��� ���������-����������� �������T<� ����������@ �<� ���������� ���<� ����T#���,�D �����<� �������F����������D ���#�������������R��<� ����R#��� �������������R�����T<� ����������<� ����R<� ����T#���,�D ���<� �����X�#�����3 �� ����E�����������������������D ��������������,������������F���������� �������������D ���������������������������� ����������)������ �����E������R�� � � � �����R�� � � � �1 ��)����� � � � ����D ������:)�����XR�;� � �����������������R�����T)����� � �����������R����T#� � �������1 ��)�����

Page 8: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.�

5�Q ��D �������������R�������������)��L$��F�����)�6�M�����������������R����B����������������)�������5�Q ���

1.2.2 - Técnicas de Descrição de Algoritmos

@ �� ���������� ���� ���� ������������ �3 ����")��� ���� ��� ������K� �� 0����� ����� ���� ��������� ,� ��� ����� �)��K������D �����)��������0������������3 ����� ����D �������H�F��D ���D �������������'���C �������������������)�������������,��������� ������ ��� ��:��������������3 ���4�������������;'�@ � ������?����,� ��� �?���� ��� ������� ��� D ��� ��� ���6��� ������� ���)�������� ���� ������ ��� ������A ��� ��� �������N� ������������� ���� ����������� ��� ����� ��� ���)���� ��� ������ ��������� �������� ���� ���)���� �� �����A ��� �4 ���� ����������������� ����������'�-����6�������� ������?���� ��������� ������������3 ���������������������������������� ����� �������������3 ����������������� ����������'�@ ������������ ���������,������������������6��������� �� �� '� @ �� 0��3 �������� ���� ��������� �� ��������)��� ��� 0��3 �� ��� ���A ��� ��� ��� �������K� ����),�� ����4������K�D ����� �������������0��������� ���������A ���������������������������D �&������� ��������:)���0��������3 �;'��

��

5�����#' * � �������������"0�����������������'���

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

1 �����B���������������

Y���B5����� �������������

��

1 �H�����0��3 ��

Page 9: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>�

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

�����H������ ���

��� ��������

�5������' ���� ����4����������6���������0��3 �������'�

�-����������� ��� 0��3 �������� ���� ���� �0���)������������ ���6����'� @ �� 0��3 �������� ���)�6� ������ ���� � ���� �����F����� �� )����6��� ���H��� ������������ ����������� �4 ���� ��� ����������'� (��K� ���� �� ��������� ��������� ����,�������������3 ������K������� ����04��������������6��'��

�5�����/' �3 �� ���������0��3 ������'�

��

��4��� � � � �

��4���

1 ���)�����#�

1 ���)�������

)�����#�X�)��������

�����)���)�����#�,������

���

)�����#�U�)��������

���

<���

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

�����)���)������������

<���

�5��

Page 10: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

�����)���L�������6�������� �����M� � � � �1 ���)�����#� � � � �1 ���)������� � � � ����)����#�X�)������������������������)��)����#�L,�����M����������������)����#�U�)����������������������������������)��)������L,�����M����������������������������������)��L)�����������M�������5�Q ���5�Q ���5��

�5������' �3 �� ������0�����/����������� ������?���'�

�-����6�������� ������?����������)����������� �����������6��������3 ����������������0��������������� ������������������� �������K������ ��������������������������������� ����������K��������D ���D ���������K�����������������������������)����������'�

�@ ����� )�������� ��� ������� ��� ���������� ��� ������?���� ,� D ��� ����� 0����� ��� ���������� ����� ��� �� ����� ��3 ������ ������)�� �� )������ 0���� ��� �������K� ��� ��F�K� ��� ��� �� ���������� ��� 0����K� ��)����� �� ������?���������������� �� �������)������� ����������������� �? ������������������� ����������'�

1.2.3 - Criação de Algoritmos

-� ������� ��� ���������� ,� 0����������� ��� �����)��)������ ��� ��������� ��� ��� ������'� I������ ��� ������ ��� ��������� ��� ��3 ��K� ������� �0��������� �������� ��)���� �� D ���������� ��� �� ������ D ��� ,� ������"�����������������������'�-���� ��3 ����������� ��������������� �����������K������ �����0����������� ���������������)��'��C ����,��������6����,�������H����������L��������������� �� ����������M��:����������� � � � � ��;K� ��� ����������� ���������������� ���������H��������������H��'�<����������������� ����������� ��3 ��,��)������� ��������������� ������ ������ ��������A ��� ���������� ��������������� ���'��3 ��������,�������������������"��K�� ���������� � � � � � � � D ��� ������������� �������� �K������F�K� ���������� ��������� �D �����K�)����6������������� ��������������� ������������,���'��������,����6�� ���������������������������������K�������D ������0���������������������������������� ������ �������'���� D ���D �����������������"� ������������������� D ���,����������� ������������� D ���D ���� �������E����)���������������������������������� ���K�D ���������H�����������F���������0�6����H���������������D ���������������'����� ��������������������������D ��� �������������������K������������ ��������6��������,�����D �������H����L� �� ��������� � �� � ��M�K������F�K��)��� �������D �����'�<���,�����H��������������?� ��D ������������H��K��������������������� ������������� ���������������F���)��������0���������������������,��������� ����0����������������������������P�

1.3 - Do Algoritmo ao Programa

1.3.1 - Representação Digital da Informação

@ ����� �������������6������� �����������������0�������K����6���������������������������� ������'�@ ����������� ���������� ��"��� ,� ��� ������� ��� ���� �������'� -� ������� �"���� ������ ������� ,� �� � �K� D ��� ���� �������� ������������������������K��� ����������� �����)�����������#'�@ ����)���������������������E� ��

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

Page 11: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

�$���� ������� )��0���� ��� ������� <� � �� � �� ��������� �<� ����� �������'� I��� ��� ���� ���� �6��K� ���� #� ���������������� ����������#�R����� �����K������������������������ ������������R����� �������������K�����/������� �������� ����/�R�.��� �����K��������������)������'�-����)����������� ������������������"��� ������������������������,��,��� ���K�)�F����������������3 �� ��E��111 = 1*22 + 1*21 + 1*20 = 7 101 = 1*22 + 0*21 + 1*20 = 5 ����������������� ���������������"��K������� 0�6����)�A ���������)��� ���������� ��)������� ����������)���������E��7 / 2 = 3 e o resto da divisão é 1 3 / 2 = 1 e o resto da divisão é 1 1 / 2 = 0 e o resto da divisão é 1

�@ ����� �������������6���������������,�������.�� �� K������������� � �� '�$������������� �������������������Z ���� ������������������0�������������������:�������H��������D �����������������������,���������������K����4������K�����4������K����';'�=�F���������������������E��

& ������ ' ��(�� ��� )*�#����)+�

1 bit N/A b 1 byte 8 bit B 1 Kilobyte 1024 byte KB 1 Megabyte 1024 Kilobyte MB 1 Gigabyte 1024 Megabyte GB

��3 ��������������������������������� ������,�������������������"��K� ���������������������H�3 �������K� ����3 �� ��'�

1.3.2 - Componentes de um Computador

C ����� ������� �������� ����� 04����:� �������;K��������4��� ����������K� �������K� ����K����?������'K������� ���������04����:� � ����;K��������4���� ����� ��������'����

�� �� ��� ����#� #� �#� ��#��� � #�� �#��/� � ##� �##��� � � #���!� � � #�#�+� � � ##��9� � � ###�'''� '''� '''� '''�

Page 12: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

���

5�����!' $�� ����������������� ������'���@ ���� ������������ �������������� �������,�����������������:$�C �G�� ������� �� � �� � �� �� ��;'�-�0�������"�������$�C �,������K� ���� ���������3 ������� ������A ��K�������������)�6'��������������3 ���������������������������D ��������������� ��������������E���

• �, �� �����:������������������������;�• ! � �����������:���������������� �������������;�• � � �������:�3 ������������������;'�

�@ �$�C �,��������4��� ��E��

�5�����+' $�� �����������$�C '�

�@ ����� ��������� 0����������� ,� �����?��� �������K� ����,�� ���H����� ��� * -(� :! ���� � � " � � �� � � # �� � �� ;'� [� ����"�����������6�������������0��������D ���� ����������������������������4�����E�

� �������������� ���D ��������������������� �������G����?���)��"��N�� �����������������������������������,������:)���0��������3 �;N�� $�����,�����������?�������6�������� ����)����N�� I��������������)�������)��)�����������,�����������?��K� ���� �����)�����D �����������������"�����)�N�

C ������$����������������� �� �?3 ��� ��������� �� �����3 �������� :��������� ������ ��� ������ ��� ������������;�

C ������-���,���B1 ?������0�������"�������:�����K��������������������� ����A ��� ����,����K� ���� ����� ����� ����������)������;������������A ���

* ���������D ����� �� ����� ��� �������� ���� �������)��������� ��� ������K� ���� ����6�������������������������� ��"����

���0,���������������

���������C ����������$����������������������������:$�C ;�

���0,���������4���

(��?������� ���

(��?���������"���

Page 13: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

� -��� �������������?���,��3 ����������� ���������Z ����D ��������������������6����E�$ ��� � � �� �:\ % �G�#���� � � �� ;K� � � �� � �� � :(% �G�#���� \ % ����#������� � � �� ;K� � �� � �� � :7 % �G�#����(% ����#����������� � �� ;��

��� �����D ����4�H������������?���* -(����#(% �:#�(����Z ��;K���D ���������,�����������?����������������������#\ % � :#� \ ���Z ��;K� �� ���� �� �������������"0��� ������ ������ �������� :��� D �������� ���������3 ����� �����������,�������� ������������4������D �������0����������,����'�$,������� �����#K��K�'''��!+K����;E��

z a 12 x 0 1 2 3 ... 256 ... 512 ... 1024

5�����9' * � �������������"0���������?�������������������� �������:* -(;����������,�����$�C ���������?����������K��3 ������)������� �������� ���)���D ��� ������������������������������� ��������������6����K������ ����3 �� ��E���

• �� ���)�������������E��������K�����N��• �� ���)��������4��E�������K�� �������N�• �� ���)�������������B��4��E������'�

�@ �����6��������� ��������������0�������K�,���������������� ���)����������6���������������"��E�

� -���6,����������������������?��� ��� ���� (���,�� �� ������ ��� ������ ���� �� ��� ������� ��������� G� ���?��� ���� )��"��� G� ����6���������

����������:����,������6� ���������;�����0��������������� �� ���)��� ��� ����6��������� ������"��� ���������� ���� ��� ����� �4����K� ��D �����K� $ * ���K� =�K�5���H�)��K�����A ���������?���������������"D �����0�����"0���������K�������������'�

1.4 - Linguagens de Programação

�����D ��������� ������� ������3 ����������������������0�K�,�������"���D ����H����F��0����������� �������'�@ �� ������������������������6������������������ ����������'��] "��)�������4)���������������������������������0�������D ���� ��������������������������H������E�

� - ��� �� �� � ��, ���E� ���� ���6����� ���� ��������� ������������ ���� ���A ��� ��� ��� ������'� -��������A ��������������4���� ��������#�K������ ��������������������������������������� ������'�

� - ��� �� �� � � ��#�. E� ���� ���������� ���� ��������4������ �����H������ ��� ���� ��� �"D ���K� �0������� ����� �������������������?��������)�6������D �&��������������#�'�

� - ��� �� �� �������� � *(��E������������������� �?3 �������� ����������H��������K� ��� ���K���������0"����������6������D ������������'�] "�����������������D ������ �������������������������E�$K�������K�1 � K�������K�$TT���2 �)�'�

1.4.1 - Tradução de Linguagens de Programação

C �� ��������,�0�������������� ������� ����D ���������� ������3 ������'�<���������K������� ����������?��������������������"D ���K� ����D ���,�������"��� ���������������������� �������'�������0������������������������6��������������� '� @ ����� ������������ ��������� D ��� 0�6����� �������������?���� 0�����:������������ ������������������4)��;� �����?�����3 ����")���:�������������"D ���;'���-� ������0������������� ��������,�)��0����������?����0�����D ����H��,�0����������� ����������������"�����������������������������������������������D ����)�������������3 ����K������������ ������� �������������'�-���������D ����������������������3 ���������� ����������D ������� ��������������������������������� �������" ����������������� ������ ������'��

Page 14: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

�C �� �������� ���������� ��� ��� ������ ���� ���0��� ��������������� D ��� 0������� ��������� ����F���'������ ���������� ���8 ������ :��� ��F�K� ������ �?����K� ���)����� ��� ��� �����4��� ������� D ��� ��)��� �� ���� �������� ����,�����������;'� <����� ����K� �������� ��� �������� �� ���� ��)���� ��� �?���� �� ��)��� ������� ���� D ��� ��� ������ ��F��������)���'�

1.5 - A Linguagem Java

2 -=-�,����������������� ������������������������F�����K������)��)��� ����� � ��# �� �� � � � �� � '����������������������������������������������������D ��� ������������3 �����������D ���D ����� �������� ������'�

1.5.1 - Compilar um Programa em Java

C ��������F���)��� ��� �����������)��)���������2 �)�������� ���������������� �����������������������������������3 �����������D ���D ���� � �������� ������������������������������������� �������)������'�<���������K��������F���)������ ������������������������6����������� �����������������'������� �������� 0��� �����)��� ������������ ��� ��� ������� )������ D ��� ��� ������ �� �6� ��� �3 ������� ��� �?�����������4��� ��� ������A ��� �� ���'� @ ����������� / (� ��������� �?�����3 ����")������� ��� � � �� � ��K� ����?����D ��� ,� ��� �������� ��� ��� ������� ��� D ��� ��F�� �3 �������'� ����� �?���� ���� ��� ���� ���� ������� ���� �H��������, ����0 �����/ (K�D ���,���� ������������3 ������������� ���������D ���,���� ���")��� ����3 ������� ���������2 �)�'��3 �����)���A ����0��������������("D ����=������ �����0����������������� ����)���:^ ���_ �K�C �3 K�1 ��3 K�������K�(��@ �K����;'��-�0�������������������)���� �����������������K���� ���������3 ������������� �����������2 �)�'��

$?����5�����2 �)�

$�� ������2 �)�

("D ����=������2 �)�:2 =(K������ ��������2 �)�;

% Z �������2 �)�

� � ������

� ������

@ �L�?��� 0����MK�D ������ ����� ������� �����K�,�����0��������������F��������� � �� 0����������G �������������� � �� � ��� '<�����0�������������������������L�?��� �"D ���MK���������������?���������,��'@ ��� � �� � � ��� ���� ����������3 ��������������������������)������ ������������'�����������"�������� ���������K���������� ��������, 0���� ����2 =(�:% �� ��& ��� ���# �� � ���;'

$?����5�����2 �)�

$�� ������2 �)�

("D ����=������2 �)�:2 =(K������ ��������2 �)�;

% Z �������2 �)�

� � ������

� ������

@ �L�?��� 0����MK�D ������ ����� ������� �����K�,�����0��������������F��������� � �� 0����������G �������������� � �� � ��� '<�����0�������������������������L�?��� �"D ���MK���������������?���������,��'@ ��� � �� � � ��� ���� ����������3 ��������������������������)������ ������������'�����������"�������� ���������K���������� ��������, 0���� ����2 =(�:% �� ��& ��� ���# �� � ���;'

5�����.' ������������������K���� ���������3 ������������� �����������2 �)�'��-� ���6����� ��� �?���� �����,��� ���� �� )�������� ��� ������ ���� ��� ��������� ����� ��������� D ��� ����� �����3 ������������D ���D ����� �������� ������K�����D ������H���D ������ ����� ����������������� ��0���A ��'������D ��� ����� ��� ������� ���H�� �� �"D ���� )������ ������ �������� ��������K� D ���D ���� �������� 2 �)�� ����"� ���� �"��3 �������'��

Page 15: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

����� ����� ��������� ��� 2 �)�� ,� ������"��� ���� ��������� ��� ��� ������K� ���� ������ �� % �� �� � ������� ' ���� ��� � ������ �� ��� � � ���( ��:2 �����\ ;���������������� ������'�

1.6 - Exercícios Práticos

�����)�������������� ���������)������������������������ ��������'���3 ���'�#E���=��0������������ �����������������������#���#�'���3 ���'� �E� � � ���� ��� )����� ��� ��� �������K� ��� ������ $�����K� 5����H��� ��� \ ��)�K� ���)������ ����� )����� ���� ��������������������'�:<���E�`$�R�:`5 /�;S!B>N�` \ �R�`$T�9/'#!;���3 ���'�/E���������������F��������#����� ������������'���3 ���'��E��������������������������������3 ������������"����D ������6������ ��������E�T��� ���3 ���'�!E���$���������� ��4����������"�����������0���������,����K���������D �����0������?� ���������������8 ������������������0��&���'���3 ���'�+E�������������������������������������� ����K�����),����������� �������3 ���'� 9E� � ��������� ����� ���������������#���K� ������� ������������'�- ?�� ���� ��������� �0�������� ������������ ��������������)����0��������'���3 ���'�.E������������������� �����,� ���'���3 ���'�>E���������������0�������������������������������D ���D ���'�

Page 16: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

2 - Formalização

2.1 - Motivação

I�����������������������������,�0�����������D ����������������0������������������������0����������������0����'������������� ��������������������K���� ���)������K�������3 ���������8 ����������������'���-�� �������� ���� ������������ ����������K� ,� ��������� D ��� ��� ��� ������� �� ���� ����3 �� �� �� ���� ���8 ����'� -�����3 �������������0���������������A �����0�������������� ������K��������8 �����)��������������D �������� �������)�� 0�6��� ��� �3 ������� ����� ���� ���� ������A ��'� -���K� ���� ��������� ,� ��������6���� ���� ����3 �� :0����� �����������;��� �������8 �����:���0����������������0����;'�

2.2 - Sintaxe

-�����3 ������������������,������F���������������D �����0�����������A ���)"�������������� �������������������K���������� ���)������0�����'�-�����3 ��������6������������������0����������������'�-�����3 ��������������������������,�����������"���'�-�����������K���� ������������� ����������K�������3 �� ����,��,���0��������),���������"����'�

� Gramática

$����������� ��� �3 �� ��� ��� ���� 0����� ������� ����� ��������� �������'� ��� �������&�� ���� 0����� �� ���� ,��������4��� ������������������������������������������)�����'��

L) �% � * � ��� ����� �� �� �� � �� � � M��

����� 0����� ���� ����� �������� ������� L) � % � * � M� �� ����� �������� )������ L�� ����� � � � � �� � �� � � M'� ���� ���� )�6K� ���������� ������� L@ � 2 ���M� � ,� �������4��� ���� ������������ � L@ M� � ������� ��� ����� � L2 ���M'� @ � �������� )������L�������������H���M��,���0������������������)������L�������M�������������������������������L�����H���M'��

��

5�����>' �������������0����'�����������������������������0����� ���E��

L) �� �� � �� � � ��� ����� �� �% � * � M��

5�����

������������������<������ ��������=������

������������������<��������� �=����� ��������� ��������<������

�������������������� ���<����

��������@ � ���2 ���� �������� ����� ���H����

Page 17: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

-�0��������"����������������������'�<���������K���0���������0�6�������K�����,���������)��������8 ����'��$��� ����� �3 �� ��� ������� )��0���� D ��� �� ����3 �� ���� ��� ����� �� ���� �� ���0����� ��������� ��� 0�����K������ �������������0����������������������

2.3 - Semântica

-����8 �����������������������0��������0�����:)��������8 ����;���������0����������������'��-� ���8 ����� ��� ���� ��������� ��� ����������� ��������� �� ���������� ��� �3 ������� ��� ���� ��������'� C ��� ����0������ ��� ������)��� �� ���8 ����� ��� ���� ��������� ��� ����������� �������� ��� 0�������� ���� ��������� ������������������������������0����'�

� Ambiguidade Semântica

-�� ����"����� ���� ���������� �������� ���� ������������� ���4����'� C �� ��������� ��������� ��� ����"��������4�����,�D �������0����������������������������0������������������������0�������0�������K��� �������K����������0�����������K�����,� ���4)���������������)������������������0����'�������3 �� ��K���������������0����E��

L' � �� ��� �� � � �� ��� �� � ���� � � �� � ����� � + � �� M��<��� ���������� ��� ����������� �� ���������� ���"����� ���� ���� �3 ���K� ����� ��������� ���� ��� ������������������:�&������"��������4����;'��

�5�����#�' ����4)������0���������0�����L���)����H���������������������������? �M�

�<���������������� ����������K�������������������������������������������� ����������K������ ���������)�������� �6���������������3 �����������������������������������K������D ����������K�D �������������0����'�C ������� ����� ��A ��������0������������������������ �����������,�������)����D ���D ����� ��������������'�

2.4 - Erros Sintácticos e Erros Semânticos

@ �������������� �������� ����������������� ��E��

� � ��� � ������� � � -�� ������A ��� ��� �������� ���� ������ ��� ������� ���� ��� ������� ��� ����"���� ��������������� ��������������6���'��

� ��������������������������� ������� ��������� ��������K� �����������0����� �������),�������H������������������� �����'�

�� � ��� � ��1 ���� � G� @ �������� ���� �3 ������� ��� ���� ��D �&���� ��� ���A ��� D ��� ���� ������ ����� ���

)��������� �� ?������� ����������'�

Page 18: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

���������������������04�����������������������6��K������������K�������������3 ������'��� �3 E������)������ ��������:3 T#;����)�6����:3 #;�

2.5 - Definição de Gramáticas (Sintaxes)

$���� F"� )���K� � ��������"����,�������F�������� ������:����3 �;� D �����0������������������� 0��������������������������������������'�<����������� ����������K���������"������0����������������"����������),������D ������� ���������)������0���������������������������� ������������������������������������������ ����������'���������0�����������"����,�������"��������������D ��������������E��

� )*�#��� �� ������ � ����F���������4�������D ���� �����������0�����������������'��

� )*�#��� � � ��� � ������ � � ���F���������4������� D �������� ����������� 0������������� ��������K�����D ��� ���� ���6����� ���� ������)��� ��� )"���� ��� �������� ���� 0������ ��� ��������� :�3 '� ��� ����"���� ����������L��������������M�;�

�� )*�#����+������ ��4�������������������� ����K�D ����� ������������������� ��� ����������������

�� 2 �� � ����3 �������� �������)�������������������)"������� �������������������'���������������0�����

������������������0�������������������� ���������4�����������:�3 '��������"���� ���������E�����0�����,��������4��� ������������������������������������������)�����;��

�-������"����� ���������������������6�����)"������ ��������A ��K��������������������% <5K�������������"�����K�������������'�

2.6 - Notação BNF

-���������% <5�0���)������� ���% � � ��� �� $ � � ���� ����, �� ��:% <5����0���L� �� $ � � �, �� ��- � �� M;'������ �������� ���)�� ���� �� ��0���� ��� ������� ���"������ ��� ���� ����"���� ������ ��� ������ ��� D ������ �������������������������������������'���<��������"���������������% <5K����6����������������� ����������E��

� )*�#��� �� ������ �G������)�� ������������������4������� )*�#��� �� ���� ������ ���+������G������)�� ���������LUM���LXM�

� � U�4�������������������������X�� @ ��4�������4��:����a��a;��� ����������������)��� ���4)���� @ ��4�������EER����0���L,���� ����� ��M�� 2 �� � � ��� ��������� � � ����������� ��D ������ ��� �4������ EER� ��� �4������ ���� �������� �� �� ������ ����

���������������������������� � U�4�����������������X�EER������������� � ����� ����3 �� ���#E����0����������������������0���������������&��� � <F> ::= <SN><SV> <SN> ::= <Det><N> <SV> ::= <V><SN>

Page 19: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

1. Uma frase (F) é constituída por um sintagma nominal (SN) seguido de um sintagma verbal (SV) 2. Um sintagma nominal (SN) é definido por um determinante (Det) seguido de um nome (N) 3. Um Sintagma verbal (SV) é formado por um verbo (V) seguido de um sintagma nominal (SN)

��3 �� ����E����0����� ��������"���������������������% <5K���������D �������� �������"���,��������4��� ���������D �&������������������4�������"���������#'�� <número binário> ::= <digito binário> | <digito binário><número binário> <digito binário > ::= 0 | 1

�-�#b���������� ����������0�������� �������"������������������4������"����������4������"������������������ �������"���-���b����������� ����������0�������4������"�������������������������#'���4����������E��� �������"����4��������������E�����#��4������������������E��� �������"��������4������"�����3 �� ���/E����0����� ������������������������������% <5�� <inteiro>::= <número > | + <número > | - <número > <número> ::= <digito > | <digito > <número > <digito> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

-�#b���������� ����������0���������������������� �������������K������ ����������������T��������� ����������������G'�-��b���������� ����������0�������� ��������������4�����������4������������������� ����'�-�/b���������0�����D ���,�����4���'���4����������E��������4��������������E�TK� K��K�#K��K�/K��K�!K�+K�9K�.K���>��4������������������E������K��� ��������������3 �� ��� �E� � � �����)�� ���� ����"���� ��� �������� % <5� ���� ��0��� �� ��� �� ���6���� ����� ���?���� ������ ������ �������H�����E��������E���������'���

<Tempo> ::= <Horas> : <Minutos> : <Segundos> <Horas> ::= 0 | 1 | 2 | 3 | 4 | 5 … | 23 <Minutos> ::= 00 | 01 | 02 | 03 | 04 | 05 … | 59 <Segundos>::= 00 | 01 | 02 | 03 | 04 | 05 … | 59

��3 �� ���!E���$����������������������"���������������% <5'��5 )6���7 �#�5 � 6�5 � 6���7 �#�5 ' 6�4��5 � 6�4�#�

Page 20: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

5 ' 6���7 ����4����������E����4��������������E��K����4������������������E��K�% ���$���;�-�0������������������,�����������������������O��#b�* �������� ��������� �� U% X��b�* �������� �������� � �� U% X��b�* �������� �������� � � �� U% X��b�* �������� �������� � � � �� U% X��b�* �������� �������� � � � � ������ ��D ���,� ���4)��������)�����������������#b����b����������� �������'���;�-�0�����������������,�����������������������O��#b�* �������� ��������� �� U% X�<��� ��D �������������� �������;�-�0����������������,�����������������������O��#b�* �������� ��������� �� U% X��b�* �������� �������� � �� U$X�/b�* �������� �������� � � �� ���� ��D ���,� ���4)��������)�����������������#b�K��b���/b����������� ��������

2.7 - Diagramas Sintácticos

C ������� ��������)������������������� �������������������"���������������% <5� ������������� �����������������"���'��

� )*�#��� ������� �������)�� ������������������ ������������������ �������D ������������������

)*�#������������� )*�#��� ����������� �������������������8 ������

�4������<�����������

�� )*�#����! ���������,������������������� �������D ������������������:�& ���,���� ����� ��M;'�

�4�������0����4�������0���

�� ! � 8��������� ������� ��� ��������� ����������������:�& ���L��M;�

�4�������0����4�������0���

��

� 2 �� �����3 ���������� ������������������� �������D ���������4������ ���������0����

Page 21: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

� 9������ ������� :�

"�

� � ����� �����3 �� ���#E����0����������������������0���������������&�����

���3 �� ����E� � ��0����� ��������"�����������������������"�����K��������� D �������� �������"���,��������4��� ���������D �&������������������4�������"���������#'���������������� �����3 ��������������������������D ���,� ���4)����� �0����������������������������� ���K��� �0����K��?������4���������������:���������������;'����������

Diagrama sintáctico para a Frase (F):

Diagrama sintáctico para o sintagma nominal (SN):

Diagrama sintáctico para o sintagma verbal (SV):

SN SVF

Det NSN

V SNSV

5 ; 6����5 )� 6�5 )0 6�

5 )� 6����5 ! �6�5 � 6�

5 )0 6����5 0 6�5 )� 6�

! ��<���

)� �/ ��������' �= ��#��- �(���

)0 ���������- ����#����

! *� ���� �������� 9������ �������

! *� ���� ������� � 9������ �������

! *� ���� ������� :�

"�

Page 22: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

�3 �� ���/E����0����� ��������������������������������"�����'��

���3 �� ����E���$����������������������"���������������% <5��. � / �001 �� �. � / �. � / �001 �� �. � / �2���. � / �2�� �. � / �001 ����� �� ��� ��� ��3 � �� � ��� � �� �����0��

��

2.8 - Exercícios Práticos

�3 ���'�#E���������������������"���������������% <5E�

<Comp>::= <Bum> <Dum+> <Bum> | <Dum+> <Bum> <Dum+>::= <Dum> <Dum+> | <Dum> <Dum> ::= B | C | D <Bum> ::= A | E

��;������0D ������4������������������"���K���������������4����������������������������'��;���K�F���0�����K�D �����������������0������ �����������������������������0���� ��������"���E�

1)EFA 2)AA 3)ACC 4)ABCDE 5)BBAAAAE 6)EDDCCE

��

#�

��

/�

��

!�

+�

9�

.�

>�

T�

<� ������������

)�#�

#�

#��

Page 23: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����/�

7)BBCA

�;�- ����������������������"������D �)���������3 ���'��E���������������������"���������������% <5E�

<S>::=<A> a <A>::=a <B> <B>::=<A> a | b

��;������0D ������4������������������"���K���������������4���������������������������������0������ ������&�������������0�����U�X'��;���K�F���0�����K�D �����������������0������ �����������������������������0���� ��������"���E�

1)aabaa 2)abc 3)abaa 4)aaaabaaaa 5)aaaba

�;�- ����������������������"������D �)����������3 ���'�/E���������������������"���������������% <5E� <Blop>::= c <Gulp> s <Gulp>::= a | b <Gulp> | x <Bat> y <Bat> ::= x | y <Bat>

��;������0D ������4������������������"���K���������������4����������������������������'��;���K�F���0�����K�D �����������������0������ �����������������������������0���� ��������"���E�

1)cas 2)cGulps 3)bay 4)cxxy 5)cxxayys

�;�- ����������������������"������D �)���������3 ���'��E�������)����������"���K������������������% <5���������6�����������������"�����K� ������0������ �������������'���3 ���'� !E� � �����)�� ���� ����"���K� ������ ��� �������� % <5� ����� ���6����� ��������� ���"�����K� ���� ��0��� �������4������ �������������������?)��'���3 ���'�+E�������)����������"���K������������������% <5���������6�����������������"�����K�D ���������0����������������� 0����E� ��� 0������ �������� ��� �� �� ������� ��� �'� ������ ������ �4������� ���� H�)��� D ���D ���� �� ����� ���)����'�C ���)�����,�������������������F����E��K��K�K��K��'���3 ���'�9E���������������������"���������������% <5E�

Page 24: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

<Princ>::= a <Meio> a <Meio> ::= b <Fim> b <Fim> ::= c | c <Fim>

��;������0D ������4������������������"���K���������������4����������������������������'��;������)����������"�����D �)����������6��������� ���������������"����'���3 ���'�.E�������)����������"���K������������������% <5���������6�����������������"�����K�D �����0���0�����������������0����E��

:#;����0�������������� ����N�:�;����0�����������������N��:/;��������������� �� ������3 ���� ��������c�����c�� D ������� D �������K����� ���� D ����3 ���� ������������������'������3 �� �������0��������������������E����K������K����K��������'�

���3 ���'� >E� � �����)�� ���� ����"���K� ������ ��� �������� % <5� ����� ���6����� ��������� ���"�����K� ���� ��0��� ����� ����� ����K� �� D ���� ���� ���� ������� D ���� ��� �������� ������� D ���� ��� �������� ����40��'� C �� ����� ��� ��������������� �������������������K�������D ������� �����������K���D ����,��������� ����4����'������3 �� ���G�'�K��'����T�'������ ����� ��� �������� ������'� C �� ����� ��� �������� ������� ���� ���� �������K� �� D ���� ,� ��� ������ ��� ��� ����K�������� ������L�M�������3 ������ �����K��� D ���� ����������� ��������'������3 �� ����'�� !N����N� ��'���T��������� ��������������������������0��'���3 ���'�#�E� ������)����������"���K� ����������������� % <5���������6�����������������"�����K� ������ �������������� �������� ��3 �����0����E� <s> <A> <s> <B> i ���D ���L-M���L% M������������������K�L�M�,���������LTM����L L���LM�,�����4�������������'������3 �� ��K��'! /'�����T/������� ��������� ��3 ��'���3 ���'�##E��������������������"���������������% <5E�

<A>::= 1 ( <B> ) 2 ( <C> ) 3 ( <C> ) | 1 ( <B> ) 2 <C> <B>::= k=m | k<>m | k<=m | k>=m <C>::= <D> + <D> | <D> - <D> | <D> * <D> <D>::= e | f | g | h

�;������0D ������4������������������"���K���������������4����������������������������'��;�- ����������������������"������D �)���������3 ���'�#�E��������������������"���������������% <5E�

<Selecção> ::= <confusão> | <confusão> ; else <Expressão> ; <confusão> ::= if ( <Condição> ) <Expressão> ; <Condição> ::= k == m | k != m | k <= m | k>=m | k<m | k>m <Expressão>::= <Id> + <Id>|<Id> - <Id> | <Id> * <Id> | <Id> / <Id> <Id> ::= e | f | g | h

�;������0D ������4������������������"���K���������������4���������������������������������0���������"������� ������&����������'��;�- ������������ ���������������"������D �)������'�

Page 25: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

��3 ���'�#/E��������������������"���������������% <5E�

<Selecção> ::= Switch ( <Expressão> ) { <Lista> } ; <Lista> ::= case valor : instrução | case valor : instrução <Lista>; <Expressão>::= <Id> + <Id> | <Id> - <Id> | <Id> * <Id> | <Id> / <Id> <Id>::= e | f | g | h

��;������0D ������4������������������"���K���������������4���������������������������������0���������"������� ������&����������'��;�- ������������ ���������������"������D �)������'��3 ���'�#�E���������������������"���������������% <5K����D ������4�����������,�comp'E�

<ins> ::= <ins1> | <ins1> ; <ins> <comp> ::= begin <ins> end <ins1> ::= <comp> | a | b | c

�;������0D ������4������������������"���K���������������4����������������������������'��;�* ������)� �����6�����������������"�����'��;�=��0D �����)��������������������0�����K���������������������"���E�

1)begin a;b; a end

2)begin c; end

3)begin end

4)begin a begin a; a; a; b end; c end

5)begin a end; begin b; c end

6) begin a begin b; c end

��3 ���'�#!E� � ������)����������"���K����6�����������������"�����K� ������0����� ������ ������������� �������4� ���:�3 'E�#K���#K�!!!!!K����';�'�* ������)� �����6�������������% <5'���

Page 26: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����+�

3 - Conceitos Básicos de Programação

3.1 - Estrutura de um Programa

@ �� ������������������������������&��� ��������� �������E�� ! �����> � �G� ������������)������?��� ����������6�������������0����������)��)��N�� +� ���> � �G������������� ���������D �����)���0������N�� Comentários – são úteis para os humanos, mas são ignorados pelo computador.�

�-��������������� ������������&������������"������D ���������������������������A ��K�������A �����������"������� 0����� ��������'� -� ����3 �� ��� 2 �)�� :�� ��������� ���6���� ����� �� ����� ������ ������� ���� �� � ����6����� ��� ����������;�������������������������������K����������0�������K� ���)���������)����K��4���������)������'�

3.2 - Identificadores

@ �������������� ����������������4���������0������������������������ ��������:0���A ��K��)��")��K������������������������������D ����������� ���;'��3 ������������ ������������������������0�������'�����2 �)�K�����������������������������E��)�������������� ������������K������������:Q ;�����0����:d ;�����������������������������:������K��4����K����������������0���;�<��� �������������� ����P���$����������������D ������������������������������K���2 �)������������������� ������������ ������K������F�E�� A � a B � b public static void � PUBLIC STATIC VOID

�-���� D ���������)���A ���������F���������?����������� ����������)��0D ������������� ����K�,��������H")������������6����K� ��� 0������������ ������������ ��������� ��� ��������������������H��������'�����,�� ������2 �)��,���������������)���A ��� �����������0���������������D ��������������)��)����� �������E��@ ������������$�������:)����������������D ������;��������� ������������� �������:�3 E�-���;�I���D ���� ������ ����� ������� ��� ������� ��� ������K� ��� �3 �� ��� �� ��� ��� �,����� :�3 E�main() –� )�����������,�����������D ��������,�����;�-�� ���)��������������� �����K���������������� �������:�3 E���� ����$����;��@ ������0����������)����������������)��������8 ����K�����,���)�������������������)������������������� �� ?������D �������������'��� � ; ������������������������ ����/ (��<identificador> ::= <letra> | <caracter><string> <letra> ::= a | b | ....| z | A | B | ... | Z | <caracter> ::= <letra> | _ | $ <string> ::= <caracter ou digito> | <caracter ou digito><string> <caracter ou digito> ::= <caracter> | <digito> <digito> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Page 27: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����9�

� � ���������I���������������������0������������)"��������2 �)����D �����������O��1_x inválido, começa por dígito X1 válido M&M inválido, carácter & é ilegal int inválido, é uma palavra reservada $dólar inválido, carácter acentuado no nome limite Inf inválido, espaço em branco

3.3 - Palavras Reservadas

@ ������0��������� ��������������H���� ����� �������������:�3 E��������-����;'�<���������K�����,�� ����������6��� ����0�������� ��)������� ��0����� ��� ��������K� ���������� ��E� 3 �(� � 2 � ��(� '� -�� ����)����* ����)��������� ���)�������6����������0�������������������D ����&��������0����� �? ��������������3 ��'�-��������8 ��������� ����������������� ���� ����������'��

�5�����##' 1 �������� ��� ��� ���)���������)������3 ��������������������2 �)��

3.4 - Símbolos e Valores

@ ��)*�#��� ���0��������2 �)����������)�����E�@ ��������E�TK� K�S���R��������� ��������E�eK�fK�:K�;���N������������� ���)���������)����K�����4��������&��������0�����������0���������� ������������6����� ����������0�'��@ ��0 ���� ������������3 �4�����D ��� ������������ ������� ���� �������K�����,��������������)�������������� ������������'��������3 �� ��E��<� �����������E�+!��� /�#�<� ����������E�/'#�#!>K� �'�����!'..0�@ ������������E�g�cK�g-c���gh�c�$�������������������E�L% �����PM�

abstract boolean break byte byvalue

case cast catch char class

const continue default do double

else extends false final finally

float for future generic goto

if implements import inner instanceof

int interface long native new

null operator outer package private

protected public rest return short

static super switch synchronized this

Throw throws transient true try

var void volatile while

S� �� ��

��

Page 28: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����.�

3.5 - Variáveis

C ���=��")���,����� �����6�����������?������ D ���� ������������������)���������������� � �'�-������)��")����������������������� �����������'��@ �)������������)��")��� ����)����������������3 ���������� �������'�<���������K���������������������)��")��� ����� ���������������)����'�$���� �������)��0�������0������������K�������6�����������������)��)����������)��")���������?���)�������������������������)��")��'�

�5�����#�' * � �������������"0�����������������)��")���

3.6 - Declaração e Inicialização

����� ����� ���� )��")��� ,� ������"��� 0�6��� �� ���� ! �������� ��)������K� ���� ,� �� �� ��0������ ��� ���� �����:����0�����;� �� ��� �������� )������� D ��� ���� ������� :� �� ��� �����;'� -� ����������� ,� ��������� ������� ����� ��)4������:N;'������,����4������D �������������2 �)�� ������������0�������D ���D ��������������������������'�

�5�����#/' ��D ������� ��������)�������������������)��")���

�� � ; ��������� ��������������������(���(�� ��� <declaração vars> ::= <modificadores> <tipo> <variaveis>; |

<tipo> <variaveis> ; <variaveis> ::= <identificador> |

<identificador>, <variaveis>

�-�+������? ������������������������������)�����������)��")�����:�3 E�total = 0;).

1002

Célula de Memória

int num = 145; Num -> EM 1002

1002

Célula de Memória

num = 210;

int total;

int count, temp, result;

��99�������� ��((������((���� �������������� �������������� ����������99��������������������������

���������������������� ����������������������������((������((������

Page 29: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����>�

<�������������0�����������)�����D ���D ���K�����������(�������������(���(��'��� � ; ������������������� ������������? �������(���(�� ��� <inicialização vars> ::= <identificador> = <expressão>;

��������K�����������K����������������6�������)��")�������������3 ������'��� � ������� int sum = 0; int base = 32, max = 149; �� � ; ������������������� �����������������������? �������(���(�� ��� <declaração com inicialização vars> ::= <modificadores> <tipo> <identificador> = <expressão>;

�[����������D �����������������������)��")���� ����� ��)�����������)������� ����������?���������"���������������0������ ������������������'�-�)��")��� �������)����� ���0���'�<�� ����6�������)��")��� 0����������)�������0����:�?������� �����������6���;'��I����������)��")���,���0������������� �������K�,����6������)������������:)����?�������3 �;'�� int nomeDaVar; // declaração nomeDaVar = 20; //inicialização nomeDaVar = nomeDaVar + 50; // Referenciação // qual o valor de nomeDaVar neste momento? int x = 50; x = x + nomeDaVar; // qual o valor de x neste momento?

3.7 - Constantes

C ���$��������� ����������������������������������������?��K��������������)��")��K��������D �����?� ������������������)���������� ����)�6'�-���K���)���������,� �����������������������������3 ���������� �������'�-�"�K�������������������������� ������������������)������0����������������������K�,��� �������������)��������������3 ����� �? �����������K��������������������������� �����'����� 2 �)�K� ,� ���6���� �� ���)��� �����)���� final� ���� ������ D ��� ����������� ����0������ �� �������� �������������'��� � �������� final int MIN_TEMP = -10;

Page 30: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/��

C ������������� ����������������������E�� final <tipo> <nome_var> ou static final <tipo> <nome_var>

� � ����� �� final double PREVISAO_VENDAS = 350000.0; static final double PREVISAO_VENDAS = 350000.0;

��������)�����K������������������)���������������������������� ������'��� � ; �������������������� �� ��� <declaração constantes> ::= <constante> | <constante>; <declaração constantes> <constante> ::= final <modificadores> <tipo> <variaveis>

������H����������������������������)��")��E�

� �&���������0������� �&�����)�����

�0�������������)����������)��")��E�

� @ �����)��������� �����������������

3.8 - Tipos de Dados

���2 �)�������������4)������� �����������E��� ��� � ��� ! �� � 3 �����(� E� ���� ��� ���� �� ���K� ������ ���6����� ���� �������� �� �����K� ����������� �� )����������������'�-���6��������������������)������'�2 ����@ ��� E� ���� ��F������ �� ����Z �K� �������� �� ��������� ��� ������ ����� ��� ������ ��� ���������� :)������� ����������������0���������������;'��� � ��������

int x = 10; // int é um tipo primitivo String str= new String (“Ola”); // String é uma referência

��

Page 31: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

��

5�����#�' * � ����������� ��"0��� ��� ���4)��� �����6����� ��� ���?��� ���� )��")��� ����������� ���� ���� �3 �� ��������������

�C �� � �� ��� ������ ,� ��0���� ���� � �� ��� (���� � D ��� ���� �� ��������� �� ����� �����> � � D ��� ������ ����� ��� ������0������'�����������)��� ����0������ ���������� ������������ ���)��'�-����������2 �)�������������� ������������ ���)������������� �����E��

� I�������� ���������������E�byte, short, int, long�� ����� ����������� ������* ���E�float, double�� C ���� ������������������E�char�� C ���� ��������)�������1 ?�����E��boolean�

3.8.1 - Inteiros

Tipo Inteiro

Tama-nho

Menor Valor

Maior Valor

byte 1 byte (8 bits)

−128 127

short 2 bytes (16 bits)

−32,768 32,767

int 4 bytes (32 bits)

−2,147,483,648 2,147,483,647

long 8 bytes (64 bits)

−9,223,372,036,854,775,808 9,223,372,036,854,775,807

�5�����#!' 0�������� � ������ ��������0�������� ���� ��������� 2 �)�'�<����� ������� �������)������� ������� ����

�������)��")���������������������� ��K�������������������������)�����D ��������)��")��� ����������$�����3 �� �������)��������4 �����������������)��")������������������� ��������� ���������� ����������E�� byte: 40 short: -22 500 int: 1 500 000 long: 4 000 000 000

���

��������

����

� �� �

� �� �

� �� �

� ���

���� � �� �

Page 32: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/��

�����3 �������������F��������� ����A ���D ��� ����������0���������������)����������� �������E��

Operação Símbolo Exemplo Resultado

Adição + 26 + 10 36

Subtracção - 26 - 1 25

Multiplicação * 26 * 10 260

Divisão / 26 / 10 2

Módulo (resto)

% 26 % 10 6

�5�����#+' $��F��������� ����A ���D ��� ����������0���������������)����������� ��������

��� ������@ ���� ��� �������� � �������� � ������ ��� �� ������� � � �����> � '� ���� �3 �� ��� �������� ��� ����������E��System.out.println(30 + 10 / 2); �-������ �����A ��� ���4)������E��

��

5�����#9'

Grau de Precedência

Operação

Alto ()

Médio -, + (unários)

*, /, %

Baixo +, -

�5�����#.' ����� �����A ��� ���4)����������������� ����������������������������������� �����&���������������

3.8.2 - Reais

C ���� ���������� ���������� ���������������������������������K�������� ���������������������������3 �������K���������3 ������:�;���3 E�!'�K�#�'/�K��'�K� �!'.K�#�'�K�!'+��9��Tipo Tamanho

float 4 bytes (32 bits)

double 8 bytes (64 bits)

5�����#>' � ������������������3 �����������2 �)��

correcto!

30 102

20+

= e 30102

35+ =

errado!

Page 33: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����//�

�Tipo Menor Valor Maior Valor

float 1.40129846432481707e-45 3.40282346638528860e+38

double 4.94065645841246544e-324 1.79769313486231570e+308

5�������' (������������)������� ������")��� ������0��������� ��������������������2 �)��

�@ ���� ������ ����� ��������� ������������ �������f� ���d� �������� D ��� ��� ���������� ��������6��������� �����float���������double'�-����&���������������� ����D ������� �������F������������������double'��

Operação Símbolo Exemplo Resultado

Adição + 5.4 + 2.0 7.4

Subtracção - 5.4 - 2.0 3.4

Multiplicação * 5.4 * 2.0 10.8

Divisão / 5.4 / 2.0 2.7

�5������#' @ ����A ��� ���4)��������������

��

Grau de Precedência

Operação

Alto ()

Médio -, + (unários)

*, /

Baixo +, -

�5�������' ������&���������� ����������������� �����������

3.8.3 - Caracteres

@ �����6�������������������������� 2 �)��,����������� ���� � ��char'�<�� ��������K����)��")������ � ��char�����6������� ������D �������0������������������������������������?��������������� , 4� ) � ' '�������?����,����� ������������������������ �������������������������D �������,���4��������������������������0�������������������������'� @ ��)����������� ��char��������������� ��� ����:�' ;E��� � ����� ���// 'a' 'X' '7' '$' ',' '\n' // são valores do tipo char char letra = 'a‘;

Page 34: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/��

3.8.4 - Lógico

@ �����6������������)��������?��������2 �)��,����������� ����� ��boolean,�D ��K��)����������K�� ����� ���������������������)������E�)��������K����0����'��-�� ���)���������)�����true���false��������� �����)�������)"����� �������� ��1 ?���� � � ������� boolean terminado = false;

3.8.5 - Operadores Lógicos

-���3 ����A ����?����� ��������6���������������� ����������?����E� ��

! NOT

&& AND

|| OR

�@ ������������ ���������?������� ����6���������������?����'��@ �<@ ��,����� ���������"���:����� ��������� ������;K���D ������D �����-<�����@ * ������ �����������"����:�&������� �������;��

a b a && b a || b a ! a true true false false

true false true false

true false false false

true true true false

true true false false

false false true true

5������/' �5�������' �����������)������������ ���������i i K�jj���P�

3.8.6 - Operadores Relacionais

* ���������������3 ����A ���������������� �������������������������)��������� ��booleanE�

Operador Designação > Maior que >= Maior ou igual a < Menor que <= Menor ou igual a == Igual a != Diferente de

Page 35: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/!�

-�0��������3 ������������� ����A ��� ���4)��� �������)"����� �����������E�

boolean char

float, double

byte, short, int, long

+-*/

%

>>=<<===!=

>>=<<===!=

>>=<<===!=

>>=<<===!=

+-*/

+!

||&&

++--

+

+

boolean char

float, double

byte, short, int, long

+-*/

%

>>=<<===!=

>>=<<===!=

>>=<<===!=

>>=<<===!=

+-*/

+!

||&&

++--

+

+

3.9 - Instruções

$���� F"� 0�� ��0����� ������������K� ���� ��� ������A ��� D ��� ������ ��� ��� �������� �� D ��� ����� ��)�� 0�6��'� -��������A ��� ����������������� ��E��

� � � ����(�� �G�����������3 ��������������� �������� Não executáveis – constituem directivas para a JVM�

�� � ����� ���// executáveis System.out.println("Olá!"); aluno.inscricao(); // não executáveis public static void main(String[ ] args) import desenhograficos.*;

�I��������������� ��3 ����K����������A ��� ��������E��

� +� ���> � �)����� �G�������������� �������)������:N;��� +� ���> � �' ���� � �����������������A ����������4��� ���)"����������A ����� ���������������� ���

L�e�k�fM��

Page 36: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/+�

� � ����� ��

// instrução simples System.out.println("Olá!"); // instrução composta { System.out.println("Olá!"); System.out.println("Bom trabalho!"); } �� � ; ������������ ��� ���> � ��� <bloco de instruções> ::= { <instruções> } <instruções> ::= <instrução>; | <instrução>;<instruções>

�C �����������������A ��� ��������������6������D ���D ������������������������������ ���� ������������6���'�@ �$�� ��������������3 ���������� �������� ���� �������������������,�����main()K�����������D ����)������������������ ��� ��� :)���������� ������������������ D ���,����,�����main����� D ���,�����������'����������K�������������D �����main��� ���������� �������� ������ ���������� ����������������������3 �������;'�-��������A ��������3 �����������D �����������K���������� ��D ��������������K������D ������ �����������K�������� ������3 �'�

3.10 - Comentários

@ �� ������"���� ��� ��� �������� ����������� ��� ������A ��� �3 �������'� �)��� ���� ����4���� ��� 0����� �3 ����� �� �� ?������� ����������������)��� ��������� ������������'�<��� �0���������� 0��������������� �������� ���������������� ������� �����'��@ ��������"�������2 �)���&������0�������E��// formato utilizado para comentários no final da linha /* formato para comentários que se estendem por mais de uma linha */

3.11 - Paragrafação / Indentação

$���������� ��� �� ���K� ��H��� ��� ������� �� ���������� ���� ������������� ���������� ��� �� ����� ��� ������'� @ ���� ����������������������6����� ������ ���������0�����������4���������� �������'������������ �����������������������������������'��C �� �������� 2 �)�� ���� ���� 0��������� ����� �� ���� 0�����'� �)�� ���� ���6���� �� ������0����K� ���� ���������� ���������������� ����������

Page 37: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/9�

� � ���������������� ���/ (�������� �������package meuteste; public class OlaMundo { public static void main(String[] args) { System.out.println("Olá Mundo!"); } } �� � ���������������� ���/ (� ������ �������package meuteste; public class OlaMundo { public static void main(String[] args) { System.out.println("Olá Mundo!"); } }

�@ �$�� �������������� ������0����������0��������H�'�

3.12 - Um programa em Java

$��������D ������ ����������� ��������D ��K������������������������K�����������������������������'��@ � ����� ��� ������ ,� 0�������� ���� ���6����K� ���� D ��� �� ���� )����� ���� ,� ���H����� ��� �������� ��� ������� ��� ��������G� ��)�����������������6����������)����'�������������6�������������� ������������� ���)����������-������������)��"������������������),�����������'��@ ��?���� ������ �������� ��������� �������������������E�� public class PrimeiroPrograma { public static void main (String args[]){ // Declaração de variáveis String nome; // Lê o nome do aluno System.out.println(“Introduza o seu nome”); nome = Le.umaString(); // Mostra a mensagem System.out.println(“Olá”+nome+“bom trabalho para a disciplina de Introdução à Programação”);

} }

������ D �������� ������� ���������6�����)�������,�������"��� �����)����� ����������?��'������ ����,�������"��������� �� � �� ��� ������ �� ����6����� �� ����� D ��� �"� ���� ������ ���� �� ����0���� G� ! �������� ��� 0 ���(�� �:String nome);�������������6����� ����������6��������E��������

�Z ����'���' �����:L�������6������������M;N�

Page 38: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/.�

�Le.umaString() ,������� �������� D ��� ��)���� ���������� ����������� ����� D ��������6����������)���������������������������������0������������������L' ���M'�@ �)�����������6���,�����6���������)��")�����nome.��(�������������6���������������E��

System.out.println(“Olá”+nome+“bom trabalho para a disciplina de Introdução à Programação”); �<��������������������"���������������)���� �����������������������������:L�M;'�I������� ��������������������)��")������������������������K������������� ����������)�������)��������������������)��")�������������������'��! �(��� � �������� ��������� � �� �� ��� �� G� 0������ D ��� ��� ���6��� ���� 0������� �� ������� ��� �?���� ��� ��� �������� ��� ��������D �����)���D ������6������������?������������" ��� ���D ���D �������)�'�<���D �����F�� �����?�� �? ���K� D ������)�������������3 ������ �?���������� �������� D ��� F"��������6")� ������ �������K�����������,�����K����� �������������������D ���06�����������D �����F���)��'��<���3 �� ������ ����������������������"��������������0����E��//Declaração de variáveis //Mostra a mensagem

3.13 - Exercícios Práticos

�3 ���'� #E� � ��� D ��� � ������������ �������������� ����������� ��� �������#O�<���� D ���������������� �������������0��������� ��'��

1 23E5 300 7E10 1E3 0 -255 23.4E300 32767 -32767

true false 8000000 -127 123 ��3 ���'� �E� ��������� D �������� ����������� ������ ����)"����'��������� �����,� )"���K� �� ��0D ����� D ��� � ����������� ���)�� �������'��

666 777.

888.0 6.66e2

483,500 1666667e -6

���3 ���'� /E� � ������� ����������� ���� ����� ��� ���� ����0�������� ��� ������� �� ���6����� ��� ���H��������� D ��� F"���D �������2 �)�E��

conta 3 diferenca -2 fraccao 0.33333 cor Azul

��3 ���'��E����D �������������������������A �����������������'�2 ���0D ��'� � �;�CONSTANT PI = 3.14;� �;�CONST e = 2.718;� �;�CONST a = 3; b = 4;

Page 39: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����/>�

�;�CONST a, b = 12;� �;�CONST a = 12; b = 12;� 0;�CONST valor = 2

�;�VAR y : INTEGER� H;�VAR x, y : CHAR;� ;�VAR x : BOLEAN;

F;�VAR x = REAL;� J ;�VAR x : CHAR;� �;�VAR a: CHAR; VAR b : REAL;

�;�VAR a : INTEGER;� ��������������������;�VAR a; b : INTEGER;� � b : CHAR; c : INTEGER; c : REAL;

��

Page 40: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

4 - Sequenciação

4.1 - Instrução de Atribuição

-���������������������� ����������6��������)����������)��")��'������)����� ��������0������������������������������������"��������������3 ������'��� � ; ������������������ �����������#�������� <instrução de atribuição>::= <identificador>=<expressão>;

�-��3 ����������������,�������������� �����������K�����������������������6���������)��")��������D �����'�<����������3 ��K��������R���)����� ���L�������)�������M'��?� �������������4������)�����������)��")���D �����F�������������������� ��������������������� ���������)��")��'�-�������)��")��K� ��������������������D �������������������������������������'�$������� ������3 �� ��E�

��

5������!' * � �������������D ���"�������������������)�������������)��")����� � ����� E��

��int conta ; conta = 25; ���������!�sum = conta; ���������!�conta = sum + 15; �����������conta = conta + 1; ���������#�

4.1.1 - Operadores de Atribuição

����)�6���,�������"����0������������ �����������������)��")��K�����������������6���� �����)�����������������������)��")��'�@ ��� ���������������������2 �)���� �0����������� ����A ��'������3 �� ��K���������������� num += conta;�,��D �)����������� num = num + conta;

������K�,������������������������)���������������)��")���conta

���������������������,�����6��������)����������conta :�������4������)�����������;�

�������R�������T�#N�

@ �)����������������)��")���total�,��������4��� ���!!�

������R�!!N�

Page 41: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

�5������+' 0���������� ��������A ��� �������� ���������������������

4.1.2 - Operadores de Incremento e Decremento

@ ��� ���������������������������������������� �������������,����������������������� ������'������ ������������������� ���������������������)��")��K� ����3 �� ��E�� conta = conta + 1;

�@ ���������� �������������������E�� conta++; // Versão Sufixo ++conta; // Versão Prefixo

�! ���������������)���� ����3 ����� ���)���� ���������������� � �� ����������������3 ��������int x = 0, y = 1; x = y++; BB�Z �,��K�3 �,�#��3 ����� ����������������� ����������������3 ��������int x = 0, y = 1; x = ++y; // y é 2, x é 2 @ �� ���������������������-- ������������������ ������E��count--; --count;

4.2 - Expressões

C ����3 �������,�������D �&�������� ��������������)������'�-���3 ����A ��� �������������������K�������� ��������)��)����'� �

@ ��������TR� R�SR�BR�W R�

�3 �� ����3 �TR�Z �3 � R�Z �3 �SR�Z �3 �BR�Z �3 �W R�Z �

�D �)����������3 �R�3 �T�Z �3 �R�3 � �Z �3 �R�3 �S�Z �3 �R�3 �B�Z �3 �R�3 �W �Z �

Page 42: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

�����������E��� � ��� > � �������� �G���)��)���� �������������,����� �� 5 * ( 2 + 3 ) + ( 7 – 3 ) / 2 Resultado: 27 -���3 ����A �������,����������������������)���������������� �������������0��������� �����������)��)�����

� � � ��� > � �- � � �� �G��&��������������������)������?�����������������4��������� ���������������������� ����������?�����

�� � ����� E���3 �� ��#E�����������)�������������������3 ����A ��K�������������,����� �����)������������ 2 � ����� 3-4*2+1 �������� ��� 2*(12%5)-(8-3)/2 �� ��� 2*12%5-8-3/2 !� ��� 2.5+6%4*2.1 +'9� �������� �S::��B+;T:/S:� #'!;;;W �:�'!T# #'!;� �� #'����������������3 �� ����E�����������������������������������3 ����A ������������A ���� 2 � ���� 3+5/2) >= (1-4%2) ���� x=3 == 4; 3 �0���0����� 8-1 != 3 + 4 0����� ‘a’ < ‘b’ || ‘c’ < ‘a’ ������ (!a && b)|| (a || !b) � � ����� ������R����������R�0����� � ��

4.2.1 - Conversões de Tipo

@ �� �����������������������3 ��������� ��������� ������������ �������'�������� ���������������������������� �K����������������"�����,��������� �'�������� ���������������)"����� ���G����)�����'��-�����)���A �������0��������������������������������D �������H�F�� ���������0�������'������D ���������������������0�����A ������� ���4)��'��3 E����)�������������� �����������)��������������������� �������������: �������� �����������;N��������"���,� ���4)������� ���������0�������'� [� ���4)������)���������)����� �������� ��D ������ �������� ����������?��K��������)���������,�)���������:��������D �������������0���" �������������0����K������)��������������;'��$�����������)�����������"������ � � �Z ���X��H����X����X������X0�����X���������-����)���������� ���������0��������G� ������)������ ��������3 ���������K�����������"���,������������������� ������� �����'��

Page 43: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����/�

� � ������� resultado_int = (int) (valor_double + valor_int)

�� � ����� E���3 �� ��#E�$������� ��������������?�����int var_int=10, resultado_int; double var_double= 5.2, resulatdo_double; resultado_double = var_int + var_double; <��������� var_int ,����)������ ��� double , �������,��0����������������������,�double resultado_int = var_int + var_double; �����������6������������������double �����)��")���int ����,� ���4)������� ���������0��������:��� ������������������;'� resultado_int = (int) (var_int + var_double); ���3 �� ���E� I���� �� )����� ��� x� ��� 0���� ���� ��������� ������A ��� �� ����� D ��� 0����� 0����� ��� ��������A ��� ������6��A �������)��")��E��double x, y=6.3, z=2.2; �� �����x= y/z; -2.8636363636363633 x= (int)y/z; -2.727272727272727 x= y/(int)z; -3.15 x= (int) y /(int)z; -3.0 x = (int)(y/z); -2.0

4.3 - Classe Math e Random

(���������K�0�������������� ������������F"�������������������0��&����K����D ����������0����� �����������������������D ��������������������' � �'��������������,��������� ��3 ������)������)���� �������������0���������������K�������D ������ �������� ���������������D ������"���)��)������������0����'���������K� ��� �����K� )����� ������ ����� ������� ����� ��� ���F����� ��� 0���A ��� ���� ����� �������� ��� �����3 ��������'� ������ 0���A ��� ���� ��� 0����� ����"���� ���� D ���� ������� ��������� ��� � ��������� ��� ������� �? ���� ��������K� ���0������������� �������������)��� ��������� �D �������������������K� ����3 �� ��K���������������� �����������?����K������6���������)������0���A ��������"����K����'�-�� �? ������������ ������������� ���������������������������F����K������������H��������������������������������D ����)�����0�������������� ����'�

Page 44: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

4.3.1 - Biblioteca de Classes

C ��� ��������� ��� �������� ,� ���� ��������� ��� �������� ��)������� ����������K� D ��� ����� ���� ���6����� ��������)��)��������� ��������'��-���������K�����������������������0��������K� ��������������6�������� ��J �����: ������;E�����J ����� � @ �F���)��java.lang �� �����������������java.util C ���"����java.applet $���������� ����� ������_ �� etc.

4.3.2 - Classe Math

C ���������������������� ��)������������������,�����������Math� � D �����������)������� ����������� ���������D ��� � ��������� �)������ 0���A ��� �����"����� :�3 E Math.sqrt()� G� �������� �� ��46� D �������N�Math.sin() G����������������������8 �����N����';��-� ������ Math� �������� ��� � �� $ � �� java.lang� K� ����� � �� $ � �� ,� � ������� ��������������� ���������������������������� ��40������ �������'����������6��������������������� �������,�������"���� ������:import;��3 ������������������'� �3 �� ��E��$���������� ��&������������ ��������)�����������'�� public class Calculo { public static void main (String args[]){ int base=2, expoente=8; int resultado; resultado = Math.pow(base, expoente); System.out.println(“O resultado é: ”+resultado); } }

4.3.3 - Classe Random

-��������Random�,� �������� � �� $ � ��java.util :0��������,������ ������������ �����������?���;'�$��������,��������������� ��J ����java.lang�,�������"���� �������3 ���������E��� � � ����F�)�'���'* �����N� I������ ,� ������"��� ���6��� ���� ������� ��� ��� � �� $ � ������ ���� D �������� ����� �� �����)��importK� ���� �������6������������������ ����'��

Page 45: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

-�������,����������������Random �

(,����� ��������* �����:;� $���������������� �����������?��� �����3 �:������;� 7 �������� ����������?����������������3 ����:;� * ������������ ����������������?��������3 ����:����;� * ������������ ����������������?�������������)����������������������3 ������:;� * ������������ ���������������?��K��������'����#'��

�� public class Calculo { public static void main (String args[]){ int base=2, expoente=8; java.util.Random r = new java.util.Random(); int resultado = r.nextInt(); System.out.println(“O resultado é: ”+resultado); } }

�-����)��������,�D ����������D ����������������)����������������������3 �����K� ����D ���������� ������ ��������6�����������������'�-����������������� ���4)��K���������������6������0������3 �4�����������)��� ���E��import java.util.Random; public class Calculo { public static void main (String args[]){ int base=2, expoente=8; Random r = new Random(); //agora já é mais legível... int resultado = r.nextInt(); System.out.println(“O resultado é: ”+resultado); } }

������ ����������������������������� ��J �������6� ���������������� �����*�� � ��� � � ����F�)�'���'SN�

Page 46: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����+�

� � ������������������� ���������������������������'� import java.util.Random; public class SomaAlet { public static void main (String args[]){ Random random = new Random(); int n1 = random.nextInt(); int n2 = random.nextInt(); int soma = n1 + n2; System.out.println(“A soma é : ”+soma); } }

4.4 - Entrada e Saída de Dados

@ �� ����������������������������� ������)���"�����������6������ ����A ��������������B���4�����������'�@ ����F�K�������� ���������������������)��������������6����'��@ ����A ��������������B���4�����������E��C ��6�����0�������������������������������������������4���������������������E������:���������;���������������������������E�,�0��������),�������������

4.4.1 - Saída de Dados

-����������������������������� ��������0�������6����������� ���������print��� println �3 �����������System.out (package�SystemK� �������out;'� I���D ���� ��� ������� ��� ��������� ������ �����)��� �������K��0������� ��������0����������println��0������������������������H��� ?�������������D ��������������������print�'� ����� ��� �����)��� ���� ��������� ��� ����� ��� ��� ��������� print� �� println �&�� ��� �������� ����� ��8 ������������������������)��K�����,���)��� ������������� ��&�����'������3 �� ��E�����������E�� System.out.print (“Olá a todos”); �����)���������E���Olá a todos��println�(�� � �print��System.out.println()

Page 47: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����9�

$������

$������

//�

@ �����������//������

-���������������� ������,�!�

(����������������������������� ��������������������4��������H���������E��System.out.println(“Primeira mensagem"); System.out.println(“Segunda mensagem"); X�����������������X������������������XQ � System.out.print() (������������������� ����������������������������� ?����)�����������E���Z ����'���' ���:a����������������a;N�System.out.print("Segunda mensagem "); ��������������������������������Q ��� � ����� E���3 �� ��#E������)�����)���������������������)��")����� ��������R�//N� � � � � System.out.println(idade); ���3 �� ����E������)���)"����������������������?�)�6�� int idade = 33; System.out.println(“O Pedro tem “ +idade+ ” anos”); ���3 �� ���/E��3 ����A �������������������������������)����

int x = 3, y = 2; System.out.println(“A soma dos dois números é “ + (x + y)); �

4.4.2 - Entrada de Dados

-� ��������� 2 �)�� �� A �� ��� ��� ��� ��������System.out.read() �� �6� ��� ���� ��� ��������� �� ����� ����������'�<���������K� �����������������,�����04������6����K� ���������������0��������������6�����0������ ����� ����������D ��������������K� ����3 �� ��K����0������������������� ���������)"�����4����K��������� ����������)��� �������40������6����K�F"�D ����������������H�������������)�6���D ���������������������D ����������������0�������������'�

4.4.3 - Classe Le

������ ��� ������ �� �0������� D ��� �3 ���� ��� ������� ��� ������ � �� ��� �����K� � ��� ��� ��� ���6��� ��� ���F����� ������ ����������� �6������������������ ���������������� �����'�-���������?�,����������D �����������6����� ���������������� L' ���MK� ����D ������������ ����������������4���� ���D ���D ������ ������������������'���������� ������������������ ������� ��������0�H����Le.javaK�D ���������������������3 �'����

Page 48: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����.�

�3 ����������0�H���������� �������� ���������� ����������E��� � � �����=����� � � $H������ int Le.umInt() float Le.umFloat() double Le.umDouble() String Le.umaString() �-����6��������D ���D ����������������� ���������,������������ ���'�� double var_double = Le.umDouble(); �� � ����� E���3 �� ��#E������)������ ��������D ������)�����8 ��������������������� ������������:�0���������/+�`;�� public class Angulo { public static void main (String args[]){ double angulo, angulo_rad; System.out.print("Valor do ângulo = "); // Lê ângulo em graus angulo = Le.umDouble(); angulo_rad = angulo * Math.PI / 180; // Converte para radianos // Mostra ângulo em radianos System.out.println ("Ângulo em radianos = "+ angulo_rad); } }

��3 �� ����E� �����)����� �������� ���� ��������� �����������������K���0��������������� 0����E�+�W ��,���������������������� ���K���W ��,�������/����H�����������H��� �"���������W �������H��0���'��public class Media { public static void main (String args[]){ double teste1, teste2, t_pratico1, t_pratico2, t_pratico3,trab_final, nota; // Lê notas dos testes, trabalhos e trabalho final System.out.print("Nota do 1º teste = "); teste1 = Le.umDouble(); System.out.print("Nota do 2º teste = "); teste2 = Le.umDouble(); system.out.print("Nota do 1º melhor trabalho prático = "); t_pratico1 = Le.umDouble(); System.out.print("Nota do 2º melhor trabalho prático = "); t_pratico2 = Le.umDouble(); System.out.print("Nota do 3º melhor trabalho prático = "); t_pratico3 = Le.umDouble();

Page 49: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"�����>�

System.out.print("Nota do trabalho final = "); trab_final = Le.umDouble(); nota = 0.6*((teste1+teste2)/2)+ 0.2*((t_pratico1+t_pratico2+t_pratico3)/3)+ 0.2*trab_final; System.out.println ("Nota = "+nota); // Mostra nota } }

4.4.4 - Classe String

C ��������������� ����0�����������������2 �)��,����������String'�[������������������������6������D ������)�� ������� �������������������������'��C ���String����)�� ������������)�����������������K� ����,�����)�6�D �����F�������������)�����������String���������� ����������������'�-���K�D ���D ����� ������������������tring ��)��)��������)��String�:String ���0����;�� String frase = “Introdução à Programação” �-�������,����������������String �(,����� �������������:;� $��������F��������� ������� char charAt(int index)� �)��)���������������� ������index����������int compareTo(String anotherString)� $�� ����������

�������int indexOf(String str)� ���������������6�������� �����������&�����������

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

String toUpperCase()� �����0����������� �����������������������������int lenght() * ������������ ������������������ �E�����������0�����A �������������,�������� ����6����� ����String�����������������������������2 �)�'��� � ����� E��String frase = “Hoje é quinta feira”; �� � 2 � �����int tamanho = frase.lenght();� � ������� � � �����H�R#>�String palavra = frase.substring(0,4);� �� � ���)��RL] �F��M�String novaPalavra = frase.toLowerCase(0,4); �� ��)�����)��RLH�F��M char c = palavra.charAt(2);� � ����� � � �RgFc�boolean b = novaPalavra.equals(“Hoje”)N� � � �R0�����int indice = frase.indexOf(“qui”); � � � ����R9�String outraPalavra = frase.substring(indice,indice+7); � � � � � � � ������������ � ���������)��RLD �����M�String outra = outraPalavra.trim(); ������� � � �����RLD ����M�

Page 50: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

4.5 - Exercícios Práticos

�3 ���'�#E���I����������������)������������3 ����A ������������O��

a + b + c + d + e a / (b + c) - d % e a + b * c - d / e a / (b * (c + (d - e)))

��3 ���'��E�����D ���������F��������������"�����������8 ��������� �����������0�����#�������0D �� ��������������������������"����E��public class ImprimeTipos{ public static void main (String[] args) { boolean b = false; char c = ‘r’; byte j = 127; short k 32767; int m = 1234567890 long n =9223372036854775870L; // L de long float x = 3.14159265F; // F de float double y = 3.141592653589793238; Syste.out.println(“b= “ + c); System.outprintln(“c= “ + c); Systemout.println(“j= “ + j); System.out.println(“k= “ + y); System.out.println(“bm= “+ b); System.out.println(“n= “ + n); System.out.print(“x= “ + x); System.out.println(“y= “ + y); } }

Figura 1

��3 ���'�/E��������)�� ��� �������� ��� 2 �)�� D ��� ����6�� ���� )��")��� ������ �� ���� �� )����� !.#�� �� ��� ������� �������� ����� ���������D ������������)����������������������)���������� �����3 �������� �����������������4���������'�-���4������ ����������)���������� �E�<�R�!.#��@ ���4��������������!K�.K�#K�����$�����H�E������B#���� �����3 ������������������H���������K����W R#���� ��������)����������������H���������'���3 ���'��E���$���������)����������3 ����A ������������K������������������������D �������������F�� ���4)��'��

23 + 34 < 123 (23+34<123) && (89.1*100>200) 89.1 * 100 > 200 (89.1*100>200) || (34.3 != 34) 78 / 2 == 50 (78/2==50) && (23+34<123) 67.23 * 200 <= 500 (67.23*200<=500) || (78/2==50)

Page 51: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

900 – 200 >= 1000 (900–200>=1000) && (67.23*200<=500) 34.3 != 34 (34.3!=34) || (78/2==50)

��3 ���'�!E��������)����� ��������D ��������������,������ ��� �����������&�������K�-���K�(�����F��H�'��������������6������)��")��������� ��� ���������������&�K������������,����������)���������������������0����E����� ���������-���E� #�'����� ���������(��E� #�'����� ���������2 ��H�� .�(,������ ��� �����E�� ##'//////���������H������)����������,���������������������������������gh�c������ ��������������������������������������A ��������4��'�=��0D �������� ��������� �������������������������������'���3 ���'�+E��������)����� �������� ���������)������������������������������A ����;�1 �������� ����������K����� ��" ��� ������������)��������������'��;�1 ��������� ��������������������)�������������K��0��������� ������'��;�1 ��������� ������������������)�������������K��0��������� ������'���3 ���'�9E��@ �� ������H���������������� ����� �)�������������������/K#W ������0�������������)������+������'� 1 ��������������"��������������������H��������������)�����)�����������������)�����������"���������'����3 ���'�.E���$���������� ����������������������)4��������������������B���:����������������������������;�������� ������������D ������?�����)����)4����������� ����'���3 ���'�>E��������)����� �������� ����������������,������������F��������#���� �������������� �������6����'���3 ���'�#�E�������� ��������� �� ����� ��� ��� )����� ��� �� ��� ������� ���� ��� �,������ Math.sin(value)� ��Math.cos(value)'�<����D ������)�������������������������������������������������'��;������)����� �������� �����������������������������������'!/+'�-���6�����������������������)��")��������������� �������� �� D �������� ������� ������� ����6��� �� ����� �������� )��")��'� �����)�� �� )����� ���� ��&�� )��")��������0����������H���������Z ����'���' �����:a���E�a�T���3 �T�a������E�a�T����3 �T�a����E�a�T�����;N���;��3 �������������������)���������,������'!�/+����3 ���'�##E���������� ������������3 ���4�������������������� �H���������������D ������)��������������������������� �� ����������� )������� �� 3 � ��� ������ �� �� ��� ��� ���)������� ��� �������� ��" ���� ����� ���������� ���� �,����� Math.sin���Math.cos'�@ ��,�����Math.PI �" �H�����)���������� ���������)��������Pi'�

Page 52: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

5 - Instruções de Controlo

�-�,��D �K� ����������3 �� ��������?���� D ���)��������������������������� D �������"�������������D ������'�<�����H��� ���H���� ��������� D ��� ��� ������� 0����� 06����� ������ �� 0��3 �� ��� �3 ������� ��� � ������'� @ �� ��F�K� ���������A ��������3 �������������� ?�������������������������������� ���D ���� ������'������0����0����������������������������� ���������D ������ ������)���� ����6�'�* ����)��� ���������������� ��3 ����3 ����������� �����������������������0������������ ��������,��3 ��������:0��3 �� ��� �3 ������;K� �� D ��� ���� ,� ���������� ����� ������A ��� ��� ��������'� @ � D ��� ������ ���6��� ��� ��)�� ,� ���� �����������������������3 ��������������� ����������������������������������������A ����?����'���3 ������������������������ ������������A ��������������E��

" )�������E� �����������������������H��������������������������)�������������3 ������N�$ 2 �������E� ����������������� ���� ��������)�6��� D �������������������������� ������A ���,��3 ��������

:��������H������������;'��

5.1 - Selecção

-��� ���������� ����A ��� �����������D �������������A ���������������)�����������3 ����A ��K�������0��������� �������6����K����������������������K����'������� ����������������������������������A ��������������E� ����� l �����3 ��������������������������A ��'��-�������������������A ��� �������E��

• )����� E�$����������������������)������3 ������������������������A �����������������3 �����N�• � �������(E���������� ������������������������ ������A ������3 ������'�$������������������K����

�3 �������������3 �����������'�• �9����E�-�� ���������������)"��������������A �����)��0���� ����,�K������������3 �������,������H���

����������������������F��)��0���������H������ ���)�'�����2 �)�����������A ������������������������������E��

• if/else

• switch - case �$���������������A ��� ��������������6�����������������&��� ������������A ������������������'�

5.1.1 - Selecção Simples: if

C ���3 �� ��������������������� ������ ���������� ������0��3 ������� �����������������E�

$�������A�

�������A ��'''�)�������

0�����

Page 53: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����!/�

5������9' 5��3 ������������������������������������� �����$������ ��������)�������������������?���'������������������������)��������K�������,��3 �������������������������������������A ��'�$���������"��K����3 ���������� �������� ���������������������������������'�����2 �)�K������� ���������������������6� �������������������������ifK���F������3 ��,����������E��! �� ��� ����������

�5������.' ����������"�����������������������������if�

�� � ; ��������� ��������� �������� ����� �B�if��� <instrução if> ::= if (<expressão-lógica>) <instrução>;

�' � ��� ���� �������������� �����������if (condição) { instruções; }

�� � ������"���public class TesteIdade { public static void main(String args[]) { int idade = 14; // Verificar se um individuo tem // idade para guiar if (idade < 16) System.out.println(“Muito novo para guiar”); } }

�� � ������$���// Programa que gera um número aleatório e reporta se // o número gerado é ou não negativo import java.util.Random; public class NumNeg { public static void main(String args[])

if instrução expressão ( )

Page 54: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

{ Random random = new Random(); int n = random.nextInt(); System.out.println(“n = ” + n); if (n < 0) System.out.println(“*** n < 0! ***”); System.out.println(“Adeus.”); } f

5.1.2 - Selecção Em Alternativa: if-else

(�����)�6����������� ����D ��K�D ������������������)��0������F��0����K�H�F��������F�������������)�����������A ������3 ������'�[�������D �������"�������������������������������)�K�D ��K��������������0��3 ������� ���������� ���������������������0����E��

�5������>' 5��3 ��������������������������������������������)��

��* ��� �������E��������������0���)��������K�,��3 �������������F��������������A ���-'����0���0����K�,��3 �������������������)�������F��������������A ���% '�@ � ���������������� ����������������A ���D ����)��� �����3 ��������������'�������������������3 ��2 �)�K������� ���������������� ������� ��������������0����E��! �� ��� ����������

�5�����/�' ����������"�����������������������������if-else�

$�������A�

-� ��������A ��'''�)�������0�����% � ��������A ��'''�

if instrução expressão

else

( )

instrução

Page 55: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

� � ; ����� ��������� ����������������(�C���D�� ���� <instrução if> ::= if (<expressão-lógica>) <instrução>; else <instrução>;

�' � ��� ���� �������������� ��������D�� ���� if (condição) { // as chavetas aparecem quando há mais que // uma instrução a executar dentro do if ou

// do else. Passa a ser composto! instruções; } else { instruções; }

�� � ������"���public class TesteNota { public static void main(String args[]) { int Nota = 14; // Resultado da avaliação if (nota < 10) System.out.println(“Nota negativa”); else System.out.println(“Nota positiva”); } }

�� � ������$�� // Programa que gera dois números aleatórios // e reporta qual deles é o menor. import java.util.Random; public class Menor { public static void main(String args[]) { Random random = new Random();

int m = random.nextInt(); System.out.println("m = " + m); int n = random.nextInt(); System.out.println("n = " + n); if (m < n) // o que acontece se m=n? System.out.println(“O menor numero e " + m);

Page 56: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����!+�

else System.out.println("O menor numero e " + n); } }

�-��������A ���if� ����������� �������������� ���������������������K������H�F��)"������������)������������������� ��������'��� � ������%�� // Determinar a ordem de grandeza // dos três números (a, b, c) usando if’s encadeados.

// código: if (a < b) if (b < c) System.out.println("a < b < c"); else if (a < c) System.out.println("a < c < b"); else System.out.println("c < a < b"); else if (a < c) System.out.println("b < a < c"); else if (b < c) System.out.println("b < c < a"); else System.out.println("c < b < a");

���U���U������U���U������U���U������U���U�����U���U������U���U����

���

����

���

����

:��U��;�O�:��U��;�O�

���

����

:��U��;�O�:��U��;�O�

���

����

:��U��;�O�

���

����

Page 57: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����!9�

� � ������E�� // Determinar a ordem da grandeza // dos três números (a, b, c) usando if’s paralelos.

// código: if (a < b && b < c) System.out.println("a < b < c"); if (a < c && c < b) System.out.println("a < c < b"); if (b < a && a < c) System.out.println("b < a < c"); if (b < c && c < a) System.out.println("b < c < a"); if (c < a && a < b) System.out.println("c < a < b"); if (c < b && b < a) System.out.println("c < b < a");

��������������K���� �����������������������if’s���� 0��������������������� ����������������� ������� ���������������0�����������������������)��0������ ��������������������������'���������������������� ��40��������������)��elseK�H"����������� ����������0�6��'�] "����������D �����else� ���K���� ������� ���8 �����K� ������ ��� ���4���K� ��� ��F�K� � ���� ����� �4)��� ��� ���� �����A ��� �0�������� ����� �������������'��� � ������F ���if ( numeroAluno > 1200) if (numeroAluno < 2000) System.out.println("O aluno tem um numero entre 1201 e 1999"); else System.out.println("O aluno tem um numero inferior ou igual a 1200");

���, ���if�����������elseA����0�������������"�����������?���K� ��������������������������D �����else��� ������������������)������������numeroAluno > 1200''''��(��� ��� ��������� �� �?���� ���� ��� ����� ��� ���������� �� �� else� �� �������� ���� ��������)�� �� ��������numeroAluno < 2000'�

�U�O� �U�O�

�U�O� �U�O�

�U�O� �U�O�

�U�O� �U�O�

�U�O� �U�O�

�U�O� �U�O�

�U�U��

�U�U��

�U�U��

�U�U��

�U�U��

�U�U��

Page 58: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����!.�

<��� ����� �3 ���� ����������� D ������ ��� ����H�� �� ������ ��� �3 ������� ��� ������A ��'� ��� ���� �)����� ������� �������� ���������������� ������2 �)��� �����������������K������� ���������������������D ���0�6���D �������H����������������F��������������A ����������,����'����2 �)�K������8 �����������������ifBelse�����D ������else�a �������a���� ������if�D ���0����3 ������������������ ����������'�@ ����F�K����� �����if�����else'��<�����������������K� ����0��������D �����else����)����������������� ������ifK����4������������������������E��if ( numeroAluno > 1200) { if (numeroAluno < 2000) System.out.println("O aluno tem um numero entre 1201 e 1999"); } else System.out.println("O aluno tem um numero inferior ou igual a 1200");

�<���������K��6�����D �������������if��������������D ����������)�� �����������������4���:�����������������F��)��������;K�0�6��� �����������������������A ������3 �������D ������numeroAluno > 1200�������)�����true'�����"�� ����������� ����������� '��� ��������H�)���if’s�����else’sK������������"����������� ���'��� � ������G ���// Se tem menos que 70 pontos, visualizar “Deve melhorar“ // Se tem mais que 95 pontos, visualizar “Trabalho // excelente!“ // Se tem entre 70 e 95, não visualizar mensagem // O código seguinte está certo ou errado? import Le; public class Pontos { public static void main(String args[])

{ int pontos = Le.umInt(); /* Sem as {} sabemos a que if pertence o else? E é esse o resultado desejado? */ if (pontos >= 70) if (pontos > 95) System.out.println(" Trabalho excelente!"); else System.out.println(" Deve melhorar ");

} }

�- ��������� ��������� D ������else� ����������� � �����if���3 �������������K���else� �����������if���������)��0������pontos > 95P��������������"����������������D ������"D ����)������2 �)��������� �����K� �������H"�������� ��������������'�-��"D �������������������� ���D ����,��� �?3 ���������������3 ������'��������������8 �����K���������������4������D ����������������� ����������� ������������������������������������� ������ ������������'�

Page 59: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����!>�

5.1.3 - Selecção Múltipla: switch - case

<���� ������ ������A �������������������������������������)��0�"����D ���,� ���4)������6����0��������0������������������0� �������������������������)��D ��������������),�'�<���������K����������������������)���D �������0�����������������)����� ������������������������),��������)������?���K��������������������������������� �� ���D ���)������ �������������� ��������0��6'����� ������� ��� 0��3 ������� ���� ���� �� ���������� ��� �������� 0����:���������� �)��� 5$-� G� 5����������� ������������������2 �)��� �������������H�;E��

�5�����/#' 5��3 ������������������������������������� �� ���

�$������ ��� ��������� �� )����� ��� �3 ������� �3 � D ��� ���� ��� ���� ��� ���������� ��� � �� ������ ��� ��������'� � ��K���� ������)���������3 �����������������)�������)#���)�'����0�������������������������K��3 �����������������������A ��������� �������'������)���������3 ����������3 �����������0�������������H�������)�������)#'''�)�K� �����������������������������A ��E�

• ����H"��������H������������)���������������� �����������H������������������A ������3 �������• H"�������������)�� �������������3 ��������D �������������H�������)����,�������������H���:���0��3 �������

���"��� ���������� ����)���������;'�����2 �)�K������������D ���������� ������ ��������������A ����������������� �� ���,������������switch'�-����������switch�,�����,���������������������������D ��K��������������)��������������)��")�������3 ����������)�����������:�����������;������H�K�������)"������������)��� ���4)��K��������������������F��������������A ������3 ������'��

���3 �

�#� ��� ��� +��

�=#� =�� =�� �����

Page 60: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+��

�������������"�����������E��! �� ��� ����������

�5�����/�' ����������"������������������������������� �� ���switch-case�

�� � ; ��������� ��������� ���������9�����C�switch/case���switch (<expressão>) { case <valor 1>: <instrução 1>; break; … case <valor n>: [case <valor m>:] <instrução x>; break; default: <instrução y>; break; }

�-����������break�� ?���������������)�� �����)����D ������ �?3 ������������F����3 ��������'����H"���������D ���������������)���������3 ����������������������������A �����)�������3 ������������break�0�6�����D ��������������switch���F����������'�@ �default�,��������D �����)�������3 ��������D �������������H������)��:����������0��3 �������,���� ��������;'�@ ��������D ���������F����3 ���������� ���������������switch�����default�������������'�-��3 ��&�������default�0������ ���������������������������� ��������������A ����3 �� �����'�] "K�����������K������A ������D ��������6��������default�����,�������"��'��� �����

• ��D ���������break������������)�����������K������6����������?�����������8 �����'�• �������������������cases������default���������F����� �������� �����K�,���������������� �"����

����������default����� ����������'��

switch (expressão)

case

constante

:

instrução break

;

{

} default

Page 61: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

• @ �break� � ?�� �� � ����� ��������� ����� :���defaultK� ��� ����� 0��� �� � ����;K� ���� ,� ������"��'�(����� �������������������� ���� ����� �������D ��������������&������������� ��������������������'�

�� � ������"���import Le; public class Pontos { public static void main(String args[]) { int pontos; System.out.print (“Digite um inteiro entre 1 e 100:> “); pontos = Le.umInt(); switch (pontos/10) { case 10: case 9: System.out.println("A - Excelente!"); break; // por exemplo, se o break nao estivesse aqui // e pontos/10 = 9, o bloco seguinte (do case 8) // era executado na mesma, conduzindo a um erro! case 8: System.out.println(“B - Bom trabalho!"); break; case 7: System.out.println("C - Pode fazer melhor!"); break; case 6: System.out.println("D - Falamos depois da aula."); break; default: System.out.println("F – Vai trabalhar!"); } } } � � ������$���import Le; public class TestaCaracter { public static void main(String args[]) { char ch = Le.umChar(); switch (ch) { case ‘0’: case ’1’: case ’2’: case ’3’: case ’4’: case ’5’: case ’6’: case ’7’: case ’8’: case ’9’: return (ch – ‘0’);

case ‘a’: case ‘b’: case ‘c’: case ‘d’: case ‘e’: case ‘f’:

return (ch – ‘a’) + 10;

case ‘A’: case ‘B’: case ‘C’: case ‘D’: case ‘E’: case ‘F’:

Page 62: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+��

return (ch – ‘A’) + 10; default: System.out.print(“Adeus.”); } } } // Não há breaks porque cada expressão tem um return que // sai do bloco antes que se tente sequer avançar // para o próximo! �If��������Switch��

• @ �if�,�� �� ����� ���������������3 ����A ���D ����� ���������)��������?�����• @ �switch�0�6�������A �������������3 ����A �����F���)���������F����������• C ��6� �����if�D ������

o @ ��� ����������������)���,� �D �����o I��������� �����������������)�����������3 �������o -������H��,���������������������)�������:� ��x>1K����y>100�&& y>200'''�;�

• C ��6� �����switch�D ������o �����������3 ����A �����F������������������)����������� ���������")���:������K�����������;�

5.2 - Repetição

[�0����������� D ������� ������������ ������������&��� ��������� ��������� ����0��K������?� �������D �����������0�&������������,����� �����)����'����H"���������0��D �����)�������3 ��������!���)�6��K�����)����������)���!���)�6���������������������?���'�<����?���3 ��������� �����)�K�,�������)����������)����K���������� ����������������?������������� �4)�����������D ��� ���� ������ ��������� ���� ������� �� ��������� ����� ��� ���� ������� ��� ������A ��� D ��� ������"������ ���������'�-�� ������A ��� D ��� ���� ������� �����)��� ����� � �� ��� ��������� ��� ���� 0��������� �0��6� ���� ��� ������A ��� ����� �����K�����,�����H������ ��������'��C �� ����� ,� ���� ��D �&���� ��� ������A ��� �3 �������� ��� 0����� �� ���)�� �� ��F���� �� ���� �������� ��� �,����'� [���������������� ����� ��E�

• $�� �E����F��������������A ������� ����• C ��� �������� ��� ������ ���������� D ��� ��������� �� �3 ������� ��� ��� �K� �� ��0������ ��� ������� 0����� ��

D ������������)�6���D ����������)�������3 ��������:�� �������������A ��;'��I����������������������0�K������F�K��� �������������A ��������� �����0��������K��6 ���D ������������������' �����+������'������ � �� ��� �����A ��� ,� �������������� �����F")��� ��� ��� ��� ����� ���� ������� �� �������� ���� �)����� �K������D ����������K� �� ���� �3 ������� ����,�� ���� ���"� 0�� ���� ����6�"� ����������'� ����� ��,�� ����K� ��������� ������� ��� �������� ��� ��������� ��� ��� ������� �� �������� ��� �������� ����� ����� ������")��� H��)��� � ����A ����3 ����������������������D ������������?��K� ����3 �� ��'�����2 �)��:����������������������������� ��������������������4)��;K�H"���&��� ������������A �������� �����E��

• while

• do-while

• for

Page 63: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+/�

5.2.1 - Instrução de Repetição while

������������������� ����������� �������)�����������������������?���� ���������������������������:������;�����3 ������������� ���������'���� ������� ���� �� D ��� ���� ������� �� 0��3 ������� �������K� �� D ��� )����� ,� D ��� ��D ������ �� �������� �)��� �� )�����)���������:true;K������ �����������������3 �������'��

�5�����//' 5��3 ���������������������������� ������while�

��������������"�����������E��! �� ���)����������

�5�����/�' ����������"���������������������� ������while�

�� � ; ��������� ������while��� <instrução while> ::= while (<expressão>) <instrução>;

�' � ��� ���� �������������� ������H = ������// as chavetas aparecem se houver mais do que uma instrução // a executar no corpo do ciclo while(condição){ instruções; }

����O�

�������A ���

=�

5�

while expressão-lógica instrução ( )

Page 64: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+��

- ?������ �D �������"�����������������������3 ���� ���������������K�H"��������� �D ��������������0�6��E��

• -��)�����������3 ����������������������D ����������������������������������������)������?����:true����false;'�

• @ ���� ���������� ����������������������� ���������� ����'������,K� ����H�)��������������������A ������� ���'�

• ������������A ��������� ��������D �������������������)������������� ��������0���)��������'�• ���������������)��0�����)����� ��������)�����0����K�������������H����"��������3 �������'�• �����D ������������������������0���K����������������������������0�����D ������3 ��������)�������������

���������H����"���������)�����0����'�������������� ���0�6���������D �����������������A ��������� �����������������������������������)���������������������������� �� ����'�

�� � ������"���public class CicloWhile { public static void main(String args[]) { int a=30, b=15, i=3; while (i>0) { a-=10;

b-=5; i--;

System.out.println(“A é “ + a + “B é “ + b); } } } // qual é o resultado produzido?

�� � ������$���/* Programa que gera um número entre 1 e 100 é verifica se o número é primo (primo é um número maior que 2 e que é divisível somente por si próprio e por 1) */ import java.util.Random; public class NumeroPrimo { public static void main(String[] args) { int d = 2; Random random = new Random(); int x = random.nextInt(100)+1; boolean primo = (x > 1); while (primo && d <= x) primo = (x % d++ != 0); if (primo) System.out.println(x + " e primo."); else System.out.println(x + " não é primo."); } } �

Page 65: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+!�

� � ������%�� public class CicloInfinito {

public static void main(String args[]) { int a=0, i=0; do { ++a; ++i; System.out.println(“A é “ + a); } while (i > 0); } } // Este é um exemplo a não seguir!

5.2.2 - Instrução de Repetição do-while

-��������������� ������do-while ,������H���������������:while;K��������������D ����3 ����������� ������������D �������������������)�����0���)��������'��)������ ���� �������������������� �����)������������������������0����K������F�K�H"�D �����������D ��������� �����������3 ��������������������D �����������������������������)���������������� ���0�������D �������������'�- ����� ��� �� ����� ��� 0������������ ���� � ������������ �� �����K� �� do-while ��� ���� ��� ��� �������������������0������E� �������3 ����������� �������������?��� ����)������)������������������� ������'������0�6�����D ����������������������D �����������������0������E��

• �����)����� ���������������K����while �����������H��������3 ������������ �K��������������0���0������������ �����'�6 ���D �����while �� ����:������ �(�? � '�

• ���� �)����� �� �������� ��� 0�K� ���do-while �3 ������ ��� ��� �� ��� �� ���������� ���� )�6'� @ �do-while �� ����"������ �(�? � '�

��������������0��3 ������K������ _ H��� ������ �������� ��������������0����E��

�5�����/!' 5��3 ������������������������ ������do-while�

����O�

�������A ���

=�

5�

Page 66: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����++�

�! �� ���)����������

�5�����/+' ����������"���������������������� ������do-while�

�� � ; ��������� ������do-while��� <instrução do-while> ::= do <instrução> while(<expressão>);

�' � ��� ���� �������������� ������do-while��� do { // as chavetas aparecem quando o corpo do ciclo tem // mais do que uma instrução a executar! instruções; } while(condição);

�� � ������"���public class CicloDoWhile { public static void main(String args[]) { int a=0, i=0; do { ++a; ++i; System.out.println(“A é “ + a); } while (i<3); } } // qual é o resultado produzido?

�� � ������$���// Programa que gera um número entre 1 e 100 // e verifica se o número é primo. import java.util.Random; public class Primo {

do

expressão-lógica

instrução ( ) while

Page 67: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+9�

public static void main(String[] args) { boolean primo; int d = 2; Random random = new Random(); int n = random.nextInt(100)+1; do primo = (n % d++ != 0); while (primo && d < n); if (primo)

System.out.println(n + " é primo."); else

System.out.println(n + " não é primo."); } }

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

• -��������������� ������while�������� ����)��������3 �������E�o ���0���)�����������������������o �3 ���������������������������������A �������������������o -��������)������������ �)�������3 ��������?����o ������3 �����������������0�������������������

• -����������do-whileE��o �3 �����������������������A �������������������o -)�������3 �������o ���0���)�����������������������K��������������

• $��������E�o -����������do-while��3 �����������������������A ��������������)�6���o -����������while��3 �����������������������A ���6�����������)�6���

5.2.3 - Instrução de Repetição for

-� ��������� for� ,� ������ ��������� ��� �� ������ ����� � ��K� �������������� D ������ ��� ���H���� ��� �������� ��D �����������������A ���������"����:�����F�K����� ��������)�6���D ������ ���������� ��������� ���������;'�-��3 ������������� ����������0������"K������������K��� ������������)����������)������������)��")��������������'��! �� ���)����������

��

5�����/9' ����������"���������������������� ������for����

for ( expressão lógica

; expressão

instrução

; expressão )

Page 68: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+.�

� � ; ������� ������������������for���� <instrução for> ::= for(<inicialização>;<expressãoC>;<incremento>) <instrução>; <inicialização> ::= <varCont> = <identificador> | <varCont> = <valor> <varCont> ::= <identificador> <expressãoC> ::= <expressão> <operador lógico> <expressão> <incremento> ::= <identificador> <operador incremental> | <identificador> ::= <identificador> = <instrução>

�• ����6�����,����3 �������D �������6���������������������)������������)��")���������������U)��$���X�• �3 ������$��� ����������������D ���,��0��������������������'�• �����������,������������D ������������)��������)��")��������������'�

�' � ��� ���� ������������ ������������������for�� for(expressão1;expressão2;expressão3){ instruções }�@ ���������0������������,����������E�

#' ���������expressão1'���' -)����expressão2'��

o ���,�)��������E��� �3 ����������������� �3 ������ ���������� ����������� ��� expressão3� ���� �� )��")��� ��� ��������� ���

expressão1��� ������������ ��������

o ���,�0����E��� ���������������

��������������0��3 ������K����for� ���� �� ���� ��������������������0�����:������������������������ ��0�������������;E�

�5�����/.' 5��3 ������������������������ ������for�

�3 �O� �������A ���

- �����3 /����3 #�

=�

5�

Page 69: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����+>�

�� � ���

• <��������forK� ��)����� ���� ����������� D ��K� �� )��")��� ��� ��������K� ��� ������������������������H���������K��?�,����H���������������������� �'���� ������������" ��� 0�����������������������������������3 �'���������������������0������)�����������" �������������������H���������� :�� ����6����� ���K�������K�����0�����������������H�;'��

• C ���������������� ���������������������������)������K�����),����� ��������)�����K� ������ ���������3 ����A �������������H�����for'��

• <����������� ����� ��� ������ ���8 �����B�?����� ��� ����������� ��� �� ������������ �� ������ ��� �� ����������H�����forK����� ��D �������������������������� ���� ��������3 �����������������������������A ���)�6��'�

�� � ������"���public class CicloFor { public static void main(String args[]) { int a=0, b=0; for (int i=0; i<3; i++) { a+=10; b+=5; System.out.println(“A é “ + a + “B é “ + b); } } }

�� � ������$���// Programa que gera aleatoriamente um número entre 1 e 100 // e verifica se o número é primo. import java.util.Random; public class Primo { public static void main(String[] args) { Random random = new Random(); int n = random.nextInt(100)+1; for (int d = 2; d < n; d++) if (n%d == 0) { System.out.println(n + " não é primo."); return; } System.out.println(n + " é primo."); } }

Page 70: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9��

����������/��3 ����A �������������H�����0�������� �����E��

• �������������expressão2K���2 �)���������D �����������������������������������,�trueK��������� �����������������0���'��

• ���������������expressão1��������6��������)��")����������������������������'��• �������� ����� ��expressão3� ��� �� �������� ��������� �� ���������� ��� ����������� ���� ������A ���

������������� ���������'���' ���� �� ������ ������ ����� ������� ���� ������A ��� ��� ��������� ����������K� ����,�� ������� ���� ������ ����������K� ��� ��F�K� �������������������������'�-������������ �����D ���������������������,���� ������������3 �������'������D ������3 ��������� �����������A ��� ���� ��������K��������������,�����"��������H������������6���������H�'��� � ������%��� public class CiclosEncadeados { public static void main(String[] args) { final int SIZE = 5; for (int x = 1; x <= SIZE; x++) { for (int y = 1; y <= SIZE; y++) { int z = x*y; if (z < 10) System.out.print(" "); System.out.print(" " + z); } System.out.println();

} }

}

�� �����<��� ,� �������H")��� ��������������� ������ ���� ���� ������'� ��������������� �04��� ������������� ��� �?���� �� ��������������� ��������K������F�K���������������������� ��3 ���������������'��C ����,�������6�")�����������������6����������D �����&������������������'��while��������do-whileI��������for��- ?����������������0������������� ������������������� ���������������������D ������� ��������)��E�D ������������)���������������������������O��C �������forK���� �����������������D ���������� ���������� ���A ���,����H���������������4�����������3 ������'����2 �)�K���for�,�0��3 4)�������0������ ����D ��� ������������6������������A ������������ ���������� ���A �����F������,������������� ����������������?���������D ������� ���������� ���A ���������F�����H���������������'�<���������K�� ���������������� ���4)�������� ����6������?����������� ����K� �������������,��,������04��������������� ����������������H��������������������'�

Page 71: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

�I���������� ���������� ���A �������,����H������� �����K������������3 �������������������������A ����� ��������� ���������������������?���K���)��������������6�����while'���)����� ����� ��� �����do-while� ��� �����A ��� ��� D ��� �� ������ ��� ������A ��� � �� �� ���K� ���� ��,�� ��� �������� ������������)�����������������������?���K���)�������3 �������� ��������������)�6'�-�"�K�,�������� �����D ������������0����������������do-while������������������ ������������A �������� �����'�

5.3 - Exercícios Práticos

$���������,������ ��������� ������� ���D ����������E�#' $��H���������8 �����������3 �������0��������������A ����������������' - ����������������������A ��������������������������������6�������������)�������0������

�3 ���+�C�J �� > � ��� ��� ���#' I���� ������������A ������������������H���O��' <������� ���K� ����D ������)������������A ��������������O�/' I����,�����F���)�������������� 0O�������_ ��HO�I��������0�������������������������������A ��������������O�

���D ���� ����������A ��������)���������������������O�2 ���0D ������������ ������������ ����'��' -�����6�����0�����������6������ ���������������������������O��������0������������D �����D ��������A ������

� ����������6������������'�!' [� ���4)�������������A ���0��������������� ��������'�=������������0����O�+' �����D ������)�������6���������������������J �����?�����������_ ��HO�9' @ �D ����� ����������� ���������0����O�.' @ �D ���,��������O�I����,������)�����D ������������������6��������������A �������� �����O�>' I���� ���� ��� ������A ��� ��� �� ������ D ��� ���H���O�I���� ��� �0�������� ������ ����O�I������ ��H�� D ��� ��)��

���6�����������O�2 ���0D �������� ����������� ����'�#�' [� ���4)������0��������������������O�I����������������A ���D ������������������� ������������O�##' @ �D ��������������0����O��3 ���++�C�' ��������� ���' ���� ���3 ���'�#E������������3 ���4���������������D �������� ���D ����� ������������������K�����������������������������������6������������������)��")��E�

�final int MAX = 25, LIMIT = 100; int num1 = 12, num2 = 25, num3 = 87;

���*�����

if (num3 >= LIMIT) System.out.println ("maçã"); System.out.println ("laranja");

System.out.println ("pera"); ��*���� �

if (num2 == MAX) {

System.out.println ("maçã"); System.out.println ("laranja");

} System.out.println ("pera");

��*���' ����if (LIMIT+num3 <= 150) {

Page 72: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9��

System.out.println ("maçã"); System.out.println ("laranja");

} else

System.out.println ("pera"); ��*���! �

if (num1 < MAX) if (LIMIT >= num2)

System.out.println ("maçã"); System.out.println ("laranja");

��*���� ����if (LIMIT%num1 + 4 == num1 + (MAX-num2)) {

System.out.println ("maçã"); System.out.println ("laranja");

} else {

System.out.println ("pera"); System.out.println ("banana");

} ��*���; �

���if (num2 > 18) if (num1 < 0)

System.out.println ("maçã"); else

System.out.println ("laranja"); System.out.println ("pera");

��*���K ����if (num3 >= MAX) {

if (MAX/num2 == 1) System.out.println ("maçã"); System.out.println ("laranja");

if (LIMIT-num3 > num1+2) System.out.println ("pera");

else {

System.out.println ("banana"); System.out.println ("kiwi");

} } else

if (num2*2 == MAX*2) System.out.println ("uva");

��*���L ����if (LIMIT >= 4*num2)

if (MAX == 25) System.out.println ("maçã");

else System.out.println ("laranja");

else System.out.println ("pera");'��

��

Page 73: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9/�

�3 ���'��E����������3 ���4������������������)����������������?����D �����)��� �����3 �������������A ����������E����*������� ����L] ����PM������)��")�������0����)�4)��� ������������*���� ��� ����L<���,�6���MK�L<���,� ���)�MK�L<���,������)�MK�������������)�����������'����*���' �-��������)��������� �D ���������������������)��")���������������6����E�

#' �0 ������' @ �����������������

���*���! ��� ���� L�����M� ��� ���� )������� ��� � �� float� ���������� ��� )��#� �� )���� 0����� �3 ���������� ����� ��L�������������� ����M� ��� ������ ���� )������� ���)����� �� ���� ���8 ���� �0����� ��� ����� �� �'���#� ��� ��� �����'��� ����L0������M��������0��������������������������H�����'E���3 ���'� /E� <��� ��������� �3 ���4���� ������ �� ��� ��� D ��� �"� ���� ����6��� ������ ��� ������ �� )����� ��� ����� �������������)��")��E�

final int MIN = 10, MAX = 20; int num = 15;

��*��������while (num < MAX) {

System.out.println (num); num = num + 1;

} ��*���� �

����do {

num = num + 1; System.out.println (num);

} while (num <= MAX);

��*���' ����for (int count1=1; count1 <= 5; count1++) {

for (int count2=1; count2 <= 5; count2++) System.out.print (count1*count2 + " ");

System.out.println(); }

��*���! ����while (num > MIN) {

System.out.println (num); num = num - 1;

} ��*���� �

���while (num < MAX) {

if (num%2 == 0) System.out.println (num); num++;

}

Page 74: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9��

��*���; ����for (int value=7; value < 0; value--)

System.out.println (value); ��*���K �

���do {

num = num + 1; if (num*2 > MAX+num)

System.out.println (num); } ����while (num <= MAX);

��*���L ����for (int value=num; value <= MAX; value++)

if (value%4 != 0) System.out.println(value);

�3 ���'��E����������3 ���4������������������)����������������?����D ��� ��������3 �������������A �������������E��

��*�����=��0������������6�����������6�����)����� ��K����6���������� _ H��'����*���� �=��0������������6�����������6�����)����� ���)�K����6��������_ H��'����*���' ��� ��������� ������ ���������������#��'����*���! ��� ��������� �������� �� �������/����/�����,�/'���3 ���'�!E�I��������������A ��������������������������D �)�������O��

A if (condição1) if (condição2) instrução

C If (condição1 || condição2) instrução

B

If (condição1) Instrução1 else if (condição2) instrução1

D If (condição1 && condição2) instrução

�����������G���D ������������������3 ���'�+E�1 �������� �������������������� �����,� ������4� ��'���3 ���'�9E�1 ����������������������������������������� �����������4���'���3 ���'�.E�1 �����&���� �������������������������D ����,�����'���3 ���'�>E������)����� �������� ������������������������������� �������6����'���3 ���'�#�E������)����� �������� ��������������&����������������� �������6����'���3 ���'�##E������)����� �������� ������������D ��������������������� �������6����'���

Page 75: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9!�

�3 ���'�#�E�5������� �������K�D �������������������������� ��������0����������6����������������,��)�4)��� ���!'���3 ���'�#/E������)����� �������K����2 �)�K�D ����������� ����������������0����������6���������� ������,��)�4)��� �����������'���3 ���'�#�E������)����� �����������2 �)�� ����)��0������������ �����������,��)�4)��� ����K�/K�!K�+K�#�K�#!����/�'���3 ���'�#!E������)����� �����������2 �)��D �������������� ������������������������ ���������������� �?3 ��'���3 ���'� #+E� �����)�� ��� �������K� D ��� �����H�� �������������� ��� �� ����� �� �0����� ��� ���6����� ��� �� �� ����� ,������)�'���3 ���'�#9E������)����� �������K�CalcIRS�D ��������������������������������H���������)��)����� ������� �����������������������������E��

Ordenado Taxa <100.000 5% >= 100 000 e < 300 000 15% > 300 000 25%

Tabela 2 ��3 ���'�#.E�@ ����)�����?�������0�����������������D ��K�������3 ����K�����������3 �������E��

��������

��

���� ����

� ������

� ������

� �� ����

��� ����

� ������

� �� ����

� ������

��

��3 ���'� #>E� �����)�� ��� �������� D ��� � �������� ���� ����� ��� !� �)���� �0�������� ��� ���6����K� ����� �H�� D ���������������������'���������)��������������K��0����������6�������� ���������)��'����3 ���'���E�$������ �������K� D ��� ������������ ��������������������������K� ������� ��� � ������ �������������� ������������"���������������K��� ������������ ������������H���:TK� K�SKB;'���3 ���'��#E������)����� ��������D ������������&��)�����������K�D ��������� ����������&����������������8 ��������D ��������0D �������8 ������������D ��"����K��?������������������'���3 ���'���E������)����� ��������D ���������������6��������#��D ������D ����"���'�@ ����� ����������)�������� ����������������D ��������0���������������D ����"����,�6�������������������46������������ ��3 ��'���3 ���'��/E�5������� ��������D ����&�������D �&��������� ������������� ���)�����������������������'�-�D �������������� ������,�������H������� �����'�-���D �&����������������?��������D ������0���������6�������� ����������)�'����3 ���'���E������)����� �������K����2 �)�K�D ������������F���'�@ �F����������������������E�@ � �����������,������� ����������?�����������������D �����������6��������������������� ��������D ������K���F�����������'���

Page 76: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9+�

�3 ���'��!E�$������ ��������D ����������������������� ��������� ������ ���)��K���������)����������0�������� �������6����'���3 ���'��+E�5������� �������K�D ��������������,������!��� �����������?���'���3 ���'��9E�$������ �������K�D ���0��������������� �������!���� ������������'���3 ���'��.E������)����� ��������D ���� ��������������� ������������ ������4� ����'���3 ���'��>E������)�K����6�������������L0��MK�������F��������������A ����D �)��������E� while (i<100) System.out.println(i,“ao quadrado é igual a ”,Math.sqr(i)); ��3 ���'�/�E������)����� ��������D ������������� ����������������?�������������#�����)��0D ��������������,�������� ���'� * ������ ��� D �������� �����,� ������� 0����������� D ������� D ��� ��F�������������)�4)��� ���#��� ������� �? ��'���3 ���'�/#E������)����� �������K����2 �)�K�D ������ ��)����0?���������0�������������E�

6)12()1(

1

2 ++=�=

nnni

n

i ��������� ��)�����������������������������������K����������� ����������?��� ���������������#��'���3 ���'�/�E�@ ����)�����?�������0�������������������D ������������������������E��public class Exercicio { public static void main(String[ ] args){ int count =0; for (int i=0; i<3; i++) resume: for (int j=0; j<4; j++) for (int k=0; k<5; i++) { ++count; if (i==1 && j==2 && k==3) break resume; } System.ou.println(“\tcount= “ + count); } }

��3 ���'�//E� @ ����)�����?������� 0������������K��������� �������������������������������0���A ��'�<���������K�����D ����������������� ���������E��public class Exercicio { public static void main(String[ ] args) { int count =0; for (int i=0; i<3; i++){ resume: for (int j=0; j<4; j++) for (int k=0; k<5; i++) { ++count; if (I==1 && j==2 && k==3) break resume; }

Page 77: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����99�

System.ou.println(“\tcount= “ + count); } } }

��3 ���'�/�E�@ ����)�����?�������0�����������������D ��K�������3 ����K�����������3 �������E��public class Exercicio { public static void main(String [ ] args){ System.out.print(0); int fib0 = 0; int fib1 = 1; int fib2 = 1; while (fib2<1000); { fib0=fib1; fib1=fib2; fib2=fib0 + fib1; System.out.print(“ , “ + fib1); } } ��3 ���'�/!E������)����� �����������2 �)�K�D ���� �������,�������������������#���#�'���3 ���'�/+E�C ��������)����������������������:?�����K�5������K�1 ����K����';�����)������������������W ����)�����������������������#�����'������)����� �������� ����������D ������)������� ����� �������������)�������������'�@ � �����������������D ��������� ���������������D ���'�-����3 �������8 �����3 �������� �����������������������'����3 ���'�/9E��� ��H��D ��������������������������������������E�������� � #'!�V�% �������5����� �'9!V������ � �'!!V�% ���� � �'.!V����*���������������� �������������������������������������������D �����0����������������'�����*���� �<���,� ���4)�������0�6��� ���������)������0��������'!V'����*���' �@ �� ���������)������� ��������!V��&�������������������W '���3 ���'� /.E� C ��� ������ �� ����� 0������ �������� ���� ��&����� ��� ��������� ������ #� �� >>�)'� -�� ����������0�������� ������ �����������������K� ��� ������������ ������ ��&�� �����������K� ��� ������� ������ ��&���� ��������'������������ ���4 ��D ����� �����������"�����������2 �)�������D �����)��")��������������� ���������� ��&������������� �� ���� 0�������K� �����)�� ���� ��������� 0� D ��� �3 ������ �� ����������� ������ �������� ��� ������� ���� ����������������E��

���&������������� -��������3 �����������U�#� (����������������#�UR������U�!� �^ #�

Page 78: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9.�

!�UR�������U���� �^ ����UR�����U�#��� �^ /�����XR�#��� (����������������

�-�������A �������������A ���0�������)����������F���A ��'���3 ���'�/>E������)����� �����������2 �)��D ������)�����������D �������)����������������������D ������)�������������3 �������K����6����������������������0c���������������D ���)��0D ������������ �������������������������������������D ������)��'��$��������D ��E��� �� E�(���! >� E�(��4�����#� #/� E���0������#� #9� E�% ���#. ��� E�(����% �����3 ���'���E������)����� ����������� 2 �)�� ���������������� F�����������)�������������������������"��K� �����������������D ��������������0����0�������#'�����������������������F������� ����������3 �����������:�����;'�$���������������F����������������)��,����������'�@ � ����������)��"��������������� ���������� ����������������'��)��"������������� �������K� �� �� ����� ��� ������ ��� 0�� ��� D ���� D �������� ��������� �� �����K� �� �� ��3 �� ��� F����� �����'� ������ ��� ���4 �� ��� D ��� ���� H"� ��)������� ��� ������ �� ��� D ��� ��� F����� �?� ���� ����������� ��� 0�� ��� �� ����� ��� �������� ��0����'�

Page 79: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����9>�

6 - Sub-Programas

� �������K� ��� �������� ������ ����� �� � ����������� ��� ���� �������� ���� �������������� �������'�(�����)�6��K���� ����������������)������������������� ��3 ��K� ����D �������������������������� ��� ������� ������������� �D �����K� ���0�������0������������������������������'���������� ��������K� ������,��������� �D �����K��&������ ������������ �� ���'� -� ����� ������,��� ��� ���������� ��� ��������� �H��� ��� L�)��� ���� ���D ���MK� ��� ��F�K��)��� �������D �����E������)��������� ��������������6 ��������������������'������� ����� ��� ���������3 ���� ���� � ����������� ���4)��� ���� �� 0����� ��� �������K� ��4� �� ���������� ��� ��� �������E�

• $����������A ������ ��������D ����3 �����������0����� ��40���K�����������F��������������A �������� ��������������0����

• C ��� )�6� ������H����� ��� ���F����K� ������ ����A ��� : ��� )�6��� ����,�� �H������� ��� ������;K� ���������� ������������������� ����������������������� ��3 �'�

�0 �� �� ���& ���? �������)�#B3 ��� �� ��C ��������� ���������������������� ��3 �����������������������������������������F������'�$������4��������� �����������6������������������ ��������� ������?����D ��������)�����������0�����"��� �����'������0��� �������0������������������������"�������������������� ����6���� ������������0�'��)�� ������3 ��&��������?���������������:'�'�D �������0�6�0����K��� ����;K� ��������������?����������������0����0�������� ��������� � ��)��" ������ ����������������� �������'��' = ���������)�#B3 ��� ����C �� ��� �������� ,� ����0����� ��� ��� ����� D ��� �H�� ,� �����4��� ���� ����������'� [� �� ����� ����� D ��� ��� ����������������0����D ������ �����������6�������� �����������D ������'�% ����������� ������������������������� ������������������ ��&���������)��������������0��H���:;K� ����D ��������F��������������A ���D �������� A �����F����3 ��������'�=������������������D ����������� �������������������������������������� ���������3 ��������,��������������� ��&������D �����������������������:,���D �������H������� ����������� ��8 ������;'��� � ������"��� resultado = Math.pow(2,4);

�$����������������������������H���������� ��������powK��3 �����������������Math�������������2 �)�'��������� �����������)�� ��������������� ��&������������ �����3 ����)�������������� �����Z '�<����������������"������)����������� :����������� ��8 ����������)��������� :3 ;� ���)������� :Z ;� ;K� D ��� �����������#+'�[��������)��������� D �����)��")��������������"�0����� ?�����H������������ �������'��� � ������$��� numero = Math.random();

�<����� ����� �H��"���� �� ��� �������� ������K� ����,�� ����������� �� ������� (��H'� ����� ��� �������� ����� ����� ����������?����K������ �������)��0���K����� ������������H��� ��8 ����������������� �����0�����������"���������D ������ �� A �'��

Page 80: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.��

' ���������)�#B3 ��� �� ��C ����� ��������,��������4��� ��E��

• $������H�E� ���F����� ��� ���)���� �����)����� :D ��� )������� ���� ������� D ���� ���� �� ���� D ��� ���)��;K������������ �������K��������� ��8 �������:D ��� ��������)�6�;������� ��&���������)��'�

• �������A ��E����F��������������A ���������"���������� �������������F���)��������� �������'���)���������������8 ����������� �������� ����D ��� ����������3 ��������������� ��'��� ���[�� ��������D ��������������� ����������F�������������������"���������)������ �� ?����D �����������)�'��)��� ����,�� ��� ��� ������ ������)��� ���� ��� ��������K� D ��� ��D ���� ��� 0����� ������ ���� D ��� ,� D ��� ���������)��K����������������������)��")��'��� � ������%���public class Programa { public static void main(String[] args) { System.out.println("1ª instrução executada pelo main"); imprimeMensagem() // chamada do sub-programa imprimeMensagem System.out.println(“3ª instrução executada pelo main"); } // imprimeMensagem : sub-programa que escreve a mensagem // “Olá Mundo!” no écran public static void imprimeMensagem() { System.out.println("Olá Mundo!"); } }

�C ����� �������� ����:������;�����D ��� ����6������������������ �������3 ������:�����F�K� ����D �����H�� ����D ����3 ���������������0����D ������"���������;'�$������������H���������D ��� ����6������������������K������� �������� �������������0����������������0����E��

• ������������• 5������

�@ �� ��������������0���A ������2 �)���H���� �������� '�] "��,������D ���� ���������)���������������������������D ���������0�6��'�

6.1 - Procedimentos

C �� �����������,������� ��������D �������,��������F��������������A ������3 ����������0��������"�������?���� ������� ����������������������0�'������ D ��� �� ����0�� ��F�� �3 �������K� �� ����������� ���� ��� ���� ���� ��� �������� ������ ��� �������'� �3 ������ ���������A ���D �������� A ���������������,���������0�'�@ ���������������������������������0��,���� ������K�����������0��H�K��� �����������0��H����������0������������ ��'�[�����D ������0���� ���������?���B���"��'�

Cabeçalho do sub-programa

Instruções a executar

Page 81: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

C �� �����������������������)������K��� D ������2 �)��,� ����0����� ���� ���)���void�������������������,�����:��� �������;'� @ � �,����� � ���(�������� ��� �3 �� ��� /� ����� ������������ ,� ��� �3 �� ��� �� ���� ��� ����������'�

6.2 - Funções

C ��� 0������ ,� ��� ��� �������� �&����� �� ��� ����������K� ���� �� �0������� ��� D ��� � �������� ��� )����� �����������������������3 ������'�[�����,�����D �������)����������������:����,�voidK���������"������ ����������;'�-�0������������0������� ����������������������,���������������������return'����+� ������2 ����������� ���������������� �����3 ������������� ���������������������0��3 ������3 ���������� ��������,���)��)������0�����������������D �������� ��������0���H�����'�� return <expressão>; �@ �)���������3 �������,���)��������������������,����'����� ������������ ������������ :�,������void;K� �� ���������return� ���� � ������� ��� 0����� �3 �4���� :����<expressão>)K� � ����� ���� ������� ������3 �����������,����� :� ?��)��0�������������������������K� ����3 �� ��;'��� � ������"���public class ExemploFuncao { public static void main(String[] args) { double varA; varA = 10.5; System.out.println(“2ª instrução executada pelo método main"); System.out.println(varA); varA = funcao(); // invocacao do método funcao System.out.println(varA); System.out.println("5ª instrução executada pelo método main"); } // funcao: soma 2 numeros aleatorios e retorna o resultado da soma static double funcao() { double valor1, valor2, resultado; valor1 = Math.random(); valor2 = Math.random(); resultado = valor1 + valor2; return resultado; } }

Page 82: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.��

�<������3 �� ��K��� ��������������� ������������)�����10.5���)��")���varA����� ��double'����������K�� ��������������������,����K�����������)��������)��")��'�-��0�6������varA = funcao();������������H��������,�����D ��������������funcao'�<������������K����������������3 ������� ����� ������������ �������'�����0������������������������3 �����������������0�K�D ��������������,������������� �����������?������� ������������0�����������������������'������� ��� �,����� 0������ �� D ��� ��������� ,� �� �������E� ,� ������� ��� �� ����� �����?��� ��� ���� ��� �H������ ����,�����random�����������Math���,����������������)���������������)��")���valor1'���������K�,����������)���� ����� �����?��� ��� ������ 0����� �� �� ���� )����� ,� ��������� ������� ��� )��")��� valor2'� ���� 0�K� ������ ��� ������������������������������)��")���resultado'������ �������K� �� 0������ ���� ��� ��������� �� ���������� ��� �"������ �0�������K� ��)��)����� �� ��������� ��� �������� ��� ��'� ����� ���� �������� �� ��������� return'� -�� 0�6��� return resultado;� �� 0������ ������K�� ����������� �� )����� ��� )��")��� ���������� �� D ���� �� �H����K� D ��� ������ ����� 0�� �� 0������ main� :����),�� ���varA=funcao()� ;'� -� ���������� ��� ������ ������ ������ ,� D ��� �� ���������� ��� �"������ �0�������� ���� �,�����funcao�0�������6����������������)��")���varA'�@ � �������� ��������������������������������������A ���D ����������3 ������K�D ������������� �������������)�������)��)��������)��")���varA����,������������ ��������������:L!b�����������3 �������� �����,��������M;'�

6.3 - Parâmetros

2 "�0������������D ��K� ���)�6��K� ���� ������������6������������������0�K������� �������� ��������������������������:����������D ���������0������ �� ������������� ����������6�H���'''� �����������������������P�5���H�K��)��K���� ���K����'�;'�-�������������������������H������� ��8 ������'�����)��")���D �����0���������0��������� ����������������D ��������� ����������������� ������)��������������������0�'�������� ��������������0�����������������0����E��

• 3 �1 ���� �; ���� ��-D ������ D ����������������������������H�������� ��������:����������� ��&���������)��''';���D ��� ������������6����� �������������������������A ���D ������� �����3 ������'�

• 3 �1 ���� � ���� �� ����,�� �H������� ��� ��8 ������� ���������K� ���� ��� )������� D ��� ��� ��8 �������0��������D ����������������3 ������������� �������'�

�� � ������"���public class ProcedimentoComParametros { public static void main(String[] args) { int pcA = 10, pcB = 20; System.out.println(“2ª instrução executada pelo método main"); procComParams(pcA, pcB); // invocação do método procComParams System.out.println(“4ª instrução executada pelo método main"); } // procComParams: recebe dois inteiros e imprime no écran a sua soma // imprime também a soma de duas variáveis // locais (internas) ao procedimento public static void procComParams( int pfA, int pfB) // PROCEDIMENTO { int varLocalC = 11, varLocalD = 12;

Page 83: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����./�

System.out.println (“2ª Instrução executada pelo metodoVoid_2"); System.out.println (pfA+pfB); System.out.println (varLocalC+varLocalD); System.out.println (“5ª Instrução executada pelo metodoVoid_2"); } }

�<����� �3 �� ��K� )����� �� ����� ��� ��8 ������� 0������ ���������� ��� �������H�� ��� �,����K� ����0����� ����� �����������pfA���pfB'�<�� �������� ��� ��� )��0������ D ��� �� )��")���pcA� ������� ��� ���� �� )�����10� ��pcB� �� )�����20'� � ��� ���� ���� ������������K� ,� �)������ ���,�����procComParams'�<����� ������� ���� ������ ����� ��8 ������ ����,��������)��")���pcA���pcB'�������������� ��8 ��������������������������K� ��D ����� ������������)��������������D �������,�����)��������H��������� ������3 ������� ������)��������������������0�'�@ ��)����������pcA���pcB������� ����K���� ���)������K� ���������������)��")���pfA���pfBK� ������K�D �����������������,�����0�6�����System.out.println (pfA+pfB); ���30K���������������� ����'��� � ������$���public class FuncaoComParams { public static void main(String[] args) { int pcA = 1, pcB = 20, varC = 30; System.out.println(“2ª instrução executada pelo método main"); System.out.println(varC); varC = funComParams(pcA, pcB); // invocação do método funComParams System.out.println(varC); System.out.println(“6ª instrução executada pelo método main"); } public static int funComParams(int pfA, int pfB) // FUNÇÃO { return (pfA+pfB); } }

�������3 �� ���,�����������H���������������K��3 �� ������ 0�������� D ��������� ����������0�����K���F��� ��8 �������0����������pfA���pfB'� @ �� ��8 ������������������pcA���pcBK����� 0������� ���������������������������������������� ��� )��")��� varC� �� ����� ���� ��8 ������� ������� :��� ��F�K� ��� )������� ��� pfA� �� pfB� ��� ��� �� ����3 ������;'����������������H"���������D ������ ��8 �������0���������)��� ����������D ������������ �����������������������D ������ ��� �������� ��������� ���� ����� ��� ��� ���� ��� ����� ��F���)��K� ��� ����� D ��� ��� ��8 ������� ��������� ���������)����������������������������3 ������������� �������'�] "�������������������������� ��8 ������� 0�������� ��8 �������������K�������������� D ���������� ��8 ������ 0������������ �������� ��8 �����������:� ��������H�)����������������D ���,� ���4)����� ��0���� ��8 �������� �����'���������,�����������2 �)�';'�-���������K����� �������� ��8 �������0�������� ��8 �������������� ���������� ������������������� ����������� ������������������,���&����������� �����'� @ ����F�K����������������,������� �������int� ������� ��� ���charK� ,� ����������� ��� ����� ���"����� ��� �� ����������� ��� �H����� ����� �,����� ��)���

Page 84: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.��

���������char����� ������int���������� ������)����������������� ��D ���D ���'�@ �� ��8 ��������&����������� ������������ ��0��������&������������������� �'�

6.4 - Passagem de Parâmetros

������� ��8 ��������������� �����������0����H������������� ������������� �H������������������������D ������D ���������������'���3 ���������� ������ ����������� ��8 ������E�

• 3 ��� 0 ����� �� ��8 ������ 0������ ������� ���� �? �� ��� )����� ��� ��8 ������ ������K� ������ D ��� ������������� ����������������������)���������? ����������������'�

• 3 ��� 2 ����@ ����� �� ��8 ������ 0������ ������� ���� ��0��&���� ���� �� )����� ��� ��8 ������ ������'� 1 ���K������������� ��������� ������ �� )����� � ������� ��� ����� ��0��&���K� �� D ��� ���� ����� �����D �&���� �����0���������)�������� ��8 ������������'�

�� ��/ (K��� ����������� ��8 �������,���� ���0��������(���'��� � ������"�M� � ��������1 ���� �����(���N���public class ProcComParams // pegando num dos exemplos anteriores... { public static void main(String[] args) { int pcA = 10, pcB = 20; // valores iniciais de pcA e pcB // vão ser usados como parâmetros actuais // para o método procComParams procComParams(pcA, pcB); // invocacao do método procComParams // vamos ver o que se passa dentro do // método... // depois de procComParams ter terminado e ter modificado // o valor dos seus parâmetros...

System.out.println(“pcA = ” + pcA); // pcA manteve o valor 10! System.out.println(“pcB = ” + pcB); // pcB manteve o valor 20! } public static void procComParams(int pfA, int pfB) // PROCEDIMENTO { // pfA recebe uma cópia do valor de pcA // pfB recebe uma cópia do valor de pcB pfA = pfA + 2; // mudamos o valor de pfA pfB = pfB * 3; // mudamos o valor de pfB // imprimimos os dois valores System.out.println (“pfA = “ + pfA); System.out.println (“pfB = “ + pfB); } }

�=�F�������������� �����������������?�������������'''��

Page 85: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.!�

� � � � � � � �

AAAAAA AAAAAB AAAAAC AAAAAD AAAAAE AAAAAF AAAAB0 AAAAB1

� � � �

AAAAAA AAAAAB AAAAAC AAAAAD AAAAAE AAAAAF AAAAB0 AAAAB1

// Antes da invocação de procComParams!

// Invocação de procComParams!

pcA pcB pfA pfB

pcA pcB pfA pfB

pfA e pfB são CÓPIAS de pcA e pcB!

pfA e pfB são CÓPIAS de pcA e pcB!

� ��� � � ��� �

pfA e pfB mudaram de valor mas pcA e pcB Não!

pfA e pfB mudaram de valor mas pcA e pcB Não!

�5�����/>' �������������������������� ����������� ��8 ������� ���)�����

�� ���������������2 �)��D ���������F��� ���)���������F������:����D ����0�������������������;'�-��)��")���D ����� ��������� ��F������ ���� ��� ��������� ��0��&����� :� ���������;� ���� 6����� ��� ���?��� ����� ������ ����������6������'�-�� ����������� ��8 ������ ���������� ������������)��")���D ����� ������������������� ��K����������� ���������� �? �� ��� ��0��&���� ���� �������'� ����� ���0��� D ��K� ��� ������� ��� ��� �������� 06������ ������� � ������������������)��")��K�����������������������������������F�������D �����H���������� ��������)&���������������'������ ��)���������� �����������&����������� �������� �����0��&���'�������� �?������������ ��������)����� D ��� ������F�������� ����������� �����K� )�F���������������������������������������0��&���E��� � ������$�M� � ��������1 ���� ����������@ ���N���public class ProcComParams // com um exemplo semelhante… { public static void main(String[] args) { // Criamos uma variável chamada exemplo, do tipo StringBuffer // StringBuffer é um tipo que representa cadeias de caracteres // (um dos tais objectos..) // exemplo é uma referência para a zona de memória onde o // objecto StringBuffer é criado e guardado StringBuffer exemplo = new StringBuffer(“Teste”); // damos essa referência como parâmetro ao método e vamos ver o // que se passa lá dentro... procComParams(exemplo); // invocacao do método procComParams System.out.println (exemplo); } public static void procComParams(StringBuffer s) // PROCEDIMENTO { // s é do tipo StringBuffer e tem uma cópia do valor // da variável exemplo. Agora apontam ambas para a mesma

Page 86: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.+�

// zona de memória. // ao modificarmos o valor do objecto apontado por s // apagando o seu conteúdo (delete) // e colocando conteúdo novo (append) // estamos a fazer com que essa alteração seja vista no // programa principal através da variável exemplo // dado que ambas estão associadas à mesma zona de memória! s.delete(0, s.length()); s.append(“Isto FOI um teste à passagem por referência!”); } }

�����������)�����)�����)��0������D ������������E��

Teste à

passagem

por

referência

Teste à

passagem

por

referência

AAAAAA AAAAAB AAAAAC AAAAAD AAAAAE AAAAAF AAAAB0 AAAAB1

// Antes da invocação de procComParams!

// Invocação de procComParams!

exemplo s

AAAAAA������Teste à

passagem

por

referência

AAAAAA������Teste à

passagem

por

referência

AAAAAA AAAAAB AAAAAC AAAAAD AAAAAE AAAAAF AAAAB0 AAAAB1

exemplo sAAAA������Isto FOI um

teste à

passagem

por

referência

AAAA������Isto FOI um teste à

passagem

por

referência

s é uma referênciapara o objecto apontado por exemplo

s é uma referênciapara o objecto apontado por exemplo

Logo, quando s muda de valor, exemplo também sofre as consequências!

Logo, quando s muda de valor, exemplo também sofre as consequências!

�5�������' �������������������������� ����������� ��8 ������� �����0��&����

6.5 - Visibilidade das Variáveis

-��������0�����������)�����������)��")���)��������������������,�D ������ �����������"������6���'�* ����������K���F�����2 �)����F�������������������K������ ��������0���"����,��������4��� ������ �������� ��� ���D ������6���)��������� ���������D ������F����������� ����������������F���)�'�:-�������������������)��� �������D �����K�������� ��O;����2 �)�K������ �������,��������4��� ���������������������'�C ���:�� � ���;��������������������,������,������H������main�D ���,��� �������� ��� ��'���������6����������� ���������D ��� ������3 ������������������������������'���3 ����� ��&�� ������� � ��������� ��� ������������� ��� ��� �������� :�)��� 5$-� G� 5����������� ��� ������������ ���2 �)��;E��

• C �� ������������,�E�o �������A ������)��")��N�o C ����� �������� ��� ����H������main()N�

Page 87: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.9�

o C �����F����������� �����������0����� ���� ����������'�• @ ����� �������������&�E�

o �������A ������)��")��N�o �������A ���������������:��������K���������K��� �����K����';N�o $H������������������� ���������: �, ��0��������������� ���� ����������;'�

�@ �������H������� �������������������� �����������������)�����K�������D ����������� ����D ���,�D ����������)�K�D ��� ��8 ����������������D ���� �����������������)�� ����6��:,���������0�����������3 �������P;'��[������� ���F�������� �����������0��A ��� D ��� �������� �������� ��� )���������������)��")��'� =���������������)��")���,������������������������������������)��")���,�������������� �������'���������������������0�������)��")��������������0����E�

• 0 ���(�� ����� �• 0 ���(�� �� ��#� �

�C ���)��")����������������������������� ��������,��H���������)��")������������������� �������'�C ���)��")���������,���������� ���D �������� ��������,����)������������4���D �������������������������3 ������'�-���� D ���H�F����������� �������� D ���������H������)��")�����������������������������K����������� 0��������)��")����0�������K�D ����3 ���������� �����������?����� ������K� ��� ��4����������� ���0����������)����������0�������K� ����3 ����������� ����������������'��C ���)��")����������,��D �����D ���,���������������4������ ��������������������,�D ����� ��������������K�������)�4)��� ������� ��� D ���D ���� ��� �������� D ��� �� �������'� ����� ��,�� ��� ������ )�4)��� ������� ��� D ���D ���� ��� ��������D �������������� ������K����)��")����������� ���������,���������6����� ������������ ��������'�<���,�������������������H��������������)��������� �����)��")�����������������A ���D ���������������������������?��������0������������D ��� ��)�������� ��������������������04���������������'������ ��,�� ����� ���� ���������� ��� ����� ���� �� �����6����� ��� �?���� ��� ������� �� �?���� �����)��)����� ����������������3 ���������6����'��� � ������"���class VisibilidadeVariaveis { private int valor1; // variável global public static int valor2 = 10; public static void mudaValor2(int v2) { // novoValor é uma variável local ao método int novoValor = v2 + valor2; valor2 = novoValor; } ... public static void main(String args[]) { // Resultado = 43! System.out.println(“Valor2 antes da chamada = ” + valor2); mudaValor2(33); System.out.println(“Valor2 depois da chamada = ” + valor2); } }

Page 88: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����..�

6.6 - Exercícios Práticos

<#8���(� ���)�������$���������,������ ��������� ������� ���D ����������E�

#' ���������������� �����������A ������ �������������0����������),����������������,�������' ����������8 ���������)�����������,������/' $�� ���������������������� ��8 ������0����������������' ������������0���������������� ����������� ��8 ������� ���)������� �����0��&����!' $�� ����������0�������������)��")������������������

�3 ���+�C�J �� > � �� �� ��� ��

#' I�����������0����� ���D ��� ����������6������������,����O��' I�������0���������������� �����������������0�����O�/' <�� 2 �)�� �3 ����� ��� ��������� ��� ����������� �� 0������ ��� 0����� �3 �4���O� ��� ���� �3 ����K� ����� ����

�� ����������O�&��3 �� ���'��' �����D ������)�������������������O�!' @ �D ������� ��8 �������0�������� ��8 �������������O�2 ���0D ������&��3 �� ���'�+' C ���)��")���������,�����)��")���D ���,����H����������"��� ��4)��� �����������6���� ����������� �������

�� ��������������F������D �������� A ��K�)������������0����O�2 ���0D �������� �����D ������'�9' @ �D ����������� ��������������������,����O�.' I���� ������ ����������� ��8 ����������H���O�I��������0����������������������O�>' ���2 �)���3 �������������� � ������ ����������� ��8 ������� D ��� �����O� 2 ���0D ������&��3 �� ���� ����

����������������������'��3 ���++�C�������������' ������ �& ����3 �, ���� �3 ��� �� ���3 ���'�#E�C ��6��������,�������� �������� ���)����K�������� ������������������6�����������6������`�������?��������������3 4������������ ��������������������������������������������������'���3 �����E������)����� ��������D ������6�������� �������:�,����;� ���������������)���������������)��")��'�<����D ������)���������)������������������������6����K���� �������� ��� ��'�������)��")�����)������������� �������'����3 ���'�/E�$�������3 4������������ �������:�,����;��������������������� ������)��")��K�0������� �����������2 �)��D ������������&�����������0�����������'����3 ���'� �E� ������ �0������� �� ������ �3 ���4��� ���� ������ ���� ������K� ���� ,K� � ��������� �� �,����� ��� ������ �� ��������������0������������������������&���� ��������������������������������'�&� �� ���� � ���� D ������ ��� ���������� �� �����'� [� �� ����� ���� �����H����� ��� ������ ���� ��� ������O� 2 ���0D ��� ��D ���,�D ���������������'����

Page 89: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����.>�

7 - Classes e Objectos

@ �� ������������2 �)�����������)��)����������������)����,�����������������������F"��3 ��������������������:������-���2 �)�� �- �����������������������0����2 �)�;'�-�-���2 �)���0�����������,�����������������,������ �����3 ������� ������ �"������� �����"����K� ��� ����A ��� ��� ������� ��� ����������K� ��� ����A ��� ��� ����������K�� ����A ��� ��� � ��B��� ��K� )��0������ ��� ������ �� ������ ������� � ����A ��� � ���'� @ 0������ ��� ����� �� D ��� ��� ����������� ����� ����������������� ���������)��)������������ ����A ��'�-������������� �����������2 �)���6 ������������������F�����'�<��� �������������F�����K���������"������D ������ ����� �����K� ������� �������������)�� 0�������� ��������K����������������� ���� �������� 0�������� ���������������������)���������D ���D ���� �������K����6����� ������������������������������'��-��4���������3 �� ��E��������?�������� �������������������������D ���,���������?)��'�-���0������������������K��������������������F�������H��� ���������'��������?���������������D �����������?)�������D ������ ����K����� �����K�������� �����������������K���������K����'��'������������� �� �������:��������;������������?)��K�D �������,����0���D �������������������'������ ������ ����K� �������� ����� D ��K� �������� �� �H�)�K� �� ������ ��� ���N� D ��� ����������� ��� ����������� �� ��� �����������������������������?)����)����������� �����'������������������������� �����������D �����������?)�������� ����� ��H��'� ��� ���������� ��� ��F������ ���� ���������� D ��� ��0���� ��� ���������� �H������� �,�����'�* � ��������� 0�����������'�����0�K������ �������� ��������D ������ ����������������������K��������� ������������������� �����������8 �������� �����'� ������ ��� ���� )��0������ D ��� H"� ������� �����?)��K� ������� ��� �� �������K� ��� F������� �������� ���0��H����K������������ 0���������0�������K���)�����������0�������K� ����� ����� 0���'������� ���8 ����� 0�����������������������������,������� ��������������������������:����������K�����H�������������;��K�D ��������������� ����� )��� �H����� ��� 0�K� ������ ������4���� ���� ���������� :�� ������� ��� �����K� ��� �3 �� ��;'�������� ���� � ���� ������� ����� ���� ��� ������� ���8 ����� D ������� 0����� ������ D ��K� ������H����� ��� ���F����K����������� ������������������������������ �������'�-����������8 ������H���������F�����'��3 ��� ������<������� �<#8��� �������� ����� ���������������������������� ����A ����� ����������F�����'�����H������ �������� ����� ��E�

• �0��������F������������"����o I��������������������4������o I���0���������������)����0�������

• �0�����0���������������F����������������������� ���������������F���)�����D ����� ����������� �� A ��

7.1 - Objectos

����8 ����������������D ������� ��������������0���A ��'���

• -�����������0������������������)���'�• �&�E��

o ����������:��0���� ����������;N�o �������:��������;�G�$����������H��������������������������K������F�����)�������������������N�o $�� ���������� ��,�����K� ����,����0����� ���� ������'������������F����������������������

�������3 ������������������ ���������'�• $�������� ���� ������� ��F������ ����),�� ��� ������ ��� ���������� :�)������� ��� �,�����;� ���� ����6��

���������������)������ ���������4������ ���������

Page 90: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>��

@ �� ��F������ ���� ������������ ��������� :����),�� ���� �������;� ����������� �� �����K� �������K� ��F���)��� �����),����'�

7.2 - Classes

�0�������������������D ���0��������������"����������'������)��������F����������F���������������� ��������������������������������'������)�� ��� ��������� :�0�������;� �� �� ��� ���������� :�,�����;� ��� ��� ��F����'�[����������� �����������F�����'���0��������������� ���E�

• �� ��0���������������������:)��")��;�• �� ��0��������� ��������������0���������F�������������������:�,�����;�

7.3 - Atributos

�0������������4����������0����������8 ������������������'��&����������� ��������������������������K��������������0�������� ����������������������������������'�

7.4 - Métodos

�0����0����������������0����������8 ������������������'����� ������������ ��������� ����������� �� )�����K� ��� �������� ������ � �� ��� �����'������������0����������������������F�����������)���������������������������'��-����6����������,������ ����������������� ��������0����E�

• ����������:��0�����D �����D �������,����������0�6������8 ����������F�������D ��� �������;��• ��(������ : �������� ��� ��)��� ���������� �� ��� ��F����� ���� D ��� ����������� �,����� ��� ����

��� ������������F���3 �������;���C ���,������������ ����������������K���D ����,��������4��� ��E�

• � ��������������• ������• �� �������� ����� ��8 ��������

�����,�������&��������������������D ���������������������������&��������������'�<���,� ���4)����������������������3 ��&��������,���������������������������'��� �����������)�����K����2 �)�K����������������������������� ������������� �������:�3 E�<���$�����;K���� �����D �����������������F��������������������� �������:�3 E�����@ �F����;'�@ �������������,������������������,�� ������ �������:�3 E�����(�����;'��@ �0��������������������,�����,����������E�� // Já vimos isto quando falámos de sub-programas! tipoRetorno nomeMetodo(listaParametros) { declaracoes; instruções; retorno (se for o caso); }

Page 91: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

7.5 - Instanciação de Classes

������������,��� �������������������8 �����:��F�����;�������������������������'�[������ �������0������������������������������������F������:�����D ��������F������F"��������8 ���������0�6����������������8 ������� ����������''';'������ ����� ���� ���8 ���� ��� ���� ������� ���6����� �� � ������� newK� D ��� D ������ ,� �3 �������� ������ �� ���?���������"��� ����������������F��������������������?��'��)��� ��� � ; ���new <nome da classe> (); ou new <nome da classe> (<lista de parâmetros actuais>); �� � ������"���Conta c = new Conta(); // A variável c é uma referência a um objecto. // A variável, na verdade, serve como apontador para o // endereço de memória que o objecto irá ocupar.

7.6 - Referenciar Objectos

* �0�������������F�������������������6" ��� �����������6����������������F���)�'�������������F�����������������������4��K������F����������������������?��� �������������'�����������,������H���������������0��&����:������ �������������D ���0�����0���������3 �� ���#;'��������������K���0��������������F��������6�������)��")���D ���0�������� �������0��������)��������������������F����'����������������K� ����,�� ����� F"�������"���K� ���� �)����A ��� �����,������ D ��� �� ��F����� ����� �3 ������K� �����F�K������,������D ���0�������0�����������������D ������� �������'��� � ������$��� System.out.println(“OLA”);

�-�������)�������� ��H������?����������������)��� �������������println� ������ ��8 ������OLA� �����F�����System.outK������F�K������������)��������,�����println������F�����System.out�D ���,��������������4���D ���������� ���������)�����3 ������,����'��� � ������%���

contaDoBanco.recebeOrdenado (500.5);

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

contaDoBanco.recebeOrdenado (500.5);

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

Page 92: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>��

-�� ��F����� contaDoBanco� : ������� ��� ����� ����� D ��� �������� �� ������� Conta� F"� ������ ��� �3 �� �������������;K�������������)����������������recebeOrdenado� D ������������ ��8 ��������)�����500.5'� @ ����F�K������������)��������,�����recebeOrdenado������� ��8 ������500.5������F�����contaDoBanco'�(,����������D ������"���0��������������Conta'��� � ������E��� Conta c; c = new Conta(“1287”,0); c.obterSaldo(); /* envia a mensagem obterSaldo para o objecto referenciado pela variável c */ Conta b = c; /* as variáveis b e c passam referenciar o mesmo objecto */ ��[� ���4)������3 ��&��������������D ���������0��&���� ��������������F����'��3 ����� ����������? �������F���������)"����)��")����� �6����������� ����'�$���������������0����������������F����K����������� ��)������������0��������������������������������)&����������0�����'�<���3 �� ����K�D ������������"������)��")��������� ��Conta ��06�����b = cK� ���"���������������)��")����� �6��������� ���������F�����D ���0������������new Conta(“1287”,0)'���� �)�������� �� �,����� ������ ������ ������ ��� ����K� �������� �� ������ ���������� :b.obterSaldo()���c.obterSaldo()����������������0;'����06������b.recebeOrdenado(600.0) ������������������+��V������������������'�$���������D �&���K���� �� ��� 06������c.obterSaldo() )��0������ D ��� �"� +��K� ��� ������ 0����� D ���b.obterSaldo() ����,�� )�� ��������� ��� +��'� ����� ��D ��� ������ ��� )��")��� ��� ����� �� ������ �� � ���� ��F����� G�b� ��c� ��������@ ��� �������� ����#8���'��� � ������F �� import java.io.*; import java.util.Random; public class Untitled1 { public static void main(String[] args) { Random objectoDaClassRandom; // declaração objectoDaClassRandom = new Random(); // instanciação int var1, var2; double varD, varG; var1 = objectoDaClassRandom.nextInt(); // mensagem var2 = objectoDaClassRandom.nextInt(50); // mensagem varD = objectoDaClassRandom.nextDouble(); // mensagem varG = objectoDaClassRandom.nextGaussian(); // mensagem System.out.println("var1= "+ var1 + " var2=" + var2); System.out.println("varD= "+ varD + " varG=" + varG); } }

Page 93: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>/�

7.7 - Métodos e Passagem de Parâmetros (revisão)

@ ���,���������������������������� ��������'�2 "�)����D ��������� �������� ������������ ��8 �������D ������������������ ����� ����� ��� ��8 ������� 0������ �� D ������ �&�� )������� ���������� ���� ����������� ��� ��8 ��������������������������'�=��������,��D ����� ����������� ��8 ������� ���������� ��������:�,����;� ��������0������������0�����E� ���)����K���� �����0��&���'��� ��/ (K�������������H����0������������� ��8 ����������� �������E�

• ���8 ���������F��� ����F�� ���)��:��K��H���K�����K�������K��������K��H��K''';�������� ��� �������� ���)����'�@ ����F�K�������������� ������,������� D ������ �������,������? �����)�������� ��8 ���������6��������)�����������,����'�

• ��������� ���D ���������F��� ���)���������F�����P�o =��")��� ������ ��������� �� ��� @ �F����� ���� ����6����� �� @ �F����K� ���� ��� �� ��������� ���

���?�����������F����P�:������0��&����;�o I��������� ��8 ������0������,��������8 ������������������:���@ �F����;���D ���,� ������� ����

�� �,����� ,� �� ��������� ��� ���?��� ����� ���"� ����6������ �� @ �F����� :�� ��0��&���� ���� ����F����;'�

o ������������������A ����3 ������������ �����������,����������������8 ������5������ D �����F�����@ �F�������0������ ���������8 ������$������������D ������,�����0���)�����'�

o @ �� ��F�K� �������� ������� ��� ����� � ��� ��� ���������� ��� ������, ��(����� O �� � ��� ���������@ ����

�� � ������G ��

� // Exemplo de um programa que demonstra a passagem por valor class PassagemPorValor { // incrementa: recebe um inteiro x e soma-lhe o valor 1 public static void incrementa(int x) { x = x + 1; System.out.println (“x = ” + x); } public static void main(String [] args) { PassagemPorValor p; p = new PassagemPorValor();

int y = 1;

System.out.println(“y = ” + y);

p.incrementa(y);

System.out.println(“y = ” + y); }

} // O valor da variável x é alterado, // mas o valor da variável y não ���

Page 94: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>��

� � ������P ���// Exemplo de um programa que demonstra a passagem por valor public class PassagemPorValor { public static void main(String[] args) { int pcA = 10, pcB = 20; System.out.println(“pcA= “ + pcA + “ pcB= “ pcB); metPassagemValor(pcA,pcB);//invocacao do método metPassagemValor System.out.println (“pcA= “ + pcA + “ pcB= “ pcB); } static void metPassagemValor(int pfA, int pfB) { pfA = 111; pfB = 222; System.out.println(“Como o que está a passar para dentro do"); System.out.println(“método são os valores 10 e 20 e não); System.out.println(“Os endereços de memória das variáveis pcA e); System.out.println (“pcB, os seus valores não são alterados); } }

�� � ������Q��

�// Exemplo que demonstra o funcionamento // das referências para objectos quando passados // como parâmetros a métodos class Referencia { // redefine: recebe uma referência para uma conta bancária // cria uma nova conta, credita a primeira e depois // credita a nova conta acabada de criar void redefine (Conta a) { Conta b = new Conta(“567-8”,55); a.creditar(100); a = b; a.creditar(100); }

public static void main(String args[]){ Referencia r; r = new Referencia(); Conta c = new Conta(“123-4”,12); r.redefine(c); System.out.println(c.getSaldo());

} } // não altera o valor de c // altera o estado do objecto referenciado por c

Page 95: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>!�

7.8 - Construtores

C ��� ���� ������ ���� � ��������� ��� ����������� ��������� ��� ��F������ ,� ������ ���6��� �������������� ���������������������F�����'�@ ������������,�����,������� ��������6����� ���������������������6����������F������������������������������'�@ ������ �������3 ����)����F���)��,�����6��������������K������F�K�������� �������������#8���'������� �3 ���� ������������� ���� ��8 ������K� ������������� ���� ��8 ������� �� ������������� �H������� ���� �� :�������������D �������)�������������������;'�[�� ��������������D ����������������,� ������public������� ���������� ��������, ������ �� ������D ����,������'�

�� � ������R ��

�public class Conta { private double saldo; public Conta() { // Construtor sem parâmetros saldo = 0.0; // quando um objecto do tipo conta é criado // com este construtor, fica com saldo 0

} public Conta(double saldoInicial) { // Construtor com parâmetros

saldo = saldoInicial; // quando um objecto do tipo Conta é // criado com este contrutor

// fica com um saldo cujo // valor inicial é dado pela variável // dada como argumento (saldoInicial) } Conta(String numeroConta, double saldoInicial) { // mais um exemplo de um construtor com parâmetros // numeroConta e saldoInicial são os parâmetros formais... numero = numeroConta; saldo = saldoInicial; }

}

�I���������������0������H������������������������D ����� ��0�������F����K����"D ����)��������������� �������������������� ������3 ������K��D �)��������E�

� <nome da classe>(){}

���������������������6�������������������F���������)����������� �K�D ��������� �������E�

• 0��������������0������� �����,����:short, byte, int, long, double,float, char;�• false��������������0������� ��boolean�• null� ������0��&������

�� ���null��� ��������������0��&��������K������3 ������K� ���������F����� �����������������D ���D ����������'��

Page 96: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>+�

� � ������":��� class Funcionario{ String nome, codigo; // Atributos float salario; Funcionario(String nomef,String ident,float sal){ // Construtor nome=nomef; codigo=ident; salario=sal; } ... } class admissao{ public static void main(String args[]){ Funcionario func = new Funcionario(“Pedro”,”15080”,150); ... } // cria uma referência chamada func para um Funcionário que é // construído com o valor inicial Pedro para o nome, // 15080 para o código e 150 para o salário }

�� � ������""���// Exemplo que demonstra a utilização de um construtor implícito // ou default class Funcionario{ // atributos String nome, codigo; float salario; // admite: atribui novos valores aos atributos de um Funcionario void admite(String nomef,String ident,float sal){ nome=nomef;

codigo=ident; salario=sal; }... } // note-se que a classe Funcionário não tem construtor! class admissao{ public static void main(String args[]){ Funcionario ze = new Funcionario(); //chamada ao construtor //default! ze.admite(“Zé”,”15090”,150); // invocação do método admite ... } }

Page 97: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>9�

7.9 - Definição de Novas Classes

�������,��������6�������������� �, ��0����� ������������K�,��������D �����F��������"�����0�����)�����������D ��������F������������)������������� ���������:��"�K�,�����D ���������)������0�6�����������������3 �� ���'�-��������$����K� ����3 �� ��K�����0�6� ����������������;'���0�����������������0�������������������������������E�

• �0������������:)��")��;�����������• �0������������:)��")��;�������8 ����:D ���)�����������������;����0���������"���• �0����������������• �0����,������

�� � ; �M ���������N���<Definição de Classe> ::= // comentários sobre a classe public class <nome da classe> { //definição dos atributos de Classe <tipo1> <atributo1>; <tipo2> <atributo2>, <atributo3>; ... //definição dos atributos de Instância <tipo1> <atributo1>; <tipo2> <atributo2>, <atributo3>; // definição dos construtores <construtores> ... // definição dos métodos de classe static void <método1>(){...} static void <método2>(<parâmetros>){...} static <tipo> <método3>(){...} static <tipo> <método4>(<parâmetros>){...} // definição dos métodos de instância void <método1>(){...} void <método2>(<parâmetros>){...} <tipo> <método3>(){...} <tipo> <método4>(<parâmetros>){...} }

Page 98: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>.�

� � ������"$���// Definir uma Classe public class A_MinhaClasse () { // Atributo double contaNoBanco; // Método private static recebeOrdenado (double ordenado) { // Instrução contaNoBanco = contaNoBanco + ordenado; } } ... // Criar um objecto a partir da classe definida, ou seja, // Instanciar a classe A_MinhaClasse contaDoBanco = new A_MinhaClasse();

7.10 - Variáveis de Classe

C ���)��")�������������,����������������������D ��� �����������������4������������ ����E������������F������D ���0�������������� ������������������ ����H�����������)����� �����������������:��������� ����H�������,������D ��������������0��;'�=��")����������������2 �)��������0������������������ ���)��������)����static�:�3 �� ��E�static double valor_cambio_dolar;'����������0�����D ����3 �����)��")������"����� ����,���3 ������,���������"����'� @ ���,���������"����� ����������)���������������������K�����,�������"�����������H�������8 �����������������D ���0���������������� ������� �����������6���:��������3 �� ����������������,��������0���������������Math;'�[� � �������� ��������� D ��� ��� �,������ ���"����� � ����� ����� ��� ����� )��")��� ������ ��� ������ ������������"����'��� � ������"%���// Exemplo que demonstra a utilização de atributos e métodos estáticos class Produtoexportacao { String nome; float preco_venda,preco_compra; static float valor_dolar; Produtoexportacao(String nome,float precov,float precoc)

{ this.nome=nome; this.preco_venda=precov; this.preco_compra=precoc;

}

...

Page 99: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����>>�

static void cambio(float valor) {

valor_dolar=valor; }

} class ComercioExterior{ public static void main(String args[])

{ float cotacao = 1.50; // Chamada de método estático sem instanciação Produtoexportacao.cambio(cotacao);

} }

7.11 - Variáveis de Instância

-�� (���(�� � ��� �� 1 ���� ���� ��� )��")��� ���� ���"����� ��0����� ����� ������K� 0���� ���� �,�����'� @ �� ��F��������������� ������������������ ������K��������������K������? ��������)��")��'�@ � )����� ����6������ ��� ����� ���� ������� �? ��� �������� ���� ��������4����� ��� �� �������� �� ��40��� ��������F����'����������������F������������������������������,�������� ��������'�

7.12 - O Operador de Auto-Referenciação this

-� ���)��������)����this� �����������6���K���������������������������,����K� ������0��������F�����������'�this� ,� ��� L� �������M� ��� ���� ��0��&���� ���� �� ��F����� ��� D ������'� C ������ ����� � �������K� ����,����� �������������������������������������������D ��� �������'�this�,����������������������D �����������F������H��������,�������L� ����M� ������ �? �����F�����D ����H��������,����'��� � ������"E��� class Funcionario{ String nome, codigo; float salario; Funcionario(String nome,String codigo,float salario) { this.nome=nome; // Acesso aos atributos this.codigo=codigo; this.salario=salario; } // neste caso o operador this ajuda ainda a resolver // ambiguidades. Caso contrário como saberíamos de que nome // codigo ou salario se estava a falar? // nome=nome não tem significado! // qual deles é o atributo da classe funcionário e qual é o // que foi passado como parâmetro? }

Page 100: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

7.13 - Exercícios Práticos

<#8���(� ���)�������$���������,������ ��������� ������� ���D ����������E�

+' $��H���������������� �������������������� �������F������9' ������������)�������������������" ����

�3 ���+�C�J �� > � �� �� ��� ��

#�' ���D ������������0���������������� ��������������������������� ��������������������������F�����'�##' C �����������������F���������������������O�����������K�������D ����:���D ���;�����0��������������������

������������O�#�' <������ ��������)������D ���������������K��,�����������������O�#/' I���� ���� ��� ������A ��� D ��� ��������� ��� ���6��� ���� �������� �� ����������� ��� ���� ������O� &� ���

�3 �� ��'�#�' [� ���4)��� ���������� ������� ���6������� ��� D ��� ���� ��0��&���� ���� ����������F����K� ������ �����

0������ �0�������� ��� ��� ����� ���� �? �� � ���� ��� �0�������'� =��������� ��� 0����O� ��� 0��� )������K������������0�������F���0���������&��3 �� ���'�

#!' �����D ������)�������������������O�#+' I�����������0������D ������H���� ������)��������������������F����O�#9' @ �D ���,��������������O�#.' I���� ����������������������H���O�#>' C ������������� ��������������� ��8 ������'���� ������������K����O���' ����� D ����������� ���)��")���������������)��")������ ���8 ���'�(�����������0����������������������

F���0D �������� ����'��#' $���� ��������������6���)��")������������O������,K�D ����������� �����������O�2 ���0D ������&��3 �� ���'���' ����� ���6��� ���� )��")��� �B��� ��� �,����� ���"���� ��� ���� ������� ��������� ��� ��������� �� ������O�

2 ���0D ��'��/' (,���������"����� �������0�����������������D ���������F������"����O�2 ���0D ��'���' @ �D ���,��� ����D ������)����� ��������H�O�

�3 ���++�C�������������' ������ �& ����3 �, ���� �3 ��� �� ���3 ���'�#E��������������������"�����E�L@ �D �����������H ��������,�����������������D ��������������������M����*�������� ��H�������������� ���������������H ������������������������������"�����'������)� ����� �������?�������0��3 ������'�����*���� �������)�����?���������� �����������2 �)�� �����0����������"������������ �����������*���' �������������,����� ����0�6������������������������ �������� ������������,����'�����*���! �������0D ���������� ����������������������)��")�������������*���� �������0D ���������� �����������H"�����������������)��")��������������*���; ��

Page 101: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

��������,�������D �����������������)������K���� ��8 �������0����������� ��8 �������������'����3 ���'��E�C �� ���������� ����,���0���� ���������������������������������3 ���Z '�����*�����������)�����?������������������2 �)�� �����������6�������0�������� ����'�����*���� ��<������������� ���������,������ ���E��

#' ��������������8 �������������� ��������' ��������������8 ���������� ��������������

���*���' �������)�����,��������K�������������"���������������������������������� �������������������������������"������������8 �����������������������������������"������������8 �����������'�����*���! �������0D ���������� ����������������������)��")��������'����*���� �������0D ���������� �����������H"�����������������)��")���������'����*���; ����������,�������D �����������������)������K���� ��8 �������0����������� ��8 �������������'����3 ���'�/E�-�0������ �������"����������������A �������� ��� �,���0���� ��E��

!!( )!

np

nC

p n p=

− ������)����� ��������D �����������$ �� ��� � ������������������ ����)����������������� �:�� ������������;������� �������6����'�@ � ����������)��0�6�����������������0��������D ������)�� ���������������0���������������� ��������H��,�0��������������3 �E��public class Factorial{ static long calcula(int n) { if (n < 2) return 1; return n*f(n-1); } }

��3 ���'��E����D ���D ����������������)����6������������� ?�����3 ������������������ �������E��public class VariavelDeClasse { static int n = 2; String descricao; public static void main(String[] args) { VariavelDeClasse v1 = new VariavelDeClasse(); v1.n = 1; VariavelDeClasse.n--; v1.descricao = "Ola"; VariavelDeClasse v2 = new VariavelDeClasse(); v2.n = 2; VariavelDeClasse.n++; v2.descricao = "isto e um teste";

Page 102: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

System.out.print(v1.descricao + " "); System.out.print(v1.n); System.out.print(" ** "); System.out.print(v2.descricao + " "); System.out.print(v2.n); } }

��;�@ ���#�SS�������������������;�@ �����SS����������������/��;�@ ���/�SS����������������/��;�@ �����SS����������������/���3 ���'�!E�<���3 ���4����������K���D ������������������������)��")�������������������)��")���������8 ���'���3 ���'�+E�$������ ��������D �������������� ������������������������/� �����K�0��������� �������6����K���� �������8 ������D ����������� ���������:�D ��"����K��?������������������;'��)������,�������������������������������������8 ����������8 �����'��� ����

#' ��8 �������D ��"������������/�����������'��' ��8 �������?����������������������'�/' ��8 ����������������������/��������0�������'��' C �� ��8 ������,� ����8 ������ D ������)��0����� �� ������������ ���� �������������"�����E��� D �����������

H ��������,�����������������D ��������������������'���)��� ��������E�

#' -�������� ����� ������ ����������������������������' -����������������� ������ �����������8 ������'��/' ����� ������ ���"� �� ������� D ��� ����,���� �������� ��� ��� �� 0�6� ���� ���� ������� ����� ���� ��� ��� ���

��F���)��� ���������'��

Page 103: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

8 - Recursividade

8.1 - Definições

-��� ����������3 ���������������� �����������A ��� �����0��������)���������������� ��������� ������������������0�����E�

• ������)��:����3 �4���;'�• * �����)��:���� �4���;'�I����������,��������"���0������������������� �? ��'�

������3 �� ��K�)�F��������"���������0���������������� ������������'������ ��������0������������0�����E��

• 5����������)��������P�R��S:� #;S:� �;SkS#�

�• 5�����������)��:��0���� �������0�����������)�����0�����;�

#PR#��P�R��S:� #;P���

�=��0�� ���D ��������������� �? ��K���4�����������0������������)��<���� ��������A ���������)��K������������ �������� ��� ������������0�����������)�����������������E��

• C �����������������:������,������ ������;��<���3 �� �������"���������0����������������,���,������ ���#PR#'�

�• @ �����������K���� �����������)�K��PR�S:� #;P�

�-� ������)����� ,� ����� ������ ��� �3 �� ��� ��� �"������ ��� ��������� �����"����K� �������� �� ������)����� ����������� ���������������?������)����������� ����������)��")��������K������������ ��8 ������'��� � ����� ���<�� �3 �� ��� �������� ���"� � ���������� �� �"������ ��� 0�������� ��� ��� �� ����� �������� ����),�� ��� 0����� ������)�� ��������)�'��+���(� public class FacInt { public static void main(String[] args) { for (int i = 0; i < 9; i++) System.out.println("f(" + i + ") = " + factorial(i)); } static long factorial(int n) { long f = 1; while (n > 1) f *= n--; return f;

Page 104: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

} } /* Output: f(0) = 1 f(1) = 1 f(2) = 2 f(3) = 6 f(4) = 24 f(5) = 120 f(6) = 720 f(7) = 5040 f(8) = 40320 */

�2 ���� �(� public class TestFactorialRec { public static void main(String[] args) { for (int i = 0; i < 9; i++) System.out.println("f(" + i + ") = " + fact(i)); } static long fact(int n) { if (n < 2) return 1; return n*fact(n-1); } } /* Output: f(0) = 1 f(1) = 1 f(2) = 2 f(3) = 6 f(4) = 24 f(5) = 120 f(6) = 720 f(7) = 5040 f(8) = 40320 */

Page 105: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

=�F� ��� ���������� ����������������������"���������0���������0:/;�R�/�S�0:�;�R�+�� j�� ��S�0:#;�R���� � j�� � #�S�#�R�#�������0���6��K��� ��� ���D ��E�

• <����� ����������������)������������"� ����������3 ������������������������������ �����'�• <����� �����������������)������������"� �������K��������������� ����������������������������'�

8.2 - Exercícios práticos

�3 ���'#� �5��������0������������)��D �������������)���������K��������R�#�T�m �T�#B/�T�n �T�#B!�T�#B<'����3 ���'�� ������)������0������������)��D �����������������������)���������K��������R�#�T�#B#P�T�m P�T�#B/P�T�#�B<P'����3 ���'/� ������)������,�����������)��D �����������������������)���������'���������R��B��T�!B!�T�#�B+�T�#9B9�T��+B.�T�'''�T:��T#;B:�T/;���3 ���'�� ������)������,�����������)��D �������������)���������� ��������3 �����),�������������������,����������E��� ����:3 ;�R�#� �3 ��T�3 �� �3 +�T�''''�P��P�+P���3 ���'!� ������)������0������������)��D �����������������������0������ ��&����op'����3 ���'+� �5������� �����������������)��D ��������������������������������������������������)��������������������� ���A ��'����3 ���'9� �5��������0������������)��D ����������������������������� �����E�����:�#K��;�R��#�K�������R�������:�#K��;�R����:���K��#�������;K�������PR������3 ���'.� �������0������o���0�������3 �E���-��������o:�K�E������;E������N���inicio se (n=m) ou (m=0) entao X = 1 senao X = X(n-1,m) + X(n-1,m-1) fim��I������)��������o:!K/;�O��I��������H�������������0���������)������������O��

Page 106: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

=�����#�

��4���

���������

=��")���#�

=��")�����

=��")���/�

=�������

=�����/�

=�����#�

=�������

=�����/� ��4���

=�������

=�������

'''�

���������

)���)���

9 - Tabelas

9.1 - Motivação

C �� �������� D ��� ��� ���� )"���� )������K� ����6���� ������ )������� ��� �������� )��")�K� 0�6� �� ��� ���)�� �����������������)�������������� �������3 ������:0�' #;'�����������������

5������#' -���6��������K� ����������������)�����������4��'��<������ ������ ������������ ����� ���� ������)������K� ����������������� ������ ����� )"���� )������'������� ���������������� ����������������������)�6:0�' �;'��������������������

5�������' (�� ����������)"����)��������

Page 107: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

���������� ������ ��� �������� D ��� �"� ���� )"���� )������� �� � �� ���� ���� ������ �)����'� @ � �������� ��)�� ������" ����LD ����������������������� �M� ���� ����6������4�������F����:0�'�/;'��

�5������/' 1 ���������)"����)�������������)����6����� �����������)�����

�-���������)�� ���������� ����������A ���,�����������������K�D ������������������D ���������������)������'�-���K� �������3 �� ������ 0�'�/� �������������6��������)������������)��")������ � �� ��������������������)������� ��������������F����:0�'�;'��

�5�������' -���6������������)"����)������������������'�

=�����#�

��4���

���������

'''�

=��")���#�

=�������

=�����/�

=�������

=�����!�

=�������

=��")�����

=��")���/�

=��")�����

=��")���!�

=��")�����

'''�

=�����#�

��4���

���������

=��")�����������

=�������

=�����/�

=�������

=�����!�

=�������

'''�

Page 108: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

9.2 - Tabela Unidimensional

��3 ����������0��������������������������������2 �)�E��

:#; U� �X�U)��")��X�q�r���:�; U� �X�q�r�U)��")��X�

�' 5 �� � �� � �0��

:#; �������q�r�N���:�; ���q�r�����N�

9.2.1 - Variáveis Simples e do tipo tabela

�5�6��������� �D �������)�������������)��")����� ���K�������E��

• ����6���������?�)����'�• �����������D �����������������������'�• ����6������ ��� ���)��K�D ���������� ������� ���)����'�

�-���������E��

• �������������������)������K����������������� �'�• ���������3 ������� ���������'�• ����)�6�������� ���������������H��03 �'�• ��������������F�)�������������K������F�K������� ������3 ������'�• �������� ����,������������������� ��)������D ��������3 ��������������0���������'�

9.2.2 - Passos para utilizar uma tabela

�@ �� ������ �������6��������������������&�K�����������K����������������0���������E��

#; �����������E��

int [] tab; // declara uma referência para uma tabela de inteiros com qualquer dimensão, iniciada com null �

��

�; �����������E��tab = new int[5] ; //criação de uma tabela com capacidade para 5 inteiros �

���������

Page 109: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

��

• C �������������F�)��,���� ���������������������������� �������new�• @ �4�����������������������������0������������������H��–1K������F�K��������������������H��

N�,����3 �������0���N-1'�$��������3 �������������������������,�������������3 �� ���'�• � ��� ��� ���������� ������� ������ �� ���� �������� ������������ �� �������� �?� ��� �������

length�:��3 E�tab.length;'�• -�������������)����������,�������������6�������6���'�

�-������������:������;��������������� ��������0����������������������������K��������������������� ��'��

int tab [ ] = {1,1,2,3,5,8,13,34}; // é criada uma tabela com // 8 elementos

���

/; �������������E��tab[1] = 12; // o elemento com o índice 1 da tabela

// passa a armazenar o valor 123 �

�����

���� #� #�� �

�� /�

���q�r� ���q/r�

���q�r����q#r�

!� .� #/� /��

k''� ���q9r�

���� #� #� �

�� /�

���q�r� ���q/r�

���q�r����q#r�

!� .� #/� /��

k''� ���q9r�

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

���q�r� ���q#r� ���q�r� ���q/r� ���q�r�

� �

Page 110: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

� � ���������� �����#�����(���(�� � ����� ��

& �� 0 ���(���)����� � � #������#������ valor = 10.6; tab[3] = 10.6; � ���� valor = Le.umDouble ( ); tab[3] = Le.umDouble ( ); )*�� System.out.println(valor); System.out.println(tab[3]); � � ��� ��� x = valor / 2; x = tab[3] / 2; ' �������� if (valor > 5.0); if (tab[3] > 5.0); 3 �1 ���� � Testa(valor); Testa(tab[3]); �� � ����� ������ ��� �������� �������#������ � ������"��<������3 �� ��� ������� ������������ �� )������,������ ��)�����������#.�����'������ ���K� ��� ������� ������� D ����������#.�)��������� �����H���������������������)���������� ��)�������E�� double [ ] pluv = new double [18];

pluv [0] = 30.55; pluv [1] = 23.94; ……. //restante valores

�� ����0����� ��������������)������K�D ��� ��������0���� ��������0������E��double total = pluv[0] + pluv[1] + pluv[2] + pluv[3] + pluv[4] + pluv[5] + pluv[6] + pluv[7] + pluv[8] + pluv[9] + pluv[10] + pluv[11] + pluv[12] + pluv[13] + pluv[14] + pluv[15] + pluv[16] + pluv[17];������double total = 0.0; for ( int ano = 0; ano < pluv.length; ano ++)

total = total + pluv[ano];

�� ������������� ���D �����4��������)����������������0��� ��������,�length-1��$�����)��������������0�������K� �������������������,������ ���� ��������������)����E� System.out.println(“O valor média de pluviosidade é”+(total/pluv.length)); ��� � ������$��@ � �3 �� ��� �������� ���� ����� ��F���)�� ���� ��� ���6����� �� )����� ��� ����� ������K� � ���� ��� ������ ��� )�������������6���K����������������K����,�������)������"3 ����������)�������:0�'�!;'�

Page 111: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

�import Le; public class MaxTotal{ public static void main(String[] args) { int n1[] = new int[5]; int soma=0; int max=0; //Ler array for(int i=0;i<5; i++) { n1[i]=Le.umInt(); System.out.println(“Introduza um valor: “); } //Mostrar array for(int i=0;i<5; i++) System.out.println("i["+i+"]="+n1[i]); //Calcular máximo, total for(int i=0;i<5; i++){ soma+=n1[i]; if (max<n1[i]) max=n1[i]; } //Mostrar resultados System.out.println("Total:"+soma); System.out.println("Media:"+(float)soma/n1.length); System.out.println("Maximo:"+max); //Nota: na media é necessário fazer uma divisao //de floats.

} �

� ��

5������!' $�������� �����H��������������������3 �� ����'�

��

�� ��

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

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

� �

���

��

��

��

������ ��� � ��������!#������������

����������������������!#������������

��

��

��

��

$�������

Page 112: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

9.2.3 - Acesso a elementos não existentes

�$�����������3 �� ����K���� �����#'�'�K����D ����,��������� �����H����������� ������������������'�$���������������������������������������3 ������K������������3 ���������� ������������������������������E�� n1 [5] = 4; //acesso a um índice que não existe �@ ��������������������E��Java.lang.ArrayIndexOutOfBoundsException:19 At Teste.main(Test.java)

9.3 - Passagem de tabelas como parâmetros de métodos

C ��� ������� ���� ���� ������� ����� ��8 ������ �� ��� �,����'� @ � D ��� ,� ������� ����� ��8 ������ : ��� )����;� ,� ����0��&�������������K���D ������0�����������������������������������������������������,����K�����������������������'������,��,� ���4)��� ����������� ��8 �������������������������������K�������D �����������������������0���������������� �'��[� ���4)��� ����������� ��8 �������������������������������K�������D �����������������������0���������������� �'��� � �������

��public class programa { public static void main(String args[]) { int [] array = new int[2]; array[0]=12; array[1]=10; for(int i=0;i<array.length;i++) System.out.println(array[i]); metodo_1(array);

for(int i=0;i<array.length;i++) System.out.println(array[i]); metodo_2(array[1]);

for(int i=0;i<array.length;i++) System.out.println(array[i]); } public static void metodo_1(int [] varray) {

for(int i=0;i<array.length;i++) array[i]=array[i]+1; } public static void metodo_2(int var) { System.out.println(“O valor recebido é :”+var); } }

Page 113: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

/* O output do código atrás apresentado, à luz dos conceitos introduzidos é: 12 10 13 11 O valor recebido é 11. 13 11 */

9.4 - Tabela Multidimensional

C ��� ��������� ���������� �� ������������ �������� )���������D ������ D ������� ��������������������� �� ����������������6K�������H������������'���$�����������������������������������������,���0��������������������4�����'����2 �)�K��������������������������,����������������������K������F��,�������������� �������������D �����������������,�������0��&���� ���������F��������� ��������'��! ����������

<tipo> [ ] [ ] <variável> �� � ������� int [ ] [ ] tab; �@ � �?���� ����� �������� ���� ����� ���� ������� ����������� ��� ������� ��� ����� ���� ������� ��� �������K� ��� D ���� ������������������0��&���'��-�����6�����,�D �������������������������� ����������� ���)��E��

1) tab = new int [2] [3]; ���2) tab [][]={1,2,3} {4,5,6};

��

���q�r� #� �

�� /� �

�� �

!� +� �

���q#r�

��

��

���� ���q�rq�r�

���q#rq�r�

���q�rq�r�

Page 114: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

����� ���������������������� ���������for�����������'��� � ������� public class Matriz { public static void main(string args[]) { int tab[][] = {{ 1, 2, 3 }, { 4, 5, 6 }}; for(int i = 0; i < tab.length; i++) for(int j = 0; j < tab[i].length; j++) System.out.print("tab[" + i + "][" + j + "] = " + tab[i][j]); } }

@ �2 �)��,��������������0����������� ���K������� ������������ �������������A ����������6������ ����������������������0����'� @ ��� ���������������������������������� ����)��������������� ������������������������ ������� �����������D ���D ������F���������� ��� ���)��'��� � �������@ ��3 �� ��������������������������6�/����)�������������� �������)�����'��Random gerador = new Random(); Integer a3[][][] = new Integer[gerador.nextInt(7)][][]; for(int i = 0; i < a3.length; i++) { a3[i] = new Integer[gerador.nextInt(7)][]; for(int j = 0; j < a3[i].length; j++) { a3[i][j] = new Integer[gerador.nextInt(5)]; for(int k = 0; k < a3[i][j].length; k++) a3[i][j][k] = new Integer(i*j+k); } } for(int i = 0; i < a3.length; i++) for(int j = 0; j < a3[i].length; j++) for(int k = 0; k < a3[i][j].length; k++) System.out.print("a3["+i+"]["+j+"]["+k+"] = " + a3[i][j][k]);

9.5 - Tabelas de Objectos

-���������������F���������������������D ��������������D �����������������,�������0��&�����������F�����������������������������'�����������������������F��������������6�������null'��

Page 115: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

� � ����� ���� #������)���� ��<���3 �� �������������� ����������,��������������������������������D �������,��������D �����������������D ���������������������������,�������0��&����������String'��final int NUM_NOMES = 5;

// Declaração e instanciação String[] nome = new String [NUM_NOMES ] ;

// Inicialização ou Atribuição nome[0] = "Smith"; nome[1] = "Jones"; nome[2] = "Miller"; nome[3] = "Lui"; nome[4] = "Gonzales";

// Saída System.out.println ("Resultado:"); for (int indice = 0; indice < nome.length; indice ++) System.out.println (“nome[“ + indice + "]: " + nome[indice]);

� #������<#8��� ����� ���� ���@ ��?�������������� �����K�� ������� ������������E��class Pessoa { String nome; int idade; String BI; public Pessoa (String pfN, String pfC, int pfI) { nome = pfN; BI = pfC; idade = pfI; } public void apresentar() { System.out.println(“Meu nome é ” + nome); System.out.println(“Tenho ”+ idade + “anos”); } public String getBI() { return BI; } public String toString() { return (“nome=“+nome+”com “+idade+”anos”); } }

Page 116: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

�-���������������������������6��������������� �����'��public class Teste { public static void main (String args[]) { Pessoa Pedro; Pessoa Nuno; Pedro = new Pessoa(“Pedro Martins”,“403.234.567”, 34); Nuno = Pedro; Pedro.apresentar(); Nuno.apresentar(); System.out.println(“O BI de Pedro e “+Pedro.getBI()); } }

������������ ������� ������?��������������������������������F���������������� �����K�D ���,���������������D �����������������,�������0��&������������F��������������� �����'��public class TabelaDeObjectos { public static void main(String[] args) { //Declaração e Instanciação da tabela Pessoa[ ] grupo = new Pessoa [4]; //Inicialização dos elementos da tabela (Atribuição) grupo[0] = new Pessoa("Pedro Sousa", "403234567",34); grupo[1] = new Pessoa("António Pereira", "233544325",37); grupo[2] = new Pessoa("João Silva", "763234567",40); grupo[3] = new Pessoa("Joaquim Feliz", "213456567",20); //Saída System.out.println("Conteúdos de cada elemento da tabela:"); for (int pessoa = 0; pessoa < 4; pessoa ++) System.out.println((pessoa + 1) + "ª Pessoa do Grupo: " + grupo[pessoa].toString ); } }

�5������+' �3 �� �����"0��������������������������������?������������

�� #������1 �����<���3 �� ���� �����������������������"��������������������8 ���K������������������� ���������������������� ��������� ����������)��������������������������,�������)������"3 ��'�

�import Le; public static void main(String args[])

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

-��?����������

2 ������)�� 2 ��D ���5��6�

��� �q�r� ��� �q#r� ��� �q�r� ��� �q/r�

Page 117: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

{ //Declaração de uma tabela de inteiros com // dimensão ainda indefinida int tab[ ]={}; int soma=0; int max=0; int i=1; //Ler 1º valor System.out.print(“Insira o 1º inteiro:> ”); int n =Le.umInt(); while (n!=0) { //criar tabela temporária int tabTemp[]=new int[tab.length+1]; //copiar valores for(int j=0;j<tab.length;j++) tabTemp[j]=tab[j]; //inserir novo valor tabTemp[tabTemp.length-1]=n; //colocar a tabela temporária na definitiva tab = tabTemp; System.out.print(“Insira o ” + j + ”º inteiro:> ”); n = Le.umInt(); } //Mostrar tabela for(i=0; i<tab.length; i++) System.out.println(“tab[“ + i + "]="+ tab[i]); //Calcular o valor máximo e a soma total for(i=0; i<tab.length; i++) { soma+ = tab[i]; if (max < tab[i]) max = tab[i]; } //Mostrar resultados System.out.println("Total:"+soma); //Nota: na media é necessário fazer uma divisao //de floats. System.out.println("Media:"+(float)soma/n1.length); System.out.println("Maximo:"+max);

Page 118: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

} - � �� ����� �����<���3 �� ����������� ������� ���������������������0?���'���������K���� ������������$�������K���F�������������������������������0������� �����E� public class Contacto { //Atributos String nome; int telefone; //Construtores public Contacto(String nomeContacto, int telefoneContacto) { nome=nomeContacto; telefone=telefoneContacto; } //Métodos public String mostrarContacto() { return nome + " - " + telefone; } public int obterTelefone() { return telefone; } public String obterNome() { return nome; } }

�����������)����������������1 ��������0?�������D ����,���� ����������������������F���������� ��$�������E� public class ListaTelefonica { //Atributos private static final int MAXCONTACTOS= 20; private Contacto contactos [] = new Contacto[MAXCONTACTOS]; private int nContactos=0; //Construtores public ListaTelefonica(){ } //Métodos public void adicionar(){ String nome; int telefone;

Page 119: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

System.out.println("Introduza o nome:"); nome= Le.umaString(); System.out.println("Introduza o telefone:"); telefone= Le.umInt(); Contacto c = new Contacto(nome,telefone); contactos[nContactos]=c; nContactos++; } public void mostrar(){ for ( int i=0; i<nContactos; i++) System.out.println(contactos[i].mostrarContacto()); } public void procurar(){ System.out.println("Qual o telefone:"); int telefone=Le.umInt(); int i=0; while(i<nContactos && contactos[i].obterTelefone()!=telefone) i++; if (i!=nContactos) System.out.println(contactos[i].mostrarContacto()); else System.out.println("O número não se encontra na lista."); } public void ordenar(){ quickSort(0, nContactos-1); } private void quickSort( int pri, int ult) { int i = pri; int j = ult; if (pri > ult) return; String pivot = contactos[(pri + ult)/2].obterNome(); do { while (contactos[i].obterNome().compareToIgnoreCase(pivot) < 0) i++; while (contactos[j].obterNome().compareToIgnoreCase(pivot) > 0) j--; if (i <= j) trocaContactos(i++, j--); } while (i <= j) ; quickSort(pri, j); quickSort(i, ult); } private void trocaContactos(int i, int j){ Contacto tmp = contactos[i]; contactos[i] = contactos[j]; contactos[j] = tmp; } //Método Main public static void main(String[] args) { ListaTelefonica listaTelefonica = new ListaTelefonica();

Page 120: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

int seleccao; do { System.out.println("Organizer"); System.out.println(""); System.out.println("1 - Adicionar"); System.out.println("2 - Mostrar"); System.out.println("3 - Procurar"); System.out.println("4 - Ordenar"); System.out.println("5 - Sair"); System.out.println("_______________________"); System.out.println("Qual a sua escolha?"); seleccao = Le.umInt(); switch(seleccao){ case 1: listaTelefonica.adicionar(); break; case 2: listaTelefonica.mostrar(); break; case 3: listaTelefonica.procurar(); break; case 4: listaTelefonica.ordenar(); break; case 5: System.out.println("Adeus."); break; default: System.out.println("Opção errada. Tente de Novo."); break; } } while(seleccao != 5); } }

9.6 - Classe Vector

9.6.1 - Introdução

-��������Vector�0�6� ��������package java.util����0����������0������&�����������������K� ��������������6������������)�������������0��&����������������),��������4����'�-���������0�������������������������������F��������� ��Vector�,�D �������� ����������������������������������������H�K���D ������D ��������������������� �����������H��03 �'��@ ������0������������������0�������������F��������� ��)������������������������ ���������������� �����)����'�C ����F��������� ��Vector��������������������0��&����� �������������ObjectK� ���������������������D ���D ����� �����)����'��

9.6.2 - Alguns métodos da classe Vector

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

Page 121: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

Vector ( ) $������)������)�6��void addElement (Object obj) -������ �� ��F����� �� ��0����� ��� 0��

���)������void insertelementAt (Object obj, int index) -������ �� ��������� ��� 4�����

�� ��0�����Object remove ( int index) * ���)�� ��� ��F����� ��� 4�����

�� ��0�������������� ��Boolean removeElement (Object obj) * ���)�� �� ������ �����&���� ���

��F������� ��0����'�void removeElementAt( int index) * ���)�� ��� ��F����� ��� 4�����

�� ��0�����void clear ( ) * ���)������������������������)������Boolean contains (Object obj) * �������������������F��������"����)������int indexOf (Object obj) * ������� �� 4����� ��� ��F�����

�� ��0����'�* ������� #����������F����������3 ���'�

Object elementAt (int index) * ������� �� @ �F����� ��� ������� ��0�����

Booleam isEmpty ( ) * �����������������)���������)���)�6��int size ( ) * ������� �� �� ����� ��� ���������� ���

)�������� ��������������0�����A �������������,�������� ����6����� ��������������K�����������������������������2 �)�'��� � �������<������3 �� ������"������������F���������������Vector�D �������6����"������'��������� ���� ���������������� ����A ������)�����K�����������K�����)��K�)��0����D ���������������"����������������4����'��public class Nomes { public static void main ( String [] args){

Vector nomes = new Vector( ); nomes.addElement(“Ricardo”); nomes.addElement(“João”); nomes.addElement(“Nuno”); nomes.addElement(“Daniel”); System.out.println(nomes); Nomes.removeElement(“João”); System.out.println(nomes);

System.out.println(“No índice 1: “ + nomes.elementAt(1)); nomes.insertAt(“Carlos”,2);

System.out.println(“Tamanho do vector“ + nomes.size()); }

}

�/*Resultado de saída do exemplo: Ricardo, João, Nuno, Daniel

Page 122: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

Ricardo, Nuno, Daniel No índice 1: Nuno Tamanho do vector 4 */

9.7 - Exercícios Práticos

�3 ���'#�G������)����� ��������D ��� �����H��������������������)��������������������������������������� �������������)����������������'�-������������ ���������������0�������������������� �������E�

• �������D �������)������������)�����������������������������• �� �������)������� ���)�������������• ��������������)���������������• ������������������)��������)�4)��� ���/� ����)�����6����• ��������� ���������������)����������������

��3 ���'�� ��� �������������,�����D ������������� ����������������������������������'�-���������������,�����,���D �����������E���

static double produto(int [ ] x)

��3 ���'/� ��� �������������,�����D �������������������)������������������'�-���������������,�����,���D �����������E���

static double min(double [ ] x) ��3 ���'�� ������)�������,�����D ��K������������D �&�������n�)�������������K������������0����������������)��������"3 ������4���������D �&���'���3 ���'!� � �����)��� ��� �,����� D ��� ������ ������ �����&����� ��� ��� ��������� ���$� ��� ���� �����'� -� ���������� ����,�����,���D �����������E����

����������������)�:�������K��H������$�;���3 ���'+� ������)������ ��������D ������)����������������������������������������'���3 ���'9� ������)�������,�����D ��������������� ����������������������)��)���� ���������������������������� �����������������)����� #�������������� ����������3 �������������'�-���������������,�����,���D �����������E��

�static int procuraTabela (double v, double[ ] tabDouble)�

��3 ���'.� � �����)��� ��� �,����� D ��� �)����� �� ������ ���� ���������� ��� ���� ������� ��� ������K� ������� ����� ��8 �����'���3 ���'>� ��� ������������,�����D ������������������������#� ���������������������������'�@ ��,�������)��������������� ����������������A ��'�-���������������,�����,���D �����������E���

static int substituicao(char c1, char c2, char[ ] tabCar) ��3 ���'#�� � �����)��� ��� �������� D ��� ��)��)�� �� ������ ��� � ����� �����&���� ��� )����� �"3 ��� �� �� ������ ��� �����������&�������)������4���������������������)����������� ��������'��

Page 123: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

�3 ���'##� ������)�������,�����D ������������������������������������������������������������'���3 ���'#�� � �����)��� ��� �������� D ��K� ���� ��� ����� ������� ��� ������� -K� ��������� ���� ������� �� 0������� �����4�������������������� ��������-'��3 �� ��E������-�R�:�#�/�+�9�.�;K��� ����������)�������������R�:�/�!�;'����3 ���'#/ � �����)��� ����,����� :�� ��� �������� D ��� �3 ������ �����,����;� D ��� ��������� ��� ��� ���������� ��� �������������������� ������������������������������'���3 ���'#�� � �����)��� ��� �������� D ��K� ����� ���� ��D �&���� ���n� )������� ������� :�������� �������������;� D ����� ��������� �� ���������� ��� n� ������������ ��� ��� ����K� ��������� �� �� ����� ��� �����&����� ��� ����� 0���� �����D �&������������'���3 ���'#!� �����������D �&����������� ����������K�0�6������ ��������D ����������������� ������D ������ A �������D �&��������0��D �&������������� �����������D �&��������'����3 �� ��E�����R�#����D �&���E���/'���.'#���'!��/'���9'���.'#��/'���+'/��9'���/'�����4��E�����<����5��D ����/'�����������.'#�����������'!�����#����9'�����������+'/�����#� �3 ���'#+� ������)������ ��������D �������������������)����������������6����)�������������������X����H������X���������'��������������������� ���������������� ������������������� ���A ����������6����D �������������������)������"3 ��'����3 ���'#9� ��� �������������,�����D ��������������"��������� ��������3 ���������������������6��'�-�����6������������������� ��������������0����� qrqFrR3 qrSZ qFrK������3 ���Z ��������)������������������'�-���������������,�����,E���

static double[][] productoExterno(double[] x, double[] y) ��3 ���'#.� ������)������ �������� D ��� ���������� �&������ ����� ������<'������ )����� ������6��� 0��������� D �����:���;�0���6����� �������'�$���������"��K�������������������������6������������ � � '�������������&����)����� ��������� �������������6��K����0���K������������������6� �����������)������� ����'���3 ���'�#>� ��0��������������^ ��������������6���� ��������K��������������������������D ������ �����������������������'��� ��������������������0���������������������������E�

• ��������������D ������������� �������_ ������• =��0���������������������������3 �������_ ������• * ���)�����������

��������������������$�������^ ���D ���� �������������D �)��������_ �����������F���0�����������������E�

• ���������������� ��������������_ �������������"�����• 1 ��������^ �������D �������������������������������• * ���)������_ ������

��3 ���'���G��� �������������������D �����H���������"���� �������������������3 ��E�

Page 124: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

���

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

�C �� % ����� ����� ��� ���F����� ��� ������� ����� �������K� �������� D ��� ������ �� �����K� ��)�������� ��������� �����������'��������D ���������������F����������������������,�������"�������������`�������������������������������������4�������!�V'��@ ���������?� ����"�����6�����)��������������������������������� �����'����� �������������������D ��� ����������������������0����������������"����������� ���������������D �������'���3 ���'�#� �$��0D �������������K���������� ���������������������������������E�����I��������������'���������������������� ����������������������������4�����E�C ���������,���� ����� ���)"����������K�$���������� �������������0������� ������� ����� ��������4����:D ����� �������������E�#K�K/K�����!;�������������:-K% K$����;'�$�����������?� ����"���������"3 ���/��-�����'��5������� ���������������D ������������������������������'�@ � ����������)������������������E�#-���#% K���������������� ��������)������ �����H��������� �0��������������#��-�����K� ������6���� �������6����'�- ?���� ������� �� �����H����������������K��� ����������)������6������������������A ��E�

• �� �����������������������������#-��������,����� �������#!�=������'�• �� �������������������������������2 ����������������������'�• @ ����������������� ������ ������������������������������������'�

�������E� ����� ����,������ � ����������K� �������� ���� �������K� ��� D ���� ��F�� ��)���� �� �������� ���� ������� ���������'��

Page 125: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

����

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

10 - Algoritmos de Procura/Pesquisa

10.1 - Introdução

-� ������� ��� �0�������� ,� ���� ���)����� D �������E� ���������� �� ������ ��� ������ ����0?����K� ���)���� �������"���K����'�����������������������������,������ ���� �������������������������������E��

• -� ���������D ������'�• -� ���������"��'�

�-� �����������0��������,�0��D ������������3 ������������������'�-���������������� 4�����)��������6�������������� �����H��'�=����� ��������0�6���������������������:� #��;�D ��� ���������,�����:������= �;��D ���� �����H������������K� ������������ ��8 �����K������)����������������������������������������������#��'�@ ��?��������������,������������ ���������E��import java.util.Random; class Tabela { public static void preenche(int tabela[]){ Random random = new Random(); for (int i=0; i < tabela.length; i++) tabela[i] = random.nextInt(100); } public static void imprime(int tabela[]){ for (int i=0; i < tabela.length; i++) System.out.println(" "+i+":"+ tabela[i]+"; "); System.out.println(); } }

10.2 - Procura Sequencial

10.2.1 - Simples

C ���0������� ������� ������������������E�• $�����������������������'�• $�� �����������)������������������ ���������������������������������'�• * � ������ ����������,���������������������������������0�����������'�

$�������������,� �����������D �����������K������������,����������� ��� ���������D ������'��

<���3 �� ����������� ������� ���������D ������ ������D �����)�����9!�:����3 ���;���� ����������������� �����'�

Page 126: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

����

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

���������

$�� ��� ���9!�����#��s ������0��������

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

�������#�

$�� ��� ���9!��������s ������0��������

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

���������

$�� ��� ���9!�����9!�s ����������

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

�@ �)����� ���������,���H��������� ������������ ���������������'��@ � �?3 ����3 �� ������6����� ��������D ��� �����H������������������� ����������������������������:����������#��;���� �����K����������K� ���������� ������D ���0������������'���public class ProcuraSequencial{ public static int procura (int tab[], int valor) { int i = 0; while (i < tab.length && tab[i] != valor ) i++; if ( i != tab.length) return(i); // retorna indice; else return(-1); } public static void main(String[] args){ int tabela[] = new int[100]; Tabela.preenche(tabela); System.out.println("Resultado:"+procura(tabela,67); System.out.println("Resultado:"+procura(tabela,33)); System.out.println("Resultado:"+procura(tabela,1)); System.out.println("Resultado:"+procura(tabela,101)); } } �

#�� ��� 9!� >� �� /�

�� #� �� ��/� !� t�����

Page 127: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

����

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

@ ��,����� �����������������4��������)������������������������������)����K�����������"����������� #'�<��� ���D ����������������� ���������� ������� �� �����������K�� while (i < tab.length && tab[ i ] != valor ) { ������������0������������������������������E��

• )��0���������)���������F����0����������������• )��0���������0������������0��������'�

�������� �� ������ )��0������ ��F�� ��������� �� �������� ����� ���������"��� ��� ������� �������� D ��� �� )����� �� �������������������)�����������'�

10.2.2 - Com Sentinela

C ����,��������6���� ���� ���������������������������������������6����������������)�������������'�C ��)��������������,����)������� �����D ��������������������� ������������D ����� ������������������'�<��������������� ��������������)����� ��������������������� �����������������������'���-� ���6����� ��� ��� )����� ��������� �3 ��� �� �3 ��&���� ��� ���� ������ �������� ��� ������� ���� ����6����� �� )�������������'�-� ������� ����������0������K��������������� ������K�,��������������)�����������������0�������������'�������0����� ���������)����������������0�������������K���������)����� ��������������3 �������������'��<���3 �� ����������� ������ ������ �������������� ���������D �������������������K����6�������� ��������D ��� �����H������������������� ����������������������������:����������#��;��� �����K����������K� ���������� ������D ���0������������'���public class ProcuraSequencialSentinela { public static int procura(int tab[], int valor){ int i = 0; tab[tab.length-1] = valor; //SENTINELA while (tab[ i ] != valor ) i++; if ( i != tab.length-1) return(i); else return(-1); } public static void main(String[] args){ int tab[] = new int[101]; Tabela.preenche(tabela); System.out.println("Resultado:"+procura(tab,67)); System.out.println("Resultado:"+procura(tab,33));

Page 128: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

����

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

System.out.println("Resultado:"+procura(tab,1)); System.out.println("Resultado:"+procura(tab,102); } }

10.3 - Procura Binária

-� ���������D �������,�������� ���K����� �����3 ����� �� �������������������������������������'������������������ ���D ��������������D ������������� ���������������������������������'��-� ���������"���,�������������)�������0��6K������3 ��� D ��� ������������� ��������� D ������ ����������"��� �����0������������������������������'��@ ����������,����������E��

#' ���������������������������'��' $������������ �������������������#b�����������������'�/' �������������� ���������,���������� D ����� � �������������������#b����������� ������K� ���������������

D ����� �����������������������������b�����������������'��' * � ������������ �������� ������#b�����������������'�!' �������������� ���������,���������D ������ �������������������#b�����������������K� ���������������D ���

�� ����������������������������#b�����������������'�+' * � �������������� �������� �������b�����������������'�9' �������������������������������0����������� ����������� �������������'�.' <��� ���D ����������� ������� ���������"�������6����� ������������������������������ �����������

�@ � �?3 ����3 �� ������6����� ��������D ����&�#����� �������� �����K����������K� ������������� ������D ���0������������'���public class ProcuraBinaria { public static int procuraBinaria(int tab[], int valor) { boolean achou = false; int alto = tab.length - 1; int baixo = 0; int meio = (alto + baixo )/2; while (!achou && alto >= baixo ) { if (valor == tab[meio ]) achou = true; else if (valor < tab[meio ]) alto = meio - 1; else baixo = meio + 1; meio = (alto + baixo )/2; } return((achou ) ? meio : -1);

Page 129: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

����

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

} � public static void main(String[] args) {

int tabela[] = new int[100 for (int i=0; i < tabela.length; i++) tabela[i] = i; System.out.println("Resultado:"+procuraBinaria (tabela,67)); System.out.println("Resultado:"+procuraBinaria (tabela,33)); System.out.println("Resultado:"+procuraBinaria (tabela,1)); System.out.println("Resultado:"+procuraBinaria (tabela,101)); } }

�� � ������M3 � , �� �������(�������� � ������#��N��

�5������9' ���D ������"���������)�������3 ����������������'�

��public static int procuraBinaria (int tab[], int valor) { boolean achou = false; int alto = tab.length - 1;

1 3 3 5 7 8 9 2

baixo

(>x) alto baixo meio

(>x)

#b���������

�b���������

alto baixo meio

(<x) /b���������

alto baixo

�b���������

(��� ��

5 7 8 9 1 3 3

3 3 5 7 8 9 1

1 3 3 5 7 8 9

Valor a Procurar

@ �)������������3 ��������������

Page 130: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

����

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

int baixo = 0; int meio = alto/2; while (!achou && alto >= baixo ){ if (valor == tab[meio ]) achou = true; else if (valor < tab[meio ]) alto = meio - 1; else baixo = meio + 1; meio = (alto + baixo )/2; } return((achou ) ? meio : -1); }

�� � ������M3 � , �� �������(������ � ������#��N�

�5������.' ���D ������"���������)������3 �����������������

10.4 - Exercícios práticos

�3 ���'#� � �� �����������,������"3 ���D ������������������)�����������������������������3 '� C ��6���������������� ���������D ������'���3 ���'�� ���� �����������,������4����D �������������4��������������������������)������������������5'�-���������������,�����,��

1 3 3 5 7 8 9 1

baixo alto meio

(>x) alto baixo meio

(>x)

#b���������

�b���������

alto baixo meio

(<x) /b���������

(��� ��

5 7 8 9 1 3 3

3 3 5 7 8 9 1

Valor a Procurar

@ �)�����#��3 ��������������

Page 131: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

����

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

public static int minimo(float[ ] tabF).

��3 ���'/� � �� �������� ���,����� ������$��� D ��� �������� �� 4����� ��� ������ �����&���� ��� ��������� �#� ��� � �����������$��'�C ��6�����,�������� ���������"��'�-���������������,�����,E��

public static int procuraCar(car[ ] tabCar, car c1) ��3 ���'�� � ������ ���� ������� ���� ��� ��� ��������� �,���� ��� ������ ���� ���� ��� �&�'� I�������� ������ ���� ���������������&�E�

#' I����0������� ��������������3 ���' ���D ������������������� ���������������)����/' I����0�����0������������������ ��������������3 ��������� ����������������'��

�3 ���'!� ��� �����������,����� ������5�����D ��� �����������������)K�D ���������������,������ ���������)���������K���������������������������3 '�$������ ���������F������6��������������������������4����������������K���������������G#'����������������,�����,��

public static int procuraInteiro(int[] v, int x, int s)

��3 ���'+� �C ������������������������ ���������������������������������������� ����K�����,����������������������� ������H������� D ��� ����������������������� ��� ����������������� ��� �� ����'�[� ������"��� � ��������� ����,����� D ���������������������� �����K��������� )����� �������� ������������������������������ �����������'�C ��6���� ���������"������� �����������������0�����'��� ����������,���������������� ���)����L�S�gM�-���������������,�����,E� public static boolean procuraPessoa(String[ ] v)

Page 132: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

11 - Algoritmos de Ordenação

11.1 - Introdução

C �� ���F����� ��� ���������� ,� ������������ ��������� ���� 0������� �� ������'� @ ��������� ��� ������ ���� 0������� �� ������������� ��������������������������������'��������D ����������� ������������ ������������0�����������������������������0?����D ����������)����������������0�����������'��������?��������������'����� ����������K������6�������������������������� �����������)���������������� ������������0������'��<��� ���������� ��� ���������� D ��� ������ ��� ������� ������6���K� )����� �� ��� D ��� ����������� �������� ������D �&����������������������"3 ������#������������'���$��� �� ��F���)�� ��� �� ����� �������� � ����A ��� D ��� ��� �� ����� ��� ������ ���� �3 �� ���� ��� ���������K� )������ ������������������������������:������;�D ��� ����������6���������������0���A ��K�����������������E��

• ������H������������������������)�������������K��������������K����������������������'�• ��������������������������������'�• �� ����������������������������������'�

�import java.util.Random; class Tabela { //(1) preenche uma tabela public static void preenche(int tabela[]) { Random random = new Random(); for (int i=0; i < tabela.length; i++) tabela[i] = random.nextInt(100); } //(2) troca as posições de dois elementos da tabela public static void troca(int tabela[], int x, int y){ int tmp; tmp = tabela[x]; tabela[x] = tabela[y]; tabela[y] = tmp; } //(3) Imprime a tabela public static void imprime(int tabela[]) { for (int i=0; i < tabela.length; i++) System.out.println(" "+i+": "+ tabela[i]+" ; "); System.out.println(); }

// O método main vai variar para cada implementação }

Page 133: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

11.2 - BubbleSort

������������������������E�• ���������� ��� ���������� �� �������K� ��� ������� ���������� ��F�������� �� ��������� ��� ����� ���

����������D ����������������0������������'�• -�,�D ���������H���0������������ ����������� ��������D �������H�F�����H������������� ���A ��'�

�<���� D ��� �� ������� �������� ��� ��������� D ������ ��� �0������ ���� �������� ��� ����� ��� D ��� ���H��� ��� ����������������������� �����'����������������������� ���� �������� �������F�������������������������������������'��@ ���� ������3 �������,� �� ������������K��������,����������������������������������������'��$���������������������������K���������� ����"������������� � � � ��� � �E��������=����������)���������� ���������������������������������� ��������������)��")����������D ��� ��������������������������� ��������H��)������������ �����'��

������� ����������������R�0�������.�#9�/�##������.�#9�/�##��������.�/�#9�##���:������R����;���.�/�##�#9���:������R����;���.�/�##���#9�:������R����;���������� ����������������R�0�������.�/�##���#9����/�.�##���#9�:������R����;���/�.�##���#9���/�.���##�#9�:������R����;���/�.���##�#9�

��������� ����������������R�0�����/���.���##�#9�:������R����;�/���.���##�#9�/�����.�##�#9�:������R����;�/�����.�##�#9�/�����.�##�#9��I������ ����������������R�0�����/�����.�##�#9�/�����.�##�#9�:������R����;�

�� .� #9� /� ##� ��

�� #� �� ��/� !� t�����

Page 134: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

/�����.�##�#9�/�����.�##�#9�/�����.�##�#9��I����� ����������������R�0�������/���.�##�#9�:������R����;���/���.�##�#9���/���.�##�#9���/���.�##�#9���/���.�##�#9����3 ��� ����������������R�0�������/���.�##�#9���/���.�##�#9���/���.�##�#9���/���.�##�#9���/���.�##�#9��������RR�0�����G�������P�

��@ ��?�������� ������������������������% ����������,�� �������������������E��public class OrdenacaoBubbleSort { public static void bubbleSort(int tabela[ ] ) { boolean houveTroca; do { houveTroca = false; for (int j = 0; j < tabela.length-1; j++) if (tabela[j] > tabela[j+1]){ Tabela.troca(tabela, j, j+1); houveTroca = true; } }while (houveTroca); �����f�� public static void main(String[] args) { int tabela[] = new int[100]; Tabela.preenche(tabela); //preenche tabela Tabela.imprime(tabela); //imprime tabela antes da ordenação bubbleSort(tabela); //ordena tabela Tabela.imprime(tabela); //imprime tabela depois de ordenada } }

Page 135: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

11.3 - ShellSort

-� ���������� � � ���� � �� ,� ���� )������� ��� ���������� � � � � ��� � �� �� �������� ��� ��� ����� �� ������K� ���� ������������F�������K�������������������� ������� ����������������)���'��$��������)����������������� ��������������� ���������������������������'�- ?�� ���� ���������� ��� ����K� ��� � �� � � � � ��� � �K� ���� �� ����)���� ����K� ����� ����)���� ,� �)���� ��� ���� �� �� ���������� ��� ��� ��������)������)���'������� ���������� ��� �����,�D ���������)������F��#�:������ ������������������������ ���� � � � ��� � �;'��<���� D ����������������H��������,������0������:�����" ��;���� D ��������������� � � � � ��� � �� ��D ������ ������� �������������������� ����� ���������F������������������� ��������������� � ������ ��������� F"�������������� ��������������������'��$��������������������D ���0������������3 �� ������� � � � ��� � �E������=�F��������)���������� ���������������������������������� ��������K����)��")����������D ��� ������������������������ ��� �������� H��)�� ������� ��� ������ �� ��� )��")��� ����)���� D ��� ��0��� �� ���8 ���� ������ ������������� ������'���

���������#�������)���R/�������R5�����%�.�#9�E�##���:������R����;��/�Q�#9���""����/�.�$���##�"P �:������R����;�������������������)���R/�������R5������%�.���E�##�#9��/�Q�����""�#9��/�.�$���##�"P �����������/������)���R#�������R5������/�.�����##�#9��/�$�Q���##�#9�:������R����;��/���E�Q�##�#9�:������R����;��/�����.�##�#9��/�����.�##�#9��������������

�� .� #9� /� ##� ��

�� #� �� ��/� !� t�����

Page 136: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����#/+�

�����)���R#�������R5������$�%���.�##�#9�:������R����;����/���.�##�#9����/���.�##�#9����/���.�##�#9����/���.�##�#9�����������!�������)���R#�������R5��������/���.�##�#9����/���.�##�#9����/���.�##�#9����/���.�##�#9����/���.�##�#9�

�@ ��?�������� ������������������������ � ���� � ��,�� �������������������E� public class OrdenacaoShell { public static void shell (int tabela[ ] ) { boolean nenhumaTroca; int intervalo; intervalo = tabela.length / 2; do { do { nenhumaTroca = true; for (int j = 0; j < tabela.length-intervalo; j++) if (tabela[j] > tabela[j+intervalo]) { Tabela.troca(tabela, j, j+intervalo); nenhumaTroca = false; } }while (!nenhumaTroca); intervalo = intervalo / 2; }while ( intervalo > 0); } public static void main(String[] args){ int tabela[] = new int[100]; Tabela.preenche(tabela); //preenche tabela Tabela.imprime(tabela); //imprime tabela antes da ordenação shell(tabela); //ordena tabela

Page 137: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����#/9�

Tabela.imprime(tabela); //imprime tabela depois de ordenada } }

11.4 - Ordenação por selecção

-����������� ����������������������� ���������������������������������K��������� �������K���������������������������� ��������������E��

o <��#b� �������������� ��������������������������� ��������������'�o <���b� �������������� ������������������'�o �������������)������'�

�$�������������)����������������D ���0������������3 �� ������� � � � ��� � �E������=�F��������)���������� ���������������������������������� ��������E���

3 �������� � ���ME�Q�"P �%�""�$N��%�.�#9�E�##���$�.�#9���##�%��)�� ����� � ���M$�Q�"P �E�""�%N�$�.�#9���##�/�$�E�#9�Q�##�/�$�%�#9�.�##�E��� ��������� � ���M$�%�"P �Q�""�EN���$�%�Q�"P �##���$�%�E�#9�##�Q��J ����� � ���M$�%�E�"P �""�QN���$�%�E�""�"P �.�$�%�E�Q�#9�""��J ������ � ���M$�%�E�Q�"P �""N���$�%�E�Q�""�"P ��

�@ ��?�������� �����������������������,�� �������������������E� public class OrdenacaoSeleccao { public static void seleccao (int tabela[ ] ){ for (int j = 0; j < tabela.length-1; j++) for (int k = j+1; k < tabela.length ; k++) if (tabela[j] > tabela[k]) Tabela.troca(tabela, k, j); }

�� .� #9� /� ##� ��

�� #� �� ��/� !� t�����

Page 138: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����#/.�

public static void main(String[] args) { int tabela[] = new int[100]; Tabela.preenche(tabela); //preenche tabela Tabela.imprime(tabela); //imprime tabela antes da ordenação seleccao(tabela); //ordena tabela Tabela.imprime(tabela); //imprime tabela depois de ordenada } }

11.5 - Algoritmo QuickSort

[�������������������)��������������,�������� ��������� � �� � ��P��

o �����H��������������������"���:3 ;�����������:�H������ )��;��o ���������)����

� )�������������������������� ���:��D ��������������;'�� @ ���� ����D ������,���� ����� �������������������������������D �������������� )��'�� @ ���� ��������,���� ����� ����������������������D �������������� )��'�� @ ���������� )���,�������������������������� ��'��

o ������* �����)��� @ ����������������� �����D �������������K�����������������,�����������)�������

�* ����)���������� ���������)���E�

�• ��F������������)����������������'�• ��F��3 ������������ )���������������������������:3 �R�)q�B�r;'�������"�����������6���������������0�������E��• �����6����R����:4��������#`� ���������������;'�• �����6���F�R�� #��:4��������� ����� ���������������;'�• ��D �������≤�FK�0�6��E�

� ��D ������)qr�U�3 ��:,���� ���≤�� #;K��������������� ��D ������)qFr�X�3 ��:,���� ���F≥��;K�������������F��� ����≤�FK��������)qr�����)qFrK���������������������������F��

• @ ���� ����D ������:����)�������≤�3 ;�,�)q�rK�'''K�)qFr���:)�6�����FU�;�• @ ���� ��������:����)�������≥�3 ;�,�)qrK�'''K�)q� #r���:)�6�����X� #;�

�@ ���� ������3 �������,� �� �����������

��S��������

�������,����������������������������������������'��-� ���� L��� ����������M� ��� ��"�� ��� ��������� 6 � �� $ � � �� ,� D ��� ,� ���� �" ��� �������� ����� ��D �&����� ��� �78 ��������������D ���������D �&������������������'�

Page 139: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

� ���������� ������������������������� �������� ���!� � �"����#/>�

�=���������������������3 �� ������ ���������)���'��

�5������>' ����������)����

�@ ��?�������� �����������������������,�� �������������������E� public static void main(String[] args) { int tabela[] = new int[100]; Tabela.preenche(tabela); //preenche tabela Tabela.imprime(tabela); //imprime tabela antes da ordenação quickSort(tabela, 0, tabela.length); //ordena tabela Tabela.imprime(tabela); //imprime tabela depois de ordenada } public static void bubbleSort(int tabela[ ] ) { int k;; do { k = 1; for (int j = 0; j < tabela.length-k; j++) if (tabela[j] > tabela[j+1]) Tabela.troca(tabela, j, j+1); } k++;

3 1 2 5 4 6 9 8 7

8 9 2 5 4 6 1 3 7

(�� 8 9 2 5 4 6 1 3 7 � F�x=4

3 9 2 5 4 6 1 8 7

� F�

3 1 2 5 4 6 9 8 7 � F�

3 1 2 4 5 6 9 8 7 �F�

U�

≥≥≥≥�� �≤≤≤≤�� �

:/'/;�

:/'/;�

��:/'#T/'�;�� F�

:/'/;�

� F��

��:/'#T/'�;��

Page 140: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

} while (k < tabela.length); }

11.6 - Exercícios práticos

�3 ���'#� � � �� ������������,����� D ��� ������������ ��8 ���������� ���������� ��������� ���������� ����������������������������������% �������������3 ���'�� � � �� �������� ����,����� D ��� ������� ����� ��8 ������ ���� ������� ��� ������� �� �������� ���������� ���������������'�C ����������������������������'���3 ���'/�� �5��������,�����D ��������������� ��8 ����������������������F���������� ��0�����"���:��������� �����0����;�������������������������������'�C ����������������H�����������3 ���'��� ��� ����������� ���������D ������������������������������������������������������������������'����3 ���'!�� ��� ����������� ��������D ��E�

#' �������������������������?)���:(���������'`�������;��' ���������������������������������/' @ ��������������� ��������K������������,�����D ��J �����'��' - ��������������������������������������'��

�3 ���'+� � C ��� ��F�� �������� �� ��� ��� ����� ������� ��� )����"��� �������� �����'� ����� � ���� ������ ��� 0�6��� ���� �D ������������������ ������������ �������������������F�K�������������������������0���������������������������������� ���������������K����������������������������F����:�������K������F�K�������H�K���F�K�)����K��6��K�)�����H�K���6������� ����;'��� �����������,�����D ��� ������������������������������F������-����=�������������������������������������F���

public class ArtigoVestuario { public int cod; public String artigo, cor; …

}

Page 141: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

12 - Anexo I – Classe Le.java

� import java.io.*; import java.awt.*; import java.awt.event.*; /** * Classe simples para ler dados de entrada (pela entrada padrão ou através de uma janela gráfica. * @versão 2 - 20 Jan 2002 * @autor: Paulo Marques ([email protected]) */ public class Le { private static final int BUF_SIZE = 1024; // Inibe o constructor por defeito private Le() { } /** * Le um inteiro da entrada padrão. * A entrada é terminada com um return. Se a entrada não for válida é mostrada a mensagem * "!!! Não é um inteiro !!!" e o utilizador pode tentar de novo. * * @devolve o número lido */ public static int umInt() { while(true) { try { return Integer.valueOf(umaString().trim()).intValue(); } catch(Exception e) { System.out.println("!!! Não é um inteiro !!!"); } } } /** * Le um double da entrada padrão. * A entrada é terminada com um return. Se a entrada não for válida é mostrada a mensagem * "!!! Não é um double !!!" e o utilizador pode tentar de novo. *

Page 142: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

* @devolve o número lido */ public static double umDouble() { while(true) { try { return Double.valueOf(umaString().trim()).doubleValue(); } catch(Exception e) { System.out.println("!!! Não é um double !!!"); } } } /** * Le um float da entrada padrão. * A entrada é terminada com um return. Se a entrada não for válida é mostrada a mensagem * "!!! Não é um float !!!" e o utilizador pode tentar de novo. * * @devolve o número lido */ public static float umFloat() { while(true) { try { return Float.valueOf(umaString().trim()).floatValue(); } catch(Exception e) { System.out.println("!!! Não é um float !!!"); } } } /** * Le uma String da entrada padrão. * A entrada é terminada com um return. * * @devolve a String lida */ public static String umaString() { String s = ""; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in), 1);

Page 143: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

s = in.readLine(); } catch (IOException e) { System.out.println("Error reading from the input stream."); } return s; } /** * Le um caracter da entrada padrão. * A entrada é terminada com um return. * * @devolve o caracter lido */ public static char umChar() { char c=' '; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in), 1); c = (char)in.read(); } catch (IOException e) { System.out.println("Error reading from the input stream."); } return c; } /** * Le um inteiro usando uma janela gráfica. * A entrada é terminada quando o botão Ok é seleccionado * * @devolve o número lido */ public static int grafUmInt(String msg) { ReadIntFrame intFrame = new ReadIntFrame(msg); return intFrame.getValue(); } /** * Le um inteiro usando uma janela gráfica. * A entrada é terminada quando o botão Ok é seleccionado * * @devolve o número lido */ public static double grafUmDouble(String msg) { ReadDoubleFrame doubleFrame = new ReadDoubleFrame(msg); return doubleFrame.getValue();

Page 144: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

} } /** * Classe auxiliar que lê inteiros numa janela * @versão 1.0 - 13 Jan 1999 * @autor Paulo Marques ([email protected]) */ class ReadIntFrame implements ActionListener { /** * Construtor. * @parâmetro msg - a mensagem a aparecer como título. */ public ReadIntFrame(String msg) { this.msg = new String(msg); valid = false; window = new Frame(); window.setTitle(msg); window.setLayout(new FlowLayout()); valueArea = new TextField("", 15); Button done = new Button(" Ok "); done.addActionListener(this); window.add(valueArea); window.add(done); window.setSize(320, 100); window.setBackground(Color.gray); window.setLocation(100, 100); window.setResizable(false); window.show(); } /** * Metodo chamado quando o Ok é seleccionado. */ public synchronized void actionPerformed(ActionEvent e) { try { value = Integer.valueOf(valueArea.getText().trim()).intValue(); valid = true; window.dispose(); notify(); } catch (Exception exc) { window.setTitle("!!! Não é um inteiro !!! --- " + msg); }

Page 145: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

} /** * Devolve o valor fornecido pelo utilizador. */ public synchronized int getValue() { try { if (!valid) wait(); } catch (Exception e) { } return value; } private boolean valid; private int value; private String msg; private Frame window; private TextField valueArea; } /** * Classe auxiliar que lê doubles numa janela. * @version 1.0 - 13 Jan 1999 * @author Paulo Marques ([email protected]) */ class ReadDoubleFrame implements ActionListener { /** * Constructor. * @parâmetro msg - a mensagem a aparecer como título. */ public ReadDoubleFrame(String msg) { this.msg = new String(msg); valid = false; window = new Frame(); window.setTitle(msg); window.setLayout(new FlowLayout()); valueArea = new TextField("", 15); Button done = new Button(" Ok "); done.addActionListener(this); window.add(valueArea); window.add(done); window.setSize(320, 100); window.setBackground(Color.gray); window.setLocation(100, 100);

Page 146: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

window.setResizable(false); window.show(); } /** * Metodo chamado quando o Ok é seleccionado. */ public synchronized void actionPerformed(ActionEvent e) { try { value = Double.valueOf(valueArea.getText().trim()).doubleValue(); valid = true; window.dispose(); notify(); } catch (Exception exc) { window.setTitle("!!! Não é um double !!! --- " + msg); } } /** * Devolve o valor fornecido pelo utilizador. */ public synchronized double getValue() { try { if (!valid) wait(); } catch (Exception e) { } return value; } private boolean valid; private double value; private String msg; private Frame window;

private TextField valueArea;

Page 147: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

13 - Anexo II – Glossário

�� �����@ �� ���0������ � ����������� ���� ��� ���)���� ���������� ������ �����"��� ���� � ����� ��� D ��� ��� ������������������"�������8 ������������������������������������������'�-�� ���)������������� ������������ �����������0��,���'���#���� ��E����F�����������A ����������� ������0�����0����������������������������������������� �������'����� �����E�$��F��������������A ���D ��������)������ �������'����#�� �����E��3 ���������F�����0��������� ��������������������� �������������3 ������ D ������"� ������� :�����F�K� D ��� �� ����� ��� ����������� ����� �0������ ���� ��� D ��� ��� ����H�� �� �����K� ������� ��)��� �� ������������0�������;'��� ���B& �E�$������������������������� ������������������������� �D �����'��' � ��� ������E�$?����������� ���� ����������'��' � ��� ���� ����(��E�* ��������������� ����������?����0����'��' ����#��E�$���'��' ��������E����������D ������������ ���������?��������������������������������������� ����������'�* �������?����0�������������������������������� ����6��?�����3 ����")��'��' ��������E��������������������?�����3 ����")����� ���������?����0������������������������ �����'��! �(���B���B' ��, ���E� (���������� ��� 0����� ��� ���������� ��� ��������� D ��� ��)��)�� ����� ��� �������� ������ ������������� �D �����K���F��������A ��������������� ���������������������� ��������������'��� ��E������������D �������������������)��")����������������)�����������'�@ ����������������F�����,������F��������)������������������������'�<��D ����6���� ��������� �������K��������������������D ��������� ��������)������������������D �������������3 ���K�������������0����D ���,���)������������������� ?�� ������ ����0������������0�������'��L ��H ��E�$�� ��������04�������������� �������:�����K���������K��������K����';��+����������E�$?���� ����6��� ������ �������������D ����� �������������������� ���4)��� ������������������ �������'��- ��� �� ��� ��� 3 ��� �����E� 1 �������� ���0���� ���6���� ���� ��)��� �� ����� �� � ����������� ��� ��������� ������ ������'����, ���� 0 ����E�<�� �"����,������� ������� ������������������ �? ��� ��� �����������),�������0�_ ���� �? ��������� ���� ����� �0���'� $�� ���� ��� ����� ��� 0����� ��� ��� ������� ��� �������'� -� �"D ���� )������ 2 �)�� ��������������������������3 ���������� ���������2 �)������ ������'��� ����E�(��������D ������)�� ������0�����0���������������������� ������0�������'������3 �� ��E���% <5����)�� ������0�������"����� ����)������� � ������ ���������K� �����������o(1 ����)�� ������0��� 0���������������K�����4�����������������)��� ������0����� ��������� ��������K����K����'��

Page 148: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

<������ E�������������)��)��������������������� ������'��J �������E�����������)������� � ��'��3 ������ ��� �E� 0����� �� �0����� ��� ������)��� ��� ��������� ����������� �� ���)���� �H�)�� �����H������ ��� �������������������K�����D ���������������������������H����������� �������������� ������'��)� ������� �������E���D ���������� ���������������������������D ��������������),����������� ������4����'�% ���������K�,������������������ ���������������� �����'��)� ���<����(�E���������������� �����������������������A �������� ��3 �����D ���,���� ���")��������?� ������������ ��� �����H��� ��� H���_ ���� ���� ���6������� �� ������������K� ����� ����,�� ���� ��� ����� �� ���?����� ��4)��� ���������� ������: ������������3 ������K� ����,���H��������� �������;�����������3 ���������������������� ����A ��K�����������������������������������������"D ����K����'��)��H ��E� ��������� ��� ��� ������'� * � �������� �� ����� �?���K� ��� �����"��� ��� H���_ ���� D ��� �� �������� �� �����04������������� ������'��)�#���� ��E�����,���H���������� ������� :����,����������������� ������������������������F�����������,�����������2 �)�;K����� �D ������ ���������D ��� ����������H����������H������������ ��������� ����D ���������F������������������������������������������ ��������������� �������'��� ��B! �H �E� $���������� ��� ���� �������� ��� D ��� �� ����� ��� �������� ����� ��� ������ �� ��������� ��� �4)��� � ���� ��0������������� ���������4)��������,���K������F�K���� ������������� �D �����'��

Page 149: ltodi.est.ips.ptltodi.est.ips.pt/ffidalgo/fich/Sebenta.pdf · ˇ ˆ ˙ ˝ ˛˚˚˜ ˛˚˚! " ˛ 4.3 - $ Math Random

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

��

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

14 - Anexo III - Bibliografia

�� 5�����������������������������2 �)���G�-��?���(�����K�(����2 ��,�(�������K�5$-�� 2 �)����0�_ ������������K�2 �H��1 �_ �K�-������^ ����Z �� ���������������2 �)���G�������$���H�K�5$-�� �������������������������C ��6�����������K���)���(�����K�(�7 ��_ ] ���� �H��2 �)�������������1 ��������G�\ ���-�����K�2 �����7 ������'�-������^ ����Z �� -���������������������������)�������B���#���,����/B�����