21. Tv클래스를 주어진 로직대로 완성하시오. 완성한 후에 실행해서 주어진 실행결과와 일치하는지 확인하라.
[참고] 코드를 단순히 하기 위해서 유효성 검사는 로직에서 제외했다.
class Mytv {
boolean isPowerOn;
int Channel;
int Volume;
final int MAX_VOLUME = 100;
final int MIN_VOLUME = 0;
final int MAX_CHANNEL = 100;
final int MIN_CHANNEL = 1;
void turnOnOff() {
}
void volumeUp(){
/*(1)*/
}
void volumeDown() {
/*(2)*/
}
void channelUp() {
/*(3)*/
}
void channelDown() {
/*(4)*/
}
public class Exercise6_21 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Mytv t = new Mytv();
t.Channel = 100;
t.Volume = 0;
System.out.println("CH:"+t.Channel+",VOL:"+t.Volume);
t.channelDown();
t.volumeDown();
System.out.println("CH:"+t.Channel+",VOL:"+t.Volume);
t.Volume = 100;
t.channelUp();
t.volumeUp();
System.out.println("CH:"+t.Channel+",VOL:"+t.Volume);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | class Mytv { boolean isPowerOn; int Channel; int Volume; final int MAX_VOLUME = 100; final int MIN_VOLUME = 0; final int MAX_CHANNEL = 100; final int MIN_CHANNEL = 1; void turnOnOff() { } void volumeUp(){ if(this.Volume < MAX_VOLUME) this.Volume++; } void volumeDown() { if(this.Volume > MIN_VOLUME) this.Volume--; } void channelUp() { if(this.Channel < MAX_CHANNEL) this.Channel++; } void channelDown() { if(this.Channel > MIN_CHANNEL) this.Channel--; } } public class Exercise6_21 { public static void main(String[] args) { // TODO Auto-generated method stub Mytv t = new Mytv(); t.Channel = 100; t.Volume = 0; System.out.println("CH:"+t.Channel+",VOL:"+t.Volume); t.channelDown(); t.volumeDown(); System.out.println("CH:"+t.Channel+",VOL:"+t.Volume); t.Volume = 100; t.channelUp(); t.volumeUp(); System.out.println("CH:"+t.Channel+",VOL:"+t.Volume); } } | cs |
22. 다음과 같이 정의된 메서드를 작성하고 테스트하시오.
메서드명 : isNumber
기 능 : 주어진 문자열이 모두 숫자로만 이루어져있는지 확인한다. 모두 숫자로만 이루어져 있으면 true를 반환하고, 그렇지 않으면 false를 반환한다. 만일 주어진 문자열이 null이거나 빈문자열""dlfkaus false를 반환한다.
반환타입 : boolean
매개변수 : String str - 검사할 문자열
[Hint]String클래스의 charAt(i)메서드를 사용하면 문자열의 i번째 위치한 문자를 얻을 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class Exercise6_22 { static boolean isNumber(String str) { boolean a = true; for(int i = 0; i < str.length(); i++) { char c = str.charAt(i); // 하나씩 검사하기 위해 charAt(i)를 사용한다. if(c < 48 || c > 57) // 0~9까지 아스키코드는 48 ~ 57까지이다. a= false; } return a; } public static void main(String[] args) { // TODO Auto-generated method stub String str = "123"; System.out.println(str+"는 숫자입니까?"+isNumber(str)); str = "1234o"; System.out.println(str+"는 숫자입니까?"+isNumber(str)); } } | cs |
[실행결과]
123는 숫자입니까?true
1234o는 숫자입니까?false
[풀이]
public class Exercise6_22 {
static boolean isNumber(String str) {
boolean a = true;
for(int i = 0; i < str.length(); i++) {
char c = str.charAt(i); // 하나씩 검사하기 위해 charAt(i)를 사용한다.
if(c < 48 || c > 57) // 0~9까지 아스키코드는 48 ~ 57까지이다.
a= false;
}
return a;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "123";
System.out.println(str+"는 숫자입니까?"+isNumber(str));
str = "1234o";
System.out.println(str+"는 숫자입니까?"+isNumber(str));
}
}
23. 다음과 같이 정의된 메서드를 작성하고 테스트하시오.
메서드명 : max
기 능 : 주어진 int형 배열의 값 중에서 제일 큰 값을 반환한다. 만일 주어진 배열이 null이거나 크기가 0인 경우, -999999를 반환한다.
반환타입 : int
매개변수 : int[] arr - 최대값을 구할 배열
public class Exercise6_23 {
/*
(1) max메서드를 작성하시오.
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] data = {3,2,9,4,7};
System.out.println(java.util.Arrays.toString(data));
System.out.println("최대값:"+max(data));
System.out.println("최대값:"+max(null));
System.out.println("최대값:"+max(new int[] {}));
[실행결과]
[3, 2, 9, 4, 7]
최대값:9
최대값:-999999
최대값:-999999
[풀이]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Exercise6_23 { static int max(int[] arr) { int a= 0; if(arr == null || arr.length == 0) return -999999; for(int i = 0; i < arr.length;i++) { if(arr[i]>a) a=arr[i]; } return a; } public static void main(String[] args) { // TODO Auto-generated method stub int[] data = {3,2,9,4,7}; System.out.println(java.util.Arrays.toString(data)); System.out.println("최대값:"+max(data)); System.out.println("최대값:"+max(null)); System.out.println("최대값:"+max(new int[] {})); } } | cs |
24번 다음과 같이 정의된 메서드를 작성하고 테스트하시오.
메서드명 : abs
기 능 : 주어진 값의 절대값을 반환한다.
반환타입 : int
매개변수 : int value
public class Exercise6_24 {
/*
(1) abs메서드를 작성하시오.
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int value = 5;
System.out.println(value+"의 절대값 : "+abs(value));
value = -10;
System.out.println(value+"의 절대값 : "+abs(value));
}
}
[실행결과]
5의 절대값 : 5
-10의 절대값 : 10
[풀이]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Exercise6_24 { static int abs(int value) { int a =(int) Math.sqrt((value)*(value)); return a; } public static void main(String[] args) { // TODO Auto-generated method stub int value = 5; System.out.println(value+"의 절대값 : "+abs(value)); value = -10; System.out.println(value+"의 절대값 : "+abs(value)); } } | cs |
'프로그래밍 언어 > JAVA' 카테고리의 다른 글
<Java> 명품 Java programming 4장 실습문제 1~4 해답 (0) | 2021.07.06 |
---|---|
<Java> 자바의 정석 6장 7번 (인스턴스메서드 작성) (0) | 2021.07.04 |
<Java>자바의 정석 5장 11번(더 큰 배열 생성하기) (0) | 2021.07.02 |
<java>자바의 정석 6장 20번 (위치 바꾸기, method작성) (0) | 2021.07.02 |
자바의 정석 6장 6번 (getDistance(),Math.sqrt(), 두점 사이의 거리 구하기) (0) | 2021.07.02 |