U E D R , A S I H C RSS

Java Study2002/영동-3주차

JavaStudy2002 3
Starter: Yggdrasil


  • ... 는 것만 빼면 다 다. 그 C럼 만만더군... 는데 뭘 그렇게 많 ...
  • 1 다.( 료)... 2 례...그리고 다가 count변가 다 다.

* 는데.. 뭐 보는것 것 같다. 고 3 다면, count 변럽게 다.

3 드는 comment solving. 3 그대로 다면, 력 데, Bug 를 맞다.

다면 class main Main 로 바꾸기를 강력(?) 다. Java 는 규 문법 다. 과 MS라면 가리기법 ? .net 가리기법 다. --neocoin

Class main
~cpp 
import java.util.*;  
public class main{  
    public static void main(String[] args)  
    {  
        Bug bug1=new Bug(); 
        System.out.println("RandomWalk");
        bug1.journey[0]='0';   
        bug1.journey[1]='0';   
        bug1.journey[2]='4';   
        for(int i=0;bug1.journey[i]!='\0';i++) 
        	bug1.move(bug1.journey[i]);
    } 
}  
Class Bug
~cpp 
public class Bug{ 
    public int x=0;  
    public int y=0;
    public int count=0;  
    public char journey[]={'\0','\0','\0','\0','\0'}; 
    Board aboard=new Board(); 
    
    public void move(char way)  
    {             
    	if(count==0)
    		aboard.board[0][0]++;            
        if(way=='0')//북     
        	x=x-1;  
        else if(way=='1')//북동  
        {    
        	x=x-1;  
            y=y+1;
        }
        else if(way=='2')//동  
        	y=y+1;    
        else if(way=='3')//남동  
        {  
          	x=x+1;  
            y=y+1;  
        }  
        else if(way=='4')//남  
          	x=x+1;  
        else if(way=='5')//남  
        {  
        	x=x+1;  
            y=y-1;  
        }  
        else if(way=='6')//      
         	y=y-1;     
        else if(way=='7')//북  
        {
        	x=x-1;  
            y=y-1;  
        } 
        if(x>=aboard.board.length)
        	x=0;
        if(x<0)
        	x=aboard.board.length-1;
        if(y>=aboard.board[0].length)
        	y=0;
        if(y<0)
        	y=aboard.board[0].length-1;
       		
       	aboard.board[x][y]++;   
              
        aboard.exhibit();  
        System.out.print("\n");   
        count++;    
	}  
} 
Class Board
~cpp 
public class Board{ 
    public int board[][]={  
                {0,0,0,0,0},  
                {0,0,0,0,0},  
                {0,0,0,0,0},  
                {0,0,0,0,0},  
                {0,0,0,0,0}  
    };                     
    public void exhibit(){  
		for(int i=0;i<board.length;i++)  
		{  
    		for(int j=0;j<board.length;j++){  
        		System.out.print(board[i][j]);  
        		System.out.print("\t");  
        	}  
        System.out.print("\n");  
    	}  
    }  
} 

1

  • 변경 다.
  • word rap 는 부 다.
  • 는 Board.count

~cpp 
public class Board {
	public char journey[] =
		{ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
	public int x = 0;
	public int y = 0;
	public int board[][] = { { 0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }
	};

	public void move(char way) {

		if (way == '0') //북
			{
			x = x - 1;
		} else if (way == '1') //북동  
			{
			x = x - 1;
			y = y + 1;
		} else if (way == '2') //동  
			{
			y = y + 1;
		} else if (way == '3') //남동 
			{
			y = y + 1;
			x = x + 1;
		} else if (way == '4') //남  
			{
			x = x + 1;
		} else if (way == '5') //남		  
			{
			y = y - 1;
			x = x + 1;
		} else if (way == '6') //  
			{
			y = y - 1;
		} else if (way == '7') //북  
			{
			y = y - 1;
			x = x - 1;
		}
		
		// WordRap   
		if (x >= board.length)
			x = 0;
		if (y >= board[0].length)
			y = 0;
		if (x < 0)
			x = board.length - 1;
		if (y < 0)
			y = board[0].length - 1;
		board[x][y]++;
		exhibit();
		System.out.println();
	}

	public void exhibit() {
		// Magic Number 
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[i].length; j++) {
				System.out.print(board[i][j] + "\t");				
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		Board aboard = new Board();
		System.out.println("RandomWalk");
		aboard.board[0][0]++;

		aboard.journey[0] = '0';
		aboard.journey[1] = '3';

		for (int i = 0; aboard.journey[i] != '\0'; i++)
			aboard.move(aboard.journey[i]);

	}

}

2

  • HashMap 다. 로 구 드를 는 것과 같다.

~cpp 
import java.util.HashMap;

public class Board {
	public char journey[] =
		{ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0' };
	public int x = 0;
	public int y = 0;
	public int board[][] = { { 0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }
	};

	class 방값 {
		방값(int x, int y) {
			this.x = x;
			this.y = y;
		}
		int x;
		int y;
	}
	public void move(char way) {
		// HashMap  량만 가
		HashMap directionMap = new HashMap();

		directionMap.put("0", new 방값(-1, 0)); // 북		
		directionMap.put("1", new 방값(-1, 1)); // 북동		
		directionMap.put("2", new 방값(0, 1)); // 동		
		directionMap.put("3", new 방값(1, 1)); // 남동		
		directionMap.put("4", new 방값(1, 0)); // 남		
		directionMap.put("5", new 방값(1, -1)); // 남		
		directionMap.put("6", new 방값(0, -1)); // 		
		directionMap.put("7", new 방값(-1, -1)); // 북

		// primitive type  String(Object) 로 변경는 꽁
		// HashMap  력된 방  x,y 더값 delta = (방값) directionMap.get("" + way);
		x += delta.x;
		y += delta.y;

		// Wordrap 
		x = (x >= board.length) ? 0 : x;
		y = (y >= board[0].length) ? 0 : y;		
		x = (x < 0) ? board.length - 1 : x;
		y = (y < 0) ? board[0].length - 1 : y;

		board[x][y]++;
		// exhibit -> printBoard 로 rename
		printBoard();
		System.out.println();
	}

	public void printBoard() {
		// Magic Number 
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[i].length; j++) {
				System.out.print(board[i][j] + "\t");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		Board aboard = new Board();
		System.out.println("RandomWalk");
		aboard.board[0][0]++;

		aboard.journey[0] = '0';
		aboard.journey[1] = '3';

		for (int i = 0; aboard.journey[i] != '\0'; i++)
			aboard.move(aboard.journey[i]);

	}
}

3

  • char jouney -> ArrayList 로 대
  • x, y -> nowX, nowY 로 rename : 로 currentXPos가 .
  • 는 Magic number , Board.jouney 가 ArrayList 므로, String 다. comment mixing 되겠.


~cpp 
import java.util.ArrayList;
import java.util.HashMap;

public class Board {	
	// char jouney -> ArrayList 로 대	
	protected ArrayList jouney = new ArrayList();
	
	// x -> nowX	
	public int nowX = 0;
	// y -> nowY
	public int nowY = 0;
	public int board[][] = { { 0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }, {
			0, 0, 0, 0, 0 }
	};

	class 방값 {
		방값(int x, int y) {
			this.x = x;
			this.y = y;
		}
		int x;
		int y;
	}
	public void move(String way) {
		// HashMap  량만 가
		HashMap directionMap = new HashMap();

		// 방 는 Magic number 
		directionMap.put("북", new 방값(-1, 0));
		directionMap.put("북동", new 방값(-1, 1));
		directionMap.put("동", new 방값(0, 1));
		directionMap.put("남동", new 방값(1, 1));
		directionMap.put("남", new 방값(1, 0));
		directionMap.put("남", new 방값(1, -1));
		directionMap.put("", new 방값(0, -1));
		directionMap.put("북", new 방값(-1, -1));
		
		// primitive type  String(Object) 로 변경는 꽁
		// HashMap  력된 방 다.
		방값 delta = (방값) directionMap.get("" + way);

		nowX += delta.x;
		nowY += delta.y;

		// Wordrap 
		nowX = (nowX >= board.length) ? 0 : nowX;
		nowY = (nowY >= board[0].length) ? 0 : nowY;
		nowX = (nowX < 0) ? board.length - 1 : nowX;
		nowY = (nowY < 0) ? board[0].length - 1 : nowY;

		board[nowX][nowY]++;

		printBoard();
		System.out.println();
	}

	public void printBoard() {
		// Magic Number 
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[i].length; j++) {
				System.out.print(board[i][j] + "\t");
			}
			System.out.println();
		}
	}

	public ArrayList getJouney() {
		return jouney;
	}

	public static void main(String[] args) {
		Board aboard = new Board();

		System.out.println("RandomWalk");
		aboard.board[0][0]++;

		// jouney getter로 jouney  다. 
		// jouney 는 aboard가 가는 것다.
		ArrayList jouney = aboard.getJouney();
		jouney.add("북");
		jouney.add("남동");

		// 력부 ArrayList 로 따른 변경
		for (int i = 0; i < jouney.size(); i++)
			aboard.move((String) jouney.get(i));
	}

}


Thread

변경 그만 둡다. 변 바꾸, IDE, 뭐가 바뀌.

ArrayList 나, HashMap 는 Vector Hashtable 과 동 다. 1.3 가된 collection framework 두가를 더 다.

collection framework를 . 그리고 보미나 . 럭.. --neocoin



Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:31
Processing time 0.0157 sec