3C-1. (ID_Tester)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package practice;
class ID{
/*싱글톤?*/
private static int counter = 0;
private int id;
public ID() { id = ++counter; }
public int getId() { return id; }
public static int getCounter() { return counter; }
}
public class ID_Tester_3C_1 {
public static void main(String[] args) {
ID a = new ID();
ID b = new ID();
System.out.println("a의 아이디: "+a.getId());
System.out.println("b의 아이디: "+b.getId());
System.out.println("부여한 아이디 개수: "+ID.getCounter());
}
}
|
cs |
3-6. (StringBinarySearch)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package practice;
import java.util.Arrays;
import java.util.Scanner;
public class StringBinarySearch_3_6 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
String[] x = {
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements","import",
"instanceof","int", "interface","long", "native",
"new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "syncronized", "this", "throw", "throws",
"transient","try", "void", "volatile", "while"
};
System.out.print("원하는 키워드를 입력하세요: ");
String ky = stdIn.next();
int idx = Arrays.binarySearch(x, ky);
if(idx<0)
System.out.println("해당 키워드가 없습니다.");
else
System.out.println("해당 키워드를 x["+idx+"]에 있습니다.");
}
}
|
cs |
3C-2. (A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package practice;
public class A_3C_2 implements Comparable<A_3C_2> {
/*
* 객체를 정렬하는 Comparable
*/
@Override
public int compareTo(A_3C_2 c) {
/*
* this가 c보다 크면 양의 값 반환
* this가 c보다 작으면 음의 값 반환
* this와 c와 같으면 0반환
*/
return 0;
}
@Override
public boolean equals(Object obj) {
/*
* this와 c와 같으면 true 반환
* 같지않으면 false 반환
*/
return super.equals(obj);
}
}
|
cs |
3-7. (X)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package practice;
import java.util.Comparator;
class X{
public static final Comarator<T> COMPARATOR = new Comp_3_7();
private static class Comp_3_7 implements Comparator<T>{
@Override
public int compare(T d1, T d2) {
//d1이 d2보다 크면 양의 값 반환
//d1이 d2보다 작으면 음의 값 반환
//d1이 s2와 같으면 0반환
return 0;
}
}
}
|
cs |
3-8. (PhysExamSearch)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package practice;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class PhysExamSearch_3_8 {
static class PhyscData{
private String name;
private int height;
private double vision;
public PhyscData(String name, int height, double vision) {
this.name = name;
this.height = height;
this.vision = vision;
}
@Override
public String toString() {
return name +" "+height+" "+vision;
}
public static final Comparator<PhyscData> HEIGHT_ORDER =
new HeightOrderComparator();
private static class HeightOrderComparator implements Comparator<PhyscData>{
@Override
public int compare(PhyscData d1, PhyscData d2) {
return (d1.height > d2.height) ? 1 :
(d1.height < d2.height) ? -1 : 0;
}
}
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
PhyscData[]x = {
new PhyscData("이나령",162,0.3),
new PhyscData("유지훈", 168,0.4),
new PhyscData("김한결",169,0.8),
new PhyscData("홍준기",171,1.5),
new PhyscData("전서현",173,0.7),
new PhyscData("이호연",174,1.2),
new PhyscData("이수민",175,2.0)
};
System.out.print("몇 cm인 사람을 찾고 있나요?: ");
int height = stdIn.nextInt();
int idx = Arrays.binarySearch(x,
new PhyscData("",height, 0.0),
PhyscData.HEIGHT_ORDER);
if(idx<0)
System.out.println("요소가 없습니다.");
else {
System.out.println("x["+idx+"]에 있습니다.");
System.out.println("찾은 데이터: "+x[idx]);
}
}
}
|
cs |
3챕터 끝!!!!!!!
'JAVA_Learn > Do it! _ Algorithm Study' 카테고리의 다른 글
#9. 3주차 과제 2 [Chap04_4-3 ~ 4-4] (0) | 2022.01.11 |
---|---|
#8. 3주차 과제 1 [Chap04_4-1 ~ 4-2] (0) | 2022.01.10 |
#6. 2주차 과제 2[Chap03_5] (0) | 2022.01.05 |
#5. 2주차 과제 1[Chap03_1 ~ 4] (0) | 2022.01.03 |
#4. 1주차 과제 4 [Chap02_2C-1, 14] (0) | 2021.12.30 |