반응형

Oval 클래스는 주어진 사각형에 내접하는 타원을 추상화한 클래스이다. Oval 클래스의 멤버는 모두 다음과같다. Oval 클래스를 선언부와 구현부로 나누어 작성하라. 

1. 정수값의 사각형 너비와 높이를 가지는 width, height 변수 멤버 

2. 너비와 높이 값이 매개 변수로 받는 생성자 

3. 너비와 높이를 1로 촉기화하는 매개 변수 없는 생성자 

4. Width와 height를 출력하는 소멸자 

5. 타원의 너비를 리턴하는 getWidth() 함수멤버 

6. 타원의 높이를 리턴하는 getHeight() 함수멤버

7. 타원의 너비와 높이를 변경하는 set(int w,int h) 함수멤버

8. 타원의 너비와 높이를 화면에 출력하는 show()함수 멤버


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
#include <iostream>
 
using namespace std;
 
class Oval {
private:
    int width;
    int height;
public:
    Oval(int a, int b) {
        width = a;
        height = b;
    }
    Oval() {
        width = 1;
        height = 1;
    }
    ~Oval() {
        cout <<"Oval 소멸 "<< "width : " << width << " " << "height : " << height << endl;
    }
    int getWidth() {
        return width;
    }
    int getHeight() {  //클래스 선언부에 직접 구현된 함수는 인라인 함수가 된다.
        return height;
    }
    void set(int w, int h);
    void show();
    
};
 
void Oval::set(int w, int h) { //width와 height를 셋 해주는 함수
    width = w;
    height = h;
}
void Oval::show() {  //width 와 height 출력 함수
    cout << "width : " << width << " " << "height : " << height << endl;
}
 
int main(void) {
    Oval a;  //Oval 형 객체 a 생성자 매개변수 없는 것이 호출되어 1,1 로 width와 height 초기화
    Oval b(34); //Oval 형 객체 b 3,4 로 width와 height 초기화
    a.set(1020); //a의 height와 width 를 10,20으로 셋
    a.show(); //출력
    cout << b.getWidth() << "," << b.getHeight() << endl//get 함수로 b의 width와 height 출력 직접 접근은 불가(private이라서)
 
    return 0;
    }
cs




반응형

'컴퓨터 & 프로그래밍 & 전자공학 > C++' 카테고리의 다른 글

SelectableRandom 클래스  (0) 2017.10.12
EvenRandom 클래스  (0) 2017.10.12
랜덤 수 출력  (0) 2017.09.16
별 출력  (0) 2017.09.09
1부터 10까지 더하기  (0) 2017.09.09
반응형

6.4> int 타입의 x, y, radius 필드를 가지는 Circle 클래스를 작성하라. Equals() 메소드를 재정의하여 두 개의 Circle 객체의 반지름이 같으면 두 Circle 객체가 동일한 것으로 판별하도록 하라. Circle 클래스의 생성자는 3개의 인자를 가지며 x, y, radius 필드를 인자로 받아 초기화한다


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
package SecondHW;
class Circle{ //default 접근의 Circle 클래스
    int x; //int 형 변수 x
    int y;//int 형 변수 y
    int radius;//int 형 변수 radius
    public Circle(int x,int y,int radius){ //Circle 클래스의 생성자
        this.x=x; //객체 의 x에 인자 x 대입
        this.y=y; //객체 의 y에 인자 y 대입
        this.radius=radius; //객체 의 radius에 인자 radius 대입
    }
     public boolean equals(Circle c2){ //java.lang 패키지에 속한 Object 클래스(자바클래스구조의 최상위)의 equals 메소드 오버라이딩
        if(this.radius==c2.radius) //객체의 radius와 인자 c2의 radius 비교
            return true//맞으면 true 반환
        else
            return false//다르면 false 반환}}
public class CircleDicision {
 
    public static void main(String[] args) {
        Circle cir1 = new Circle(1,2,3); //cir1 의 x=1,y=2,radius=3으로 초기화
        Circle cir2 = new Circle(2,4,3); //cir2 의 x=2,y=4,radius=3으로 초기화
        System.out.printf("cir1의  x :  %d y : %d  rad : %d \n",cir1.x,cir1.y,cir1.radius); //객체의 인트형 변수들 출력
        System.out.printf("cir2의  x :  %d y : %d  rad : %d \n",cir2.x,cir2.y,cir2.radius); //객체의 인트형 변수들 출력
        if(cir1.equals(cir2)) //cir1.equals(cir2)가 참이면
            System.out.println("같은 원입니다."); //같은 원 출력
        else //아니면
            System.out.println("다른 원입니다."); //다른 원 출력
    }
}
 
cs




반응형

'컴퓨터 & 프로그래밍 & 전자공학 > JAVA' 카테고리의 다른 글

가위바위보  (0) 2017.06.19
대문자 개수 세기  (0) 2017.06.19
추상 클래스  (0) 2017.06.18
상속 클래스  (0) 2017.06.18
ArrayUtility class  (0) 2017.06.18
반응형

4.2>. 다음과 같은 멤버를 가지는 직사각형을 표현하는 Rectangle 클래스를 작성하라.

- int 타입의 x1, y1, x2, y2 필드 : 사각형을 구성하는 두 점의 좌표

- 생성자 2: 매개 변수 없는 생성자와 x1, y1, x2, y2의 값을 설정하는 생성자

- void set(int x1, int y1, iny x2, int y2) : x1, y1, x2, y2좌표 설정

- int square() : 사각형 넓이 리턴

- void show() :좌표와 넓이 등 직사각형 정보의 화면 출력

) 사각형의 좌표는 (0,0), (0,0)입니다.

- boolean equals(Rectangle r) : 인자로 전달된 객체 r과 현 객체가 동일한 직사각형이면 true 리턴

 

Rectangle을 이용한 main() 메소드는 다음과 같으며 이 main() 메소드가 잘 작동하도록 하라.

public class RectManager {

             public static void main(String args[]) {

                           Rectangle r = new Rectangle();

                           Rectangle s = new Rectangle(1,1,2,3);

                           r.show();

                           s.show();

                           System.out.println(s.square());

                           r.set(-2,2,-1,4);

                           r.show();

                           System.out.println(r.square());

                          if(r.equals(s))

                                        System.out.println(" 사각형은 같습니다.");

             }

}



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
package HW1_JAVA;
class Rectang{ //Rectang class 선언
        int x1,y1,x2,y2; //int 형 변수 x1,y1,x2,y2 선언
        Rectang(){  //constructor Rectang 아규먼트 없을때
            this.x1=0;  // 객체의 x1,y1,x2,y2에 0 대입
            this.y1=0;
            this.x2=0;
            this.y2=0;}
        Rectang(int x1, int y1, int x2, int y2){
            this.x1=x1;  //객체의 x1,y1,x2,y2에 인자 x1,y1,x2,y2 대입
            this.y1=y1;     //하는 constructor (오버로딩)
            this.x2=x2;
            this.y2=y2;}
        void set(int x1, int y1, int x2, int y2){
            this.x1=x1; //객체의 x1,y1,x2,y2에 인자 x1,y1,x2,y2 대입하는 메소드 set
            this.y1=y1;
            this.x2=x2;
            this.y2=y2;}
        int square(){ //메소드 square 선언
            int length=Math.abs(this.x2-this.x1); //밑변은 x2-x1한 것의 절대값
            int height=Math.abs(this.y2-this.y1); //높이는 y2-y1한 것의 절대값
            return length*height;} //밑변*높이 반환
        void show(){ //메소드 show 선언, 좌표와 밑변 넓이를 화면에 출력해준다. printf를 사용하였다.
            System.out.printf("사각형의 좌표는 (%d,%d),(%d,%d) 입니다.\n",this.x1,this.y1,this.x2,this.y2);
            System.out.printf("사각형의 밑변은 %d, 높이는 %d입니다.\n",Math.abs(this.x2-this.x1),Math.abs(this.y2-this.y1));;
            System.out.printf("사각형의 넓이는 %d입니다.\n",square());}
        boolean equals(Rectang r){ //리턴값 true와 false만 있는 메소드 equals
        if((Math.abs(this.x2-this.x1)==Math.abs(r.x2-r.x1))&&(Math.abs(this.y2-this.y1)==Math.abs(r.y2-r.y1)))
            return true//밑변길이와 높이를 비교해서 둘다 같으면 true 반환
        else //아니면 false 반환
            return false;}}
public class Square {
    public static void main(String[] args) {
        Rectang r = new Rectang();  //인자없는 생성자로 생성한 Rectang의 객체와 레퍼런스 변수 r
        Rectang s = new Rectang(1,1,2,3); //인자를 넣어준 Rectang의 객체와 레퍼런스 변수 s
        r.show(); //r이 가리키는 객체의 show 메소드 호출
        s.show(); //s가 가리키는 객채의 show 메소드 호출
        System.out.println(s.square()); //s가 가리키는 객체의 square 메소드 호출 후 리턴 값 출력
        r.set(-2,2,-1,4); //r이 가리키는 객체의 set 메소드 호출 
        r.show(); //r이 가리키는 객체의 show 메소드 호출
        System.out.println(r.square()); //r이 가리키는 객체의 square 메소드 호출 후 리턴 값 출력
        if(r.equals(s)) //만약 r.equals(s)가 ture를 반환하면(r과 s 비교)
            System.out.println("두 사각형은 같습니다.");  // 해당 스트링 출력
    }}
 
cs




반응형

'컴퓨터 & 프로그래밍 & 전자공학 > JAVA' 카테고리의 다른 글

상속 클래스  (0) 2017.06.18
ArrayUtility class  (0) 2017.06.18
돈 단위 나누기  (0) 2017.06.18
2차원 배열  (0) 2017.06.18
정수 오름차순 정렬기  (0) 2017.06.18

+ Recent posts