Intro to Java Programming(Liang.10th)--02

2.2 Writing a simple program

Algorithms can be described in natural languages or in pseudocode (natural language mixed with some programming code).

ComputeArea

public class ComputeArea {
   public static void main(String[] args) {
     double radius; // Declare radius
     double area; // Declare area
     // Assign a radius
     radius = 20; // radius is now 20
     // Compute area
     area = radius * radius * 3.14159;
     // Display results
     System.out.println("The area for the circle of radius " +
     radius + " is " + area);
   }
 }

concatenate strings

The plus sign (+) has two meanings: one for addition and the other for concatenating (combining) strings.

2.3 Reading input form Console

How to

  1. Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in,as follows:
Scanner input = new Scanner(System.in);

The syntax new Scanner(System.in) creates an object of the Scanner type. The syntax Scanner input declares that input is a variable whose type is Scanner. The whole line Scanner input = new Scanner(System.in) creates a Scanner object and assigns its reference to the variable input.

  1. An object may invoke its methods. To invoke a method on an object is to ask the object to perform a task. You can invoke the nextDouble() method to read a double value as follows:
double radius = input.nextDouble();

specific import && wildcard import

The Scanner class is in the java.util package. It is imported in line 1. There are two types of import statements: specific import and wildcard import. The specific import specifies a single class in the import statement. For example, the following statement imports Scanner from the package java.util.

import java.util.Scanner;

The wildcard import imports all the classes in a package by using the asterisk as the
wildcard. For example, the following statement imports all the classes from the package
java.util.

import java.uitl.*;

no performance difference

2.4 Identifiers

identifier naming rules

  1. An identifier is a sequence of characters that consists of letters, digits, underscores
    (_), and dollar signs ($).
  2. An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot
    start with a digit.
  3. An identifier cannot be a reserved word. (See Appendix A for a list of reserved words.)
  4. An identifier cannot be true, false, or null.
  5. An identifier can be of any length.
    Do not name identifiers with the $ character. By convention, the $ character should be
    used only in mechanically generated source code.
    For example, numberOfStudents is better than numStuds, numOfStuds, or numOfStudents.
    These names also provide a generic tone to the code snippets.

2.5 variables

Variables are used to represent values that may be changed in the program.

2.6

assignment statement

In Java, an assignment statement is essentially an expression that evaluates to the value
to be assigned to the variable on the left side of the assignment operator. For this reason, an
assignment statement is also known as an assignment expression. For example, the following
statement is correct:

System.out.println(x = 1);

which is equivalent to

x = 1;
System.out.println(x);

2.7 Named constants

Declare a constant

final double PI = 3.14159; // Declare a constant

2.8 Naming Conventions

  1. Use lowercase for variables and methods. If a name consists of several words, concatenate them into one, making the first word lowercase and capitalizing the first letter of each subsequent word—for example, the variables radius and area and the method print.
  2. Capitalize the first letter of each word in a class name—for example, the class names
    ComputeArea and System.
  3. Capitalize every letter in a constant, and use underscores between words—for example, the constants PI and MAX_VALUE.

2.9

math.pow(2,3)//2的三次方

2.10

2.10.1 Integer literals

By default, an integer literal is a decimal integer number. To denote a binary integer literal, use a leading 0b or 0B (zero B), to denote an octal integer literal, use a leading 0 (zero), and to denote a hexadecimal integer literal, use a leading 0x or 0X (zero X).
For example,

System.out.println(0B1111); // Displays 15
System.out.println(07777); // Displays 4095
System.out.println(0XFFFF); // Displays 65535

2.10.2 Floating-Point Literals

A float value has 7 to 8 number of significant digits and a double value has 15 to 17 number
of significant digits.

2.10.3 Scientific Notation

why called floating-point?

  1. The float and double types are used to represent numbers with a decimal point.
    Why are they called floating-point numbers? These numbers are stored in scientific notation internally. When a number such as 50.534 is converted into scientific notation,
    such as 5.0534E+1, its decimal point is moved (i.e., floated) to a new position.

underscores in numbers

  1. To improve readability, Java allows you to use underscores between two digits in a
    number literal. For example, the following literals are correct.
    long ssn = 232_45_4519;
    long creditCardNumber = 2324_4545_4519_3415L;
    However, 45_ or _45 is incorrect. The underscore must be placed between two digits

2.11 Evaluating Expressions and Operator Precedence

Java expressions are evaluated in the same way as arithmetic expressions.

2.12 Case Study: Displaying the Current Time

You can invoke System.currentTimeMillis() to return the current time.
The currentTimeMillis method in the System class returns the current time in milliseconds elapsed since midnight, January 1, 1970 GMT. This time is known as the UNIX epoch. The epoch is the point when time starts, and 1970 was the year when the UNIX operating system was formally introduced.
You can use this method to obtain the current time, and then compute the current second,
minute, and hour as follows.

  1. Obtain the total milliseconds since midnight, January 1, 1970, in totalMilliseconds by invoking System.currentTimeMillis() (e.g., 1203183068328 milliseconds).
  2. Obtain the total seconds totalSeconds by dividing totalMilliseconds by 1000 (e.g., 1203183068328 milliseconds / 1000 = 1203183068 seconds).
  3. Compute the current second from totalSeconds % 60 (e.g., 1203183068 seconds % 60 = 8, which is the current second).
  4. Obtain the total minutes totalMinutes by dividing totalSeconds by 60 (e.g., 1203183068 seconds / 60 = 20053051 minutes).
  5. Compute the current minute from totalMinutes % 60 (e.g., 20053051 minutes % 60 = 31, which is the current minute).
  6. Obtain the total hours totalHours by dividing totalMinutes by 60 (e.g., 20053051 minutes / 60 = 334217 hours).
  7. Compute the current hour from totalHours % 24 (e.g., 334217 hours % 24 = 17,which is the current hour)

2.13 Augmented Assignment Operators

The operators +, -, *, /, and % can be combined with the assignment operator to form
augmented operators.

2.14 Increment and Decrement Operators

The increment operator (++) and decrement operator (––) are for incrementing and
decrementing a variable by 1.
i++ postincrement
i-- postdecrement
++i preincrement
–i predecrement

2.15 Numeric Type Conversions

Floating-point numbers can be converted into integers using explicit casting

2.16 Software Development Process

The software development life cycle is a multistage process that includes requirements specification, analysis, design, implementation, testing, deployment, and maintenance.

Developing a software product is an engineering process. Software products, no matter how large or how small, have the same life cycle: requirements specification, analysis, design, implementation, testing, deployment, and maintenance, as shown in Figure 2.3.

At any stage of the software development life cycle, it may be necessary to go back to a previous stage to correct errors or deal with other issues that might prevent the software from functioning as expected

system design

System design is to design a process for obtaining the output from the input. This phase
involves the use of many levels of abstraction to break down the problem into manageable
components and design strategies for implementing each component. You can view each
component as a subsystem that performs a specific function of the system. The essence of
system analysis and design is input, process, and output (IPO).

java.lang

math包

2.17 Case Study: Counting Monetary Units

This section presents a program that breaks a large amount of money into smaller
units

学习体会:

前两章好理解,之前收藏了背包九讲,难理解,可以交替着看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书是Java语言的经典教材,中文版分为基础篇和进阶篇,主要介绍程序设计基础、面向对象编程、GUI程序设计、数据结构和算法、高级Java程序设计等内容。本书以示例讲解解决问题的技巧,提供大量的程序清单,每章配有大量复习题和编程练习题,帮助读者掌握编程技术,并应用所学技术解决实际应用开发中遇到的问题。您手中的这本是其中的基础篇,主要介绍了基本程序设计、语法结构、面向对象程序设计、继承和多态、异常处理和文本I/O、抽象类和接口等内容。本书可作为高等院校程序设计相关专业的基础教材,也可作为Java语言及编程开发爱好者的参考资料。 作者:(美国)粱勇(Y.Daniel Liang) 译者:戴开宇 梁勇(Y.Daniel Liang),现为阿姆斯特朗亚特兰大州立大学计算机科学系教授。之前曾是普度大学计算机科学系副教授。并两次获得普度大学杰出研究奖。他所编写的Java教程在美国大学Java课程中采用率极高。同时他还兼任Prentice Hall Java系列丛书的编辑。他是“Java Champion”荣誉得主,并在世界各地为在校学生和程序员做JAVA程序设计方法及技术方面的讲座。 戴开宇,复旦大学软件学院教师,工程硕士导师。中国计算机学会会员。博士毕业于上海交通大学计算机应用专业,2011~2012年在美国佛罗里达大学作访问学者。承担多门本科专业课程、通识教育课程以及工程硕士课程,这些课程被评为校精品课程,上海市重点建设课程,IBM—教育部精品课程等。
This book is a brief version of Introduction to Java Programming, Comprehensive Version, 8E. This version is designed for an introductory programming course, commonly known as CS1. This version contains the first twenty chapters in the comprehensive version. This book uses the fundamentals-first approach and teaches programming concepts and techniques in a problem-driven way. The fundamentals-first approach introduces basic programming concepts and techniques before objects and classes. My own experience, confirmed by the experiences of many colleagues, demonstrates that new programmers in order to succeed must learn basic logic and fundamental programming techniques such as loops and stepwise refinement. The fundamental concepts and techniques of loops, methods, and arrays are the foundation for programming. Building the foundation prepares students to learn object-oriented programming, GUI, database, and Web programming. Problem-driven means focused on problem-solving rather than syntax. We make introductory programming interesting by using interesting problems. The central thread of this book is on solving problems. Appropriate syntax and library are introduced to support the writing of a program for solving the problems. To support teaching programming in a problemdriven way, the book provides a wide variety of problems at various levels of difficulty to motivate students. In order to appeal to students in all majors, the problems cover many application areas in math, science, business, financials, gaming, animation, and multimedia. 精美文字版pdf电子书。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值