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

Lecture An introduction to computer science using java (2nd Edition): Chapter 3 - S.N. Kamin, D. Mickunas, E. Reingold

25 64 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 25
Dung lượng 318,23 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 3 - Fundamental data types of Java. In this chapter we will: Discuss four important data types: integers, real numbers, strings, characters; describe the process of developing and debugging a Java program.

Trang 1

   

Chapter 3

Fundamental Data Types of Java

Lecture Slides to Accompany

An Introduction to Computer Science

Using Java (2nd Edition)

byS.N Kamin, D Mickunas, E Reingold

Trang 2

   

Chapter Preview

In this chapter we will:

• discuss four important data types

Trang 5

   

Integer Arithmetic Operations

Symbol Operation Example

Trang 6

   

Precedence Rules

1 Evaluate all subexpressions in parentheses

2 Evaluate nested parentheses from the

inside out

3 In the absence of parenthes e s or within

parenthes e s

a Evaluate *, /, or % before + or –

b Evaluate sequences of *, /, and % operators

from left to right

c Evaluate sequences of + and – operators from

left to right

Trang 12

   

Real Arithmetic Operations

Symbol Operation Example

+ Addition 4.50e01 + 5.30e00 =

4.8e00

Trang 14

   

Trang 16

   

String Method Examples

OutputBox out = new OutputBox();

String s1 = “Here is a test string”;

out.println(s1.indexOf(“s”)); // prints 6out.println(s1.indexOf(“x”)); // prints -1

out.println(s1.length()); // prints 22

out.println(s1.substring(8,14));

// prints ‘a test’

Trang 17

   

Trang 19

   

Characters

• Any key you type on the keyboard generates

a character which may or may not be

displayed on the screen (e.g nonprinting

Trang 20

   

Important Literal Characters

‘A’, … ,‘Z’ Uppercase letters

‘a’, … ,‘z’ Lowercase letters

Trang 21

   

Common Debugging Problems

• Misleading compiler error messages

– Syntax errors indicated on one-line may actually reflect an error made on an earlier line

Trang 22

   

Limitations of Numeric Variables

• Unlike the integers mathematics the type int

is not infinitely large and it is possible to

compute a value incorrectly because the

value is too large to be stored in an int

variable storage location

• Unlike the real numbers in mathematics the

type double is not dense, it is not always

possible to test double expressions for

equality and obtain a correct result due to

rounding errors in representations

Trang 23

   

Mixing Numeric Data Types

• Java will automatically convert int expressions to double values without loss of information

int i = 5;

double x = i + 10.5;

• To convert double expressions to int requires a

typecasting operation and truncation will occur

i = (int) (10.3 * x)

• To round-up instead of truncating add 0.5

i = (int) (10.3 * x + 0.5)

Trang 25

   

Characters as Integers

• It is legal to assign a char to an int variable

int i = ‘a’; // assigns 97 to i

• It is legal to assign a n int to an char

variable

char c = 97; // assigns ‘a’ to c

• It is possible to perform arithmetic on char variables

char ch = ‘a’;

ch = ch + 1; // assigns ‘b’ to ch

Ngày đăng: 11/01/2020, 20:03

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN