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

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

24 50 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 24
Dung lượng 235,81 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 5 - Java syntax and style. This is an important chapter because it explains and contrasts two related aspects of writing code in a high-level programming language: language syntax rules and good programming style.

Trang 1

Java Syntax and Style

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

Trang 2

• Review reserved words and standard names

• Learn the proper style for naming classes,

methods, and variables

• Learn to space and indent blocks of code

Trang 3

Comments

• Comments are notes in plain English inserted

in the source code

• Comments are used to:

 document the program’s purpose, author, revision history, copyright notices, etc.

 describe fields, constructors, and methods

 explain obscure or unusual places in the code

 temporarily “comment out” fragments of code

Trang 4

Formats for Comments

• A “block” comment is placed between /* and

weight *= 2.2046; // Convert to kilograms

Trang 5

Javadoc Comments

• Used by the JDK’s special utility program

javadoc to automatically generate

documentation in HTML format from the

source code

• Should precede a class, a method, or a field

• Can use special javadoc tags:

 @param – describes a parameter of a method

 @return - describes the method’s return value

Trang 6

Common style

Trang 7

Reserved Words

• In Java a number of words are reserved for a special purpose

• Reserved words use only lowercase letters

• Reserved words include:

 primitive data types: int , double , char , boolean , etc.

 storage modifiers: public , private , static , final , etc.

 control statements: if , else , switch , while , for , etc.

 built-in constants: true , false , null

• There are about 50 reserved words total

Trang 8

Programmer-Defined Names

• In addition to reserved words, Java uses standard names for library packages and classes:

String , Graphics , JFrame , JButton ,

java.awt , javax.swing

• The programmer gives names to his or her classes, methods, fields, and variables

Trang 9

Names (cont’d)

• Syntax: A name can include:

 upper- and lowercase letters

 digits

 underscore characters

• Syntax: A name cannot begin with a digit

• Style: Names should be descriptive to

improve readability

Trang 10

Names (cont’d)

• Programmers follow strict style conventions

• Style: names of classes begin with an

uppercase letter, subsequent words are

capitalized:

public class ActorWorld

• Style: names of methods, fields, and

variables begin with a lowercase letter,

subsequent words are capitalized:

private int sideLength;

public void moveTo()

Trang 11

Names (cont’d)

• Method names often sound like verbs:

setBackground, getText, moveForward, stop

• Field names often sound like nouns:

color, steps, button, controlPanel

• Constants often use all caps:

PI, PIXELS_PER_INCH

• It is OK to use short names for temporary

“throwaway” variables:

i, k, x, y, str

Trang 13

Syntax

• The compiler catches syntax errors and

generates error messages

• Text in comments and literal strings within double quotes are excluded from syntax

checking

• Before compiling, carefully read your code a couple of times to check for syntax and logic errors

Trang 14

Syntax (cont’d)

• Pay attention to and check for:

 matching braces { } , parentheses ( ) , and

brackets [ ]

 missing or extraneous semicolons

 correct symbols for operators

+, -, =, <, <=, ==, ++, &&, etc.

 correct spelling of reserved words, library names and programmer-defined names, including

upper/lower case

Trang 15

public static int sqrt (int x) .

Extraneous semicolon

Spelling

(p P,

if If)

Missing semicolon

Trang 16

Style

• Arrange statements on separate lines

• Insert blank lines between fragments of code

• Indent code within braces

• Use comments judiciously: comment

constructors, methods, fields, important steps, but not every statement in the

program

Trang 17

move();

steps++;

} else {

turn();

turn();

steps = 0;

} }

Trang 19

Blocks, Indentation

• Java code consists mainly of declarations and control statements

• Declarations describe objects and methods

• Control statement describe actions

• Declarations and control statements end with

a semicolon

• No semicolon is used after a closing brace

(except in certain array declarations)

Trang 20

Blocks, Indentation (cont’d)

• Braces mark nested blocks

• Braces indicate that the statements within

them form one compound statement.

• Statements inside a block are indented, usually by two spaces or one tab

Trang 21

private static void merge(double[] a,

int from, int middle, int to)

{

int i = from, j = middle + 1, k = from;

while (i <= middle && j <= to)

Method’s body

One compound statement

One compound statement

Trang 22

Review:

• Name as many uses of comments as you can

What does the javadoc program do?

• Explain the difference between syntax and style

• Why is style important?

• Roughly how many reserved words does Java have?

Trang 23

Review (cont’d):

• Explain the convention for naming classes,

methods, and variables

• Which of the following are syntactically valid names for variables: C, _denom_, my.num,

AvgScore, count1, 7seas? Which of them are

in good style?

• What can happen if you put an extra

semicolon in your program?

Trang 24

Review (cont’d):

• What are braces used for in Java?

• Is indentation required by Java syntax or style?

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