본문 바로가기
IT/자바

자바 과제 복습

by 독서실총무J 2017. 5. 30.
반응형

하루가 넘어가기 전에 글을 쓰려했는데

혼자 뒹구르르르 거리다 보니 늦었다


그래도 쓴다는 자체에 의미를 가지고 

오늘도 신나게 복습을 하여 보자


이번엔 저번 포스팅에서 언급했던

과제 복습을 해보도록 하겠다


답으로 나오는 코드는 내가 직접

풀이 했었다면 좋았겠지만

해답으로 풀어준 소스이다


내가 그때 어떻게 했는지 기억이 

잘 나지 않는데 풀긴 풀었다

엄청 허접하게 해서 그렇지



과제를 바로 설명해보자면

substring, indexOf, lastIndexOf 만 사용하여

위와 같은 결과창을 띄우면 된다


쉬워보이지만 막 개념을 배운 상태에서

했던터라 생각보다 쉽지 않았던 기억이 난다


두가지 버젼의 답이 있으니

두개다 쭈욱 보고 이해하길 바란다


오늘은 별로 쓸게 없어서 너무 좋다

코드도 붙여넣기만 하면 되니

복습이 매일 이랬으면 좋겠다


class Homework_Loop{
	public static void answer1(String str){
		boolean flag = true;
		int idx = 0;
		while(flag){
			idx = str.indexOf(" ",idx);
			if(idx == -1){
				idx = str.length();
				flag = false;
			}
			System.out.println(str.substring(0,idx));
			idx++;
		}
	}

	public static void answer2(String str){
		boolean flag = true;
		int idx = str.length()-1;
		while(flag){
			idx = str.lastIndexOf(" ",idx);
			if(idx == -1){
				idx = 0;
				flag = false;
			}else{
				idx++;
			}
			System.out.println(str.substring(idx));
			idx -= 2;
		}
	}

	public static void answer3(String src){
		int start = 0;
		int end = 0;
		boolean flag = true;
		while(flag){
			end = src.indexOf(" ",start);
			if(end == -1){
				flag = false;
				end = src.length();
			}
			System.out.println(src.substring(start,end));
			start = end +1;
		}
	}
	public static void answer4(String src){
		int start = src.length();
		int end = src.length();
		boolean flag = true;
		while(flag){
			start = src.lastIndexOf(" ",start-1);
			if(start == -1){
				flag = false;
			}
			System.out.println(src.substring(start+1,end));
			end = start;
			start--;
		}
	}
	public static void main(String[] args){
		String str = "태산이 높다하되 하늘 아래 뫼이로다.";
		answer1(str);
		System.out.println();
		answer2(str);
		System.out.println();
		answer3(str);
		System.out.println();
		answer4(str);
	}
}


이것이 첫번째이고 다음 것도 있다


class Homework_RecursiveCall{
	public static void answer1(String str){
		int idx = str.lastIndexOf(" ");
		if( idx != -1){
			answer1(str.substring(0,idx));
		}
		System.out.println(str);
	}

	public static void answer2(String str){
		int idx = str.indexOf(" ");
		if(idx != -1){
			answer2(str.substring(idx+1));
		}
		System.out.println(str);
	}
	public static void answer3(String str){
		int idx = str.indexOf(" ");
		if(idx != -1){
			System.out.println(str.substring(0,idx));
			answer3(str.substring(idx+1,str.length()));
		}else{
			System.out.println(str);
		}
	}
	public static void answer4(String str){
		int idx = str.lastIndexOf(" ");
		if(idx != -1){
			System.out.println(str.substring(idx + 1,str.length()));
			answer4(str.substring(0,idx));
		}else{
			System.out.println(str);
		}
	}

	public static void main(String[] args){
		String str = "태산이 높다하되 하늘 아래 뫼이로다.";
		answer1(str);
		System.out.println();
		answer2(str);
		System.out.println();
		answer3(str);
		System.out.println();
		answer4(str);
	}
}


포스팅을 날로 먹으니 몸과 마음이 편하다

내일도 자바 복습을 위하여 일찍 자야겠다

반응형

'IT > 자바' 카테고리의 다른 글

StringBuffer와 JFrame 나머지 예제들  (0) 2017.06.02
JFrame 예제 4개  (0) 2017.05.31
String 문자열에 관한 고찰  (0) 2017.05.29
Exception 나머지 예제와 throws  (0) 2017.05.28
자바 Exception 예외 처리  (0) 2017.05.25

댓글