1. Trang chủ
  2. » Khoa Học Tự Nhiên

Using Classes and Objects

29 397 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 29
Dung lượng 541,5 KB

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

Nội dung

Last Time: Creating Objects The GregorianCalendar Class The String Class Wrapper Classes Packages Math Class Formatting Output... Class Libraries• A class library is a collection of clas

Trang 1

Chapter 3

Using Classes and Objects

Trang 2

Last Time:

Creating Objects The GregorianCalendar Class The String Class

Wrapper Classes

Packages Math Class Formatting Output

Trang 3

Class Libraries

• A class library is a collection of classes that we

can use when developing programs

• The Java standard class library is part of any Java

development environment

• Its classes are not part of the Java language per

se, but we rely on them heavily

• Various classes we've already used (System ,

Scanner, String) are part of the Java standard

class library

• Other class libraries can be obtained through third

party vendors, or you can create them yourself

Trang 4

• The classes of the Java standard class library are

organized into packages

• Some of the packages in the standard class library

Network communication Utilities

XML document processing

Trang 5

The import Declaration

• When you want to use a class from a package, you

could use its fully qualified name

java.util.Scanner

• Or you can import the class, and then use just the

class name

import java.util.Scanner;

• To import all classes in a particular package, you

can use the * wildcard character

import java.util.*;

Trang 6

The import Declaration

• All classes of the java.lang package are

imported automatically into all programs

• It's as if all programs contain the following line:

import java.lang.*;

• That's why we didn't have to import the System or

String classes explicitly in earlier programs

• The Scanner class, on the other hand, is part of

the java.util package, and therefore must be imported

Trang 7

Packages Math Class Formatting Output Enumerated Types

Trang 8

The Math Class

• The Math class is part of the java.lang package

• The Math class contains methods that perform

various mathematical functions

Trang 9

The Math Class

• The methods of the Math class are static methods

(also called class methods)

• Static methods can be invoked through the class

name – no object of the Math class is needed

value = Math.cos(90) + Math.sqrt(delta);

• See Quadratic.java (page 129)

Trang 10

Math Methods

abs(x) Returns the absolute

value of x

any numeric type same as

argument ceil(x) Returns smallest

Trang 11

Math Methods

max(x,y) Returns the larger of x

pow(x,y) Returns x y An error

will occur if x = 0 and y

<=0, or x < 0 and y is not a whole number

any numeric type double

random() Returns a

pseudorandom number between 0.0 and 1.0

Trang 12

Math Methods

rint(x) Returns the closest

whole number to x

round(x) Returns the integer

value closet to x

double or float long or int

sqrt(x) Returns the positive

square root of x for

x > 0.0

Trang 13

4 

GM p

10 m m

Trang 14

• radius of a circle of latitude

• distance between two points

• Simulating the rolling of a die – random number between 1 and 6

r Θ rLat

2 1

dist    

Trang 15

Packages Math Class Formatting Output Enumerated Types

Trang 16

Formatting Output

• It is often necessary to format values in certain

ways so that they can be presented properly

• The Java standard class library contains classes

that provide formatting capabilities

• The NumberFormat class allows you to format

values as currency or percentages

• The DecimalFormat class allows you to format

values based on a pattern

• Both are part of the java.text package

Trang 17

Formatting Output

• The NumberFormat class has static methods that

return a formatter object

getCurrencyInstance() getPercentInstance()

• Each formatter object has a method called format

that returns a string with the specified information

in the appropriate format

Trang 18

See Purchase.java (page 131)

fmt1.format(totalCost));

Enter the quantity: 3 Enter the unit price: 1.50 Subtotal: $4.50

Output

Trang 19

Formatting Output

• The DecimalFormat class can be used to format a

floating point value in various ways

• For example, you can specify that the number

should be truncated to three decimal places

• The constructor of the DecimalFormat class

takes a string that represents a pattern for the

formatted number

Trang 20

See CircleStats.java (page 134)

// Round the output to three decimal places

DecimalFormat fmt = new DecimalFormat ("0.###");

System.out.println ( "The circle's area: " +

Trang 21

Packages Math Class Formatting Output Enumerated Types

Trang 22

Enumerated Types

• Java allows you to define an enumerated type,

which can then be used to declare variables

• An enumerated type establishes all possible

values for a variable of that type

• The values are identifiers of your own choosing

• The following declaration creates an enumerated

type called Season

enum Season {winter, spring, summer, fall};

• Any number of values can be listed

Trang 23

• Enumerated types are type-safe – you cannot

assign any value other than those listed

Trang 24

Ordinal Values

• Internally, each value of an enumerated type is

stored as an integer, called its ordinal value

• The first value in an enumerated type has an

ordinal value of zero, the second one, and so on

• However, you cannot assign a numeric value to an

enumerated type, even if it corresponds to a valid ordinal value

Trang 25

Enumerated Types

• The declaration of an enumerated type is a special

type of class, and each variable of that type is an object

• The ordinal method returns the ordinal value of

the object

• The name method returns the name of the identifier

corresponding to the object's value

Trang 26

See IceCream.java (page 137)

//************************************************************* // IceCream.java Author: Lewis/Loftus

enum Flavor {vanilla, chocolate, strawberry, fudgeRipple,

coffee, rockyRoad, mintChocolateChip,

cookieDough}

// Creates and uses variables of the Flavor type.

Trang 27

See IceCream.java (page 137)

System.out.println ();

cone3 = cone1;

System.out.println ();

}

}

Trang 28

See IceCream.java (page 137)

cone1 value: rockyRoadcone1 ordinal: 5

cone1 name: rockyRoad

cone2 value: chocolatecone2 ordinal: 1

cone2 name: chocolate

cone3 value: rockyRoadcone3 ordinal: 5

cone3 name: rockyRoadOutput

Trang 29

In class exercise

• Write an application that creates and prints a

random phone number of the form XXX-XXX-XXXX Include the dashes in the output Do not let the

first three digits contain an 8 or 9, make sure that the second set of three digits is not greater than

742, and that neither the first set or second set of digits begin with a 0 or 1 Hint: Think through the easiest way to construct the phone number Each digit does not have to be determined separately.

Ngày đăng: 14/06/2017, 18:12

TỪ KHÓA LIÊN QUAN

w