운동 문제
(6-7)
class Marine {
int x = 0, y = 0;
int hp = 60;
static int weapon = 6; // 모든 병사의 공격력은 공통값이고 바뀌면 다같이 바뀌어야 하기 때문이다.
static int armor = 0; // 공격력과 같은 이유
static void weaponUp() { // static 변수에 대해 작업을 하는 메서드이므로 static을 붙여야 한다.
weapon++;
}
static void armorUp() { // static 변수에 대해 작업을 하는 메서드이므로 static을 붙여야 한다.
armor++;
}
void move(int x, int y) {
this.x = x;
this.y = y;
}
}
(6-8)
- 2번: 개체 생성은 new에 의해 수행됩니다.
- #5: 생성자도 오버로드될 수 있습니다.
(6-9)
- 2번: 클래스 메서드 내에서 사용할 수 없습니다.
(6-10)
- 3번: 반환 유형은 중요하지 않습니다.
- 4: 매개변수의 이름은 중요하지 않습니다.
(6-11)
- #2: 긴 추가(긴 a, 긴 b) { return a+b; } (파라미터 타입이 다르기 때문에 오버로드입니다.
) - 3: int add(바이트 a, 바이트 b) { return a+b; }
- 4: int add(long a, int b) { return (int)(a+b) }
- 긴 추가(int a, int b) { return a+b; }
(6-12)
- #3: 초기화 블록의 경우 생성자보다 먼저 실행된다.
- 5: 클래스 변수는 인스턴스 변수보다 먼저 초기화됩니다.
(6-13)
- 초기화 순서는 기본 – 명시적 초기화 – 초기화 블록 – 생성자입니다.
(6-14)
- #1: 지역 변수는 자동 초기화가 불가능하므로 따로 초기화를 해줘야 합니다.
- 5: 힙이 아닌 스택 영역에 생성됩니다.
(6-15)
- 2: 메서드가 호출 스택에 남아 있으면 메서드가 종료되지 않습니다.
(6-16)
class Exercise6_16 {
public static void change(String str) {
str += "456";
}
public static void main(String() args) {
String str = "ABC123";
System.out.println(str); // ABC123 출력
change(str);
System.out.println("After change:" + str); // After change:~~ABC123456 출력~~ ABC123 출력..???
}
}
댓글
더보기
문자열의 내용은 수정할 수 없기 때문에 추가 작업을 수행하면 새 문자열이 생성되고 새 문자열의 주소가 지역 변수 str에 저장됩니다.
변경 방법 종료 후 로컬 변수는 사라지기 때문에 main 함수에 표시되는 값은 변경되지 않습니다.
(6-17)
class Exercise6_17 {
static int() shuffle(int() arr) { // 처음에 static을 안 붙였었고, 에러가 나왔다.
왜 붙여야 되지?
if(arr==null || arr.length == 0) return arr; // 매개변수로 어떤 값이 넘어올지 모르기 때문에 값의 유효성체크는 반드시 해야 한다.
for (int i=0; i<arr.length; i++) {
int s = (int)(Math.random()*10);
int tmp = arr(s);
arr(s) = arr(i);
arr(i) = tmp;
}
return arr;
}
public static void main(String() args) { // 이게 static이 있어서 그런건가?
int() original = {1,2,3,4,5,6,7,8,9};
System.out.println(java.util.Arrays.toString(original));
int() result = shuffle(original);
System.out.println(java.util.Arrays.toString(result));
}
}
(6-18)
class Exercise6_18 {
static boolean isNumber (String str) {
if(str==null || str.equals("")) return false; // 유효성 체크
boolean answer = true;
for (int i=0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
answer = false;
break;
}
}
return answer;
}
public static void main(String() args) {
String str = "123";
System.out.println(str + "는 숫자입니까? " + isNumber(str));
str = "1234o";
System.out.println(str + "는 숫자입니까? " + isNumber(str));
}
}
(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 = !
isPowerOn;
}
void volumeUp() {
if (volume < MAX_VOLUME) volume++;
}
void volumeDown() {
if (volume > MIN_VOLUME) volume--;
}
void channelUp() {
if (channel == MAX_CHANNEL) channel = 1;
else channel++;
}
void channelDown() {
if (channel == MIN_CHANNEL) channel = 100;
else channel--;
}
}
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)
class Exercise6_20 {
static int max(int() arr) {
if (arr == null || arr.length == 0) return -999999;
else {
int max = -999999; // 초기화를 주어진 배열의 첫 번째 값으로 하는 것이 더 좋아보인다.
for (int i : arr) if (i > max) max = 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)
class Exercise6_21 {
static int abs(int i) {
int result;
result = i >= 0 ? i:-i;
return result;
}
public static void main(String() args) {
int value = 5;
System.out.println(value + "의 절대값" + abs(value));
value = -10;
System.out.println(value + "의 절대값" + abs(value));
}
}
내 말
더보기
어렵다 어렵다… 객체지향이다… 멘탈이 점점 안좋아졌다.
설명을 잘 듣고 이해하고 코드까지 작성했는데 하루 지나고 잊어버린 것 같아요. 나는 바보다