More Related Content
More from Wongyos Keardsri
More from Wongyos Keardsri (20)
Java-Answer Chapter 08-09 (For Print)
- 1. ANSWER 08-09 Computer Programming using Java 1 2 Computer Programming using Java ANSWER 08-09
CHAPTER เมท็อด 6) double getLen(double dx, double dy) { ตัวแปร dx ประกาศซํา
double dx = Math.abs(dx);
ANS-08
ANS- (Methods) return Math.sqrt(dx*dx + dy*dy);
}
public กับ private
โจทย์ข้อที 1 [ระดับง่ าย] 7) public private boolean checkLen(int x[], int y[]) {
return x.length == y.length; อยู่ด้วยกันไม่ได้
1) 6) 11) 16) }
2) 7) 12) 17) 8) public float getLocationPoint() {
return 0.0; คืนค่า double ซึงใหญ่กว่า float
3) 8) 13) 18) }
9) public static int fac(int x) {
4) 9) 14) 19) if(x <= 1) return 1; เมท็อดทีเรียกใช้ ตวมันเอง (Recursion)
ั
else return fac(x – 1) * x;
5) 10) 15) 20) }
10) int[] getThreeMember(int[] x) {
int n[] = { x[0], x[1], x[2] }; การคืนค่าอาเรย์ไม่ต้องใส่วงเล็บเหลียม
โจทย์ข้อที 2 [ระดับง่ าย] return n[];
}
1) public static double square( double x )
โจทย์ข้อที 4 [ระดับง่ าย]
2) protected static double root ( int x )
1) public static double addRealNumber(double a, double b, double c) {
return a + b + c;
3) private static void showName( String name ) }
4) static int countX (int num[], int x) 2) public static void printX(int x) {
System.out.println(x);
5) }
public static double[] revArray (double num[])
3) public static double divideByInt(double x, int y) {
โจทย์ข้อที 3 [ระดับง่ าย] return x / y;
}
1) public static printError(String msg) {
ไม่มี return type (ควรเป็ น void)
System.err.println(msg);
}
4) public static String fullName(String fname, String lname) {
2) protected static int flip(int n) {
ไม่มี return value (เพิม return n) return fname + " " + lname;
if (n == 1) n = 0; }
}
3) public static float max(long x, y) {
พารามิเตอร์ y ไม่มีประเภทข้ อมูล
if (x > y) return x;
else return y; โจทย์ข้อที 5 [ระดับง่ าย]
}
public static int fac(int n) {
4) private static void showChar() { แม้ return type เป็ น void ก็สามารถ int f = 1;
for(char i = '0'; i <= '9'; i++) { for (int i = 1; i <= n; i++) {
System.out.println(i); เขียนคําสัง return ได้ แต่ไม่ต้องใส่ f *= i;
} }
return; ค่าทีจะคืนกลับ
return f;
}
}
5) static String calGrade(int x) {
if (x > 80) return "A"; กรณี else ไม่มีคําสัง return
else if (x < 50) return "F";
else System.out.println("C");
}
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 2. ANSWER 08-09 Computer Programming using Java 3 4 Computer Programming using Java ANSWER 08-09
โจทย์ข้อที 6 [ระดับง่ าย] โจทย์ข้อที 11 [ระดับง่ าย]
public static double findMax(double a[]) { ข้ อ เขียนคําสังเพือเรียกใช้ งานเมท็อด ข้ อ เขียนคําสังเพือเรียกใช้ งานเมท็อด
double max = a[0];
for (int i = 1; i < a.length; i++) {
if(a[i] > max) max = a[i]; 1. set(1, 2); 6. boolean ck = check(2011);
}
return max;
} 2. int n = cals(1, 2.0F); 7. int f = flip(1, true);
โจทย์ข้อที 7 [ระดับปานกลาง] int x[] = { 1, 2, 3 };
3. String s = toString(x);
8. double n[] = inputArray();
public static int underX(int a[], int x) {
int count = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] < x) count++; 4. int n[] = get(1, "Java"); 9. showLogo();
}
return count;
double n[] = { 2.0, 1.0 };
} 5. sort(n);
10. byte b = f(1, 2, 3);
โจทย์ข้อที 8 [ระดับปานกลาง]
public static boolean isPrime(int n) {
for (int i = 2; i < n; i++) { โจทย์ข้อที 12 [ระดับง่ าย – ระดับยาก]
if (n % i == 0) return false; import java.util.Scanner;
} public class ArrayUtility {
return true;
} //[ระดับง่ าย] เมท็อด main(…)
public static void main(String[] args) {
โจทย์ข้อที 9 [ระดับปานกลาง] ข้ อ เขียนคําสังเพือเรี ยกใช้ งานเมท็อด เมท็อดทีเรี ยก
public static int memberOfArray(int a[], int x) { 1. System.out.println(count(a, 2)); count(…)
for (int i = 0; i < a.length; i++) {
if (a[i] == x) return i + 1;
2. System.out.println(mode(a)); mode(…)
} 3. System.out.println(majority(a)); majority(…)
return -1;
} 4. System.out.println(median(a)); median(…)
5. System.out.println(range(a)); range(…)
โจทย์ข้อที 10 [ระดับยาก] } //End of main
public static int[] appendArray(int a[], int b[]) {
int ab[] = new int[a.length + b.length]; //[ระดับปานกลาง] เมท็อด swap(…)
for (int i = 0; i < ab.length; i++) {
if (i < a.length) ab[i] = a[i]; public static void swap(int a[], int i, int j) {
else ab[i] = b[i – a.length]; int temp = a[i];
} a[i] = a[j];
return ab; a[j] = temp;
} }
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 3. ANSWER 08-09 Computer Programming using Java 5 6 Computer Programming using Java ANSWER 08-09
//[ระดับปานกลาง] เมท็อด sort(…) //[ระดับง่ าย] เมท็อด max(…)
public static void sort(int a[]) { public static int max(int a[]) {
for (int i = a.length - 1; i >= 1; i--) { sort(a);
for (int j = 0; j < i; j++) { return a[a.length - 1];
if (a[j] > a[j + 1]) { }
swap(a, j, j + 1);
}
} //[ระดับง่ าย] เมท็อด min(…)
}
} public static int min(int a[]) {
sort(a);
return a[0];
}
//[ระดับปานกลาง] เมท็อด count(…)
public static int count(int a[], int k) {
int c = 0; //[ระดับง่ าย] เมท็อด range(…)
for (int i = 0; i < a.length; i++) { public static int range(int a[]) {
if (a[i] == k) c++; return max(a) - min(a);
} }
return c;
}
} //End of class
//[ระดับยาก] เมท็อด mode(…) โจทย์ ข้อที 13 [ระดับยาก]
public static int mode(int a[]) { import java.util.Scanner;
int iMode = 0; public class OneMainManyMethods {
for (int i = 0; i < a.length; i++) { public static int[] inputArray(String arrName) {
if (count(a, a[i]) > count(a, a[iMode])) iMode = i; Scanner kb = new Scanner(System.in);
} System.out.print("Enter " + arrName + "[] size: ");
return a[iMode]; int a[] = new int[kb.nextInt()];
} for (int i = 0; i < a.length; i++) {
System.out.print("Enter member: ");
a[i] = kb.nextInt();
//[ระดับยาก] เมท็อด majority(…) }
public static int majority(int a[]) { return a;
if (count(a, mode(a)) > a.length / 2) return mode(a); }
else return -1;
} public static String isArrayEquals(int a[], int b[]) {
if (a.length == b.length) {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) return "not equals";
//[ระดับปานกลาง] เมท็อด median(…) }
public static double median(int a[]) { return "equals";
double me = 0.0; } else {
sort(a); return "not equals";
if (a.length % 2 == 0) { }
me = (a[a.length / 2] + a[a.length / 2 - 1]) / 2.0; }
} else {
me = a[a.length / 2];
}
return me;
}
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 4. ANSWER 08-09 Computer Programming using Java 7 8 Computer Programming using Java ANSWER 08-09
public static void sortArray(int a[]) { โจทย์ข้อที 15 [ระดับง่ าย]
for (int i = a.length - 1; i >= 1; i--) {
import java.util.Scanner;
for (int j = 0; j < i; j++) {
public class Overload {
if (a[j] > a[j + 1]) {
int t = a[j]; //เมท็อด main(…)
a[j] = a[j + 1]; public static void main(String[] args) {
a[j + 1] = t;
} showName();
} showName("Noob Na-Chula");
} showName("Mr.", "Noob Na-Chula");
} showName("Noob Na-Chula", 5);
public static void printArray(int a[], String arrName) { } //End of main
System.out.print(arrName + "[]: ");
for (int i = 0; i < a.length; i++) { //เมท็อด showName(…)
System.out.print(a[i] + " ");
public static void showName() {
}
System.out.println(); System.out.println("Sudteen Rukjava");
}
}
public static void main(String[] args) { //เมท็อด showName(…)
int a[] = inputArray("a");
int b[] = inputArray("b"); public static void showName(String n) {
System.out.println("Check array equals before sort: " + System.out.println(n);
isArrayEquals(a, b)); }
sortArray(a);
sortArray(b);
System.out.println("Check array equals after sort: " + //เมท็อด showName(…)
isArrayEquals(a, b));
printArray(a, "a"); public static void showName(String t, String n) {
printArray(b, "b"); System.out.println(t + " " + n);
} }
} //End of class //เมท็อด showName(…)
public static void showName(String n, int x) {
โจทย์ข้อที 14 [ระดับง่ าย] for (int i = 1; i <= x; i++) {
System.out.println(n);
ข้ อ หมายเลขเมท็อด ผลลัพธ์ ข้ อ หมายเลขเมท็อด ผลลัพธ์ }
}
1. 1 100.0 9. 4 และ 2 100
2. 7 2 10. 7 และ 1 25.0 } //End of class
3. 2 121 11. 6 1.02
4. 2 36 12. [Error] [Error]
5. 3 false 13. 6 0.01
6. 5 49 14. 3 true
7. 6 10.02 15. 2 1
8. 4 4.0
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 5. ANSWER 08-09 Computer Programming using Java 9 10 Computer Programming using Java ANSWER 08-09
CHAPTER คําสังแบบซับซ้อนและการประย ุกต์ } else {
System.out.println(n);
ANS-09
ANS- (Advanced Statements and Applications) return "Negative";
}
โจทย์ ข้อที 1 [ระดับง่ าย] }
1) n > 0 && n % 2 == 0 && n < 1000 && n != 12 && n != 112
โจทย์ข้อที 4 [ระดับปานกลาง]
2) n > 0 && (n % 3 == 0 || n % 5 == 0) && n <= 100 public static boolean isEngStudent(String id, int cid) {
if (id.length() == 10 && (cid + "").length() == 7) {
3) m == 4 || m == 6 || m == 9 || m == 11 if (id.substring(0, 3).equals("533") &&
id.substring(8, 10).equals("21")) {
4) if (cid / 100000 == 21 && cid % 1000 / 100 == 1) {
m >= 1 && m <= 12 && m != 2 return true;
1 2 } else {
3 9 10 1 2 5
5) s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") 5 4 3 return false; 5 2 1
0 8 1 9 2 1 1 0 1 9 1
}
เลขประจําตัวนิสิต
} else { รหัสวิชา
โจทย์ข้อที 2 [ระดับปานกลาง] return false;
}
public static int getDaysOfMonth(int m, int y) { } else {
if (m < 1 || m > 12) { return false;
return 0; }
} else if (m == 2) { }
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
return 29;
} else {
return 28; โจทย์ ข้อที 5 [ระดับง่ าย]
}
1)
} else if (m == 4 || m == 6 || m == 9 || m == 11) { 16 รอบ i j k sum
return 30;
3 12
} else { 8
return 31; 4 13
} 2)
} sum = 14, i = 2, j = 8, k = 4 3 11
7
4 12
1
โจทย์ข้อที 3 [ระดับปานกลาง] 3) 3 10
sum = 9, i = 1, j = 5, k = 3 6
public static String getSize(int n) { 4 11
if (n >= 0) {
double m = Math.sqrt(n); 3 9
5
System.out.println(m); 4 10
if (m >= 0 && m <= 5) {
if (2 * m > 7) return "Little"; 3 13
8
else return "Undefined"; โจทย์ข้อที 6 [ระดับปานกลาง] 4 14
} else if (m > 5 && m <= 10) {
if (3 * m > 22) return "Medium"; loopXYZ(2, 7, 1); 3 12
else return "Undefined"; 2,7,1 7
} else if (m > 10 && m <= 25) { 4 13
2,6,1 2
if (4 * m > 80) return "Very Big"; 2,5,1 3 11
else if (4 * m > 60 && 4 * m <= 80) return "Big"; 6
3,7,1 4 12
else return "Undefined";
} else {
i = 5
j = 7 3 10
return "Giant"; 5
} k = 2 4 11
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 6. ANSWER 08-09 Computer Programming using Java 11 12 Computer Programming using Java ANSWER 08-09
โจทย์ ข้อที 7 [ระดับปานกลาง] //calEquation(…)
public static void formulaAtoB(int a, int b) { public static double[] calEquation(int a, int b, int c) {
for (int i = a; i <= b; i++) { double s[] = new double[a * ((int)Math.abs(b - 5) / 5 + 1) *
for (int j = 1; j <= 12; j++) { ((int)Math.abs(c - 12) / 2 + 1)];
System.out.println(i + " x " + j + " = " + (i * j)); int i = 0;
} for (int x = 1; x <= a; x++) {
System.out.println("------------"); for (int y = 5; y <= b; y += 5) {
} for (int z = 12; z >= c; z -= 2) {
} for (int n = 9; n >= 1; n--) {
s[i] += ((n * 2) - 1) * Math.pow(x,n) * Math.pow(y,10-n) *
Math.pow(z, n - 1);
}
โจทย์ข้อที 8 [ระดับปานกลาง] i++;
public class BarGraph { }
public static void main(String[] args) { }
showBarGraph(5, 2); }
showBarGraph(3, 4); return s;
showBarGraph(2, 6); }
} //End of main } //End of class
//showBarGraph(…)
public static void showBarGraph(int x, int y) { โจทย์ข้อที 10 [ระดับง่ าย] โจทย์ ข้อที 11 [ระดับง่ าย]
for (int i = 1; i <= x; i++) {
iterABC(1, 15, 2); 0
for (int j = y; j >= 1; j--) {
System.out.print(i + "," + j + " "); 1 15 2 10
for (int k = 1; k <= i * j; k++) { 2 14 1 1
System.out.print("|"); 3 13 0 2
} 4 12 -1 8
System.out.println(" " + (i * j)); 5 11 -2 2,8
}
}
} 5 รอบ 4 รอบ
} //End of class
โจทย์ข้อที 9 [ระดับยาก] โจทย์ข้อที 12 [ระดับยาก]
import java.util.Scanner; import java.util.Scanner;
public class TheEquation { import java.util.io.*;
public class Calculation {
public static void main(String[] args) { //เรี ยกใช้ เมท็อด toStringCalculation(…)
double s[] = calEquation(10, 10, 10); public static void main(String[] args) {
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]); System.out.println(toStringCalculation(10, 20, 30));
}
} //End of main
} //End of main
public static int inputNumber(String s) {
Scanner kb = new Scanner(System.in);
System.out.print("Enter " + s + ": ");
return kb.nextInt();
}
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 7. ANSWER 08-09 Computer Programming using Java 13 14 Computer Programming using Java ANSWER 08-09
//toStringCalculation(…) public static void starD(int n) {
public static String toStringCalculation(int a, int b, int c) { for (int i = n; i >= 1; i--) {
int p = inputNumber("p"); for (int j = 1; j <= n; j++) {
int q = inputNumber("q"); if (j < i) {
int r = inputNumber("r"); System.out.print(" ");
String text = ""; } else {
for (int x = a, y = b, z = c; System.out.print("*");
x + y + z < 1000; }
x += p, y += q, z += r) { }
double s = 0.0; System.out.println();
for (int i = 10, dx = 10, dy = 1, dz = 10; }
i >= 1;
i--, dx--, dy++, dz += 2) { } //End of method
s += i * Math.pow(x,dx) * Math.pow(y,dy) * Math.pow(z,dz);
} public static void starE(int n) { [ระดับปานกลาง]
text += x + "t" + y + "t" + z + "t => " + s + "n";
for (int i = 1; i <= n; i++) {
}
for (int j = 1; j <= n; j++) {
return text;
if (j < i) {
}
System.out.print(" ");
} else {
} //End of class System.out.print("*");
}
}
โจทย์ข้อที 13 [ระดับง่ าย - ระดับยาก] System.out.println();
}
public class Shape {
public static void starA(int n) { [ระดับง่ าย]
} //End of method
for (int i = 1; i <= n; i++) {
public static void starF(int n) {
for (int j = 1; j <= n; j++) { [ระดับปานกลาง]
System.out.print("*"); for (int i = 1; i <= n; i++) {
} for (int j = 1; j <= n; j++) {
System.out.println(); if (i == 1 || i == n || j == 1 || j == n) {
} System.out.print("*");
} else {
} //End of method System.out.print(" ");
}
public static void starB(int n) { [ระดับง่ าย] }
for (int i = 1; i <= n; i++) { System.out.println();
for (int j = 1; j <= i; j++) { }
System.out.print("*");
} } //End of method
System.out.println();
public static void starG(int n) {
} [ระดับปานกลาง]
for (int i = 1; i <= n; i++) {
} //End of method for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n ||
public static void starC(int n) { [ระดับง่ าย] j == i || j == n - i + 1) {
System.out.print("*");
for (int i = n; i >= 1; i--) { } else {
for (int j = 1; j <= i; j++) { System.out.print(" ");
System.out.print("*"); }
} }
System.out.println(); System.out.println();
} }
} //End of method } //End of method
[ระดับปานกลาง]
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 8. ANSWER 08-09 Computer Programming using Java 15 16 Computer Programming using Java ANSWER 08-09
public static void starH(int n) { [ระดับยาก] โจทย์ข้อที 14 [ระดับยาก]
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) { public static void revWords(String file) throws IOException {
System.out.print(" "); Scanner in = new Scanner(new File(file));
} while (in.hasNext()) {
for (int j = 1; j <= (2 * i - 1); j++) { String line = in.nextLine();
System.out.print("*"); String text = "";
} while (line.length() > 0) {
System.out.println(); if (!line.substring(0, 1).equals(" ")) {
} String word = "";
if (line.indexOf(" ") > 0) {
word = line.substring(0, line.indexOf(" "));
} //End of method } else {
word = line.substring(0, line.length());
public static void starI(int n) { [ระดับยาก] }
for (int i = 1; i <= n; i++) { for (int j = word.length() - 1; j >= 0; j--) {
for (int j = 1; j <= n; j++) { text = text + word.substring(j, j + 1);
if ((j >= i && j <= n - i + 1) || }
(j <= i && j >= n - i + 1)) { line = line.substring(word.length());
System.out.print("*"); } else { //line.substring(0, 1) is equals " "
} else { text = text + line.substring(0, 1);
System.out.print(" "); line = line.substring(1);
} }
} }//End of while
System.out.println(); System.out.println(text);
} }//End of while
}
} //End of method
public static void starJ(int n) { [ระดับยาก] โจทย์ข้อที 15 [ระดับเทพ]
for (int i = 1; i <= n; i++) { import java.util.Scanner;
for (int j = 1; j <= n; j++) { public class OperationsOfArrays {
if ((i >= j && i <= n - j + 1) || //เมท็อด removeDuplicatedMembers(…)
(i <= j && i >= n - j + 1)) {
System.out.print("*"); public static int[] removeDuplicatedMembers(int a[]) {
} else { int t[] = new int[a.length];
System.out.print(" "); int index = 0;
} for(int i = 0; i < a.length; i++) {
} if (i == 0) {
System.out.println(); t[index] = a[i];
} index++;
} else {
int j;
} //End of method for (j = 0; j < index; j++) {
if (t[j] == a[i]) break;
public static void main(String[] args) {
}
starA(11); if (j == index) {
starB(11); t[index] = a[i];
starC(11); index++;
starD(11); }
}
}
} //End of main
int n[] = new int[index];
} //End of class
for (int i = 0; i < n.length; i++) n[i] = t[i];
return n;
}
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)
- 9. ANSWER 08-09 Computer Programming using Java 17 18 Computer Programming using Java ANSWER 08-09
//เมท็อด unionArray(…) //เมท็อด printArray(…)
public static int[] unionArray(int a[], int b[]) { public static void printArray(int a[]) {
int n[] = new int[a.length + b.length]; for (int i = 0; i < a.length; i++) {
for (int i = 0; i < n.length; i++) { String comma = ", ";
if (i < a.length) n[i] = a[i]; if (i == 0) System.out.print("{ ");
else n[i] = b[i - a.length]; if (i == a.length - 1) comma = " }n";
} System.out.print(a[i] + comma);
n = removeDuplicatedMembers(n); }
return n; }
}
public static void main(String[] args) {
//เมท็อด intersectArray(…) int a[] = { 7, 7, 2, 4, 5, 6, 5, 3, 2, 3 , 0, 10};
public static int[] intersectArray(int a[], int b[]) { int b[] = { 2, 3, 4, 2, 1, 1, 4, 5, 6, 5, 3, 2, 3 };
a = removeDuplicatedMembers(a);
printArray(a);
b = removeDuplicatedMembers(b);
printArray(b);
int t[] = new int[a.length];
printArray(removeDuplicatedMembers(a));
int index = 0;
printArray(removeDuplicatedMembers(b));
for (int i = 0; i < a.length; i++) {
printArray(unionArray(a, b));
for (int j = 0; j < b.length; j++) {
printArray(intersectArray(a, b));
if (a[i] == b[j]) {
printArray(complementArray(a, b));
t[index] = a[i];
index++;
break; } //End of main
} } //End of class
}
}
int n[] = new int[index];
for (int i = 0; i < n.length; i++) n[i] = t[i];
return n;
}
//เมท็อด complementArray(…)
public static int[] complementArray(int a[], int b[]) {
a = removeDuplicatedMembers(a);
b = removeDuplicatedMembers(b);
int t[] = new int[a.length];
int index = 0;
for (int i = 0; i < a.length; i++) {
int j = 0;
while (j < b.length) {
if (a[i] == b[j]) break;
j++;
}
if (j == b.length) {
t[index] = a[i];
index++;
}
}
int n[] = new int[index];
for (int i = 0; i < n.length; i++) n[i] = t[i];
return n;
}
© สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์) © สงวนลิขสิทธิ พฤศจิกายน 2553 (ปรับปร ุงครังที 7 ฉบับใช้ติวภาค 2/2553) เรียบเรียงโดย วงศ์ยศ เกิดศรี (แบงค์)