프로그래밍 언어/JAVA

<Java> 자바의 정석 9장 1~4,7번

창조적생각 2021. 7. 14. 19:36

1.다음과 같은 실행결과를 얻도록 SutdaCard 클래스의 equals()를 멤버변수인 num,isKwang의 값을 비교하도록 오버라이딩하고 테스트 하시오.

 

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
public class Exercise9_1 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SutdaCard c1 = new SutdaCard(3,true);
        SutdaCard c2 = new SutdaCard(3,true);
        
        System.out.println("c1="+c1);
        System.out.println("c2="+c2);
        System.out.println("c1.eqauls(c2):"+c1.equals(c2));
    }
 
}
 
class SutdaCard{
    int num;
    boolean isKwang;
    
    SutdaCard(){
        this(1true);
    }
    
    SutdaCard(int num, boolean isKwang){
    this.num = num;
    this.isKwang = isKwang;
    }
    public boolean equals(Object obj) {
/*
(1)
 
*/
    }
    public String toString() {
        return num +(isKwang ? "k":"");
    }
    
}
 
cs

 

[실행결과]

c1=3k
c2=3k
c1.eqauls(c2):true

 

[풀이]

1
2
3
4
5
6
        SutdaCard sc = (SutdaCard)obj;
        if(sc.num == num || sc.isKwang == isKwang)
            return true;
        else return false;
            
    }
cs

2.다음과 같은 실행결과를 얻도록 Point3D클래스의 equals()를 멤버변수인 x,y,z의 값을 비교하도록 오버라이딩하고, toString()은 실행결과를 참고하여 적절히 오버라이딩하시오.

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
public class exercise9_2 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Point3D p1 = new Point3D(1,2,3);
        Point3D p2 = new Point3D(1,2,3);
        
        System.out.println(p1);
        System.out.println(p2);
        System.out.println("p1 == p2?"+(p1==p2));
        System.out.println("p1.equals(p2)?"+(p1.equals(p2)));
        
    }
 
}
 
class Point3D{
    int x,y,z;
    
    Point3D(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    Point3D(){
        this(0,0,0);
}
    public boolean equals(Object obj)
/*
(1)
*/
public String toString()
/*
(2)
 
*/
cs

[실행결과]

[1,2,3]
[1,2,3]
p1 == p2?false
p1.equals(p2)?true

[풀이]

1
2
3
4
5
6
7
8
9
    public boolean equals(Object obj) {
        Point3D p3 = (Point3D)obj;
        if(p3.x == x && p3.y == y && p3.z == z)
            return true;
        else return false;
    }
    public String toString() {
        return "["+x+","+y+","+z+"]";
    }
cs

3.다음과 같은 실행결과가 나오도록 코드를 완성하시오.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class exercise9_3 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String fullpath = "c:\\jdk1.8\\work\\PathSeparateTest.java";
        String path = "";
        String fileName = "";
        /*
        (1)
    */         
        
        System.out.println("fullpath: "+fullpath);    
        System.out.println("path: "+path);
        System.out.println("fileName: "+fileName);
                
        
    }
 
}
cs

[실행결과]

fullpath: c:\jdk1.8\work\PathSeparateTest.java
path: c:\jdk1.8\work
fileName: PathAeparateTest.java

 

[풀이]

 

1
2
    path = path.concat("c:\\jdk1.8\\work");
    fileName = fileName.concat("PathAeparateTest.java");
cs

 

4. 다음과 같이 정의된 메서드를 적성하고 테스트하시오

메서드명 : printGraph

기     능 : 주어진 배열에 담긴 값만큼 주어진 문자를 가로로 출력한 후, 값을 출력한다.

반환타입 : 없음

매개변수 : int[] dataArr - 출력할 그래프의 데이터

              char ch - 그래프로 출력할 문자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
public class exercise9_4 {
    static void printGraph(int[] dataArr,char ch)
    /*
 
     (1) 메서드 
   */
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printGraph(new int[] {3,7,1,4},'*');
    }
 
}
cs

 

[실행결과]

***3
*******7
*1
****4

 

[풀이]

1
2
3
4
5
6
7
8
9
10
11
12
{
        int cnt = 0;
        for (int i =0; i< dataArr.length;i++) {
            for(int j = 0; j<dataArr[i];j++) {
                System.out.print("*");
            cnt++;
            }
            System.out.println(cnt);
            cnt = 0;
        }
        
    }
cs

7.다음과 같이 정의된 메서드를 작성하고 테스트 하시오.

메서드명 : contains

기    능 : 첫번째 문자열(src)에 두번째 문자열(target)이 포함되어 있는 지 확인한다.

포함되어 있으면 true, 그렇지 않으면 false를 반환한다.

반환타입 :boolean

매개변수 : String src, String target

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package Exercise9_7;
 
public class exercise9_7 {
/*
(1)
 
*/
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(contains("12345","23"));
        System.out.println(contains("12345","67"));
    }
 
}
 
cs

[풀이]

 

1
2
3
4
5
 
    public static boolean contains(String src,String target) {
    if(src.contains(target))
        return true;
    else return false;}
cs

 

728x90