관리 메뉴

신나게 개발하기

홍대 수업 자바 한글 스트링 인식해서 갯수 세주기 str.getBytes().length; 본문

홍익대 Java/수업

홍대 수업 자바 한글 스트링 인식해서 갯수 세주기 str.getBytes().length;

Yi,Junho 2009. 7. 24. 15:07

abstract class AbstractDisplay {
    public abstract void open();       
    public abstract void print();      
    public abstract void close();      
    public final void display() {      
        open();                        
        for (int i = 0; i < 5; i++) {  
            print();                   
        }
        close();                       
    }
}

 class CharDisplay extends AbstractDisplay { 
    private char ch;                               
    public CharDisplay(char ch) {                  
        this.ch = ch;                              
    }
    public void open() {                           
        System.out.print("<<");                    
    }
    public void print() {                          
        System.out.print(ch);                      
    }
    public void close() {                          
        System.out.println(">>");                  
    }
}

class StringDisplay extends AbstractDisplay { 
    private String str;
 int width;
    public StringDisplay(String str) {                  
        this.str = str;                              
  width=str.getBytes().length; //한글 스트링이 자리 두개씩 차지해서 이걸 써야함 ..
    }
    public void open() {   
  System.out.print("+");
  for (int i=0; i<width ;i++ )
       {
     System.out.print("-");
       }
 System.out.println("+");                          
 }                    
   
  
                         
   
    public void print() {                          
        System.out.println("|"+ str+"|");                      
    }
    public void close() {      
   System.out.print("+");
       for (int i=0; i<width ;i++ )
       {
     System.out.print("-");
       }
  System.out.println("+");                         
 }                    
   
  
}


public class Main {
    public static void main(String[] args) {
        AbstractDisplay d1 = new CharDisplay('H');                 
        AbstractDisplay d2 = new StringDisplay("Hello, world.");   
        AbstractDisplay d3 = new StringDisplay("안녕하세요.");    
        d1.display();                                              
        d2.display();                                              
        d3.display();                                              
    }
}

0 Comments
댓글쓰기 폼