학원/실습

*20일차 실습

pringspring 2022. 2. 21. 10:02

[문제 1]

 - 패키지명 : com.exception.charcheck
 1. 사용자 정의 예외클래스 만들기
클래스명 : CharCheckException
 2. 클래스 만들기
클래스명 : CharacterProcess
기본 생성자
메소드
//전달된 문자열값에서 영문자가 몇개인지 카운트해서 리턴
public int countAlpha(String s){}
//단, 공백문자가 있으면, CharCheckException 발생
 3. 실행용 클래스 : Run (main 포함)
실행할 메소드 작성 : public void test1(){}
키보드로 문자열을 입력받아 사용
countAlpha 메소드로 문자열 전달하고, 실행결과 받아 출력함.
- 반드시 try ~ catch 문 사용.
에러메시지 : "체크할 문자열 안에 공백 포함할 수 없습니다."

 

 

package com.exception.charcheck;

public class CharCheckException extends RuntimeException {
	public CharCheckException() {}
	
	
	public CharCheckException(String message,Throwable cause,boolean enableSuppression, boolean writableStackTrace) {
		super(message,cause,enableSuppression,writableStackTrace);
	}
	
	public CharCheckException(String message, Throwable cause) {
		super(message,cause);
	}
	public CharCheckException(String message) {
		super(message);
	}
	public CharCheckException(Throwable cause) {
		super(cause);
	}
}
---------------------------------
package com.exception.charcheck;

public class CharacterProcess {

	public CharacterProcess() {
		super();
	}
	
	public int countAlpha(String s) {
		int cnt =0;
		
		for(int i=0;i<s.length();i++) {
			if(s.charAt(i)==' ') {				
				throw new CharCheckException("체크할 문자열 안에 공백을 포함할 수 없음");
			}else if(s.toLowerCase().charAt(i)>='a' && s.toLowerCase().charAt(i)<='z') {
				cnt++;
			}
		}
		return cnt;
	}
	

}
--------------------------------------
package com.exception.charcheck;

import java.util.Scanner;

public class Run {

	public static void main(String[] args) {
		Run run =new Run();
		run.test();
	}

	private void test() {
		Scanner sc =new Scanner(System.in);
		CharacterProcess ch = new CharacterProcess();
		try {
			System.out.print("문자 입력:  ");
			String str = sc.nextLine();
			System.out.println("입력한 문자열에서 영문자는" + ch.countAlpha(str)+"개 입니다.");
		}catch(Exception e) {
			System.out.println(e.getMessage());
		}
		
	}
	

}

 

 

 

[문제 2]

- 패키지명 : com.exception.number
 1. 사용자정의 예외 클래스 : NumberRangeException
 2. 클래스 : NumberProcess
기본 생성자
메소드
// 임의의 정수 두개를 전달받아, 첫번째 수가 두번째수의 배수인지 확인하고
배수가 맞으면 true 를 리턴하고, 아니면 false를 리턴함
//단, 전달된 첫번째와 두번째 수가 1~100사이의 값이 아니면
NumberRangeException 발생시킴
에러메시지 : "1부터 100사이의 값이 아닙니다."
public boolean checkDouble(int a, int b){}
 3. 클래스 Run : 실행메소드에서 test() 메소드 실행 
    public void test(){}
    //2 개의 정수를 입력받아, checkDouble 메소드로 전달하면서 실행함
    리턴된 결과로  결과로 "ooo의 배수이다/배수가 아니다." 출력함

 

package com.exception.number;

public class NumberRangeException extends RuntimeException {

	public NumberRangeException() {
		super();
	}

	public NumberRangeException(String message, Throwable cause, boolean enableSuppression,
			boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
	}

	public NumberRangeException(String message, Throwable cause) {
		super(message, cause);
	}

	public NumberRangeException(String message) {
		super(message);
	}

	public NumberRangeException(Throwable cause) {
		super(cause);
	}

}
------------------------------------
package com.exception.number;

public class NumberProcess {
	public boolean checkDouble(int a, int b) {
		if((a>=1 && a<=100)  && (b>=1 && b<=100)) {
			if(a%b==0) {
				return true;
			}else {
				return false;
			}
		}else{
			throw new NumberRangeException("1~100사이의 값이 아닙니다.");
		}
	}
}
--------------------------------------
package com.exception.number;

public class NumberProcess {
	public boolean checkDouble(int a, int b) {
		if((a>=1 && a<=100)  && (b>=1 && b<=100)) {
			if(a%b==0) {
				return true;
			}else {
				return false;
			}
		}else{
			throw new NumberRangeException("1~100사이의 값이 아닙니다.");
		}
	}
}