Chapter 1: The way of the program
1.1 What is a programming language
参考: https://blog.youkuaiyun.com/qq_36627886/article/details/80402959
High-level Language and Low-level Language
High-level language:
- programming language
- Portable: can be run on different computer
low-level language:
-
what computer can read
-
only run on one kind of computer
To translate a program (from high-level to low-level)
-
-
interpreting:
- translate line-by-line and carry out commands alternately
- suitable for high-level language
Compiling:
- translate all at once and carry out commands at once
- suitable for low-level language
-
advantage & disadvantage (reference)
-
-
the high-level program is called the source code, and the translated
program is called the object code or the executable.
Advantage of Java
java can translate program into byte code(it is as fast as machine language but protable as high-level language)
1.2 What is a program
- A program is a sequence of instructions that specifies how to perform a computation
- instruction: input, ouput, math, testing, repetition
- programming is the process of breaking tasks up into subtasks that are simple enough to be performed with one of these basic operations.
1.3 What is debugging
bugs: programming error
Debugging: find them and correct them
types of errors
- syntax error
- run-time error
- logic error
experiemental debugging
when program have errors, you modify your program and test your hypthesis about the error types.
1.4 formal and natural language
- formal language: evovle naturally
-
natural language: designed for specific purpose (programming is designed to express computation)
-
difference between them:
- ambiguity
- redundancy
- literalness
1.5 The first program
class Hello {
// main: generate some simple output
public static void main(String[] args) {
System.out.println("Hello, world.");
}
}
class CLASSNAME {
public static void main (String[] args) {
STATEMENTS
}
}
-
Here CLASSNAME indicates a name chosen by the programmer.
-
main is a method, which is a named collection of statements. The name
main is special; it marks the place in the program where execution begins. -
System.out.println is a method provided by one of Java’s libraries. A
library is a collection of class and method definitions.
Chapter 2 Variables
2.1 more printing
-
println() will add a return
-
print() will not
2.2 variables
When you declare a variable, you create a named storage location.
2.3 assignment
When you make an assignment to a variable, you give it a value.
2.4 printing variables
2.5 keywords
There are certain keywords that are reserved in Java because they are used by the compiler to parse the structure of your program, and if you use them as variable names, it will get confused.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html.
2.6 operators
Java is performing integer division.
eg. 59/60 = 0
2.7 order of operations
-
When more than one operator appears in an expression, the order of eval
uation depends on the rules of precedence. -
add parenthesis to increase the precedence
2.8 operators for strings
-
you cannot perform mathematical operations on Strings
-
but you can use “+” to combine strings and intergrals
Eg.
int number;
number = 5;
print("hello" + number);
2.9 composition
The left side of an assignment has to be a variable name, not an expression. The following is illegal: minute+1 = hour
chapter 3 Viod methods
3.1 floating-point
-
int 0 is 0
-
double 0 is 0.0 (floating-point)
floating-point: A type of variable (or value) that can contain fractions as
well as integers. The floating-point type we will use is double.
3.2 converting from double to int
-
The simplest way to convert a floating-point value to an integer is to use a
typecast. -
Typecasting takes precedence over arithmetic operations,
eg
double pi = 3.14159;
int x = (int) pi;
So x = 3
3.3 Math methods
Methods: A named sequence of statements that performs a useful function.
Methods may or may not take parameters, and may or may not return
a value.
Argument: A value that you provide when you invoke a method. This value must have the same type as the corresponding parameter.
3.4 composition
Java methods can be composed, meaning that you use one expression as part of another.
double x = Math.cos(angle + Math.PI/2)
3.5 adding new methods
public static void NAME( LIST OF PARAMETERS ) {
STATEMENTS
}
To name a method: Java methods start with a lower case letter and use “camel caps,” which is a cute name for jammingWordsTogetherLikeThis.
Parameter: A piece of information a method requires before it can run.
Parameters are variables: they contain values and have types.
3.6 classes and methods
Class: A named collection of methods. So far, we have used the Math class
and the System class, and we have written classes named Hello and
NewLine.
When we invoke a method in the class, we have to specify the name of the class and the name of the method.
Math.pow();
3.7 programs with multiple methods
-
follows the order of execution
-
Execution always begins at the first statement of main, regardless of where it is in the program
3.8 parameters and arguments
3.9 stack diagrams
-
For each method there is a gray box called a frame that contains the
method’s parameters and variables. -
The name of the method appears out
side the frame. As usual, the value of each variable is drawn inside a box
with the name of the variable beside it.
3.10 methods with multiple parameters
you have to declare the type of every parameter
public static void printTime(int hour, int minute) {
System.out.print(hour);
System.out.print(":");
System.out.println(minute);
}
3.11 Methods that return values
Some of the methods we are using, like the Math methods, return values.
Other methods, like println and newLine, perform an action but they don’t
return a value.
chapter 4 conditionals and recursion
4.1 The modulus operator
modulus: An operator that works on integers and yields the remainder
when one number is divided by another. In Java it is denoted with a
percent sign(%).
eg. 3 % 2 = 1
4.2 conditional execution
if (x > 0) {
System.out.println("x is positive");
}
The expression in parentheses is called the condition.
Relational operators:
x == y // x equals y
x != y // x is not equal to y
x > y // x is greater than y
x < y // x is less than y
x >= y // x is greater than or equal to y
x <= y // x is less than or equal to y
Remember that = is the assignment operator, and == is a comparison operator.
4.3 alternative execution
parity (evenness or oddness)
4.4 chained conditionals
chaining: A way of joining several conditional statements in sequence.
If(){} else if{} else{}
4.5 nested conditionals
nesting: Putting a conditional statement inside one or both branches of
another conditional statement.
if() {} else{
If() {}
else{}
}
4.6 the return statement
uses return to exit the method.
4.7 type conversion
4.8 recursion
When a method invokes itself, that’s called recursion, and such methods
are recursive.
4.9 stack diagrams for recursive methods
base case: A condition that causes a recursive method not to make a recur
sive call