Object-Oriented Programming เนื้อหามิดเทอม 2023/1

August 23, 2023

Midterm

  • Object, Class
  • Method
  • Basic Array
  • Array List, Multidimensional Arrays

Midterm exam guideline (77 คะแนน, 30%)

ออกถึง Array 2 มิติ / ArrayList

  • ตอนที่ 1 (14 คะแนน)
    • ทฤษฎี OOP (True / False)
    • หาผลลัพธ์
  • ตอนที่ 2 (28 คะแนน)
    • หาผลลัพธ์โปรแกรม OO + Array
    • แก้โปรแกรม + ดู Error ทุกบท
  • ตอนที่ 3 (3 ข้อ, 35 คะแนน)
    • ArrayList
    • OO
    • Algorithm
  • บอกในคราส (ไม่ชัวร์)
    • Binary Search

Weeks

  • 2 12/07/2023

    • String.format("", vars, ...)

      • เช่น

        System.out.println(String.format("Area of the circle (radius = %.2f): %.3f", radius, area)); // Area of the circle (radius = 1.00): 3.142
    • Primative Data type: ตัวแปรพื้นฐาน

    • Casting: การแปลงประเภทข้อมูล

      • Conversion: การแปลงประเภทข้อมูล
        • Widening conversion: ข้อมูลเล็ก \rArr ข้อมูลใหญ่
        • Narrowing conversion: ข้อมูลใหญ่ \rArr ข้อมูลเล็ก
      • Explicit casting: บังคับ cast (ข้อมูลใหญ่ \rArr ข้อมูลเล็ก)
      • Implicit casting: Java cast มาให้ (ข้อมูลเล็ก \rArr ข้อมูลใหญ่)
    • Reference Data type: ไม่ได้เก็บโดยตรงแต่เก็บ Refernce ของมัน:

      • Class: เช่น

        // ชนิด Scanner (ประเภท Class) ชื่อ sc // ละต้องใส่ Object เข้าไป Scanner(System.in); // new จะเป็นการ Allowcate memory ให้ Object Scanner sc = new Scanner(System.in); // การรับ Input, เรียก method next ของ data type นั้นๆ double radius = sc.nextDouble(); // สามารถทำได้เพราะ Int ขนาดเล็กกว่า Double radius = sc.nextInt(); // XX ไม่ได้เพราะ Double ใหญ่กว่า XX int x = sc.nextDouble();
      • Array, String

    • การใช้ float / double / long / int

      // เพราะทศนิยมจะ default ที่ double // XX ไม่ได้ XX float f = 1.0; // ได้ float f = 1.0f; // จำนวนเต็ม default ที่ int // ได้ หรือไม่มี l ก็ได้ เพราะ int เล็กกว่า long long l = 10l; 1.23E+7 1.11E-7
      int a = 5 / 2; // a = 2 double b = 5.0 / 2; // b = 2.5 b = 5 / 2; // b = 2.0 a = (int)(5.0 / 2); // a = 2
    • i++, ++i

      // i++: ใช้ก่อนค่อยบวก int i = 10; int a = 10 * (++i); // a = 110, i = 11 // ++i: บวกแล้วค่อยนำมาใช้ต่อ i = 10; a = 10 * (i++); // a = 100, i = 11
    • แปลง string เป็น int / double

      // Int Wrapper Class: Integer // เป็น Refererence data type int y = Integer.parseInt("100"); double x = Double.parseDouble("100.10");
    • Assignment Precedence: จะทําจากขวาไปซ้ายเรียกว่า Right-associative

      int a = 10, b = 10, c = 10; a = b += c = 5; System.out.println(String.format("a: %d, b: %d, c: %d", a, b, c)); // a: 15, b: 15, c: 5
  • 4 26/07/2023

    • Instantiation: การสร้าง Object (Instance) จากคราส

      • Methods: Function, Instance Method
      • Attribute: Field, Instance Variable
    • UML Class Diagram: เขียนอธิบาย Class และ Object

      • Visibility: ระดับการมองเห็น
        • - Private: ใช้ได้ภายใน Class
        • # Protected: ใช้ได้เฉพาะคราสแม่และลูก (Inheritance)
        • + Public: ใช้ได้จากทุกๆ คราส
        • <ins>ขีดเส้นใต้</ins>: static ตัวแปรสาธารณะ
      • Association: ความสัมพันธ์ระหว่าง Class
        • Composition: มีคราสอื่นเป็นส่วนประกอบ
      Composition
      • เช่น
      UML Class Diagram
    • Methods

      class A { // Method signator: Return Type, Visibility, Parameter public void B() { // Methods Body ... } }
      • Mutator methods (setter): เปลี่ยนข้อมูลภายในวัตถุ
      public void setRadius(double r) { this.radius = r; }
      • Accessor methods (getter): ดึงข้อมูลจากวัตถุ
      public double getRadius() { return this.radius; }

      Instance methods (non-static) คือ methods ที่ต้องมี Instanciate Object ก่อนถึงจะใช้ได้ แต่ถ้าเป็น static methods จะสามารถใช้ได้เลย

      public void getArea() { // ✅ return max(10, 20); } public static int max(int num1, int num2) { // ❌ double a = this.getArea(); if(num1 > num2) { return num1; } return num2; }
      • การเรียก

        ⬇️ เรียก / จาก ➡️staticnon-static
        static
        non-static
      • Fields / Attribute

        • Instance variable: จะเกิดเวลามี Object
        • Class variable: static: ตัวแปรสาธารณะ
          • Class constant: ตัวแปร final สาธารณะ
        class A { // Instance variable int i; // Class / Static variable static int j; // Class constant final static int j; }

        Concept การเรียกเหมือนกับ methods

      • Constructors

        • ชื่อเดียวกับ Class
        • เพื่อกำหนดค่าเริ่มต้นให้กับ Object (Initializing Object)
        • Constructors ที่ไม่ Parameters จะเรียกว่า Default Constructor
        class Circle { private double r; // Default Constructor Circle() { r = 0; } Circle(double r) { this.r = r; } } myCircle = new Circle(5.0);
    • Visibility

      • default: จะเข้าถึงได้โดยทุกๆคลาสใน package เดียวกัน
      • public: เรียกได้ทั้งหมด ทั้ง package เดียวกันและต่าง package
      • private: สามารถจะเข้าถึงได้เฉพาะภายในคลาส
      • protected: การอ้างถึงต้องอยู่ในรูปแบบของ this หรือ super เท่านั้น
  • 5 09/08/2023

    • ขอบเขตของตัวแปร: Instance scope

      • เห็นตั้งแต่ { ของ Class ไปถึง } ของ Class

      • แต่สามารถใช้ this เพื่อบอกถึง Object ตัวนั้นๆ ได้ๆ

        class A { // Instance scope int x; // Instance variable (ตำแหน่งไม่มีผล) void f() { // Instance method / Local scope int x; // Local variable // By default will use nearest variable x = 10; // Local variable this.x = 20; // Instance variable } int y; // This is cool }
      • สามารถใช้ this กับ Constructor ได้เหมือนกัน

        class A { int x; A() { this(1); // Will cal another constructor, A(1) } A(int x) { this.x = x; } }
    • Methods: กลุ่มคำสั่ง

      • Methods signature: method name + parameter list

        • Modifier
        • Return value type
        • Method name
        • Parameters list
      • Methods body: ส่วนชุดคำสั่ง

        // Method signature // public static: Method modifier // int: Return value type // (int num1, int num2): Parameter list public static int main(int num1, int num2) { // Method body int result; if(num1 > num2) result = num1; else result = num2; // return value return result }
      • Invoke a method: การเรียกใช้ methods

        // จะมีการ Copy ค่า x, y ไปใน method max // x, y จะเรียกว่า actual paramters หรือ arguments int z = max(x, y);
      • ถ้า Code มี if และ else if แต่ไม่มี else อาจจะทำให้เกิด Complitation error เพราะ java จะเข้าใจว่าไม่มี Return value

        public static int xMethod(int n) { if (n > 0) return 1; else if (n == 0) return 0; else if (n < 0) return -1; }
        • แก้เป็น ถึงจะมั่นใจได้แน่นอนว่ามี Return value
        public static int xMethod(int n) { if (n > 0) return 1; else if (n == 0) return 0; else return -1; }
      • ประเภทของ Methods

        • ไม่มี return type / ไม่มี arguments

          public void add() {}
        • ไม่มี return type / มี arguments

          public void add(int x, int y) {}
        • มี return type / ไม่มี arguments

          public int add() {}
        • มี return type / มี arguments

          public int add(int x, int y) {}
      • Pass parameters ของ java จะเป็นการ Pass by Value เสมอ หรือการ Copy value เข้าไปใน methods เมื่อเรียกใช้ methods

        • ถ้าเป็น Object จะเป็นการโยน Reference เข้าไป (ตย. Circle@a09ee92)
        • เวลาดึงค่าจะได้ค่าเดียวกัน เวลาแก้ก็จะแก้ค่าเดียวกันเช่นกัน หรือ Pass by Refernece
      • Ambiguous Invocation: ถ้ามี Methods ที่สามารถใช้งานได้สองอัน จะได้ Compiler Error เนื่องจาก Java แยกไม่ออกว่าควรใช้อันไหน

        public class AmbiguousOverloading { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { if (num1 > num2) return num1; else return num2; } }
    • ในห้อง 1, 5, 6, 7 ที่เหลือการบ้าน

  • 7 23/08/2023

    • Lab 1,2,5 การบ้านทุกบททุกข้อภายในหลังสอบ 1 วีค

    • Proposal เกมที่จะทำ

      • ชื่อเกม
      • ขอบเขต
        • ตัวอย่างหน้าจอ
        • ตัวละคร
    • Array 1d

      // เก็บที่อยู่ใน Stack memory, จอง 10 ช่อง int[] x; // Dynamic memory allocate // สร้างช่องที่ Heap memory int[] x = new int[10];
    • Array 2d

      // 1. Define array variable // in stack memory int[][] x; // 2. Create array and assign referece to variable // in heap memory x = new int[10][10]; // or 2 in 1 // x is in stack memory, and new int[10][10] is in heap memory int[][] x = new int[10][10];
      • Pass array to methods

        public static int sum2DArray(int[][] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { sum += arr[i][j]; } } return sum; }
      • Return array from methods

        public static int[][] inputArray(int row, int col) { int[][] out = new int[row][col]; Scanner sc = new Scanner(System.in); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { out[i][j] = sc.nextInt(); } } return out; }
      • Array of object

        int studentCount = 3; // Array of object reference Student[] stds = new Student[studentCount]; for (int i = 0; i < studentCount; i++) { System.out.print(String.format("%d. ", i + 1)); stds[i] = new Student(sc.next(), sc.next(), sc.nextDouble()); }
        Array of Class
    • ArrayList

      • ไม่ต้องระบุขนาด (ใช้บ่อยเด้อ)

        ArrayList<Student> arr = new ArrayList<>(); Scanner sc = new Scanner(System.in); System.out.print("Enter number of student: "); int n = sc.nextInt(); for (int i = 0; i < n; i++) { Student newStd = new Student(sc.next(), sc.next(), sc.nextDouble()); arr.add(newStd); } // looping // size() is a method!! for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i)); } // remove arr.remove(0);
      • รับ Parameters เป็น ArrayList

        public static void printStudentArrayList(ArrayList<Student> arr) { for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i)); } }
    • Wrapper Class จะแปลงจาก Primative class \rArr Reference class

      • จาก int \rArr Integer

      • จาก double \rArr Double

      • เช่น

        ArrayList<Integer> arrInt = new ArrayList<>(); arrInt.add(1); // Boxing int x = arrInt.get(1); // Unboxing
    • ประโยชน์ของ Array

      • Searching
      • Sorting
        • Selection sort

          loop value, idx in arr from n-1 to 0 max_val = max(arr[0, idx]) swap(max_val, value)
        • Insertion sort

          Insertion sort