1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 6 - Maria Litvin, Gary Litvin

32 30 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 32
Dung lượng 260,01 KB

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

Nội dung

Chapter 6 - Data types, variables, and arithmetic. In this chapter, the learning objectives are: Discuss primitive data types; learn how to declare fields and local variables; learn about arithmetic operators, compound assignment operators, and increment/decrement operators; discuss common mistakes in arithmetic.

Trang 1

Data Types, Variables,

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

Trang 2

Objectives:

• Discuss primitive data types

• Learn how to declare fields and local

Trang 3

mov ax,q mov bx,100 sub bx,ax mov q,bx

Trang 4

Variables (cont’d)

• Variables can be of different data types: int,

char, double, boolean, etc

• Variables can hold objects; then the type is the class of the object

• The programmer gives names to variables

• Names of variables usually start with a

lowercase letter

Trang 7

JButton go = new JButton("Go");

String firstName = args[0];

Trang 8

Variables: Scope

• Each variable has a scope —

the area in the source code

where it is “visible.”

• If you use a variable outside its

scope, the compiler reports a

syntax error

• Variables can have the same

name when their scopes

do not overlap

{ int k = ;

}

for (int k = ) {

.

}

Trang 9

Fields

• Fields are declared outside all

constructors and methods

• Fields are usually grouped together,

either at the top or at the bottom of the class

• The scope of a field is the whole class

Trang 10

public class SomeClass

{

}

Fields

Construct ors and methods Scope

Or:

Trang 11

Local Variables

• Local variables are declared inside a

constructor or a method

• Local variables lose their values and are

destroyed once the constructor or the method

is exited

• The scope of a local variable is from its

declaration down to the closing brace of the block in which it is declared

Trang 12

Local Variables (cont’d)

public class SomeClass

Local variable declared

Trang 13

variables, etc.).

Trang 15

Declares a local variable

radius; the value of the field radius remains 0.0

Trang 16

Java Methods

Trang 17

Strings

• String is not a primitive data type

• Strings work like any other objects, with two exceptions:

 Strings in double quotes are recognized as literal constants

 + and += concatenate strings (or a string and a number or an object, which is converted into a string)

"Catch " + 22 "Catch 22"

Trang 19

Symbolic Constants

• Symbolic constants are initialized final

variables:

private final int sideLength = 8;

private static final int BUFFER_SIZE = 1024; public static final int PIXELS_PER_INCH = 6;

Trang 20

Why Symbolic Constants?

• Easy to change the value throughout the program, if necessary

• Easy to change into a variable

• More readable, self-documenting code

• Additional data type checking by the

compiler

Trang 21

Arithmetic

• Operators: +, -, /, * , %

• The precedence of operators and

parentheses is the same as in algebra

• m % n means the remainder when m is

divided by n (for example, 17 % 5 is 2;

2 % 8 is 2)

• % has the same rank as / and *

• Same-rank binary operators are performed in order from left to right

Trang 22

Arithmetic (cont’d)

• The type of the result is determined by the types of the operands, not their values; this rule applies to all intermediate results in

expressions

• If one operand is an int and another is a

double, the result is a double; if both

operands are ints, the result is an int

Trang 23

The double type of

the result doesn’t

help: ratio still gets the value 0.0.

Trang 24

Arithmetic (cont’d)

• To get the correct double result, use double

constants or the cast operator:

double ratio = 2.0 / 3;

double ratio = 2 / 3.0;

int m = , n = ;

double factor = (double)m / (double)n;

double factor = (double)m / n;

double r2 = n / 2.0;

Casts

Trang 25

Arithmetic (cont’d)

int ptsOnDie = (int)(Math.random() * 6) + 1; int miles = (int)(km * 1.61 + 0.5);

Returns a

double

Converts kilometers to miles, rounded to the nearest integer

Trang 27

a = a + 1; a++;

a = a - 1; a ;

Do not use these in larger expressions

Trang 28

From Numbers to Strings

• The easiest way to convert x into a string is to concatenate x with an empty string:

String s = x + "";

• The same rules apply to System.out.print(x)

'A' 123 -1 1 3.14 Math.PI

Trang 29

From Objects to Strings

• The toString method is called:

public class Fraction

Trang 30

• What is meant by the scope of a variable?

• What is the scope of a field?

• What is the scope of a local variable?

Trang 31

Review (cont’d):

• Is it OK to give the same name to variables in different methods?

• Is it OK to give the same name to a field and

to a local variable of the same class?

• What is the range for ints?

• When is a cast to double used?

Trang 32

what is the value of dC?

• When is a cast to int used?

• Should compound assignment operators

be avoided?

Ngày đăng: 04/11/2020, 23:14

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN