1. Rect ν΄λž˜μŠ€μ— μž…λ ₯λ°›λŠ” λ©”μ†Œλ“œ, λ‘˜λ ˆκ΅¬ν•˜λŠ” λ©”μ†Œλ“œ, 넓이 κ΅¬ν•˜λŠ” λ©”μ†Œλ“œ, 좜λ ₯ν•˜λŠ” λ©”μ†Œλ“œλ‘œ λ‚˜λˆ μ„œ κ΅¬ν˜„ν•œ ν›„

값을 κ΅¬ν•˜μ—¬λΌ.

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
import java.util.Scanner;
 
class Rect{//μ§μ‚¬κ°ν˜• ν΄λž˜μŠ€ μ„€κ³„
 
        //데이터 ( μ†μ„±, μƒνƒœ) → λ³€μˆ˜
        int w, h;                //κ°€λ‘œ μ„Έλ‘œ
 
        //κΈ°λŠ₯ (λ™μž‘, ν–‰μœ„) →λ©”μ†Œλ“œ
        void input(){                //κ°€λ‘œμ„Έλ‘œμž…λ ₯() 
                                    //μ‚¬μš©μžλ‘œ λΆ€ν„° κ°€λ‘œ,μ„Έλ‘œλ₯Ό μž…λ ₯ λ°›κΈ° λ•Œλ¬Έμ— λ§€κ°œλ³€μˆ˜ ν•„μš”μ—†μŒ
                                    // λ°˜ν™˜κ°’이 μ—†μœΌλ©΄ void μ‚¬μš©, μ—†μ„ λ•Œ μ•„무것도 μ•ˆμ“°λŠ” λ¬Έλ²•μ€ μ΄λ―Έ μƒμ„±μžκ°€ κ°€μ§€κ³  κ°”λ”°.
            
            Scanner sc = new Scanner(System.in);
 
            System.out.print("κ°€λ‘œ μž…λ ₯:");
            w = sc.nextInt();
 
            System.out.print("μ„Έλ‘œ μž…λ ₯:");
            h = sc.nextInt();
            
        }
        int calArea(){                //넓이계산()//λͺ¨λ‘ λ©€λ²„이기 λ•Œλ¬Έμ—
                                    //넓이에 λŒ€ν•œ λ³€μˆ˜κ°€ μ—†μœΌλ‹ˆκΉ return κ°’μœΌλ‘œ int λ‘œ κ³±ν•΄μ„œ μ€€λ‹€.//객체도 λ°˜ν™˜ν•΄μ€„μˆ˜ μžˆμŒ
            int result;
            result= w * h ;
            return result;            //return λ’€μ— μ μ–΄ λ΄€μž μ•„무 μ˜λ―Έ μ—†μŒ
 
        }
        int calLength(){            //λ‘˜λ ˆ κ³„μ‚°()
 
            int result;
            result = (w + h) *2;
            return result;
 
        }
        void print(int a, int l)    {//κ²°κ³Ό μΆœλ ₯()//λ§€κ°œλ³€μˆ˜ μ΄λ¦„은 μƒκ΄€μ—†λ‹€
 
            System.out.println("κ°€λ‘œ : " + w);    //κ°€λ‘œ : 10
            System.out.println("μ„Έλ‘œ : " + h);    //μ„Έλ‘œ : 20
            System.out.println("넓이 : " + a);    //넓이 : xxx
            System.out.println("λ‘˜λ ˆ : " + l);  //λ‘˜λ ˆ : xxx
        }
}
public class Test065{
 
    public static void main(String[] args){
 
        //Rect μΈμŠ€ν„΄μŠ€ μƒμ„± -- Rectλ₯Ό μ‚¬μš©ν•˜λ €λ©΄ κ°μ²΄λ₯Ό μƒμ„±ν•΄μ•Όν•œλ‹€./이제 λΆ•μ–΄λΉ΅
        Rect ob = new Rect();
 
        //μž…λ ₯ λ©”μ†Œλ“œ ν˜ΈμΆœ
        ob.input();
 
        //넓이 μ—°μ‚° λ©”μ†Œλ“œ ν˜ΈμΆœ
         int area = ob.calArea();    //ob.calArea();호좜되면 55라고 κ°’을 λ‘κ³ κ°€μ„œ κ·Έκ±Έ λ‹΄μ„ λ³€μˆ˜κ°€ ν•„μš”
        
        //λ‘˜λ ˆ μ—°μ‚° λ©”μ†Œλ“œ ν˜ΈμΆœ
         int length = ob.calLength();
 
         //κ²°κ³Ό λ©”μ†Œλ“œ ν˜ΈμΆœ
         ob.print(area , length);
 
    }
}
cs

+ Recent posts