1. Trang chủ
  2. » Giáo án - Bài giảng

Giáo trình Java cơ bản 07

39 302 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 39
Dung lượng 537,34 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

 The value is stored in a memory location  The name of the variable is an alias for that memory location within the program... 7/9 Variable declarations  A variable’s type tells the

Trang 2

► Variables

Trang 3

 The value is stored in a memory location

 The name of the variable is an alias for that

memory location within the program

Trang 4

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a temperature in celsius: ");

Trang 5

– Start with a letter or the underscore symbol

– Rest consists of letters, digits or underscore symbol

– Examples

sum, RATE, count, data2, Big_bonus, X_1 height, Height, HEIGHT, _height, bigBonus – The dollar sign is also permitted but it is not a good

idea to use it as it is usually reserved for special

purposes

Trang 6

Identifiers

– Use only letters, starting with a lower case

letter, with words after the first one having an

initial capital

shoeSize

– This convention is sometimes referred to as

camelBack notation

 Java is case sensitive, therefore it

distinguishes between uppercase and

Trang 8

Keywords

 Keywords or reserved words are those that

have special pre-defined meaning

particular, not as the name of a variable)

– Examples

int

if

else

Trang 9

7/9

Variable declarations

 A variable’s type tells the compiler what sort of

information the variable can contain

 The type of every variable must be declared before the variable is used

 We declare a variable by declaring its type,

followed by the variable name

int tempInCelsius;

double totalWeight;

 Variables can also be initialised at declaration

(assign a value to it)

int tempInCelsius = 0;

double totalWeight = 0.07;

Trang 10

► Primitive data types

Trang 11

7/11

Primitive data types

floating-point

Trang 12

Integer data types

positive and negative values)

 4 types of integers in Java that store values

in different amounts of memory

range of values they can store

specific reason for using another type

Trang 13

7/13

Real number data types

 Floating-point number types store numbers with

fractional parts

 2 types of floating-point numbers in Java that store

values in different amounts of memory

 Not all numbers can be stored exactly using point representation

floating- The more memory used, the larger the range of values that can be stored, and the more precise they are

 Use the double type (8 bytes) unless you have a

specific reason for using another type

Trang 14

► Assignment

Trang 15

7/15

Assignment

 The assignment statement is used to set or

change the value of data stored by a

Trang 16

Assignment

 An assignment statement has the form

<variable> = <expression>;

 where the expression on the right is evaluated and

then the resulting value is assigned to (stored in)

the variable on the left

 Example

area = width * height;

bottles = bottles - 1;

Trang 17

7/17

Literals

write its data values in a program

Trang 18

► Operations on primitive

data types

Trang 19

7/19

Operators and operands

operands (arguments) and returns a value

– Most operators are binary operators (i.e have

Trang 20

Arithmetic expressions

Trang 22

► Input & output statements

Trang 26

Summary

the object System.out and its methods print

or println

of things as arguments (numbers,

characters, boolean values, strings,

expressions, etc.)

Trang 27

7/27

Input

– The traditional Java mechanism for reading input from the keyboard is complex: it requires you to

understand about 8 different concepts to read in a single integer

– So we initially use the Scanner class to simplify

the input process

– Examples

numberOfFriends = keyboard.nextInt( );

singleWeight = keyboard.nextDouble( );

Trang 28

public class TemperatureConverter

{

public static void main(String[ ] args)

{

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter a temperature in celsius: ");

double tempInCelsius = keyboard.nextDouble( );

double tempInFahrenheit = (tempInCelsius * 9) / 5 + 32;

Trang 29

7/29

Class exercise

– Write a program to read in a number and write

out the number, its square, and its cube

– Example

 input: 4

 output: 4 16 64

Trang 30

Solution

Trang 31

7/31

► More arithmetic operators

and mathematical functions

Trang 32

Increment and decrement

unary operators

++x increase x by 1, return the new value of x

(increment x and then use it)

x++ increase x by 1, return the old value of x

(use x and then increment it)

x decrease x by 1, return the new value of x

x decrease x by 1, return the old value of x

e.g. y = 10 + x++ (if x is initially 2, y will be 12)

y = 10 + ++x (if x is initially 2, y will be 13)

Trang 34

 For the sake of clarity, we will only use these operators in statements by themselves

int count = 0;

// other statements

count++; // equivalent to ++count and

// count = count + 1 in this case

Increment and decrement

unary operators

Trang 35

7/35

Assignment and arithmetic

assignment operators

= assignment (return the value assigned)

+= add and assign

-= subtract and assign

*= multiply and assign

/ = divide and assign

%= calculate remainder and assign

e.g x += y

may be used instead of x = x + y

Trang 36

Mathematical functions

in the Math class

Trang 38

public class PowerTable

{

// Example from last lecture

public static void main(String[ ] args)

Trang 39

7/39

Next lecture

types

point numbers

Ngày đăng: 24/03/2016, 22:07

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN