자바 프로그래밍
-
자바의 접근제어2009.08.20
-
수업정리 8월 19일2009.08.19
-
예제 8월 19일 고양이 톰으로 카나리아 트위터 잡기 게임2009.08.19
-
연습문제 8월 18일자2009.08.19
-
예제 8월 14일2009.08.17
자바의 접근제어
2009. 8. 20. 01:59
자바의 접근제어를 요점만 간단히 설명드립니다.
1. 디폴트 접근 : 패키지 접근 혹은 friendly라고도 하며, 같은 패키지안에서는 지정된 멤버에 접근 가능.
2. public 접근 : 지정된 멤버는 어디서나 접근이 가능함.
3. private 접근 : 클래스 내에서만 접근 가능.
4. protected 접근 : 상속관계에 있는 클래스간의 접근을 허용. 부모 클래스(베이스 클래스)의 멤버로의 접근을 허용. 패키지 접근도 허용.
수업정리 8월 19일
2009. 8. 19. 15:19
Java 문법 정리
1. 변수와 리터럴, 기본타입
1) 논리형 : boolean
2) 문자형 : char
3) 숫자형 : byte, short, int, long, float, double
2. 연산자
1) 대입연산자 : =
2) 산술연산자 : *, /, %, +, -
3) 산술대입연산자 : *=, /=, %=, +=, -=
4) 단항연산자 : ++, --, -(부호), +(부호)
5) 조건연산자(삼항연산자) : (조건) ? true일때 값 : false일때 값
6) 비교연산자 : >, >=, <, <=, ==, !=
7) 논리연산자 : !, &&, ||
8) 비트연산자 : ^, &, |
9) 쉬프트연산자 : <<, <<<, >>
3. 조건문
1) if문
2) if..else문
3) if..else if...else문(다중분기문)
4) switch문
4. 반복문
1) for문
2) while문
3) do..while문
4) 향상된 for문
5. 클래스
1) 구조
클래스명, 멤버변수(객체의 상태 저장), 메서드(객체의 동작), 생성자(멤버변수를 특정 값으로 초기화)
예)
class 클래스명 { 멤버변수 선언문; ... 생성자() { } 리턴타입 메서드() { 실행문; ... [리턴문;] } }
예제 8월 19일 고양이 톰으로 카나리아 트위터 잡기 게임
2009. 8. 19. 12:02
/* 새 */ import java.util.*; class Bird { int x; int y; Bird() { } Bird(int x, int y) { this.x = x; this.y = y; } void fly() { Random rnd = new Random(); switch (rnd.nextInt(4)) { case 0: x++; break; case 1: x = (x < 0) ? 0 : x - 1; break; case 2: y++; break; default: y = (y < 0) ? 0 : y - 1; break; } System.out.println("트위터의 위치 : " + "(" + x + ", " + y + ")"); } public static void main(String[] args) { Bird twitter = new Bird(10, 5); for (int i = 0; i < 10; i++) { twitter.fly(); } } }
/* 고양이 톰 */ class Cat { static final int MAX = 3; int x; int y; void goNorth(int distance, Bird bird) { y += (distance > MAX) ? MAX : distance; if (x == bird.x && y == bird.y) { System.out.println("톰이 트위터를 잡았습니다."); System.exit(0); } System.out.println("톰의 위치: (" + x + ", " + y + ")"); bird.fly(); } void goSouth(int distance, Bird bird) { y -= (distance > MAX) ? MAX : distance; y = (y < 0) ? 0: y; if (x == bird.x && y == bird.y) { System.out.println("톰이 트위터를 잡았습니다."); System.exit(0); } System.out.println("톰의 위치: (" + x + ", " + y + ")"); bird.fly(); } void goEast(int distance, Bird bird) { x += (distance > MAX) ? MAX : distance; if (x == bird.x && y == bird.y) { System.out.println("톰이 트위터를 잡았습니다."); System.exit(0); } System.out.println("톰의 위치: (" + x + ", " + y + ")"); bird.fly(); } void goWest(int distance, Bird bird) { x -= (distance > MAX) ? MAX : distance; x = (x < 0) ? 0: x; if (x == bird.x && y == bird.y) { System.out.println("톰이 트위터를 잡았습니다."); System.exit(0); } System.out.println("톰의 위치: (" + x + ", " + y + ")"); bird.fly(); } public static void main(String[] args) { Cat tom = new Cat(); Bird twitter = new Bird(5, 5); tom.goNorth(2, twitter); tom.goNorth(3, twitter); tom.goEast(1, twitter); tom.goWest(5, twitter); } }
/* 톰과 트위터 */ import java.util.Scanner; class Stage3 { public static void main(String[] args) { int x = 0; int y = 0; Cat tom = new Cat(); Bird twitter = new Bird(10, 10); System.out.println("트위터 잡기 시작!"); twitter.fly(); while(true) { Scanner input = new Scanner(System.in); String cmd = input.next(); if (cmd.equals("e") || cmd.equals("E")) { tom.goEast(input.nextInt(), twitter); } else if (cmd.equals("w") || cmd.equals("W")) { tom.goWest(input.nextInt(), twitter); } else if (cmd.equals("n") || cmd.equals("N")) { tom.goNorth(input.nextInt(), twitter); } else if (cmd.equals("w") || cmd.equals("W")) { tom.goSouth(input.nextInt(), twitter); } else { System.out.println("에러!!"); } } } }
연습문제 8월 18일자
2009. 8. 19. 00:36
8월15일 연습예제
거북이 구현하기
/* 거북이 */ class Tuttle { int x; int y; int speed; void initialize() { x = 0; y = 0; speed = 0; } void stop() { speed = 0; } void speedUp() { speed++; } void speedDown() { speed = (speed <= 0) ? 0 : speed - 1; } void goNorth() { y++; } void goSouth() { y = (y <= 0) ? 0 : y - 1; } void goEast() { x++; } void goWest() { x = (x <= 0) ? 0 : x - 1; } void getStatus() { System.out.println("거북이의 위치는 " + "(" + x + ", " + y + ")이고, "); System.out.println("속도는 " + speed + "m/h입니다."); } public static void main(String[] args) { Tuttle t = new Tuttle(); t.speedUp(); t.goNorth(); t.getStatus(); } }
탱크로 벽 부수기
/* 탱크 */ class Tank { void shoot(Wall w) { w.strike(); } }
/* 무너저내리는 벽 */ class Wall { public int durability; Wall() { durability = 10; } void strike(int power) { System.out.println("꽝!"); durability -= power; if (durability > 0) { System.out.println("현재 내구력 : " + durability); } else { crumble(); } } void crumble() { System.out.println("와르르!!"); } }
/* 벽돌 벽 */ class Brick extends Wall { Brick() { durability = 5; } }
/* 탱크로 벽 맞추기 */ class Stage { public static void main(String[] arg) { Brick w1 = new Brick(); Wall w2 = new Wall(); Tank2 t = new Tank2(); t.shootGun(w1); t.shootGun(w1); t.shootCannon(w1); } }
총과 대포를 발사하는 탱크
/* 탱크 */ class Tank2 { void shootGun(Wall w) { w.strike(1); } void shootCannon(Wall w) { w.strike(5); } }
거북이 벽에 부딪치다.
/* 장애물 탐지 거북이 */ class Tuttle2 { int x; int y; void goNorth(Wall2 w) { y++; if (x >= w.x1 && x <= w.x2 && y == w.y1) { shriek(); w.soundEffect(); } } void goSouth(Wall2 w) { y = (y < 0) ? 0 : y - 1; if (x >= w.x1 && x <= w.x2 && y == w.y1) { shriek(); w.soundEffect(); } } void goEast(Wall2 w) { x++; if (x >= w.x1 && x <= w.x2 && y == w.y1) { shriek(); w.soundEffect(); } } void goWest(Wall2 w) { x = (x < 0) ? 0 : x - 1; if (x >= w.x1 && x <= w.x2 && y == w.y1) { shriek(); w.soundEffect(); } } void shriek() { System.out.println("꽥!"); } }
/* 길이 요소를 갖는 벽 */ class Wall2 { int x1; int y1; int x2; int y2; void soundEffect() { System.out.println("꽝!!"); } Wall2() { } Wall2(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; } }
class Stage2 { public static void main(String[] args) { Wall2 w = new Wall2(5, 5, 9, 10); Tuttle2 t = new Tuttle2(); t.goEast(w); t.goEast(w); t.goEast(w); t.goEast(w); t.goEast(w); t.goEast(w); t.goNorth(w); t.goNorth(w); t.goNorth(w); t.goNorth(w); t.goNorth(w); t.goNorth(w); t.goNorth(w); } }
/* 1에서 4사이의 난수 발생 */ import java.util.Random; class GenRandom { public static void main(String[] args) { for (int i = 0; i < 10; i++) { Random rnd = new Random(); System.out.print( rnd.nextInt(4) + " "); } System.out.println(); } }
/* 커맨드 라인 입력 받기 */ import java.util.Scanner; class CmdInput { public static void main(String[] args) { System.out.print("입력하세요: "); Scanner sc = new Scanner(System.in); System.out.println("입력한 문자는 : "); while (sc.hasNext()) { System.out.println(sc.next()); } sc = null; } }
예제 8월 14일
2009. 8. 17. 01:20
class Hakjum { public static void main(String[] args) { int kor = new Integer(args[0]); int eng = new Integer(args[1]); int mat = new Integer(args[2]); int sum = kor + eng + mat; int avg = sum / 3; String hakjum = "가"; if (avg > 100 || avg < 0) { System.out.println("입력 오류"); /* } else if (avg >= 90) { */ } else { switch (avg / 10) { case 10: case 9: hakjum = "수"; break; case 8: hakjum = "우"; break; case 7: hakjum = "미"; break; case 6: hakjum = "양"; break; default: hakjum = "가"; break; } } } }
/* for문 */ class Loop { public static void main(String[] args) { int count = 0; for (int i = 13; i >= 2; i -= 2) { count += 1; } System.out.println("FOR문은 " + count + "번 실행됩니다."); } }
/* http://jeongsam.net 잔돈 계산 (조건) 잔돈은 1,000원, 100원, 10원 3종류가 있음. (실행) java Jandon 1560 (출력) 1560원은 1,000원 1개 100원 5개 10원 6개 */ class Jandon { public static void main(String[] args) { System.out.println(args[0] + "원은"); for (int i = 1000, count = new Integer(args[0]) / 1000, don = new Integer(args[0]) % 1000; i >= 10; i /= 10, count = don / i, don %= i) { System.out.println(i + ": " + don); System.out.println(i + "원 : " + count + " 개"); } } } /* for (int i = 1000; i >= 10 ; i /= 10) { int count = don / i; don %= i; System.out.println(i + "원 : " + count + " 개"); } */ /* for (int i = 0; i < 3; i++) { switch(i) { case 0: t = don / 1000; don = don % 1000; break; case 1: h = don / 100; don = don % 100; break; default: ten = don / 10; break; } } */
/* 요일출력 1일이 토요일로 가정 (조건) 0:일요일, 1:월요일, 2:화요일, ..., 6:토요일 (실행) java GetDays 14 (출력) 오늘은 금요일입니다. */
class MyCalendar { public static void main(String[] args) { int count = 0; System.out.println("\t일\t월\t화\t수\t목\t금\t토"); for (int i = 1; i <= 6; i++) { count++; System.out.print("\t"); } for (int i = 1; i <= 31 ;i++) { System.out.print("\t" + i); count++; if (count % 7 == 0) System.out.println(); } } }