computer programming 2.1

60
1 พื นฐานในภาษา Java

Upload: saranyu-srisrontong

Post on 21-Jul-2015

34 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Computer Programming 2.1

1

พนฐานในภาษา Java

Page 2: Computer Programming 2.1

2

ขอมลพนฐาน (Primitive Data Type)

Page 3: Computer Programming 2.1

3

Page 4: Computer Programming 2.1

4

ตวแปร (Variable)

การต งชอตวแปร (Identifiers)

ควรมความหมาย ไมมชองวางและเครองหมาย เชน ? ! @ # % ^& * ( ) [ ] { } . , ; : “ ‘ / และ \

ตวแรกของชอตวแปรควรเปนตวอกษรตวพมพเลก (a-z, _ หรอ $ )

หามใชค าศพทสงวน เชน name, void, class, float ฯลฯ

ตวพมพใหญ/เลก มความหมายตางกน

ไมควรใชตวพมพใหญทกตว เพราะอาจเชอมโยงกบภาษาอนไมไ

Page 5: Computer Programming 2.1

5

การประกาศตวแปร[<access_specifier>] [<modifier>] <data_type> <variable_name1>

[, <variable_name2>] ...;

public ใชนยามตวแปรเพอใหน าไปใชกบ Class หรอโปรแกรมอนได

private ใชนยามตวแปรเพอใหใชไดเฉพาะใน Class ทสรางตวแปรหรอ Method น นข นมา

protected ใชนยามตวแปรเพอใหใชไดเฉพาะ Class ทสรางขนมาดวยวธการสบทอด (Inheritance) โดยปกตใชกบ Base

Class

static ใชนยามตวแปรทตองการใหใชงานไดกบทก Method ในClass

final ใชนยามตวแปรทตองการใชเกบขอมลคาคงท (Constant)

ซงไมสามารถเปลยนคาได

Page 6: Computer Programming 2.1

6

การก าหนดคาใหกบตวแปร[<access_specifier>] [<modifier>] <data_type>

<variable_name1> = <value> [, <variable_name2> = <value>]...;

หรอ[<access_specifier>] [<modifier>] <data_type>

<variable_name>; <variable_name> = <value>;

การก าหนดคาจ านวนเตม

int r = 1, b = 1, g = 1; long i2 = 300 * 30;

private int employeeid = 0;

int salary; salary = 5000;

int a = 024, b = 036;

int data_c = 0x1D, data_d = 0x36;

Page 7: Computer Programming 2.1

7

การก าหนดคาจ านวนจรงfloat d1 = 34.0 - 0.1;

double d2 = 1.0/2.0;

static final float PI = 3.141159;

private float salary = 0.0F;

static final float TAX_RATE = .0725F;

การก าหนดคาตรรกะboolean done =true;

Page 8: Computer Programming 2.1

8

public class TestCalc{ public static void main(String[] args)

{ final double BOTTLE_VOLUME = 2.5;final double CAN_VOLUME = 0.35;int bottles = 4;int cans = 10;double total = (bottles * BOTTLE_VOLUME) + (cans * CAN_VOLUME);System.out.println("Total Volume is " + total); } }

Page 9: Computer Programming 2.1

9

การก าหนดคาตวอกษรตองอยในสญลกษณหยาฝน (Single Quote)

Page 10: Computer Programming 2.1

10

Page 11: Computer Programming 2.1

11

StringString message = “Welcome”;String firstName = “Sippakorn”, lastName = “Saengthong”;private String name;private String name = “ ”;String name1, name2;

name1 = “Tanisorn”; name2 = name1;

การหาความยาวของ Stringint n = fname.length();

การดงขอความบางสวนทเกบในตวแปรแบบ StringString fname = “Samphan”;String sub = fname.substring (0,4);String sub = fname.substring (1);

การเชอมตอขอความString s1 = “Chantana”String s2 = “Promsiri”String name1 = s1 + s2;

Page 12: Computer Programming 2.1

12

class test

{ public static void main(String[] args)

{ String fname = "Chutimond";

int n = fname.length();

System.out.println("Length of name = " + n);

String a = fname.substring(0,1);

System.out.println("First character of name = " + a);

String lname = "Bunmark";

String b = lname.substring(2-1,4);

System.out.println("3 char. of lname from position 2 = " + b);

int m = lname.length();

String c = lname.substring(m-1,m);

System.out.println("Last character of surname = " + c); } }

Page 13: Computer Programming 2.1

13

Page 14: Computer Programming 2.1

14

Page 15: Computer Programming 2.1

15

Page 16: Computer Programming 2.1

16

ตว าเนนการ (Operator)

Arithmetic OperatorsInteger Arithmetic Operators : +, -, *, / และ %Floating-point Arithmetic Operators : +, -, *, / และ % Arithmetic Assignment Operators : +=, -=, *=, /= และ%= Increment and Decrement Arithmetic Operators : ++x, x++, - -x และ x- -

หมายเหต 1. ( )2. ++, --3. *, /, %4. +, -5. +=, -=, *=, /=, %=

Assignment Operatorsตวอยางเชน x = (y = y + 1) + 1;

Page 17: Computer Programming 2.1

17

// Integer Arithmetic Operators public class IntArithOper{ public static void main(String[ ] args)

{ System.out.println("Integer Arithmetic Operators ");System.out.println("1 + 2 = " + (1 + 2));System.out.println("1 - 2 = " + (1 - 2));System.out.println("1 * 2 = " + 1 * 2);System.out.println("1 / 2 = " + 1 / 2);System.out.println("1 % 2 = " + 1 % 2); } }

Page 18: Computer Programming 2.1

18

// Floating Arithmetic Operatorspublic class FloatArithOper{ public static void main(String[ ] args)

{ System.out.println("Floating Arithmetic Operators ");System.out.println("1.0 + 2.0 = " +(1.0 + 2.0));System.out.println("1.0 - 2.0 = " +(1.0 - 2.0));System.out.println("1.0 * 2.0 = " +1.0 * 2.0);System.out.println("1.0 / 2.0 = " +1.0 / 2.0);System.out.println("1.0 % 2.0 = " +1.0 % 2.0); } }

Page 19: Computer Programming 2.1

19

// Arithmetic Assignment Operators : Integer

public class IntArithAssOper

{ public static void main(String[ ] args)

{ int x = 1;

System.out.println("Arithmetic Assignment Operators : Integer");

x += 2; System.out.println("x += 2 is " + x);

x -= 2; System.out.println("x -= 2 is " + x);

x *= 2; System.out.println("x *= 2 is " + x);

x /= 2; System.out.println("x /= 2 is " + x);

x %= 2; System.out.println("x %= 2 is " + x); } }

Page 20: Computer Programming 2.1

20

// Arithmetic Assignment Operators : Floatpublic class FloatArithAssOper{ public static void main(String[ ] args)

{ float y =1f; System.out.println("Arithmetic Assignment Operators : Float");y += 2; System.out.println("y += 2 is " + y);y -= 2; System.out.println("y -= 2 is " + y);y *= 2; System.out.println("y *= 2 is " + y);y /= 2; System.out.println("y /= 2 is " + y);y %= 2; System.out.println("y %= 2 is " + y); } }

Page 21: Computer Programming 2.1

21

//Increment and Decrement Operators public class TestIncDecOper{ public static void main(String[ ] args)

{ int a, b, x = 1, y = 1; a = x++; b = ++y;System.out.println("x = 1 , y = 1");System.out.println("a = x++ , b = ++y");System.out.println("x+ = " + x+" , +y = " +y);System.out.println("a+ = " + a+", +b = " +b); } }

Page 22: Computer Programming 2.1

22

//Assignment Operators public class TestAssOper{ public static void main(String[ ] args)

{ int x, y;x = y = 1;System.out.println("x = y = 1");System.out.println("x = "+ x + ", y = " + y);x = (y = y + 1) + 1;System.out.println("x = (y = y + 1) + 1");System.out.println("x = "+ x + ", y = " + y); } }

Page 23: Computer Programming 2.1

23

Bitwise OperatorsBoolean Bitwise Operators ~ (Bitwise Unary Not) & (Bitwise And)| (Bitwise Or) ^ (Bitwise Exclusive Or)

>> (Shift Right) << (Shift Left)

>>> (Shift Right Zero Fill)

Assignment Bitwise Operators &= (Bitwise And Assignment)|= (Bitwise Or Assignment)^= (Bitwise Exclusive Or Assignment)>>= (Shift Right Assignment)<<= (Shift Left Assignment)>>>= (Shift Right Zero Fill Assignment)

Page 24: Computer Programming 2.1

24

//Bitwise Operators public class TestBitwiseOper{ public static void main(String[ ] args)

{ int A = 6, B = 7;System.out.println("A = " + Integer.toBinaryString(A));System.out.println("B = " + Integer.toBinaryString(B));System.out.println("~A = " + Integer.toBinaryString(~A));System.out.println("A & B = " + Integer.toBinaryString(A&B));System.out.println("A | B = " + Integer.toBinaryString(A|B));System.out.println("A ^ B = " + Integer.toBinaryString(A^B));System.out.println("A << 1 = " + Integer.toBinaryString(A<<1));System.out.println("A >> 1 = " + Integer.toBinaryString(A>>1));System.out.println("A >>> 1 = " + Integer.toBinaryString(A>>>1));} }

Page 25: Computer Programming 2.1

25

Relational Operators

== Equal To != Not Equal To

> Greater Than < Less Than

>= Greater Than or Equal To

<= Less Than or Equal To

Logical Operators

Boolean Logical Operators & Logical And | Logical Or

^ Logical Exclusive Or ! Logical Unary Not

Short-Circuit Logical Operators

&& Short-Circuit And || Short-Circuit Or

Assignment Logical Operators

&= And Assignment |= Or Assignment

^= Exclusive Or Assignment

Page 26: Computer Programming 2.1

26

//Relational Operator

public class TestRelationalOper

{ public static void main(String args[])

{ boolean a = true, b = false;

System.out.println(" a = " + a);

System.out.println(" b = " + b);

System.out.println(" a & b = " + (a & b));

System.out.println(" a | b = " + (a | b));

System.out.println(" a ^ b = " + (a ^ b));

System.out.println(" !a = " + (!a)); } }

Page 27: Computer Programming 2.1

27

// Logical Operator ใชส ำหรบ Operands ทมชนดเปน boolean เทำนนpublic class TestBooleanLogicalOper { public static void main(String args[])

{ boolean a = true, b = false;System.out.println("a = true : a = " + a);System.out.println("b = false : b = " + b);System.out.println();

// กลมท 1 Boolean Logical OperatorsSystem.out.println("1. Boolean Logical Operators");

// Logical AndSystem.out.println("Logical And => a & b = " + (a & b));

// Logical OrSystem.out.println("Logical Or => a | b = " + (a | b));

// Logical Exclusive OrSystem.out.println("Logical Exclusive Or => a ^ b = " + (a ^ b));

// Logical Unary NotSystem.out.println("Logical Unary Not => !a = " + (!a));System.out.println();

} }

Page 28: Computer Programming 2.1

28

public class TestShortandAssLogicalOper { public static void main(String args[])

{ boolean a = true, b = false;System.out.println("a = true : a = " + a);System.out.println("b = false : b = " + b);System.out.println();

// กลมท 2 Short-Circuit Logical OperatorsSystem.out.println("2. Short-Circuit Logical Operators");

// Short-Circuit AndSystem.out.println("Short-Circuit And => a && b = " + (a && b));

// Short-Circuit OrSystem.out.println("Short-Circuit Or => a || b = " + (a || b));System.out.println();

// กลมท 3 Assignment Logical OperatorsSystem.out.println("3. Assignment Logical Operators");

// And AssignmentSystem.out.println("And Assignment => a &= b = " + (a &= b));

// Or AssignmentSystem.out.println("Or Assignment => a |= b = " + (a |= b));

// Exclusive Or AssignmentSystem.out.println("Exclusive Or Assignment => a ^= b = " + (a ^= b)); } }

Page 29: Computer Programming 2.1

29

Conditional Operators

(<condition>) ? <expression 1> : <expression 2>

//Conditional Operatorpublic class TestCondOper { public static void main(String args[])

{ int x = Integer.parseInt(args[0]);int y = Integer.parseInt(args[1]);

//input x = 5, y = 3System.out.println((x > y)? x : y); } }

Page 30: Computer Programming 2.1

30

ล าบในการประมวลผลของ Operator

ล าดบ Operator ประเภท Assoc.

1 ()

2 ++ (Increment), -- (Decrement)

!

~ (Complement)

(type_cast)

การค านวณ

boolean

integer

ทกรปแบบ

R

R

R

R

3 *, /, % การค านวณ L

4 +, - การค านวณ L

5 << (Left shift), >> (Right shift),

>>> (Zero fill)

จ านวนเตม L

6 <, <=, >, >=, Instanceof() การเปรยบเทยบobject

L

7 ==, != ขอมลพนฐานและ object

L

Page 31: Computer Programming 2.1

31

ล าดบ Operator ประเภท Assoc.

8 & (Bitwise AND) จ านวนเตม L

9 ^ (Bitwise XOR) จ านวนเตม L

10 | (Bitwise OR) จ านวนเตม L

11 && (AND) boolean L

12 || (OR) boolean L

13 ?: boolean R

14 =, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=,

^=, !=

อน ๆ R

Page 32: Computer Programming 2.1

32

ใหประมวลผลหาคาของนพจนa * b – c != a / b – c && --a > b++ || b % --c > 0

1. Increment และ Decrement Operator

2. ตวกระท าทางคณตศาสตร *, /, %

3. ตวกระท าทางคณตศาสตร -

4. Relational Operator

5. ไมเทากน

6. &&

7. ||

1 1 12 2 2

3 34 4

5

6

7

Page 33: Computer Programming 2.1

33

1. จงแสงผลลพธจากการท างานของโปรแกรมตอไปน (item1.java)

public class three

{ public static void main(String args[])

{ int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6;

int ans1 = c * c + c % b;

int ans2 = b + e / c - c * d;

int ans3 = b * (a - (d / e) / b) * (b - e % c);

int ans4 = a + b - c / d / e * f;

System.out.println("ans1 is " + ans1 + " และ " + "ans2 is " + ans2);

System.out.println("ans3 is " + ans3 + " และ " + "ans4 is " + ans4);

}

}

การบาน ครงท 1

Page 34: Computer Programming 2.1

34

2. จงแสงผลลพธจากการท างานของโปรแกรมตอไปน (item2.java)

public class four{ public static void main(String args[])

{ boolean a = true, b = true, c = true;boolean ans1 = !a && b;boolean ans2 = a && b || c;boolean ans3 = a || (b && c);boolean ans4 = a && b || c;System.out.print("ans1 is " + ans1 + " และ ");System.out.println("ans2 is " + ans2);System.out.print("ans3 is " + ans3 + " และ ");System.out.println("ans4 is " + ans4); } }

Page 35: Computer Programming 2.1

35

3. จากขอมลคอ data=Faculty of Liberal Arts and Scienceจงเขยนโปรแกรม item3.java เพอใหมการแสงผลลพธวา

FLAS พรอมทงใหแสงผลตวอกษร 3 ตวแรกของชอนสต และตวอกษร 3 ตวส ทายของนามสกลนสต

สงภายในวน

Page 36: Computer Programming 2.1

36

การแปลงชน ของตวแปร (Type Conversion)

1.การแปลงชน ขอมลโยอตโนมต (Implicit)

กรณทนพจนประกอบวยตวแปรหลายชน Java จะตรวจสอบชน ขอมลของตวแปรในนพจน แลวเปลยนชน ขอมลใหเปนแบบทใชเนอทในหนวยความจ ามากทส โยจะแปลงชนขอมลโยอตโนมต เมอชน ขอมลของตวแปรสามารถใช

รวมกนไ และตวแปรปลายทางมขนาใหญกวาหรอเทากบตวแปรตนทาง

2.การแปลงชน ขอมลแบบ Explicit หรอ Type Casting

เปนการประกาศเปลยนแปลงประเภทของขอมลโยผใชเอง

(target_type) expression;

Page 37: Computer Programming 2.1

37

3. การแปลงชน ขอมลโยใช Type Wrapper

เปนการสราง Object ขนมากอนทจะเรยก Method มาใชงาน

public double doubleValue()

public float floatValue()

public int intValue()

public long longValue()

public String toString()

Page 38: Computer Programming 2.1

38

// Type Conversion : Implicitpublic class TestImplicit{ public static void main(String args[ ])

{ int a = 15/5;System.out.println("int a = 15/5 => " + a);

// method ชอ toBinaryString จำก Class ชอ Integer เปลยนขอมล Integer ใหอยในรป Binary

System.out.println("a = " + Integer.toBinaryString(a));System.out.println();

// int b = (15/5)*(float)2;float b = a * (float) 2;

// byte c = c*2;byte c = 5;System.out.println("float b = a * (float) 2 => " + b);System.out.println("byte c = 5");System.out.println("b/c => " + b/c);

Page 39: Computer Programming 2.1

39

/* method ชอ floatToIntBits จำก Class ชอ Float เปลยนขอมล Float

ใหอยในรป Bit ของจ ำนวนเตมแบบ Integer */

System.out.println("Integer.toBinaryString(Float.floatToIntBits(b/c))");

System.out.println(Integer.toBinaryString(Float.floatToIntBits(b/c)));

System.out.println();

double d = 10.0d;

System.out.println("double d = 10.0d");

System.out.println("(b+c)/d => " + (b+c)/d);

/* method ชอ doubleToLongBits จำก Class ชอ Double เปลยนขอมล Double

ใหอยในรป Bit ของจ ำนวนเตมแบบ Long

method ชอ toBinaryString จำก Class ชอ Long เปลยนขอมล Long ใหอยในรป Binary */

System.out.println("Long.toBinaryString(Double.doubleToLongBits((b+c)/d))");

System.out.println(Long.toBinaryString(Double.doubleToLongBits((b+c)/d))); }}

Page 40: Computer Programming 2.1

40

// Type Conversion : Explicit หรอ Type Casting

public class TestExplicit

{ public static void main(String[ ] args)

{ char A = '0';

int B = 2;

System.out.println("char A = '0' => "+(int)A);

System.out.println("int B = 2");

System.out.println("(int) (A/B) => " + (int) (A/B)); } }

Page 41: Computer Programming 2.1

41

/* Type Conversion : Type Wrapper เปนกำรสรำง Object ขนมำกอนจะเรยก Method มำใชงำน*/

public class TestTypeWrapper{ public static void main(String[ ] args)

{ String var1 = "25.75";Double d1 = Double.valueOf(var1);System.out.println("String var1 = 25.75");System.out.println("Double d1 = Double.valueOf(var1)");System.out.println("d1 => " + d1);double d2 = d1.doubleValue();System.out.println("double d2 = d1.doubleValue();");System.out.println("d2 => " + d2);System.out.println();

Page 42: Computer Programming 2.1

42

String var2 = "500";

Integer int1 = Integer.valueOf(var2);

System.out.println("String var2 = 500");

System.out.println("Integer int1 = Integer.valueOf(var2)");

System.out.println("int 1 => " + int1);

int int2 = int1.intValue();

System.out.println("int int2 = int1.intValue()");

System.out.println("int 2 => " + int2); } }

Page 43: Computer Programming 2.1

43

public class TestChar { public static void main(String args[])

{ System.out.println(" ASCII Character ");System.out.println(" for (int i = 32; i < 138 - 10; i+=10) ");System.out.println(" for (int j = i; j < i+10; j++)");for (int i = 32; i < 138 - 10; i+=10){ for (int j = i; j < i+10; j++)

System.out.print(j + " " + (char)j + " ");System.out.println(); } } }

Page 44: Computer Programming 2.1

44

public class TestChar1

{ public static void main(String args[])

{ char ch1, ch2;

ch1 = 88;

ch2 = 'Y';

System.out.println("ch1 is " + ch1);

System.out.println("ch2 is " + ch2); } }

Page 45: Computer Programming 2.1

45

Method toLowerCase ใชในการแปลงแบบอกษรจากตวพมพใหญเปนตวพมพเลก

รปแบบ ชอตวแปร.toLowerCase()

ตวอยาง String test = initials.toLowerCase()

Method toUpperCase ใชในเปนการแปลงแบบอกษรจากตวพมพเลกเปนตวพมพใหญ

รปแบบ ชอตวแปร.toUpperCase()

ตวอยาง String test = initials.toUpperCase()

การแปลงแบบอกษร

Page 46: Computer Programming 2.1

46

/* Method toLowerCase แปลงจำกอกษรตวพมพใหญเปนตวพมพเลก

Method toUpperCase แปลงจำกอกษรตวพมพใหญเปนตวพมพเลก*/

public class TesttoLowerCase

{ public static void main(String[ ] args)

{ String firstName = "Aปปกร";

String lastName = "Mสงทอง";

String initials = firstName.substring(0,1) + lastName.substring(0,1);

int age = 27;

String password = initials.toLowerCase( ) + age;

System.out.println(password); } }

Page 47: Computer Programming 2.1

47

การก าหนรปแบบของตวเลข

Method getNumberInstance ใชก าหนจ านวนตวเลขหลงทศนยมโยก าหนจ านวนสงส และต าส ซงตอง import package ของ Java ชอ ‘java.text’ เขามาใชงานวย

ตวอยาง

NumberFormat format1 =NumberFormat.getNumberInstance();

format1.setMaximumFractionDigits(7);

format1.setMinimumFractionDigits(5);

Method getCurrencyInstance หากตองการใหมเครองหมาย $ ปรากฏรวมวยโยไมตองพมพเอง

ตวอยาง

NumberFormat format2 = NumberFormat.getCurrencyInstance();

Page 48: Computer Programming 2.1

48

import java.text.*;public class TestgetNumberInstance{ public static void main (String [ ] args) // NumberFormat.getNumberInstance()

{ int quarters = 2;int dollars = 3;double total = dollars + quarters * 0.125;final double TAX_RATE = 8.5;double tax = total + TAX_RATE / 100;NumberFormat format1 = NumberFormat.getNumberInstance();

format1.setMaximumFractionDigits(7);format1.setMinimumFractionDigits(5);System.out.println("Total : $" + format1.format(total));System.out.println("Tax : $" + format1.format(tax));

// NumberFormat.getCurrencyInstance()NumberFormat format2 = NumberFormat.getCurrencyInstance();

System.out.println("Total : $" + format2.format(total));System.out.println("Tax : $" + format2.format(tax)); } }

Page 49: Computer Programming 2.1

49

import java.text.*;

DecimalFormat ชอออบเจกต = new DecimalFormat(pattern);

Page 50: Computer Programming 2.1

50

Page 51: Computer Programming 2.1

51

การรบขอมลทางแปนพมพ

ตองใชค าสง IOException และ Package ชอ java.io รวมกนเสมอ

การใช System.in.read

ใชรบขอมลเพยง 1 ตวอกษร โยขอมลทเปน ASCII ตองแปลงเปนตวอกษรกอนวยวธ Type Casting คอ น าชน ของขอมลผลลพธไปไวหนาของขอมลทตองการแปลง และ

ตวอยาง c = (char)System.in.read();

Page 52: Computer Programming 2.1

52

import java.io.*;

class TestSysteminRead

{ public static void main(String args [ ]) throws IOException

{ char c;

System.out.print("Please key a character => ");

c = (char)System.in.read();

System.out.println("Your input is " + c); } }

Page 53: Computer Programming 2.1

53

import java.io.*;

class TestSysteminRead1

{ public static void main(String args [ ]) throws IOException

{ char buf = '\0';

System.out.println("Initial buf is " + buf);

StringBuffer bufOut = new StringBuffer();

System.out.print("Please key one character => ");

while ((buf = (char)System.in.read()) != '\n')

{ bufOut.append(buf); }

System.out.println("Your input data is " + bufOut); } }

Page 54: Computer Programming 2.1

54

การใช BufferedReader รวมกบ InputStreamReader

รปแบบ

InputStreamReader reader = new InputStreamReader (System.in);

BufferedReader Stdin =new BufferedReader (reader);

หรอ

BufferedReader Stdin = new BufferedReader

(new InputStreamReader (System.in));

และใช Method ชอ readLine ในการรบขอมลทางจอภาพงน

Input = Stdin.readLine();

ในการตรวจสอบความผ พลาสามารถท าไโยใชค าสง try และ catch

Page 55: Computer Programming 2.1

55

import java.io.*;

class TestBufferedReader

{ public static void main(String[ ] args) throws IOException

{ BufferedReader Stdin = new BufferedReader

(new InputStreamReader (System.in));

String Input = " ";

System.out.print("Please key any data => ");

Input = Stdin.readLine( );

System.out.println("Your input data is => " + Input); } }

Page 56: Computer Programming 2.1

56

import java.io.*;class TestTryCatch{ public static void main(String[ ] args)

{ try{ BufferedReader Stdin =new BufferedReader

(new InputStreamReader (System.in));String Input = " ";System.out.print("Please key any data => ");Input = Stdin.readLine( );System.out.println("Your input data is => " + Input); }

catch (IOException e){ System.out.print(e);

System.exit(1); } } }

Page 57: Computer Programming 2.1

57

// import java.io.BufferedReader;// import java.io.InputStreamReader;// import java.io.IOException;import java.io.*;class TestCalc{ public static void main(String args [ ]) throws IOException

{ final double A_Level = 4.0;final double B_Level = 3.0;BufferedReader stdin = new BufferedReader

(new InputStreamReader (System.in));String input = " ";System.out.print("Amount of Subject grade A => ");input = stdin.readLine( );int a = Integer.parseInt(input);

System.out.print("Amount of Subject grade B => ");input = stdin.readLine( );int b = Integer.parseInt(input);double GPA = ((a * A_Level) + (b * B_Level))/ (a + b);System.out.println("GPA is " + GPA); } }

Page 58: Computer Programming 2.1

58

การรบขอมลจากแปนพมพโยใช DataInputStream

คลาส DataInputStream เปนสบคลาสของ FilterInputStream และ InputStream ตวแปรทตองผานใหคลาสDataInputStream คอ System.in (InputStream)

รปแบบ

DataInput input = new DataInputStream(System.in);

คลาส DataInputStream ม Method ในการอานขอมล งน

1. readLine() อานขอมลตวอกษรทจบวยการขนบรรท ใหม

2. readInt() และ readLong() อานขอมลจ านวนเตม

3. readFloat() และ readDouble() อานขอมลจ านวนจรง

4. readUnsignedByte() อานจ านวนเตมทไมรวมเครองหมาย

Page 59: Computer Programming 2.1

59

import java.io.*;public class TestDataInput { public static void main(String args [ ]) throws IOException

{ DataInput input = new DataInputStream(System.in);String text = " ";int noOfguest = 0;double rate = 0;System.out.print("Amount of guest => : ");try{ noOfguest = Integer.parseInt(input.readLine());

// System.out.println("Amount of guest is " + noOfguest);System.out.print("Rate of rent per person per night is ");Double x = new Double(input.readLine());rate = x.doubleValue();}

catch (Exception e){ System.out.print(e);

System.exit(1);}System.out.println("Total rent is " + (rate * noOfguest)); } }

Page 60: Computer Programming 2.1

60

แบบฝกห ท 1จงเขยนโปรแกรมเพอค านวณผลลพธของนพจนตอไปน

5 + 1 / 73 * 3 + 3 % 22 + 5 / 3 + -3 * 42 * (1 + -(4 / 5) / 2) * (2 - 5 % 3)

จงเขยนโปรแกรมเพอตรวจสอบผลการค านวณของนพจนตอไปน

a+b*c!=b/c%2&&a*b/--c||b++/a>0a*b/c==b>0||a%b+--a/c++