기본 문법 정리
- 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 + ")");
}
} - 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);
}
} - 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);
}
} - 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 |