You are on page 1of 16

Data Operations and Processing - 02

CHAPTER

02

Computer Programming using Java

(Data Operations and Processing)

1. (Data Types and Variables)


1.
(Data Types)
1.1
(Primitive Data Types) 4
1) (Integer)


byte
8 bits
-128 127
short
16 bits
-32,768 32,767
int
32 bits
-2,147,483,648 2,147,483,647
-9,223,372,036,854,775,808
long
64 bits
9,223,372,036,854,775,807
2) (Floating Point / Real Number)


float
32 bits
-3.40292347E+38 3.40292347E+38
-1.79769313486231570E+308
double
64 bits
1.79769313486231570E+308


0
0
0
0L


0.0F
0.0D

3) (Character) (Letter) (Digit) (Symbol) 1





char
16 bits
'\u0000' '\uFFFF' (0 - 65535)
'\u0000'
'A' 'a' ( '')
4) (Boolean) (True) (False)



boolean
false

true false
1.2 (Reference Data Types) 1 ( )
1) (String)
0



String
null

"Java Chula" "1" "F50"


"" (Empty String) ( "")
2553 ( 7 2/2553) ()

15

16

Computer Programming using Java

02 - Data Operations and Processing

1.3 ()
byte

short

int

long

float

double

2.
1) ( )
(1) '$' (Dollar Sign)
'_' (Underscore) num, $_$, _hey, $1, xxx3, test1_$
(2) 3girl1man, 2you, 5hundredMile
(3) 1
(Reserved Words / Keywords) 53
(4)
abstract
catch
do
final
implements
native
public
switch
true

assert
char
double
finally
import
new
return
synchronized
try

boolean
class
else
float
instanceof
null
short
this
void

break
const *
enum
for
int
package
static
throw
volatile

byte
continue
extends
goto *
interface
private
strictfp
throws
while

case
default
false
if
long
protected
super
transient

(5)
(
)
(6) ( )
(7) (Case-Sensitive) var Var
2) ( )
(1) () /
(2) ( Editor )
(3)
stdID, firstName, birthDate
(4)
hostName, numberOfCourse, currentTime, firstDayOfMonth

PI, MONTH_OF_YEAR
(5)
'$' '_'
(6)
3.
1)
< > <>;

int x;

double num;

String lastName;

boolean check;

2553 ( 7 2/2553) ()

Computer Programming using Java

Data Operations and Processing - 02

2)

(Constant)

< > = <>;

x = 5; num = 4.01; lastName = "Rukchula"; check =


3) ( 1 2 )

true;

a = b;

< > <> = <>;

int x = n; double num = 4.01; char ch = 'A';


4)

float y = 2.9f;

Comma

< > <>, <> = <>, ...;

int x = n, year, age = 18, z, a;

4.
1)

byte short
byte x = 12; short y = 199;
int ()
int dec = 26;
int ()
0 () int oct = 032;
int ()
0x 0X int hex = 0x1A;
long
l L long n = 26L;
2)

float
double
double
double

f F float f = 1.3F;
( ) double d = 1.3;
()
d D double d = 1.3D;
( a x 10b) a e

E b ()
8
double s = 93.478e8; 93.478 x 10
5
double t = 631E-5; 631 x 10
200
double u = 2.0e+200; 2.0 x 10

3)

char (
)
Single Quote char c =

'a';

2553 ( 7 2/2553) ()

17

18

Computer Programming using Java

02 - Data Operations and Processing


char ( Unicode)

Unicode Single Quote char c = '\u0043';


'\u0030' 48 () '0'
'\u0041' 65 () 'A'
'\u0061' 97 () 'a'
'\u0E01' 3585 () ''
char ( )
Single Quote char tab = '\t';
String (
) Double Quote String s = "Bank";
String ( ) String String t = new String("CU");
1 [ ] (:) (;) (15 )
1)
integer x; .
2)
int x = 1.5; .
3)
byte x = 1000; .
int null = 5; .
4)
5)
Int y = 1; .
6)
float cal = 15.763; .
String id = "1"; .
7)
char c = 'Tuksin'; .
8)
String s1 = 'Apisit'; .
9)
10)
String 3Type = "One Two Three"; .
11)
short _$abcdefghijklmnopqrstuvwxyz0123456789$_; .
12)
boolean flag = 1; .
13)
float = 139L; .
String _ = ""; .
14)
15)
int ch = '\u0044'; .
2 []

(15 )



var1 = 014;
1.
var2 = 014f;
2.
var3 = 'C';
3. int
2553 ( 7 2/2553) ()

Data Operations and Processing - 02


4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

Computer Programming using Java

var4 = 0E-0;
double

var5 = 0x1D;
var6 = 1.0e2.0;
var7 = .1000;
var8 = 08;
var9 = null;
var10 = 100E012;
var11 = 1f + "";
var12 = 1e1 + "";
var13 = 0d;
var14 = 3.f;
var15 = 0xbeef;

2. (Input and Output the Data)


1. (Input the Data)


Scanner
1) Scanner Scanner Scanner
import java.util.Scanner;
1
2
3
4

import java.util.Scanner;
public class Test {
...
}

2)


Scanner <> = new Scanner(System.in);

1
2
3
4
5
6
7

import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
...
}
kb
}

System.in

(keyboard)

2553 ( 7 2/2553) ()

19

20

Computer Programming using Java

02 - Data Operations and Processing

3)


1) int nextInt() nextInt

( )

int <> = <>.nextInt();

nextInt()

int num = kb.nextInt(); ( int kb num)


2) long nextLong()
long <> = <>.nextLong();

long n = kb.nextLong(); ( long kb n)


3) float nextFloat()
float <> = <>.nextFloat();

float f = kb.nextFloat(); ( float kb f)


4) double nextDouble()
double <> = <>.nextDouble();

double d = kb.nextDouble(); ( double kb d)


5) String nextLine()
String <> = <>.nextLine();

String s1 = kb.nextLine(); ( String

kb s1)
6) String ( \t \n)
next()
String <> = <>.next();

String s2 = kb.next();

( String kb s2)

3 [ ]

(15 )
Scanner kb = new Scanner(System.in);


1.
2.

var1 = kb.nextInt();

087

var2 = kb.nextLong();

-0

2553 ( 7 2/2553) ()

Computer Programming using Java

Data Operations and Processing - 02


3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

int

7.0

var4 = kb.nextFloat();

22.50000

long

46L

float

7.2F

double

-0.1

double

-13

double

var10 = kb.nextInt();

double

var11 = kb.nextInt();

1.0

int

var12 = kb.nextDouble();

1.0

var13 = kb.nextLine();

22.50000

var14 = kb.nextLine();

Hi java CU

var15 = kb.next();

Hi java CU

var16 = kb.nextInt();

191

String

2. (Output the Data)



(Monitor/Screen) 1
1) print()
System.out.print(...);

System.out

2) println()
System.out.println(...);


1) System.out.print(num);
2) System.out.print(13);
3) System.out.print(x * y + (10 z));
4) System.out.print("Hello Java");
5) 1-4 System.out.print("Money = " + (x *

y + 92.4) + " Baht");

+

2553 ( 7 2/2553) ()

21

22

Computer Programming using Java

02 - Data Operations and Processing

3.


1 import java.util.Scanner;
Scanner
2 public class Test {
3
public static void main(String[] args) {

4
Scanner kb = new Scanner(System.in);
5
System.out.print("Please enter number: ");
6
int num = kb.nextInt();

7


8
...
9
print()
10
System.out.println("Result is " + num);
11
}
12 }
println()


Please enter number:

Please enter number: 15 .

Please enter number: 15


Result is 15
.



15
Enter
15 num
num

4 [ ]
-
id, name,
age gpa
(10 )
1

2553 ( 7 2/2553) ()

Data Operations and Processing - 02

Computer Programming using Java

5 []

a b
a b b
a (10 )
1

Enter
Enter
Value
Value

a:
b:
of
of

13
21
a: 21
b: 13

3. (Operators and Standard Class)


1. (Operators) 5
1) (Arithmetic Operators)

x + y a

x - y a
*

x * y a
/

x / y a
%
(Modulo)
x % y a


+ b + c
- b - c
* b * c
/ b / c
% b % c

2) (Increment and Decrement Operators)


x++
x 1 x = 2, y = 1 x++
++x
1 x x = 2, y = 1 ++x
x- x 1 x = 2, y = 1 x---x
1 x x = 2, y = 1 --x

+ y = 3
+ y = 4
+ y = 3
+ y = 2

2553 ( 7 2/2553) ()

23

24

Computer Programming using Java

02 - Data Operations and Processing

3) (Comparison/Relational Operators)


==

x == y ( )

!=

x != y ( )

<

x < y ( )

>

x > y ( )

<=

x <= y ( )

>=

x >= y ( )

4) (Logical Operators)


(x >= y) && (x != 0)
&&
(AND)
(x == y) || (x == z)
||
(OR)
!(x <= y)
!
(NOT)
5) (Assignment Operators)

x
+=
x += y
x = x + y
x
-=
x -= y
x = x - y
x
*=
x *= y
x = x * y
x
/=
x /= y
x = x / y
x


= y y x
+= 14
x = x + 14
-= 1
x=x-1
*= y--
x = x * y-/= 5.8
x = x / 5.8

6 [ ] (5 )



a = 21, b = 0;
1. int
System.out.println("a" + a - b);
2.

double x = 1.0, y = 1.000;


System.out.println(x > y);

3.

double a = 1.6; a += 5.1;


System.out.println(a % 4);

4.

int x = 6;
System.out.println(x-- + ++x - --x - x-- + x++ + x);

5.

boolean a = true, b = true, c = false;


System.out.println(a || b && c);

2. ( )
/

()
1. (Parentheses)
x (2 + y) (2 + y) x
2. (Method Call) () Math.sqrt(a + 2) Math.sqrt()
2553 ( 7 2/2553) ()

Data Operations and Processing - 02

Computer Programming using Java

/

+
(Unary Plus)
2 * +a +a 2
3.
(Unary Minus)
a / 3 a 3
*
(Multiplication)
x * 3 + 2 x * 3 2
4. (Division)
/
3 5 / x 5 / x 3
%
(Modulo)
y % 2 + 1 y % 2 1
+
(Addition)
x + 17 > 19 x + 17 >
5.
(Subtraction)
x 9.5 = 42.5 x 9.5 =
6. : (<), (<=), (>), (>=)
7. : (==), (!=)
&&
8. (Logical AND)
a || b && c && ||
||
9. (Logical OR)
x = d > 3 || d <= 15 || =
=
10. (Assignment)


7 [ ]
(5 )
1)

a * (b + -(c / d) / e) + (f g % h)

2)

(1.0 / 2.0) * Math.sin(x Math.PI / Math.sqrt(y))

3)

a * b + (-c) / d / e * f - g % h

4)

cal = (1 x % 2.5 + y * z) + 3 * k -7 / w

5)

cal = x == 5 || x <= 3 && x >= -3 || (x != 0)

3.

20

1
2

+ - * / %
1.

()
= += -= *= /=
2.

[1] (
1)
== != < > <= >=
3.

boolean
+ - * / % = += -= *= /=
char
4.

2553 ( 7 2/2553) ()

25

26

Computer Programming using Java

1
5.

6.
char
7.
char
8.
char
9.
char
10.
char
11.
char
12.
char
13.
14.
String
15.
16.
String
17.
String
18.
19. boolean
20.
-

02 - Data Operations and Processing

++ -++ -+ - * / %


char
( int)

= += -= *= /=

char

char

== != < > <= >=

char

boolean

++ --

[2]
-

++ --

char

char

String

String

String

String

String

== !=

String

boolean

= && ||

boolean

boolean

boolean

boolean

+ - * / %
+= -= *= /=
=

char
char
char

( )

1 2
[2]
2 int
[1]

8 [ ]
(10 )


1. (5 + 4) + ((3 + 2L) 1)
2. (5 2 / 1) + 0.000 + '\u0000'
3. 5.0F * 6 ('D' - 'A')
*4. 2 % (0.4 + 0.3F)
*5. !("Java" == "Jaba");
6. (13 / 5) + (2 / 3)
7. '0' - '1' + "5" + false
8. 4E1 / 2
9. !false || true && false || !true
10. 0.0 / (10 + 'q' 'Y') != 0L
2553 ( 7 2/2553) ()

Computer Programming using Java

Data Operations and Processing - 02

4. (Type Conversions)
1)
19
double a = 34 + 1.0F;
float b = 4L / 3;

//a = 35.0
//b = 1.0F

int c = 'A';
char d = 65;

//c = 65
//d = 'A'

2) (Casting)

(< >) < />

(/ )

byte b = 50;
b = (byte)(b * 2);

//b = 100

char c = '1';
int num = (int) c;

double x = 5.99;
long y = (long) x;

//y = 5

int ten = (int) "10";


boolean f = (boolean) 1;

//num = 49
//Error
//Error

9 [ ]

(10 )


1. (byte)(3 + 5.4) / 7
2. (3 + 5.4) / (float) 7
3. (float)((3 + 2) / 7)
4. char c = (double) 'A';
5. char c = (int) 'b'; c += 1.7;
6. char c = 67 + (int) 3.91
7. (int) 19.8E-1
8. (short)((double) 1 / 2) == 00L + 0
9. (byte) 011 + (short) 1.9991
10. (byte) '0' '6' + (char) 1.5
5. (Mathematic Class)


double
1. Math.toRadians(degree)

double
2. Math.toDegrees(radian)

double
3. Math.sin(radian)
(Sine)
double
4. Math.cos(radian)
(Cosine)
2553 ( 7 2/2553) ()

27

28

Computer Programming using Java

5.
6.
7.
8.
9.
10.
11.

02 - Data Operations and Processing


Math.sqrt(x)
double

Math.pow(x, y)
double, double
xy
Math.abs(x)
double
(Absolute)
Math.max(x, y)
double, double
x y
Math.min(x, y)
double, double
x y
Math.log10(x)
double

Math.PI
PI 3.141592653589793
Math.random()

[0.0, 1.0)
12.
(int)(Math.random() * (b a + 1) + a)
a b
double ( 12 int)
10 [ ] (Assignment Statement)

(6 )

1.

2r 2
b 2 4ac

2.

mn

3.

3x 3 + 4 y 4
+ xy
( x + y)( x y)

p2 q2

2mn
p+q

11 [ ]
n
(6 )

1. 0 9
2. 1 10
3. 0 10
4. 0 987
5. 1 500
6. 2 600
2553 ( 7 2/2553) ()

Data Operations and Processing - 02

Computer Programming using Java

12 []

4
(10 )
Enter number: 1234
1

Reversed number: 4321


Enter number: 9753
Reversed number: 3579

13 []

6 2 2
(10 )
1

2553 ( 7 2/2553) ()

29

30

Computer Programming using Java

02 - Data Operations and Processing

14 [] 1
(Money Machine)
1000 500 100 50 20
10 5 2 1
(15 )
Money : 2897
1

1000 Baht
500 Baht
100 Baht
50 Baht
20 Baht
10 Baht
5 Baht
2 Baht
1 Baht

:
:
:
:
:
:
:
:
:

2
1
3
1
2
0
1
1
0

Do you know?
1 / 0
1.0 / 0.0
-1.0 / 0.0
1.0 / (1.0 / 0)
0.0 / 0.0

=
=
=
=
=

Error
Infinity
-Infinity
0.0
NaN

2553 ( 7 2/2553) ()

You might also like