Escolar Documentos
Profissional Documentos
Cultura Documentos
Zhen Jiang
CIS Dept.
Temple University
1050 Wachman Hall, Main Campus
Email: zhen.jiang@temple.edu
03/31/16
Table of Contents
03/31/16
http://www.cis.temple.edu/~jiang/Welcome.java
http://www.cis.temple.edu/~jiang/Welcome1.java
03/31/16
Environment
03/31/16
Compile (javac)
source code
(Hello.java)
03/31/16
Execute (java)
byte code
(Hello.class)
output
Details
Names
Main
{ } and ( )
Println and print (p93)
;
03/31/16
Textbook
Highlighted by content in ppt slides
Indexed by page number in ppt slides
Discussion
Summary
Learning target
http://www.cis.temple.edu/~jiang/CIS1068_07_summary.pdf
www.cis.temple.edu/~jiang/ShowButtonDemo.pdf
www.cis.temple.edu/~jiang/ButtonDemo.pdf
www.cis.temple.edu/~jiang/WindowDestroyer.pdf
Three keys
03/31/16
Simple Output
Hello.java,
compiler output:
2 errors found:
File: Hello.java [line: 2]
Error: Hello.java:2: <identifier> expected
File: Hello.java [line: 3]
Error: Hello.java:3: ';' expected
03/31/16
10
First lesson
03/31/16
11
Java is case-sensitive
compiler output:
12
pronounced print-linn
03/31/16
13
03/31/16
14
Examples:
"hello"
"This is a string"
"This, too, is a string.
03/31/16
16
\t
\n
\"
\\
tab character
newline character
quotation mark character
backslash character
Example:
System.out.println("Hello!\nHow are \"you\"?");
03/31/16
17
18
19
Examples:
/* A comment goes here. */
/* It can even span
multiple lines. */
// This is a one-line comment.
03/31/16
20
03/31/16
21
03/31/16
22
Simple Input
http://www.cis.temple.edu/~jiang/1068FirstProgram.pdf
03/31/16
23
Variable
03/31/16
24
Examples:
int x;
double myGPA;
int varName;
03/31/16
25
x: ? y: ?
(The memory still has no value yet.)
03/31/16
26
27
Name, p103
03/31/16
28
Examples:
legal:susan
TheCure
myMethod
second_place _myName
ANSWER_IS_42 $variable
name2
illegal: me+u
side-swipe
jim's
49er
question?
hi there ph.d
2%milk suzy@yahoo.com
03/31/16
method1
29
default
do
double
else
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
30
Data types
03/31/16
31
32
Temperature
Sum of a group of integers
Average of a group of integers
Number of seconds left in a game
03/31/16
33
Lesson two:
03/31/16
34
Discrete Types
byte
short
int
long
03/31/16
Continuous Types
float
double
Non-numeric Types
boolean
char
35
Type
Representati
on
Bits
Bytes
#Values
boolean
True or False
N/A
char
a or 7 or \n
16
216 = 65,536
byte
,-2,-1,0,1,2,
28 = 256
short
,-2,-1,0,1,2,
16
216 = 65,536
int
,-2,-1,0,1,2,
> 4.29
million
long
,-2,-1,0,1,2,
> 18
quintillion
float
0.0, 10.5,
-100.7
03/31/16
32
36
Arithmetic (Use of
variables)
17/3=?
http://www.cis.temple.edu/~jiang/Variable.p
df
03/31/16
37
Examples:
x = 2 * 4;
x: 8
myGPA = 3.25;
03/31/16
myGPA: 3.25
38
Example:
int x;
x = 3;
System.out.println(x);
// 3
x = 4 + 7;
System.out.println(x);
// 11
03/31/16
39
40
Example:
int x;
int x;
Example:
int x;
System.out.println(x); // ERROR: x has no value
03/31/16
41
42
43
double
float
long
int
char
boolean
short
byte
03/31/16
44
Examples:
1+4*3
3
"CSE142"
(1 + 2) % 3 * 4
03/31/16
45
+
*
/
%
03/31/16
addition
subtraction or negation
multiplication
division
modulus, a.k.a. remainder
46
Example:
3 * 4 evaluates to 12
03/31/16
47
Examples:
1425 / 27 is 52
35 / 5 is 7
84 / 10 is 8
156 / 100 is 1
24 / 0 is illegal (what do you think happens?)
03/31/16
48
Example:
14 % 4 is 2
1425 % 27 is 21
3
4 ) 14
12
2
52
27 ) 1425
135
75
54
21
45 % 6
4 % 2
8 % 20
11 % 0
03/31/16
49
03/31/16
50
Parentheses
Multiplication, Division, Mod
Addition, Subtraction
03/31/16
()
* / %
+ 51
1 * 2 + 3 * 5 / 4
\_/
|
2
+ 3 * 5 / 4
\_/
|
2
+ 15
/ 4
\___/
|
2
+
3
\________/
|
5
03/31/16
1 + 2 / 3 * 5 - 4
\_/
|
1 +
0
* 5 - 4
\___/
|
1 +
0
- 4
\______/
|
1
- 4
\_________/
|
-3
52
Examples:
4.2 * 3 is 12.6
1 / 2.0 is 0.5
Examples:
(int)4.2 is 4
(double)17 is 17.0
03/31/16
7 / 3 * 1.2 + 3 / 2
\_/
|
2
* 1.2 + 3 / 2
\___/
|
2.4
+ 3 / 2
\_/
|
2.4
+
1
\________/
|
3.4
Notice how 3 / 2 is
still 1 above, not 1.5.
53
Examples:
"hello" + 42 is
1 + "abc" + 2is
"abc" + 1 + 2is
1 + 2 + "abc"is
"abc" + 9 * 3is
"1" + 1is "11"
4 - 1 + "abc"is
"hello42"
"1abc2"
"abc12"
"3abc"
"abc27" (what happened here?)
"3abc"
03/31/16
54
03/31/16
249.6
83.2
55
Example:
System.out.println(0.1 + 0.2);
The output is 0.30000000000000004!
03/31/16
56
03/31/16
57
ERROR!
This is known as overflow: trying to store
something that does not fit into the bits reserved
for a data type.
http://en.wikipedia.org/wiki/Arithmetic_overflow
Overflow errors are NOT automatically detected!
03/31/16
58
int n = 2000000000;
System.out.println(n * n);
// output: -1651507200
03/31/16
59
03/31/16
60
03/31/16
61
Legal assignment
03/31/16
62
Shorthand
<variable>++;
<variable>--;
Examples:
int x = 2;
x++;
// x = x + 1;
// x now stores 3
03/31/16
63
after executing
int m = 4;
int result = 3 * (++m)
result has a value of 15 and m has a value of 5
after executing
int m = 4;
int result = 3 * (m++)
result has a value of 12 and m has a value of
03/31/16
64
+=
-=
*=
/=
%=
+
*
/
%
(<exp>);
(<exp>);
(<exp>);
(<exp>);
(<exp>);
Examples:
x += 3 - 4;
gpa -= 0.5;
number *= 2;
03/31/16
// x = x + (3 - 4);
// gpa = gpa (0.5);
// number = number * (2);65
Declaration/initialization statement
syntax:
<type> <name> = <expression>;
Examples:
double myGPA = 3.95;
int x = (11 % 3) + 12;
03/31/16
66
Examples:
int a, b, c;
double x, y;
Examples:
int a = 2, b = 3, c = -4;
double grade = 3.5, delta = 0.1;
67
03/31/16
http://www.cis.temple.edu/~jiang/CircleCalculation2.pdf
68
03/31/16
69
Syntax
public static final
Variable_Type <name> = <Constant>;
Examples
public static final double
PI = 3.14159;
public static final String MOTTO =
"The customer is always right.";
By convention, uppercase letters are used
for constants.
03/31/16
70
Swap.java
http://www.cis.temple.edu/~jiang/Swap.
pdf
Payroll.java
http://www.cis.temple.edu/~jiang/Payroll
.pdf
03/31/16
71
03/31/16
72
String
char
String
Represents individual
characters
Represents sequences of
characters
Primitive type
e.g.:
T
t
3
%
\n
e.g.:
We the people
1. Twas brillig, and the slithy
toves\n
T
03/31/16
73
Example, p67.
char letter = 'S';
System.out.println(letter);
// prints S
System.out.println((int)letter);
// prints 83,
// explained on p932
03/31/16
74
03/31/16
75
03/31/16
76
indexOf(str)
returns the index where the start of the given string appears in this
string (-1 if not found)
length()
substring(index1,index2)
returns the characters in this string from index1 up to, but not
including, index2
toLowerCase()
toUpperCase()
03/31/16
77
Some examples:
String s = Cola;
int len = s.length();
// len == 4
char firstLetter = s.charAt(0); // C
int index = s.indexOf(ol); // index == 1
String sub = s.substring(1,3); // ol
String up = s.toUpperCase(); // COLA
String down = s.toLowerCase(); // cola
03/31/16
78
Displaying message
Input P116-118
converting a string to number, p123
Output P121-122
converting a number to string
03/31/16
http://www.cis.temple.edu/~jiang/Payroll2.pdf
79
Summary of programs in
discussion
Welcome.java
Welcome.java
Hello.java
Exercises (slide 17-18, 49, 54)
FirstProgram.java
Variable.java
ChangeMaker.java
CircleCalculation2.java
Swap.java
Payroll.java
Payroll2.java (a similar program ChangeMakerWindow.java)
03/31/16
80
Summary of Concepts
03/31/16
81
Printf (p101)
Delimiters for input (p99)
DecimalFormat for output(p921)
http://www.cis.temple.edu/~jiang/PayrollDialog.pdf
or the similar program on page 125
http://www.cis.temple.edu/~jiang/ChangeMakerWind
ow.pdf
03/31/16
82