프로그래밍 언어/JAVA

<Java> 명품 Java programming 5장 5번, 6번, 7번, 8번

창조적생각 2021. 7. 7. 17:12

[5~8] 다음은 2차원 상의 한 점을 표현하는 point 클래스이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Point{
    private int x,y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getx() {
        return x;
    }
    public int gety() {
        return y;
    }
    protected void move(int x, int y) {
        this.x = x;
        this.y = y;
    }
        
    
}
cs

5. point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라. 다음 main()메소드를 포함하고 실행 결과와 같이 출력되게 하라.

 

1
2
3
4
5
6
7
8
9
public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColorPoint cp = new ColorPoint(5,5,"Yellow");
        cp.setXY(10,20);
        cp.setColor("Red");
        String str = cp.toString();
        System.out.println(str+"입니다.");
    }
 
cs

[실행결과]

Red색의 (10,20)의 점입니다.

 

[풀이]

 

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
class Point{
    private int x,y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getx() {
        return x;
    }
    public int gety() {
        return y;
    }
    protected void move(int x, int y) {
        this.x = x;
        this.y = y;
    }
        
    
}
class ColorPoint extends Point{
    String str;
    public ColorPoint(int x, int y, String str) {
        super(x,y);
        this.str = str;
    }
    public void setXY(int x, int y) {
        move(x,y);
    }
    public void setColor(String str) {
        this.str = str;
    }
    public String toString() {
        
        return str+"색의 ("+getx()+","+gety()+")의 점";
    }
}
public class practice5_5_6 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColorPoint cp = new ColorPoint(5,5,"Yellow");
        cp.setXY(10,20);
        cp.setColor("Red");
        String str = cp.toString();
        System.out.println(str+"입니다.");
    }
 
}
 
cs

 

6. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라. 다음 main() 메소드를 포함하고 실행 결과와 같이 출력되게 하라.

1
2
3
4
5
6
7
8
        ColorPoint zeroPoint = new ColorPoint();
        System.out.println(zeroPoint.toString() + "입니다.");
        
        ColorPoint cp = new ColorPoint(10,10);
        cp.setXY(55);
        cp.setColor("Red");
        System.out.println(cp.toString() + "입니다.");
    }
cs

[실행결과]

검정색의 (0,0)의 점입니다.
Red색의 (5,5)의 점입니다.

 

[풀이]

 

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
class Point{
    private int x,y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getx() {
        return x;
    }
    public int gety() {
        return y;
    }
    protected void move(int x, int y) {
        this.x = x;
        this.y = y;
    }
        
    
}
class ColorPoint extends Point{
    String str;
    
    public ColorPoint(int x, int y, String str) {
        super(x,y);
        this.str = str;
    }
    public ColorPoint(int x, int y) {
        super(x,y);
    }
    public ColorPoint() {
        super(00);
        str = "검정";
    }
    
    public void setXY(int x, int y) {
        move(x,y);
    }
    public void setColor(String str) {
        this.str = str;
    }
    public String toString() {
        
        return str+"색의 ("+getx()+","+gety()+")의 점";
    }
 
    
    
}
public class practice5_5_6 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ColorPoint zeroPoint = new ColorPoint();
        System.out.println(zeroPoint.toString() + "입니다.");
        
        ColorPoint cp = new ColorPoint(10,10);
        cp.setXY(55);
        cp.setColor("Red");
        System.out.println(cp.toString() + "입니다.");
    }
 
}
cs

 

7. Point를 상속받아 3차원의 점을 나타내는 Point 3D 클래스를 작성하라.

다음 main()메소드를 포함하고 실행결과와 같이 출력되게 하라. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
        // TODO Auto-generated method stub
        Point3D p = new Point3D(1,2,3);
        System.out.println(p.toString()+"입니다.");
        
        p.moveup();
        System.out.println(p.toString()+"입니다.");
        
        p.moveDown();
        p.move(10,10);
        System.out.println(p.toString()+"입니다.");
        
        p.move(100,200,300);
        System.out.println(p.toString()+"입니다.");
    }
    }
 
}
cs

[실행결과]

(1,2,3)의 점입니다.
(1,2,4)의 점입니다.
(10,10,3)의 점입니다.
(100,200,300)의 점입니다.

 

[풀이]

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
class Point{
    private int x,y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getx() {
        return x;
    }
    public int gety() {
        return y;
    }
    protected void move(int x, int y) {
        this.x = x;
        this.y = y;
    }
        
    
}
 
class Point3D extends Point{
    int z;
    public Point3D(int x, int y, int z) {
        super(x,y);
        this.z = z;
    }
    public String toString() {
        return "("+getx()+","+gety()+","+z+")의 점";
    }
    
    public void moveUp() {
        z++;
    }
    public void moveDown() {
        z--;
    }
    void move(int x, int y,int z) {
        move(x,y);
        this.z = z;
    }
}
public class practice5_5 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Point3D p = new Point3D(1,2,3);
        System.out.println(p.toString()+"입니다.");
        
        p.moveUp();
        System.out.println(p.toString()+"입니다.");
        
        p.moveDown();
        p.move(10,10);
        System.out.println(p.toString()+"입니다.");
        
        p.move(100,200,300);
        System.out.println(p.toString()+"입니다.");
    }
 
}
cs

 

8. point를 상속받아 양수의 공간에서만 점을 나타내는 PositivePoint 클래스를 작성하라. 다음 main()메소드를 포함하고 실행결과와 같이 출력되게 하라.

 

 

[실행결과]

(10,10)의 점입니다.
(10,10)의 점입니다.
(0,0)의 점입니다.

 

[풀이]

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
class Point{
    private int x,y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getx() {
        return x;
    }
    public int gety() {
        return y;
    }
    protected void move(int x, int y) {
        this.x = x;
        this.y = y;
    }
        
    
}
 
class PositivePoint extends Point{
    public PositivePoint() {
        super(0,0);
    }
    public PositivePoint(int x, int y) {
        super(x,y);
        if(x<=0 || y<=0)
            super.move(0,0);
    }
    public void move(int x, int y) {
        if(x>0 && y>0
        super.move(x,y);
    }
    public String toString() {
        return "("+getx()+","+gety()+")의 점";
        
    }
}
public class practice5_5 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PositivePoint p = new PositivePoint();
        p.move(10,10);
        System.out.println(p.toString()+"입니다.");
        
        p.move(-5,5);
        System.out.println(p.toString() + "입니다.");
        
        PositivePoint p2 = new PositivePoint(-10,-10);
        System.out.println(p2.toString() + "입니다.");
    }
 
}
cs

 

728x90