본문 바로가기
Studynote/Computer Science 12

10.07.제 9장 java패키지 실습

by e.sunie 2018. 11. 5.

10.07 제 9장 java패키지 실습


[9-1]

소스코드

package ch9;

 

public class Ex9_1 {

public static void main(String[] args) {

SutdaCard c1 = new SutdaCard(3,true);

SutdaCard c2 = new SutdaCard(3,true);

System.out.println("c1=" + c1);

System.out.println("c2=" + c2);

System.out.println("c1.equals(c2)=" + c1.equals(c2));

 

}

 

}

 

class SutdaCard {

int num

boolean isKwang

 

SutdaCard() {

this(1,true);

}

 

SutdaCard(int num, boolean isKwang) {

this.num = num;

this.isKwang = isKwang;

}

 

public boolean equals(Object obj) {

if(obj instanceof SutdaCard){

SutdaCard c = (SutdaCard) obj;

return num == c.num && isKwang == c.isKwang

}

else return false

 

}

 

public String toString() {

return num + (isKwang"k" : "");

}

}

 

실행결과

c1=3k

c2=3k

c1.equals(c2)=true

 



[9-5]

소스코드

package ch9;

 

public class Ex9_5 {

public static int count(String src,String target) {

int count = 0;

int pos = 0;

 

while(true)

{

pos = src.indexOf(target,pos);

 

if(pos != -1) {

count++;

pos += target.length();

}else {

break

}

 

}

 

return count

}

 

 

public static void main(String[] args) {

System.out.println(count("12345AB12AB345AB","AB"));

System.out.println(count("12345","AB"));

}

 

}

 

 

실행결과

3

0

 


[9-9]

소스코드

package ch9;

 

public class Ex9_9 {

 

public static String delChar(String src, String delCh) {

StringBuffer sb = new StringBuffer(src.length());

 

for(int i = 0; i<src.length(); i++) {

char ch = src.charAt(i);

//chrk delch에 포함되있지 않으면 (indexOf()로 못찾으면) sb에 추가

if(delCh.indexOf(ch) == -1) //indexOf(int ch)로 호출

sb.append(ch);

}

return sb.toString(); //StringBuffer에 저장된 내용을 String으로 변환

}

 

public static void main(String[] args) {

System.out.println("(1!22@3#4~5)"+"->"+delChar("(1!2@3^4~5","~!@#$%^&*()"));

System.out.println("(1 2 3 4\t5)"+"->"+delChar("(1 2 3 4\t\5)","\t"));

 

/*

String a = "1!2@3^4!5";

String b = "~!@#$%^&*()";

 

System.out.println("(1!22@3#4~5)"+"->"+delChar(a,b));

 

*/

}

}

 

실행결과

(1!22@3#4~5)->12345

(1 2 3 45)->(1 2 3 4)

 


[9-10]

소스코드

package ch9;

 

public class Ex9_10 {

 

static String format(String str, int length, int alignment) {

// 1. length의 값이 str의 길이보다 작으면 length만큼만 잘라서 반환한다.

int diff = length - str.length();

if (diff < 0)

return str.substring(0, length);

// 2. 1의 경우가 아니면, length크기의 char배열을 생성하고 공백으로 채운다.

char[] source = str.toCharArray(); // 문자열을 char배열로 변환

char[] result = new char[length];

for (int i = 0; i < result.length i++)

result[i] = ' ' // 배열 result를 공백으로 채운다.

// 3. 정렬조건(alignment)의 값에 따라 문자열(str)을 복사할 위치를 결정한다.

switch (alignment) {

case 0:

default:

System.arraycopy(source, 0, result, 0, source.length);

break

case 1:

System.arraycopy(source, 0, result, diff / 2, source.length);

break

case 2:

System.arraycopy(source, 0, result, diff, source.length);

break

}

// 4. 2에서 생성한 char배열을 문자열로 만들어서 반환한다.

return new String(result);

} // static String format(String str, int length, int alignment) {

 

public static void main(String[] args) {

 

String str = "가나다"

 

System.out.println(format(str, 7, 0));

System.out.println(format(str, 7, 1));

System.out.println(format(str, 7, 2));

}

 

}

 

실행결과

가나다   

  가나다 

    가나다

 


댓글