variable, data type, expression, operators data input, data output

27
Variable, Data type, Expression, Operators Data input, Data output Ubon Ratchathani University Mukdahan Campus : อ.ออออออ ออออ อออ 1 1106 102 ออออออออ ออออออออออออออออ (Object – Oriented Programming)

Upload: sandro

Post on 05-Jan-2016

89 views

Category:

Documents


5 download

DESCRIPTION

1106 102 การเขียนโปรแกรมเชิงวัตถุ (Object – Oriented Programming). Variable, Data type, Expression, Operators Data input, Data output. Ubon R atchathani University Mukdahan Campus : อ.วรยุทธ วงศ์นิล. Variable. การประกาศตัวแปร ต้องตั้งชื่อตัวแปร ต้องกำหนดประเภทข้อมูลที่จะเก็บ - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Variable, Data type, Expression, Operators Data input, Data output

1

Variable, Data type, Expression, OperatorsData input, Data outputUbon Ratchathani University Mukdahan Campus : อ.วรยุ�ทธ วงศ์�นิ ล

1106 102 การเขี�ยุนิโปรแกรมเชิ ง ว�ตถุ�

(Object – Oriented Programming)

Page 2: Variable, Data type, Expression, Operators Data input, Data output

Variable การประกาศ์ต�วแปร ต�องต��งชิ� อต�วแปร ต�องก!าหนิดประเภทขี�อม%ลท� จะเก'บ ต�องประกาศ์ก)อนิใชิ�

2

public class HelloWorld { public static void main(String[] args) { int number = 0; double sum = 0.0; }}

Page 3: Variable, Data type, Expression, Operators Data input, Data output

Variable ต�วแปร คื�อ ชิ� อท� ก!าหนิดขี,�นิมาเพื่� อใชิ�ในิการจ�ดเก'บ

ขี�อม%ล ร%ปแบบการต��งชิ� อ data_type variableName

- ชิ� อประกอบด�วยุต�วอ�กษร ต�วเลขี $ หร�อ _- ห�ามขี,�นิต�นิด�วยุต�วเลขี- ต�วอ�กษรใหญ่)ไม)เหม�อนิต�วเล'ก- ต�องไม)ซ้ำ!�าก�บคื!าสงวนิขีองภาษาจาวา- ต�องไม)ซ้ำ!�าก�บชิ� ออ� นิขีองโปรแกรมท� ใชิ�มาก)อนิ

3

Page 4: Variable, Data type, Expression, Operators Data input, Data output

Variablevirus11 pailin Publicmonth_of_year

7eleven String Yahoo! Kilo per-hour

ร%ปแบบการก!าหนิดคื)า variableName = variable หร�ออาจจะท!าการก!าหนิดคื)าให�ต�วแปรในิขี��นิตอนิการ

ประกาศ์เลยุก'ได� เชิ)นิ float score = 73.25f

4

Page 5: Variable, Data type, Expression, Operators Data input, Data output

Variable

ถุ%ก

ผิ ด5

int n ;double a, b, c ;double x = 0, y, z = 8.5 ;String s = “hello” ;int p ; double q ;Int s ;string t ;y = 6.6 ;double y ;int p, double q ;

Page 6: Variable, Data type, Expression, Operators Data input, Data output

Variableการให�คื)าก�บต�วแปร ต�วแปรเก'บได�เฉพื่าะแบบท� ประกาศ์ไว�

int a = 20 ; //ถุ%กdouble x = 1.25 ; //ถุ%กint b = 1.5 ; //ผิ ดdouble y = 2 ; //ถุ%กint n = “1” ; //ผิ ดString s = 1 ; //ผิ ด

6

Page 7: Variable, Data type, Expression, Operators Data input, Data output

Variable ต�วแปร (variable) ม�คืวามหมายุเด�ยุวก�นิก�บฟิ6ลด� (field) ชินิ ดขีองต�วแปร

- instance variable (ท� ไม)ใชิ)ต�วแปรแบบ static) เป7นิคื)าท� จะไม)เหม�อนิก�นิในิแต)ละ object

- class variable (ต�วแปร static) คื�อต�วแปรในิคืลาส จะม�เพื่�ยุงชิ�ดเด�ยุวเท)านิ��นิ

- local variable คื�อ ต�วแปรท� ถุ%กใชิ�ภายุในิเมธอดเท)านิ��นิ เพื่� อใชิ�เก'บขี�อม%ลชิ� วคืราว

- parameters คื�อ ต�วแปรท� ถุ%กส)งไปยุ�งเมธอดต)างๆ

7

Page 8: Variable, Data type, Expression, Operators Data input, Data output

Operators ต�วด!าเนิ นิการทางคืณิ ตศ์าสตร�ม�ล!าด�บคืวามส!าคื�ญ่

ไม)เท)าก�นิ ด�งนิ��

8

ลำ��ดั�บที่� เครื่ องหม�ย1 ()2 ++, --3 *, /, %4 +, -5 +=, -=,

*=, /=, %=

Page 9: Variable, Data type, Expression, Operators Data input, Data output

Variableจ!านิวนิเต'ม VS จ!านิวนิจร ง5 / 2 ผิลล�พื่ธ� 2

5.0 / 2.0 ” 2.5

5.0 / 2 ” 2.5

5 / 2.0 ” 2.5

5.25 / 0.5 ” 10.5

9

Page 10: Variable, Data type, Expression, Operators Data input, Data output

Expressionต�วอยุ)าง

2 * 3 + 8 / - (2 – 4) – 1

= 2 * 3 + 8 / - ( - 2) – 1

= 2 * 3 + 8 / 2 – 1

= 6 + 8 / 2 – 1

= 6 + 4 – 1

= 10 – 1

= 9 10

Page 11: Variable, Data type, Expression, Operators Data input, Data output

Expression 2 + 3 * 4 = - 5 – 3 * 2 = (5 + 1 * 5) % 5 + 1 = 2 * (5 + (17 % 3 / 2) + 26) * 2 + 4 =

11

Page 12: Variable, Data type, Expression, Operators Data input, Data output

ก�รื่ต่�อ StringSystem.out.println( “a” + “b” ); //”ab”

System.out.println( “a” + 1 ); //”a1”

System.out.println( 1 + “a” ); //

System.out.println( “1” + “2” ); //

System.out.println( “1” + 2 ); //

System.out.println( 1 + “2” ); //

System.out.println( “a” + 1 + 2 ); //

System.out.println( “a” + ( 1 + 2) );//

System.out.println( “a” + ( 1 – 2 )); //

System.out.println( “a” + 1 – 2 ); //

12

Page 13: Variable, Data type, Expression, Operators Data input, Data output

Expressionต�วอยุ)าง การหาพื่��นิท� ขีองวงกลมร�ศ์ม� 4

ได�ผิลล�พื่ธ�เป7นิ พื่��นิท� วงกลมร�ศ์ม� 4.0 = 50.26544

13

public class HelloWorld { public static void main(String[] args) { double r = 4; double pi = 3.14159; double area = pi * r * r; System.out.println("พื่��นิท� วงกลมร�ศ์ม� "+ r +" = " + area); }}

Page 14: Variable, Data type, Expression, Operators Data input, Data output

Type Conversionint a = 1, b = 2, c ;

double d = 2.6 ;

double x = 7 ;

c = (int)d ; ผิลล�พื่ธ� 2

d = (double)a; ” 1.0

d = (double)( a / b ); ” 0

14

Page 15: Variable, Data type, Expression, Operators Data input, Data output

Constantการก!าหนิดคื)าคืงท� ให�ก�บต�วแปรสามารถุท!าได�โดยุเพื่ มคื!าว)า “final” เขี�าไปเชิ)นิ final float TAX_RATE = 0.0725f ;

หากต�องการให�ต�วแปรหร�อเมธอดใดๆ ถุ%กเร�ยุกใชิ�ได�จากท�กท� หร�อท�ก class สามารถุท!าได�โดยุเพื่ มคื!าว)า “static”

เชิ)นิ static final float TAX_RATE = 0.0725f ;

15

Page 16: Variable, Data type, Expression, Operators Data input, Data output

Data input (Package java.io)แบ)งออกเป7นิ 2 ว ธ� คื�อ

- การร�บขี�อม%ลโดยุใชิ�เมธอด read() เป7นิการร�บท�ละต�วอ�กษรแบบ char

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

- การใชิ� BufferedReader ร)วมก�บ InputStreamReader โดยุใชิ�เมธอด readLine() ร�บขี�อม%ลแบบ String ทางแป:นิพื่ มพื่�คืร��งละหลายุต�วอ�กษร

InputStreamReader reader = new InputStreamReader(System.in);

BufferedReader stdin = new BufferedReader(reader);

String input = stdin.readLine();

16

Page 17: Variable, Data type, Expression, Operators Data input, Data output

ก�รื่ใช้� read()import java.io.* ;

class Read1{

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

char ch;

System.out.print(“Input one character : ”);

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

System.out.println(“Your input : ” + ch );

}

}

17

Page 18: Variable, Data type, Expression, Operators Data input, Data output

การใชิ� BufferedReader ร)วมก�บ InputStreamReader

import java.io.*;

class Read2 {

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

InputStreamReader reader = new InputStreamReader(System.in);

BufferedReader stdin = new BufferedReader(reader);

String input = “”;

System.out.println(“Please input any character : ”);

input = stdin.readLine();

System.out.println(“Your input :” + input);

}

}18

Page 19: Variable, Data type, Expression, Operators Data input, Data output

Data input (Package java.util) เป7นิการร�บขี�อม%ลท� เป7นิต�วเลขีและนิ!าไปคื!านิวณิต)อได�เลยุด�วยุคืลาส Scanner โดยุใชิ�เมธอด nextInt()

import java.util.*;

public class InputTest1{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println(“Please input number 1”);

int num1 = sc.nextInt();

System.out.println(“Please input number2”);

int num2 = sc.nextInt();

int result = num1 + num2;

System.out.println(“num1 + num2 = ” + result);

}

}19

Page 20: Variable, Data type, Expression, Operators Data input, Data output

Data input (Package java.util)

คืลาส Scanner จะไม)ม� Exception มาตรวจสอบคืวามผิ ดพื่ลาดขีองขี�อม%ล แต)จะม�เมธอด

hasNext() ไว�ตรวจสอบเง� อนิไขีแบบ boolean

NextLine() ไว�เล� อนิไปท� บรรท�ดถุ�ดไปมาชิ)วยุในิการตรวจสอบ

20

Page 21: Variable, Data type, Expression, Operators Data input, Data output

Data input & output (Swing)

Swing ถุ�อ package หนิ, งในิ Java ท� ม�การท!างานิขีองขี�อม%ลร�บเขี�าและการแสดงผิลในิร%ปแบบกราฟิ6ก เร�ยุกใชิ�ด�วยุคื!าส� ง

Import javax.swing.* ;

21

Page 22: Variable, Data type, Expression, Operators Data input, Data output

JOptionPaneคืลาส JOptionPane ม�เมธอดหล�กอยุ%) 2 เมธอด คื�อ

- showInputDialog() คื�อ เมธอดส!าหร�บสร�างจอภาพื่เพื่� อให� user ป:อนิขี�อม%ล ซ้ำ, งเมธอดจะร�บขี�อม%ลท� อยุ%)ในิร%ปขีอง String

String in =JOptionPane.showInputDialog(“input”);

- showMessageDialog() คื�อ เมธอดท� ใชิ�แสดงผิลล�พื่ธ�แจ�งแก) user เท)านิ��นิ JOptionPane.showMessageDialog(nulll, “sum is :”, “result”, JOptionPane.INFORMATION_MESSAGE);

22

Page 23: Variable, Data type, Expression, Operators Data input, Data output

Data input & output (Swing)

ต�วอยุ)างการแสดงผิลแบบ GUI

import javax.swing.* ;

Class TestSwing{

public static void main(String args[]){

JOptionPane.showMessageDialog(null, “Welcom\

nto\nJava\nProgramming!”);

System.exit(0);

}

} 23

Page 24: Variable, Data type, Expression, Operators Data input, Data output

ช้นิ�ดัของจอภ�พที่�ใช้�แสดังในิ showMessageDialog()

Message Dialog Type

ก�รื่ที่��ง�นิ

JOptionPane.ERROR_MESSAGE

แสดงจอภาพื่ให�ทราบว)าม� Error เก ดขี,�นิ

JOptionPane.INFORMATION_MESSAGE

แสดงจอภาพื่เพื่� อแสดงผิลล�พื่ธ� (Default)

JOptionPane.WARNING_MESSAGE

แสดงจอภาพื่เพื่� อเต�อนิ

JOptionPane.QUESTION_MESSAGE

แสดงจอภาพื่เพื่� อถุาม

24

Page 25: Variable, Data type, Expression, Operators Data input, Data output

ต่�วอย��ง ก�รื่ห�ผลำรื่วมของเลำข 2 จ��นิวนิ Import javax.swing.JOptionPane

class Addition{

public static void main(String args[]){

String firstNum, secondNum;

int num1, num2, sum;

firstNum=JOptionPane.showInputDialog(“Enter first integer”);

secondNum=JOptionPane.showInputDialog(“Enter second integer”);

num1=Integer.parseInt(firstNum);

num2=Integer.parseInt(secondNum);

sum=num1 + num2;

JOptionPane.showMessageDialog(null, “The sum is” + sum, “Result”, JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

} 25

Page 26: Variable, Data type, Expression, Operators Data input, Data output

Lab 3ให�เขี�ยุนิโปรแกรมคื!านิวณิจ!านิวนิเง นิต)อเด�อนิในิการผิ)อนิรถุ Toyota new Vios

โดยุม�เง� อนิไขีคื�อ - ราคืารถุ 559,000 บาท- user สามารถุก!าหนิดเอง ต�องการดาวนิ�ก�

เปอร�เซ้ำ'นิต�ก'ได�- ท� เหล�อผิ)อนิโดยุให� user กรอก จ!านิวนิเด�อนิท�

ต�องการผิ)อนิ เชิ)นิ 12 24 36 48 60 72 หร�อ 84 เด�อนิ- ไม)ม�ดอกเบ��ยุในิการผิ)อนิ

26

Page 27: Variable, Data type, Expression, Operators Data input, Data output

Lab 3ต่�วอย��งผลำลำ�พธ์%

เปอร�เซ้ำ'นิต�ท� ต�องการดาวนิ� : 25จ!านิวนิเด�อนิท� ต�องการผิ)อนิ : 48คื�ณิต�องผิ)อนิชิ!าระเป7นิเง นิ 8734.38 บาท

ต)อเด�อนิ

27