Submit Search
Java-Answer Chapter 10-11
2 likes
1,601 views
Wongyos Keardsri
1 of 14
Download now
Downloaded 158 times
1
2
3
4
5
6
7
8
9
10
11
12
13
14
More Related Content
PDF
Java-Answer Chapter 12-13
Wongyos Keardsri
PDF
Java-Answer Chapter 08-09
Wongyos Keardsri
PDF
Java-Chapter 08 Methods
Wongyos Keardsri
PDF
Java-Answer Chapter 10-11 (For Print)
Wongyos Keardsri
PDF
Java-Chapter 13 Advanced Classes and Objects
Wongyos Keardsri
PDF
Java-Answer Chapter 08-09 (For Print)
Wongyos Keardsri
PDF
Java-Answer Chapter 01-04
Wongyos Keardsri
PDF
Java-Answer Chapter 07
Wongyos Keardsri
Java-Answer Chapter 12-13
Wongyos Keardsri
Java-Answer Chapter 08-09
Wongyos Keardsri
Java-Chapter 08 Methods
Wongyos Keardsri
Java-Answer Chapter 10-11 (For Print)
Wongyos Keardsri
Java-Chapter 13 Advanced Classes and Objects
Wongyos Keardsri
Java-Answer Chapter 08-09 (For Print)
Wongyos Keardsri
Java-Answer Chapter 01-04
Wongyos Keardsri
Java-Answer Chapter 07
Wongyos Keardsri
What's hot
(20)
PDF
Java-Answer Chapter 05-06
Wongyos Keardsri
PDF
Java-Answer Chapter 07 (For Print)
Wongyos Keardsri
PDF
Java-Answer Chapter 01-04 (For Print)
Wongyos Keardsri
PDF
Java-Answer Chapter 12-13 (For Print)
Wongyos Keardsri
PDF
Java-Chapter 11 Recursions
Wongyos Keardsri
PDF
Java-Answer Chapter 05-06 (For Print)
Wongyos Keardsri
PDF
Java-Chapter 12 Classes and Objects
Wongyos Keardsri
PDF
Java-Chapter 01 Introduction to Java Programming
Wongyos Keardsri
PDF
Discrete-Chapter 09 Algorithms
Wongyos Keardsri
PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
PDF
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
sawed kodnara
PPTX
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
Kanomwan Jeab
PDF
อนุพันธ์
krurutsamee
PDF
ปริพันธ์
พัน พัน
PPT
การอินทีเกรต
ANNRockART
PDF
662305 08
Nitigan Nakjuatong
PDF
เฉลยอินทิเกรต
krurutsamee
PDF
6.Flow control
UsableLabs
PDF
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
ชัชชญา ช่างเจริญ
PPT
Java Programming [8/12] : Arrays and Collection
IMC Institute
Java-Answer Chapter 05-06
Wongyos Keardsri
Java-Answer Chapter 07 (For Print)
Wongyos Keardsri
Java-Answer Chapter 01-04 (For Print)
Wongyos Keardsri
Java-Answer Chapter 12-13 (For Print)
Wongyos Keardsri
Java-Chapter 11 Recursions
Wongyos Keardsri
Java-Answer Chapter 05-06 (For Print)
Wongyos Keardsri
Java-Chapter 12 Classes and Objects
Wongyos Keardsri
Java-Chapter 01 Introduction to Java Programming
Wongyos Keardsri
Discrete-Chapter 09 Algorithms
Wongyos Keardsri
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
sawed kodnara
สูตรอนุพันธ์ของฟังก์ชัน อนินท์ญา
Kanomwan Jeab
อนุพันธ์
krurutsamee
ปริพันธ์
พัน พัน
การอินทีเกรต
ANNRockART
662305 08
Nitigan Nakjuatong
เฉลยอินทิเกรต
krurutsamee
6.Flow control
UsableLabs
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
ชัชชญา ช่างเจริญ
Java Programming [8/12] : Arrays and Collection
IMC Institute
Ad
Similar to Java-Answer Chapter 10-11
(20)
PDF
Java Programming: อะเรย์และคอลเล็กชั่น
Thanachart Numnonda
PDF
หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา ปาโจด ม.5
สิรินยา ปาโจด
PDF
หน่วยที่2 โครงสร้างข้อมูล นาย ธนพงษ์ น่านกร เลขที่ 1 ชั้น ม.5
palmyZommanow
PDF
Java-Chapter 10 Two Dimensional Arrays
Wongyos Keardsri
PPT
Array2D
Nitigan Nakjuatong
PDF
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
Little Tukta Lita
PPT
Array 2
นายพิศณุ ท่าสอน
PPTX
บทที่ 6
Wittaya Kaewchat
PPT
Array
Nitigan Nakjuatong
PPT
09 multi arrays
a-num Sara
PPT
1 test
xuou888
PPTX
..Arrays..
poohwapol
DOC
ตัวแปรชุดและตัวแปรอักขระ
Areeya Onnom
PDF
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Thanachart Numnonda
PDF
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
Naphamas
PDF
Java-Chapter 07 One Dimensional Arrays
Wongyos Keardsri
PPT
03 input math
ชญานิษฐ์ ทบวัน
PPTX
Computer Programming 3
Saranyu Srisrontong
PDF
งาน
Noom Sataporn
PDF
Ass4 3
โรงเรียนอุตรดิตถ์ดรุณี
Java Programming: อะเรย์และคอลเล็กชั่น
Thanachart Numnonda
หน่วยที่2 เรื่อง โครงสร้างข้อมูล นางสาว สิรินยา ปาโจด ม.5
สิรินยา ปาโจด
หน่วยที่2 โครงสร้างข้อมูล นาย ธนพงษ์ น่านกร เลขที่ 1 ชั้น ม.5
palmyZommanow
Java-Chapter 10 Two Dimensional Arrays
Wongyos Keardsri
Array2D
Nitigan Nakjuatong
บทที่ 5 ข้อมูลชนิดอาร์เรย์และสตริง 6.1
Little Tukta Lita
Array 2
นายพิศณุ ท่าสอน
บทที่ 6
Wittaya Kaewchat
Array
Nitigan Nakjuatong
09 multi arrays
a-num Sara
1 test
xuou888
..Arrays..
poohwapol
ตัวแปรชุดและตัวแปรอักขระ
Areeya Onnom
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Thanachart Numnonda
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
Naphamas
Java-Chapter 07 One Dimensional Arrays
Wongyos Keardsri
03 input math
ชญานิษฐ์ ทบวัน
Computer Programming 3
Saranyu Srisrontong
งาน
Noom Sataporn
Ass4 3
โรงเรียนอุตรดิตถ์ดรุณี
Ad
More from Wongyos Keardsri
(20)
PDF
How to Study and Research in Computer-related Master Program
Wongyos Keardsri
PPT
The next generation intelligent transport systems: standards and applications
Wongyos Keardsri
PPT
IP address anonymization
Wongyos Keardsri
PDF
SysProg-Tutor 03 Unix Shell Script Programming
Wongyos Keardsri
PDF
SysProg-Tutor 02 Introduction to Unix Operating System
Wongyos Keardsri
PDF
SysProg-Tutor 01 Introduction to C Programming Language
Wongyos Keardsri
PDF
Discrete-Chapter 11 Graphs Part III
Wongyos Keardsri
PDF
Discrete-Chapter 11 Graphs Part II
Wongyos Keardsri
PDF
Discrete-Chapter 11 Graphs Part I
Wongyos Keardsri
PDF
Discrete-Chapter 10 Trees
Wongyos Keardsri
PDF
Discrete-Chapter 08 Relations
Wongyos Keardsri
PDF
Discrete-Chapter 07 Probability
Wongyos Keardsri
PDF
Discrete-Chapter 06 Counting
Wongyos Keardsri
PDF
Discrete-Chapter 05 Inference and Proofs
Wongyos Keardsri
PDF
Discrete-Chapter 04 Logic Part II
Wongyos Keardsri
PDF
Discrete-Chapter 04 Logic Part I
Wongyos Keardsri
PDF
Discrete-Chapter 03 Matrices
Wongyos Keardsri
PDF
Discrete-Chapter 02 Functions and Sequences
Wongyos Keardsri
PDF
Discrete-Chapter 01 Sets
Wongyos Keardsri
PDF
Discrete-Chapter 12 Modeling Computation
Wongyos Keardsri
How to Study and Research in Computer-related Master Program
Wongyos Keardsri
The next generation intelligent transport systems: standards and applications
Wongyos Keardsri
IP address anonymization
Wongyos Keardsri
SysProg-Tutor 03 Unix Shell Script Programming
Wongyos Keardsri
SysProg-Tutor 02 Introduction to Unix Operating System
Wongyos Keardsri
SysProg-Tutor 01 Introduction to C Programming Language
Wongyos Keardsri
Discrete-Chapter 11 Graphs Part III
Wongyos Keardsri
Discrete-Chapter 11 Graphs Part II
Wongyos Keardsri
Discrete-Chapter 11 Graphs Part I
Wongyos Keardsri
Discrete-Chapter 10 Trees
Wongyos Keardsri
Discrete-Chapter 08 Relations
Wongyos Keardsri
Discrete-Chapter 07 Probability
Wongyos Keardsri
Discrete-Chapter 06 Counting
Wongyos Keardsri
Discrete-Chapter 05 Inference and Proofs
Wongyos Keardsri
Discrete-Chapter 04 Logic Part II
Wongyos Keardsri
Discrete-Chapter 04 Logic Part I
Wongyos Keardsri
Discrete-Chapter 03 Matrices
Wongyos Keardsri
Discrete-Chapter 02 Functions and Sequences
Wongyos Keardsri
Discrete-Chapter 01 Sets
Wongyos Keardsri
Discrete-Chapter 12 Modeling Computation
Wongyos Keardsri
Java-Answer Chapter 10-11
1.
ANSWER 10-11
Computer Programming using Java 1 CHAPTER อาเรย์สองมิติ ANS-10 (Two Dimensional Arrays) โจทย์ ข้อที่ 1 [ระดับง่ าย] 1) int a[][] = {{0,0,0},{0,0,0}}; 2) int b[][] = {{1,1},{1,1},{1,1},{1,1},{1,1}}; 3) String s[][] = {{"Java"},{"Java"},{"Java"}}; 4) String t[][] = {{"Java","Java","Java"}}; 5) double em[][] = {{}}; โจทย์ ข้อที่ 2 [ระดับง่ าย] ข้ อที่ 1 อาเรย์ m[][] ข้ อที่ 2 อาเรย์ n[][] ข้ อที่ 3 อาเรย์ p[][] 1 2 3 4 1 1 2 3 2 3 4 5 2 3 2 3 3 4 5 6 3 4 5 3 4 5 6 4 5 6 7 4 5 6 7 4 5 6 โจทย์ ข้อที่ 3 [ระดับง่ าย] 1) boolean matrix[][] = new boolean[5][8]; 2) String chess[][] = new String[8][8]; 3) int tranMatrix[][] = new int[4][9]; 4) double data[][] = new double[300][3]; 5) n[7] = new int[4]; 6) n[4] = new int[x * x + 1]; © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
2.
2
Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 4 [ระดับปานกลาง] public class RainStatistic { public static double[][] getTable() { double rain[][] = new double[12][]; for (int i = 0; i < 12; i++) { if (i == 1) { rain[i] = new double[28]; // rain[i] = new double[29]; } else if (i == 3 || i == 5 || i == 8 || i == 10) { rain[i] = new double[30]; } else { rain[i] = new double[31]; } } return rain; } public static void main(String[] args) { double x[][] = getTable(); } //End of main } //End of class โจทย์ ข้อที่ 5 [ระดับง่ าย] 1) int x = num[51][49]; 2) char c = code[9][60]; 3) int var1 = bank[0][1]; 4) double var2 = bank[1][bank[1].length - 1]; 5) code[6][4] = x; 6) sName[0][9] = s1; 7) sName[1][2] = s2; 8) a[a.length - 1][a[a.length - 1].length - 1] = y; © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
3.
ANSWER 10-11
Computer Programming using Java 3 โจทย์ ข้อที่ 6 [ระดับง่ าย] public class The2DArray { public static void setArray(int n[][]) { for (int i = 0; i < n.length; i++) { for (int j = 0; j < n[i].length; j++) { n[i][j] = (i + 1) * (j + 1); } } } public static void main(String[] args) { int x[][] = new int[5][6]; setArray(x); } //End of main } //End of class โจทย์ ข้อที่ 7 [ระดับปานกลาง] public class EqualityOf2DArray { public static boolean isArrayEquals(int x[][], int y[][]) { if (x.length == y.length) { for (int i = 0; i < x.length; i++) { if (x[i].length != y[i].length) return false; } for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { if (x[i][j] != y[i][j]) return false; } } return true; } else { return false; } } public static void main(String[] args) { int m[][] = {{1},{2,3},{3,4,5,6},{4,5,6,7}}; int n[][] = {{1},{2,3},{3,4,5},{4,5,6,7}}; boolean ch = isArrayEquals(m, n); } //End of main } //End of class © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
4.
4
Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 8 [ระดับปานกลาง - ระดับยาก] import java.util.Scanner; public class DataExperiment { public static double max(double t[][]) { double maxData = t[0][0]; for (int i = 0; i < t.length; i++) { for (int j = 0; j < t[i].length; j++) { if (t[i][j] > maxData) maxData = t[i][j]; } } return maxData; } //End of max(…) public static double min(double t[][]) { double minData = t[0][0]; for (int i = 0; i < t.length; i++) { for (int j = 0; j < t[i].length; j++) { if (t[i][j] < minData) minData = t[i][j]; } } return minData; } //End of min(…) public static double middleRange(double t[][]) { return (max(t) + min(t)) / 2.0; } //End of avgRange(…) public static double meanOfMonth(double t[][], int m) { double sum = 0.0; int i = (m + 3) % 12; for (int j = 0; j < t[i].length; j++) { sum += t[i][j]; } return sum / t[i].length; } //End of meanOfMonth(…) public static double dataOfDay(double t[][], int d, int m) { return t[(m + 3) % 12][d - 1]; } //End of dataOfDay(…) © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
5.
ANSWER 10-11
Computer Programming using Java 5 public static void main(String[] args) { Scanner kb = new Scanner(System.in); double test[][] = new double[12][]; for (int i = 0; i < test.length; i++) { if (i == 5) { test[i] = new double[28]; } else if (i == 0 || i == 2 || i == 7 || i == 9) { test[i] = new double[30]; } else { test[i] = new double[31]; for (int j = 0; j < test[i].length; j++) { System.out.print((j + 1) + "/" + ((i + 9) % 12) + " : "); test[i][j] = kb.nextDouble(); }//End of for j }//End of for i //========= Method Call ========= System.out.println(max(test)); System.out.println(min(test)); System.out.println(middleRange(test)); System.out.println(meanOfMonth(test, 10)); System.out.println(dataOfDay(test, 13, 6)); } //End of main(…) } //End of Class โจทย์ ข้อที่ 9 [ระดับง่ าย – ระดับยาก] import java.util.Scanner; public class MatrixOperation { //[ระดับปานกลาง] เขียนเมท็อด addMatrix(…) public static double[][] addMatrix(double x[][], double y[][]) { double add[][] = new double[x.length][x[0].length]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { add[i][j] = x[i][j] + y[i][j]; } } return add; } //[ระดับปานกลาง] เขียนเมท็อด mulScalarMatrix(…) public static double[][] mulScalarMatrix(double x[][], double n) { double mulS[][] = new double[x.length][x[0].length]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { mulS[i][j] = n * x[i][j]; } } return mulS; } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
6.
6
Computer Programming using Java ANSWER 10-11 //[ระดับยาก] เขียนเมท็อด transposeMatrix(…) public static double[][]transposeMatrix(double x[][]) { double at[][] = new double[x[0].length][x.length]; for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { at[j][i] = x[i][j]; } } return at; } //[ระดับยาก] เขียนเมท็อด mulMatrix(…) public static double[][] mulMatrix(double x[][], double y[][]) { double mul[][] = new double[x.length][y[0].length]; for (int i = 0; i < mul.length; i++) { for (int j = 0; j < mul[i].length; j++) { for (int k = 0; k < y.length; k++) { mul[i][j] += x[i][k] * y[k][j]; } } } return mul; } //[ระดับง่ าย] เขียนเมท็อด printMatrix(…) public static void printMatrix(double x[][]) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + "t"); } System.out.println(); } } //[ระดับง่ าย] เขียนเมท็อด main(…) public static void main(String[] args) { //Define and creat matrix double a[][] = {{1,3,5,9},{2,6,4,3}}; double b[][] = {{3,6},{4,8},{1,0}}; double c[][] = {{0,2,4},{5,3,1},{7,11,2},{6,6,9}}; //Calculate matrix m double t1[][] = transposeMatrix(mulScalarMatrix(a, 2.0)); double t2[][] = transposeMatrix(mulScalarMatrix(b, 0.25)); double t3[][] = mulMatrix(t1, t2); double t4[][] = mulScalarMatrix(c, -1.0); double m[][] = addMatrix(t3, t4); © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
7.
ANSWER 10-11
Computer Programming using Java 7 //Print matrix System.out.println("----- Matrix a -----"); printMatrix(a); System.out.println("----- Matrix b -----"); printMatrix(b); System.out.println("----- Matrix c -----"); printMatrix(c); System.out.println("----- Matrix m -----"); printMatrix(m); } //End of main } //End of class โจทย์ ข้อที่ 10 [ระดับยาก] public static int[] splitRowOfArray(int a[][], int row) { int sp[]; if (row > 0 && row <= a.length) { sp = new int[a[row - 1].length]; for (int i = 0; i < sp.length; i++) { sp[i] = a[row - 1][i]; } } else { sp = new int[0]; } return sp; } โจทย์ ข้อที่ 11 [ระดับยาก] public static int[] splitColumnOfArray(int a[][], int col) { int sp[]; if (col > 0 && col <= a[0].length) { sp = new int[a.length]; for (int i = 0; i < sp.length; i++) { sp[i] = a[i][col - 1]; } } else { sp = new int[0]; } return sp; } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
8.
8
Computer Programming using Java ANSWER 10-11 CHAPTER การเวียนเกิด ANS-11 (Recursion) โจทย์ ข้อที่ 1 [ระดับง่ าย] เรี ยก เรี ยก เรี ยก เรี ยก เรี ยก เรี ยก f(5) f(4) f(3) f(2) f(1) f(0) ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ เวียนเข้ า 0 1 + 1 + 2 + 2 + 2 + 3 + 3 + 3 + 3 + 4 + 4 + 4 + 4 + 4 + 5 + 5 + 5 + 5 + 5 + 5 + รอบที่ 1 รอบที่ 2 รอบที่ 3 รอบที่ 4 รอบที่ 5 รอบที่ 6 คืนค่ า คืนค่ า คืนค่ า คืนค่ า คืนค่ า คืนค่ า 15 10 6 3 1 0 ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ ผลลัพธ์ 0 1 + 1 + 2 + 2 + 2 + เวียนออก 3 + 3 + 3 + 3 + 4 + 4 + 4 + 4 + 4 + 5 + 5 + 5 + 5 + 5 + 5 + รอบที่ 1 รอบที่ 2 รอบที่ 3 รอบที่ 4 รอบที่ 5 รอบที่ 6 โจทย์ ข้อที่ 2 [ระดับง่ าย] โจทย์ ข้อที่ 3 [ระดับง่ าย] โจทย์ ข้อที่ 4 [ระดับง่ าย] การเรียกใช้ คําตอบ การเรียกใช้ คําตอบ การเรียกใช้ คําตอบ mul(4, 3) 12 gcd(28, 16) 4 expo(4) 16 mul(5, 7) 35 gcd(9, 14) 1 expo(7) 128 mul(20, 10) 200 gcd(75, 30) 15 expo(11) 2048 © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
9.
ANSWER 10-11
Computer Programming using Java 9 จงแสดงรายละเอียดของ expo(0), expo(1), expo(2) และ expo(3) expo(0) expo(1) expo(2) 1 2 4 expo(0) expo(0) expo(1) expo(1) 1 1 2 2 expo(0) expo(0) expo(0) expo(0) 1 1 1 1 expo(3) 8 expo(2) expo(2) 4 4 expo(1) expo(1) expo(1) expo(1) 2 2 2 2 expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) expo(0) 1 1 1 1 1 1 1 1 โจทย์ ข้อที่ 5 [ระดับง่ าย] การเรียกใช้ และคําตอบ printX(4) printX(7) 1234 1234567 123 123456 12 12345 1 1234 123 12 1 โจทย์ ข้อที่ 6 [ระดับปานกลาง] พารามิเตอร์ n พารามิเตอร์ m 0 1 2 3 4 0 1 2 3 4 5 1 2 3 4 5 6 2 3 5 7 9 11 3 5 13 29 61 125 © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
10.
10
Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 7 [ระดับง่ าย] n กรณีฐาน f n) = 1 ( n = 1 ∑ i3 1 f n) = ( n ≥ 1 i=1 กรณีเวียนเกิด f n) = f n − 1) + 1 / n 3 ( ( n > 1 public static double f(int n) { if (n <= 1) { return 1.0; } else { return f(n - 1) + 1.0 / (n * n * n); } } โจทย์ ข้อที่ 8 [ระดับง่ าย] public static int f(int n) { if (n <= 0) { return 0; } else { return f(n - 1) + (int) Math.pow(n, n) + (2 * n); } } โจทย์ ข้อที่ 9 [ระดับง่ าย] public static double g(int n) { if (n <= 0) { return 0.0; } else if (n == 1) { return 1.0; } else { return g(n - 1) * (1.0 + 1.0 / n); } } โจทย์ ข้อที่ 10 [ระดับง่ าย] public static int fac(int n) { if (n < 2) { return 1; } else { return fac(n - 1) * n; } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
11.
ANSWER 10-11
Computer Programming using Java 11 โจทย์ ข้อที่ 11 [ระดับง่ าย] public static int fibo(int n) { if (n < 2) { return n; } else { return fibo(n - 1) + fibo(n - 2); } } โจทย์ ข้อที่ 12 [ระดับง่ าย] fibo(5) fibo(4) 5 fibo(3) 3 2 fibo(3) fibo(2) fibo(2) fibo(1) 2 1 1 1 fibo(2) fibo(1) fibo(1) fibo(0) fibo(1) fibo(0) 1 1 1 0 1 0 fibo(1) fibo(0) 1 0 โจทย์ ข้อที่ 13 [ระดับปานกลาง] public static double pow(double a, int b) { if (b < 0) { return pow(a, b + 1) * 1.0 / a; } else if (b == 0) { return 1.0; } else { return pow(a, b - 1) * a; } } โจทย์ ข้อที่ 14 [ระดับยาก] public static double f(int n) { if (n <= 1) { return 1.0; } else { return 1.0 + (n / f(n - 1)); } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
12.
12
Computer Programming using Java ANSWER 10-11 โจทย์ ข้อที่ 15 [ระดับยาก] 1) public static double g(int n, int m) { if (n <= 1) { return 1; } else { if (n % 2 == 0) { return g(n – 1, m) - Math.pow(n, m); } else { return g(n – 1, m) + Math.pow(n, m); } } } 2) public static double h(int n) { if (n <= 1) { return 1; } else { if (n % 2 == 0) { return h(n - 1) + 1.0 / n; } else { return h(n - 1) - 1.0 / n; } } } โจทย์ ข้อที่ 16 [ระดับปานกลาง] public static int search(int a[], int k) { return search(a, k, 0); } public static int search(int a[], int k, int i) { if (i < a.length) { if (a[i] == k) { return i; } else { return search(a, k, i + 1); } } else { return -1; } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
13.
ANSWER 10-11
Computer Programming using Java 13 โจทย์ ข้อที่ 17 [ระดับปานกลาง] public class OverHundredNumber { public static void main(String[] args) { int d[] = { 99, 101, 13, 78, 200, 534, 47, 1234, 736 }; System.out.println(overHundred(d)); } public static int overHundred(int d[]) { return overHundred(d, 0, 0); } public static int overHundred(int d[], int i, int count) { if (i < d.length) { if (d[i] > 100) { return overHundred(d, i + 1, count + 1); } else { return overHundred(d, i + 1, count); } } else { return count; } } } //End of class โจทย์ ข้อที่ 18 [ระดับปานกลาง] public class TheString { public static void main(String[] args) { System.out.println(revStr("Computer")); } public static String revStr(String s) { return revStr(s, "", s.length() - 1); } public static String revStr(String s, String rs, int i) { if (i >= 0) { rs += s.substring(i, i + 1); return revStr(s, rs, i - 1); } else { return rs; } } } //End of class โจทย์ ข้อที่ 19 [ระดับยาก] public static void formulaAtoB(int a, int b) { formulaAtoB(a, b, 1); } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
14.
14
Computer Programming using Java ANSWER 10-11 public static void formulaAtoB(int a, int b, int i) { if (a <= b) { if (i <= 12) { System.out.println(a + " x " + i + " = " + (a * i)); formulaAtoB(a, b, i + 1); } else { System.out.println("-------------"); formulaAtoB(a + 1, b); } }//End of if (a <= b) } โจทย์ ข้อที่ 20 [ระดับยาก] public static int[] addArray(int a[], int b[]) { int sum[] = new int[a.length]; return addArray(a, b, sum, 0); } public static int[] addArray(int a[], int b[], int sum[], int i) { if (i < sum.length) { sum[i] = a[i] + b[i]; return addArray(a, b, sum, i + 1); } else { return sum; } } โจทย์ ข้อที่ 21 [ระดับยาก] public static boolean isMatrixEquals(int a[][], int b[][]) { if (a.length == b.length && a[0].length == b[0].length) { return isMatrixEquals(a, b, 0, 0); } else { return false; } } public static boolean isMatrixEquals(int a[][], int b[][], int i, int j) { if (i < a.length) { if (j < a[i].length) { if (a[i][j] != b[i][j]) { return false; } else { return isMatrixEquals(a, b, i, j + 1); } } else { return isMatrixEquals(a, b, i + 1, 0); } } else { return true; } } © สงวนลิขสิทธิ์ พฤศจิกายน 2553 (ปรับปร ุงครังที่ 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) ้
Download