DES231 · Lab 1 · ตอนที่ 2

เรียกตัวเองให้จบ และขยับของในอาเรย์ให้ถูกทาง

จบตอนนี้น้องจะเขียนเมธอดที่เรียกตัวเองได้เองจากโจทย์เปล่า ๆ และรู้ว่าเวลาแทรกหรือลบของในอาเรย์ ลูปต้องเดินไปทางไหนถึงจะไม่ทับข้อมูลครับ กดเล่นทีละฉาก เมื่อถึงคำถาม เสียงจะหยุดรอให้น้องตอบครับ

ประมาณ 20 นาที ของที่น้องมีแล้ว: คลาส · เมธอด · constructor · main · for · while · if/else · ลูปซ้อนลูป แผง “สามคำถามก่อนเขียนลูป” จาก Lab 0 คำถามให้กดตอบ 16 จุด โจทย์ในใบงานครบทั้ง 5 ข้อ

1ทำซ้ำโดยไม่ใช้ลูป ทำได้ไหม

2สองกรณีที่ทุกฟังก์ชันเรียกตัวเองต้องมี

กฎสามข้อจากสไลด์หนึ่ง ต้องมีกรณีหยุด (base case) · สอง ทุกครั้งที่เรียกต้องเปลี่ยนค่าให้เข้าใกล้จุดหยุด · สาม ต้องเรียกตัวเองจริง ๆ · บรรทัด return n * factorial(n - 1); ยังคืนค่าไม่ได้จนกว่างานที่เปิดใหม่จะคืนค่ากลับมา — งานที่เปิดทีหลัง จบก่อนเสมอครับ

3สามคำถามก่อนเขียนฟังก์ชันเรียกตัวเอง

รูปทรงเหมือนแผงเดิมของ Lab 0 เป๊ะ — สามช่อง กรอกครบเมื่อไรโค้ดออกมาเอง ต่างแค่คำถามข้างในครับ

4โจทย์เรียกตัวเองในใบงาน — ลองทำเองทีละข้อ

Exercise 1subsum — บวกลบสลับกันไป
Complete the recursive program for the subtraction sum (subsum) for a positive input n. In this problem, you need to determine the base case and the general case by yourself.
เปิดโจทย์และวิธีทำ Exercise 1
Exercise 2sumDigit — บวกเลขทีละหลัก
Complete the recursive program for the sumDigit that computes the sum of the digits in an input which assumes to be a positive integer.
เปิดโจทย์และวิธีทำ Exercise 2
เครื่องมือใหม่สองอัน% 10 คือหารเอาเศษ ได้หลักสุดท้าย · / 10 บนจำนวนเต็มคือตัดหลักสุดท้ายทิ้ง เพราะการหารของ int ปัดเศษทิ้งเสมอครับ
Exercise 3sumEven — เติมสามช่องที่อาจารย์เว้นไว้
Complete the recursive method int sumEven(int n) to calculate the sum of all even numbers from 1 to n. In this problem you need to design the base case by yourself.
เปิดโจทย์และวิธีทำ Exercise 3

5อาเรย์ — แถวของช่อง เลขประจำช่องเริ่มที่ศูนย์

สองเลขที่ต้องแยกให้ออกsize คือความจุที่จองไว้ ตั้งครั้งเดียวตอนสร้างแล้วเปลี่ยนไม่ได้ (จึงมีคำว่า final อยู่ข้างหน้า) · load คือจำนวนของที่มีอยู่จริง เปลี่ยนทุกครั้งที่เพิ่มหรือลบ — ช่องที่ใช้งานจริงคือช่อง 0 ถึง load-1 เท่านั้นครับ

6แทรกของ — ทิศของลูปคือทุกอย่าง

Exercise 4 aaddFirst — แทรกหัวแถว
Complete the method addFirst(int val) that inserts a new value to the first location of an array.
เปิดโจทย์และวิธีทำ Exercise 4 a
Exercise 4 b & caddLast · addAtIndex
Complete the method addLast(int val) that inserts a new value to the last location of an array. · Complete the method addAtIndex(int val, int index) that inserts a new value val to a specific location index of an array.
เปิดโจทย์และวิธีทำ Exercise 4 b กับ c
กฎที่ใช้ได้ทั้งหกข้อทิศของลูปต้องสวนทางกับทิศที่ของเคลื่อนที่ — แทรกคือของเคลื่อนไปขวา ลูปจึงต้องเริ่มจากช่องท้ายถอยมาช่องหน้า · ลบคือของเคลื่อนมาซ้าย ลูปจึงต้องเริ่มจากช่องหน้าไปช่องท้าย · และมียามเฝ้าประตูสองแบบเสมอ เพิ่มเช็ค load >= size · ลบเช็ค load == 0 ครับ

7ลบของ · อ่านค่า · เขียนค่า

Exercise 4 d · e · fremoveFirst · removeLast · removeAtIndex
Complete the method int removeFirst() that removes and returns a value in the first location of an array. · Complete the method int removeLast() that removes and returns a value in the last location of an array. · Complete the method int removeAtIndex(int index) that removes and returns a value in the location index of an array.
เปิดโจทย์และวิธีทำ Exercise 4 d ถึง f
Exercise 4 g & hgetElementAtIndex · setElementAtIndex
Complete the method int getElementAtIndex(int index) that gets a value of array at location index. · Complete the method void setElementAtIndex(int val, int index) that sets value at index of an array to val.
เปิดโจทย์และวิธีทำ Exercise 4 g กับ h

8ตัวชี้สองตัววิ่งเข้าหากัน

Exercise 5findPairWithSum
Write a Java method findPairWithSum(int sum) that finds a pair of numbers in the ascending array, that add up to a specific target sum. Return true, if there is at least a pair that add up to the target sum else, return false.
เปิดโจทย์และวิธีทำ Exercise 5

9เจอโจทย์ไม่เคยเห็น เริ่มจากตรงไหน

สามอย่างที่นำไปใช้ต่อได้

สามคำถามก่อนเขียนฟังก์ชันเรียกตัวเอง: หยุดเมื่อไร · รอบนี้เก็บอะไรไว้เอง · ส่งอะไรต่อให้ตัวเองรอบหน้า ตอบครบสามข้อแล้วโค้ดจะออกมาเองครับ
สามคำถามก่อนเขียนลูป (ของเดิมจาก Lab 0): วนกี่รอบ · แต่ละรอบทำอะไร · แต่ละรอบเปลี่ยนอะไร — ทุกเมธอดของอาเรย์คือลูป จึงใช้แผงเดิมได้ทั้งหมดครับ
ทิศของลูปสวนทางกับทิศที่ของเคลื่อน: แทรกเดินถอยหลัง · ลบเดินไปข้างหน้า · และก่อนแตะอาเรย์ต้องผ่านยามเฝ้าประตูก่อนเสมอครับ