Lecture1 Types, Variables, Operators
1. Programming languages
比CPU指令好理解,需要被翻译成CPU指令
2. Java特征
在 virtual machine上运行,比python复杂,比C++简单
3. Compiling Java
source code(.java) -- javac → Bytecode(.class) --java
4. First Program
| Java |
|---|
| class Hello{
public static viod main(String[] arguments){
// Program execution begins here
System.out.println("Hello World.");
}
}
|
5. Second Program
| Java |
|---|
| class Hello{
public static viod main(String[] arguments){
System.out.println("Hello World.");
System.out.println("Line number2");
}
}
|
6. Types
Kinds of values that can be stored and manipulated
- boolean
- int
- double
- String
7. Variables
Named location that stores a value of one particular type
8. Assignment
- Use “=” to give variables a value
- can be combined with a variable declaration
| Java |
|---|
| class Hello3{
public static void main(String[] arguments){
String foo = "IAP 6.092"
System.out.println(foo):
foo = "Something else";
}
}
|
9. Operators
Symbols that perform simple computations
order: parentheses-Multiplication and division-addition and subtraction
| Java |
|---|
| class DoMath{
public static void main(String[] arguments){
double score 1.0 + 2.0 * 3.0;
System.out.println(score);
score = score / 2.0;
System.out.println(score);
}
}
|
String Concatenation (+)
| Java |
|---|
| String text = "hello" + " world";
text = text + " number" + 5; // text = "hello world number 5", python cannot do this
|
Assignment1 Gravity Calculator
| Java |
|---|
| class GravityCalculator {
public static void main(String[] arguments) {
double gravity = -9.81; // Earth's gravity in m/s^2
double initialVelocity = 0.0;
double fallingTime = 10.0;
double initialPosition = 0.0;
double finalPosition = 0.5 * gravity * fallingTime * fallingTime +
initialVelocity * fallingTime + initialPosition;
System.out.println("The object's position after " + fallingTime +
" seconds is " + finalPosition + " m.");
}
}
|