Java Program Examp import java.util. Vector import javax. swing. class CurrencyConverter double rateHktoEuro =0.0885: Field (Data Member) double rateHKtoUS =0.128; Declarations double convertEuro2us( double amoun tEuro / details skipped Method declarations double convertUS2HK( double amountS / details skipped K国 Michael Fung, CS&E, The Chinese University of HK 6
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 6 Java Program Example Field (Data Member) Declarations Method Declarations import java.util.Vector; import javax.swing.*; class CurrencyConverter { double rateHKtoEuro = 0.0885; double rateHKtoUS = 0.128; double convertEuro2US ( double amountEuro ) { // details skipped } double convertUS2HK ( double amountUS ) { // details skipped } }
Data Item declaration x Data items are used to store data Such as fields. local variables. constants t We give name(an identifier) to each data item e. g. Ma liu shui, Tai Po road, shatin is a piece of data This information is called CUHKaddress k Each data item must bear a type e.g. CUHKaddress is of type String(text) K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 7 Data Item Declaration Data items are used to store data. – Such as fields, local variables, constants, … We give name (an identifier) to each data item. – e.g. “Ma Liu Shui, Tai Po Road, Shatin.” is a piece of data. This information is called CUHKaddress. Each data item must bear a type. – e.g. CUHKaddress is of type String (text)
Identifier Names introduced by programmers in computer languages are called identifiers e Address he11。Wor1d One2Free just say hi Ring K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK 8
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 8 Identifier Names introduced by programmers in computer languages are called identifiers. – e.g. Address hello_World One2Free j_u_s_t___2___say___hi Ring
Java identifiers t Valid components ABCDEFGHIUKLMNOPORSTUVWXYZ abcdefghijklmnopqrstuvwxyz 1234567890 underscore Space is disallowed x Must start with a letter or underscore e.g. 12Free is disallowed while One 2Free is okay x Case sensitive e.g. Hello is not equal to hello nor HellO K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 9 Java Identifiers Valid components – ABCDEFGHIJKLMNOPQRSTUVWXYZ – abcdefghijklmnopqrstuvwxyz – 1234567890 – _ “underscore” Space is disallowed. Must start with a letter or underscore. – e.g. 12Free is disallowed while One2Free is okay Case sensitive. – e.g. Hello is not equal to hello nor HellO
Choice of Identifiers t easy to understand and remember e.g. noofEnquiries, Trees, timeToLive name, address, count isok, Listi experimentl2 t May use multiple words(without space!) e.g. grade point average, myBirthday numberofstuder七s Dont be laz e.g. i,j,k,a b, c d e K凶心 2008-20092a Michael Fung, CS&E, The Chinese University of HK
2008-2009 2a Michael Fung, CS&E, The Chinese University of HK 10 Choice of Identifiers Easy to understand and remember. – e.g. noOfEnquiries, Trees, timeToLive, name, address, count, isOK, List, experiment12 May use multiple words (without space!). – e.g. grade_point_average, myBirthday, numberOfStudents Don’t be lazy! – e.g. i, j, k, a, b, c, d, e, …