반응형

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
반응형

4.4 다음 두 개의 static 메소드를 가진 ArrayUtility2 클래스를 만들어보자. ArrayUtility2 클래스를 이용하는 테스트용 프로그램도 함께 작성하라.


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
package HW1_JAVA;
class ArrayUtility2{//s1과 s2를 연결한 새로운 배열 리턴
    static int[] concat(int s1[], int s2[]){ //int형배열을 리턴으로하는 메소드 concat(인자 int형배열 2개)
        int conc[]=new int[s1.length+s2.length]; //s1과 s2 길이를 더한 길이를 갖는 인트형 배열 conc
        int i; //int 형 변수 i 선언
        for(i=0;i<conc.length;i++//i=0부터 conc의 길이보다 작을 때까지 반복
        {
            if(i<=4//i가  4 이하이면 conc[i]에 s1[i] 대입
            conc[i]=s1[i];
            else    //i가 5이상이면 conc[i]에 s2[i-5] 대입
            conc[i]=s2[i-5];
        }
        return conc;}  //conc 배열 리턴
    static int[] remove(int s1[], int s2[]){ //s1에서 s2 배열의 숫자를 모두 삭제한 새로운 배열 리턴
        int i; int j; //인트형 변수 i,j 선언
        int s3[]=new int[s1.length]; //s1.length의 길이를 갖는 배열 s3 선언
        LABEL : for(i=0;i<s1.length;i++//i는 0부터 s1.length 미만까지 반복
        {
            for(j=0;j<s2.length;j++//j는 0부터 s2.length 미만까지 반복
            {
                if(s1[i]==s2[j])  //s1[i]와 s2[j]가 같으면 루프 중단 후 LABEL로 점프(증가식은 진행됨)
                    continue LABEL;
            }
            s3[i]=s1[i]; //s3[i]에 s1[i] 대입
        }
        return s3;} } //배열 s3 리턴
public class ArrUse {
    public static void main(String[] args) {
        ArrayUtility2 ut=new ArrayUtility2(); //ArrayUtility2 class의 객체와 레퍼런스 변수 선언
        int a[]={1,2,3,4,5}; //int형 배열 a 선언 및 초기화
        int b[]={3,4,5,6,7}; //int형 배열 b 선언 및 초기화
        int c[]=new int[a.length+b.length]; //int형 배열 c 선언
        int d[]=new int[a.length]; //int형 배열 d 선언
        int i; //int형 변수 i 선언
        c=ut.concat(a, b); //배열 c에 concat 메소드의 리턴값 대입
        d=ut.remove(a, b); //배열 d에 remove 메소드의 리턴값 대입
        for(i=0;i<c.length;i++//i=0부터 c.length 미만까지 반복
        {
            System.out.print(c[i]+" "); //c[i]와 띄어쓰기 출력
        }
        System.out.println(); // 개행문자 출력
        for(i=0;i<d.length;i++//i=0부터 d.length 미만까지
        {
            if(d[i]!=0//int형 배열은 0으로 자동초기화 되기 때문에 출력안하려면 논리판별
            System.out.print(d[i]+" "); //d[i]와 띄어쓰기 출력
        }}}
 
cs






반응형

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

추상 클래스  (0) 2017.06.18
상속 클래스  (0) 2017.06.18
직사각형 클래스  (0) 2017.06.18
돈 단위 나누기  (0) 2017.06.18
2차원 배열  (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