본문 바로가기
Study/Java

Chapter06.객체지향 프로그래밍1(2)

by 로롤로롱 2023. 7. 9.

6_14)

1번 - 지역변수는 자동 초기화가 안되므로 초기화를 해야함

5번 - 지역변수는 호출스택(call stack)에 생성된다. 힙(heap)영역에는 인스턴스가 생성되는 영역

 

6_15)

2번 - 종료된 상태가아니라 대기상태

 

6_16)

ABC123

After change:ABC123

 

6_17)

public class Exercise6_17 {
	public static void main(String[] args) {
		int[] original= {1,2,3,4,5,6,7,8,9};
		System.out.println(java.util.Arrays.toString(original));
		
		int[] result = suffle(original);
		System.out.println(java.util.Arrays.toString(result));
	}

	public static int[] suffle(int[] original) {
		// 유효성 체크
		if(original==null || original.length==0) {
			return original;
		}
		
		int temp=0;
		for(int i=0; i<9; i++) {
			int n = (int)(Math.random()*9);
			temp = original[n];
			original[n]=original[i];
			original[i]=temp;
		}
		
		return original;
	}
}

6_18)

public class Excercise6_18 {
	public static void main(String[] args) {
		String str = "123";
		System.out.println(str + "는 숫자입니까? "+isNumber(str));
		
		str = "1234o";
		System.out.println(str + "는 숫자입니까? "+isNumber(str));
	}

	public static boolean isNumber(String str) {
		// 유효성 체크
		if(str==null || str.length()==0) {
			return false;
		}
		
		for(int i=0; i<str.length(); i++) {
			if('0'>str.charAt(i)||'9'<str.charAt(i)) {
				return false;
			} 
		}
		return true;
	}

}

6_19)

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() {
		// isPowerOn의 값이 true면 false, false면 true
		if(isPowerOn) isPowerOn = false;
		else isPowerOn = true;
	}
	
	void volumeUp() {
		// 볼륨의 값이 맥스 볼륨보다 작을때만 값을 1증가
		if(volume < MAX_VOLUME) {
			volume++;
		}
	}
	
	void volumeDown() {
		// 볼륨값이 민볼륨보다 클때 값1감소
		if(volume > MAX_VOLUME) {
			volume--;
		}
	}
	
	void channelUp() {
		// 채널 값 1 증가
		// 채널이 맥스채널이면 민채널로 변경
		if(channel == MAX_CHANNEL) {
			channel = MIN_CHANNEL;
		} else channel++;
	}
	
	void channelDown() {
		// 채널 1 감소
		// 값이 민 채널이면 맥스채널로 변경
		if(channel == MIN_CHANNEL) {
			channel = MAX_CHANNEL;
		} else channel--;
	}
}

public class Exercise6_19 {

	public static void main(String[] args) {
		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);
	}

}

6_20)

public class Exercise6_20 {
	
	static int max(int[] data) {
		int max = 0;
		
		if(data == null || data.length==0) {
			max = -999999;
			return max;
		}
		
		for(int i=0; i<data.length; i++) {
			if(max<data[i]) {
				max = data[i];
			}
		}
		
		return max;
	}

	public static void main(String[] args) {
		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[] {}));
	}

}

6_21)

public class Exercise6_21 {
	
	static int abs(int value) {

		return value<0?value*(-1):value;
	}

	public static void main(String[] args) {
		int value = 5;
		System.out.println(value+"의 절대값 : "+abs(value));
		value = -10;
		System.out.println(value+"의 절대값 : "+abs(value));
	}

}

'Study > Java' 카테고리의 다른 글

Chapter08.예외처리  (0) 2023.07.09
Chapter07.객체지향 프로그래밍2  (1) 2023.07.09
Chapter06. 객체지향 프로그래밍I(1)  (0) 2023.06.25
03. 연산자  (0) 2023.04.03
02. 변수  (0) 2023.04.02

댓글