giáo trình Java By Example phần 6
Số trang: 66
Loại file: pdf
Dung lượng: 168.67 KB
Lượt xem: 22
Lượt tải: 0
Xem trước 7 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu giáo trình java by example phần 6, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 6Of course, there are rules for choosing constant and variable names (also known as identifiers becausethey identify a program object). You cant just type a bunch of characters on your keyboard and expectJava to accept them. First, every Java identifier must begin with one of these characters: A-Z a-z _ $The preceding characters are any uppercase letter from A through Z, any lowercase letter from a throughz, an underscore, and the dollar sign.Following the first character, the rest of the identifier can use any of these characters: A-Z a-z _ $ 0-9As you may have noticed, this second set of characters is very similar to the first. In fact, the onlydifference is the addition of the digits from 0 through 9. NOTE Java identifiers can also use Unicode characters above the hexadecimal value of 00C0. If you dont know about Unicode characters, dont panic; you wont be using them in this book. Briefly put, Unicode characters expand the symbols that can be used in a character set to include characters that are not part of the English language.Using the rules given, the following are valid identifiers in a Java program: number number2 amount_of_sale $amountThe following identifiers are not valid in a Java program: 1number http://www.ngohaianh.info amount of sale &amount item#Example: Creating Your Own IdentifiersSuppose that youre now ready to write a program that calculates the total number of parking spaces leftin a parking garage. You know that the total number of spaces in the garage is 100. You further knowthat the vehicles in the garage are classified as cars, trucks, and vans. The first step is to determine whichvalues would be good candidates for constants. Because a constant should represent a value thats notlikely to change from one program run to another, the number of vehicles that the garage can hold wouldmake a good constant. Thinking hard (someone smell wood burning?), you come up with an identifier ofTOTALSPACES for this value. In Java, the constants definition looks like this: final int TOTALSPACES = 100;In this line, the keyword int represents the data type, which is integer. You should be able to understandthe rest of the line.Now, you need to come up with the mathematical formula thatll give you the answer you want. First,you know that the total number of vehicles in the garage is equal to the sum of the number of cars,trucks, and vans in the garage. Stating the problem in this way not only clarifies what form thecalculation must take, but also suggests a set of good identifiers for your program. Those identifiers arecars, trucks, vans, and total_vehicles. So, in Java, your first calculation looks like this: total_vehicles = cars + trucks + vans;The next step is to subtract the total number of vehicles from the total number of spaces that the garageholds. For this calculation, you need only one new identifier to represent the remaining spaces, which isthe result of the calculation. Again, stating the problem leads to the variable name, which might beremaining_spaces. The final calculation then looks like this: remaining_spaces = TOTALSPACES - total_vehicles; http://www.ngohaianh.infoData TypesIn attempting to give you a quick introduction to constants and variables, the preceding sections skippedover a very important attribute of all constants and variables: data type. You may remember mymentioning two data types already, these being floating point (represented by the float keyword) andinteger (represented by the int keyword). Java has eight different data types, all of which representdifferent kinds of values in a program. These data types are byte, short, int, long, float,double, char, and boolean. In this section, youll learn what kinds of values these various data typesrepresent.Integer ValuesThe most common values used in computer programs are integers, which represent whole number valuessuch as 12, 1988, and -34. Integer values can be both positive or negative, or even the value 0. The sizeof the value thats allowed depends on the integer data type you choose. Java features four integer datatypes, which are byte, short, int, and long. Although some computer languages allow both signedand unsigned integer values, all of Javas integers are signed, which means they can be positive ornegative. (Unsigned values, which Java does not support, can hold only positive numbers.)The first integer type, byte, takes up the least amount of space in a computer ...
Nội dung trích xuất từ tài liệu:
giáo trình Java By Example phần 6Of course, there are rules for choosing constant and variable names (also known as identifiers becausethey identify a program object). You cant just type a bunch of characters on your keyboard and expectJava to accept them. First, every Java identifier must begin with one of these characters: A-Z a-z _ $The preceding characters are any uppercase letter from A through Z, any lowercase letter from a throughz, an underscore, and the dollar sign.Following the first character, the rest of the identifier can use any of these characters: A-Z a-z _ $ 0-9As you may have noticed, this second set of characters is very similar to the first. In fact, the onlydifference is the addition of the digits from 0 through 9. NOTE Java identifiers can also use Unicode characters above the hexadecimal value of 00C0. If you dont know about Unicode characters, dont panic; you wont be using them in this book. Briefly put, Unicode characters expand the symbols that can be used in a character set to include characters that are not part of the English language.Using the rules given, the following are valid identifiers in a Java program: number number2 amount_of_sale $amountThe following identifiers are not valid in a Java program: 1number http://www.ngohaianh.info amount of sale &amount item#Example: Creating Your Own IdentifiersSuppose that youre now ready to write a program that calculates the total number of parking spaces leftin a parking garage. You know that the total number of spaces in the garage is 100. You further knowthat the vehicles in the garage are classified as cars, trucks, and vans. The first step is to determine whichvalues would be good candidates for constants. Because a constant should represent a value thats notlikely to change from one program run to another, the number of vehicles that the garage can hold wouldmake a good constant. Thinking hard (someone smell wood burning?), you come up with an identifier ofTOTALSPACES for this value. In Java, the constants definition looks like this: final int TOTALSPACES = 100;In this line, the keyword int represents the data type, which is integer. You should be able to understandthe rest of the line.Now, you need to come up with the mathematical formula thatll give you the answer you want. First,you know that the total number of vehicles in the garage is equal to the sum of the number of cars,trucks, and vans in the garage. Stating the problem in this way not only clarifies what form thecalculation must take, but also suggests a set of good identifiers for your program. Those identifiers arecars, trucks, vans, and total_vehicles. So, in Java, your first calculation looks like this: total_vehicles = cars + trucks + vans;The next step is to subtract the total number of vehicles from the total number of spaces that the garageholds. For this calculation, you need only one new identifier to represent the remaining spaces, which isthe result of the calculation. Again, stating the problem leads to the variable name, which might beremaining_spaces. The final calculation then looks like this: remaining_spaces = TOTALSPACES - total_vehicles; http://www.ngohaianh.infoData TypesIn attempting to give you a quick introduction to constants and variables, the preceding sections skippedover a very important attribute of all constants and variables: data type. You may remember mymentioning two data types already, these being floating point (represented by the float keyword) andinteger (represented by the int keyword). Java has eight different data types, all of which representdifferent kinds of values in a program. These data types are byte, short, int, long, float,double, char, and boolean. In this section, youll learn what kinds of values these various data typesrepresent.Integer ValuesThe most common values used in computer programs are integers, which represent whole number valuessuch as 12, 1988, and -34. Integer values can be both positive or negative, or even the value 0. The sizeof the value thats allowed depends on the integer data type you choose. Java features four integer datatypes, which are byte, short, int, and long. Although some computer languages allow both signedand unsigned integer values, all of Javas integers are signed, which means they can be positive ornegative. (Unsigned values, which Java does not support, can hold only positive numbers.)The first integer type, byte, takes up the least amount of space in a computer ...
Tìm kiếm theo từ khóa liên quan:
lập trình Java tin học ứng dụng lập trình windows lập trình C# mẹo hay cho tin học thủ thuật windowsGợi ý tài liệu liên quan:
-
Tài liệu bồi dưỡng giáo viên sử dụng SGK Tin học 10 Cánh diều (Định hướng Tin học ứng dụng)
61 trang 216 0 0 -
101 trang 196 1 0
-
20 trang 179 0 0
-
Cách gỡ bỏ hoàn toàn các add on trên Firefox
7 trang 166 0 0 -
Giáo trình Mạng máy tính (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
189 trang 163 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 157 0 0 -
bảo mật mạng các phương thức giả mạo địa chỉ IP fake IP
13 trang 154 0 0 -
Giáo trình Tin học ứng dụng: Phần 1 - Trường ĐH Tài nguyên và Môi trường Hà Nội
125 trang 145 0 0 -
Bài giảng Tin học ứng dụng: Kiểm định trung bình - Trường ĐH Y dược Huế
25 trang 138 0 0 -
Giáo trình Quản trị mạng (Nghề: Tin học ứng dụng - Trung cấp) - Trường Cao đẳng Cộng đồng Đồng Tháp
173 trang 120 1 0