java

/*
새
*/
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;
  }
}

변수와 리터럴

변수는 한 마디로 그릇입니다. 그리고 그 그릇에 담기는 값을 리터럴이라고 합니다.

int i = 10;

위의 구문을 풀어보면 변수 'i'를 선언하는데 변수의 타입은 'int'이며 정수(Integer)를 의미합니다. 그리고 변수 'i'에 초기값으로 '10'을 담고 있습니다. '='를 '대입연산자'라고 하며 변수에 값을 담을 때 사용하는 연산자입니다.

변수를 사용할 때는 다음 2가지 원칙을 지키도록 합니다.

  1. 변수를 사용하기 전에 반드시 선언해야 한다.
    int i;
  2. 선언된 변수는 초기화를 한다. 선언된 변수에는 쓰레기 값이 들어 있으므로 그대로는 사용할 수 없습니다.
    i = 0;

위의 설명처럼 2행에 걸쳐 선언과 초기화를 해도 되지만 보통은 처음 예처럼 선언과 동시에 초기화를 해줍니다.

데이터 타입

Java의 데이터 타입은 크게 기본 타입(primitive type)과 참조 타입(reference type)으로 나눌 수 있습니다. 기본 타입은 논리값, 정수값 등의 데이터 타입을 말하며, 참조 타입은 나중에 배우게 될 객체를 가리키기 위한 데이터 타입입니다.

기본 타입은 다음과 같이 구분할 수 있습니다.

기본 테이터 타입
타입 예약어 크기 범위
논리형 boolean 1 bit true, false
문자형 char 2 byte 0 ~ 65535 (유니코드 값)
정수형 byte 1 byte -128 ~ 127
short 2 byte -32,768 ~ 32,767
int 4 byte -2,147,483,648 ~ 2,147,483,647
long 8 byte -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807
실수형 float 4 byte -3.4E38 ~ 3.4E38
double 8 byte -1.7E308 ~ 1.7E308

 

+ Recent posts