기본 문법 정리


  1. import java.io.*;
    class MyPoint1
    {
        private int x;
        private int y;
        protected static BufferedReader in;
        static{
               in = new BufferedReader(new InputStreamReader(System.in));
                 }        // static 이유 : 객체마다 메모리를 낭비해 가면서 in 객체를 가질 필요 없이 공유만 하면 되기 때문
        protected MyPoint1() throws IOException{
           System.out.print("x = ");
           this.x = Integer.parseInt(in.readLine()); // 아스키 로 값 받아 int 형으로 변환 , this 를 통해 자신을 가리킴.
           System.out.print("y = ");
           this.y = Integer.parseInt(in.readLine());
           }
        protected void disp(){       // 출력용 함수
            System.out.println();
            System.out.println("점(x, y) = (" + x + "," + y + ")");
             }
    }
  2. class Circle1 extends MyPoint1{
        private int r;
        public Circle1() throws IOException{
            super();
            System.out.print("r = ");
            this.r = Integer.parseInt(in.readLine());
             }
        public void disp(){
            super.disp();
            System.out.println("반지름 r = " + this.r);
             }
     }
  3. class Rect1 extends MyPoint1{
           private int w;
           private int h;
           public Rect1() throws IOException{
                 super();  // MyPoint1 생성자 생성.
                 System.out.print("w = ");
                  this.w = Integer.parseInt(in.readLine());
                  System.out.print("h = ");
                  this.h = Integer.parseInt(in.readLine());
                 }
           public void disp(){
               super.disp();
               System.out.println("폭 =" + this.w + ", 높이 = " + this.h);
              }
    }
  4. public class Round13_Ex13{
        public static void main(String[] ar) throws IOException{
               MyPoint1[] mp = new MyPoint1[10];
               for(int i = 0; i < mp.length; i++)
                  {
                      System.out.println();
                      System.out.print("1. 원 2. 사각형 3. 보기 4. 종료 ==> ");
                      int x = System.in.read() - 48;
                      System.in.read(); // '\r' 값 처리
                      System.in.read(); // '\n' 값 처리
                      if(x==1){
                                mp[i] = new Circle1();
                                  }
                      else if(x==2) {
                                mp[i] = new Rect1();
                                 }
                      else if(x==3) {
                             System.out.println();
                             System.out.println("=== 보기 ===");
                             for(int a = 0; a < i; a++)
                                {
                                    mp[a].disp();
                                }
                             System.out.println("=== 보기 ===");
                             System.out.println();
                             i--;
                           }
                      else if(x ==4){
                             System.out.println("프로그램을 종료합니다");
                             System.exit(0);
                                  }
                     else {
                             System.err.println("잘못 입력하셨습니다");
                            }
                }
               System.out.println();
               System.out.println("수고 하셨습니다.");
           }
    }

이 글은 스프링노트에서 작성되었습니다.

'Programming > Java' 카테고리의 다른 글

java로 Broadcast 계산  (6) 2008.04.10
자바 입-출력과 폼 3 (JFC 기본)  (2) 2008.02.29
자바 입-출력과 폼 2 (메뉴바)  (0) 2008.02.28
자바 입-출력과 폼  (0) 2008.02.27
자바 기본 입출력  (0) 2008.02.25
:
BLOG main image
어떤것은 절대 바뀔 수 없지만 어떤것은 바뀌거든. by keiruX

카테고리

분류 전체보기 (524)
끄적끄적 (418)
음악 (8)
세상 (44)
책 체크 (9)
OS or 이론 (11)
Programming (12)
Hacking & Security (2)
Site (0)
Tip (13)
Tool (6)
창고 (0)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

달력

«   2025/02   »
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
Total :
Today : Yesterday :