Study/Java

Chapter09.java.lang 패키지

로롤로롱 2023. 7. 16. 20:46

9_1)

class Exercise9_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;
	
	public SutdaCard() {
		this(1, true);
	}

	public SutdaCard(int num, boolean isKwang) {
		this.num = num;
		this.isKwang = isKwang;
	}
	
	public boolean equals(Object obj) {
		SutdaCard c = (SutdaCard)obj;
		
		if(this.num == c.num && this.isKwang == c.isKwang) {
			return true;	
		} else return false;
	}
	
	public String toString() {
		return num + (isKwang ? "K":"");
	}
	
}

9_2)

class Exercise9_2 {
	public static void main(String[] args) {
		Point3D p1 = new Point3D(1,2,3);
		Point3D p2 = new Point3D(1,2,3);
		
		System.out.println(p1);
		System.out.println(p2);
		System.out.println("p1==p2?"+(p1==p2));
		System.out.println("p1.equals(p2)?"+(p1.equals(p2)));
	}
}

class Point3D {
	int x,y,z;
	Point3D(int x, int y, int z) {
		this.x=x;
		this.y=y;
		this.z=z;
	}
	Point3D() {
		this(0,0,0);
	}
	public boolean equals(Object obj) {
		if(obj instanceof Point3D) {
			Point3D p = (Point3D)obj;
			return this.x==p.x && this.y==p.y && this.z==p.z;
		}
		return false;
	}
	public String toString() {
		return "["+x+","+y+","+z+"]";
	}
}

9_3)

class Exercise9_3 {
	public static int count(String src, String target) {
		int count = 0; // 찾은 횟수
		int pos = 0; // 찾기 시작할 위치

		while(src.indexOf(target,pos)!=-1) {
			pos += src.indexOf(target,pos);
			count++;
		}
		return count;
	}
	
	public static void main(String[] args) {
		System.out.println(count("12345AB12AB345AB", "AB"));
		System.out.println(count("12345", "AB"));
	}
}

9_4)

class Exercise9_4 {
	public static boolean contains(String src, String target) {
		return src.indexOf(target)>0;
	}
	
	public static void main(String[] args) {
		System.out.println(contains("12345", "23"));
		System.out.println(contains("12345", "67"));
	}
}

9_5)

class Exercise9_5 {
	
	public static String delChar(String src, String delCh) {
		StringBuffer sb = new StringBuffer(src.length());
		
		for(int i=0, n=src.length(); i<n; i++) {
			
			char c = src.charAt(i);
			
			if(delCh.indexOf(c)==-1) {
				sb.append(c);
			}else {
				continue;
			}	
		}
		
		return sb.toString();
	}
	
	public static void main(String[] args) {
		
		System.out.println("(1!2@3^4~5)"+" -> " + delChar("(1!2@3^4~5)","~!@#$%^&*()"));
		System.out.println("(1 2 3 4\t5)"+" -> "+ delChar("(1 2 3 4\t5)"," \t"));
		
	}
}

9_6)

class Exercise9_6 {
	public static void main(String[] args) {
		String[] phoneNumArr = {
				"012-3456-7890",
				"099-2456-7980",
				"088-2346-9870",
				"013-3456-7890"};
		
		ArrayList<String> list = new ArrayList<>();
		Scanner s = new Scanner(System.in);
		
		while(true) {
			System.out.print(">>");
			String input = s.nextLine().trim(); // trim()으로 입력내용에서 공백을 제거
			
			if(input.equals("")) {
				continue;
			} else if(input.equalsIgnoreCase("Q")) {
				System.exit(0);
			}
			// Pattern, Matcher 클래스 사용
			Pattern pattern = Pattern.compile(input); // 입력받은 문자열로 패턴
			for(int i=0, n=phoneNumArr.length; i<n; i++) {
				// true면 리스트에 put, replaceAll로 "-" 변환
				Matcher matcher = pattern.matcher(phoneNumArr[i].replaceAll("-", ""));
				if(matcher.find()) {
					list.add(phoneNumArr[i]);
				}
				
			}

			if(list.size()>0) { // 검색결과가 있으면
				System.out.println(list); // 검색결과를 출력하고
				list.clear(); // 검색결과를 삭제
			} else {
				System.out.println("일치하는 번호가 없습니다.");
			}
		}
		
	}
}