Contents
[6-1, 6-2]SutdaCard.java[6-3, 6-4, 6-5]Student.java[6-6]Exercise6_6.java[6-7]MyPoint.java[6-8]Answer[6-9]Answer[6-10]Answer[6-11]Answer[6-12]Answer[6-13]Answer[6-14]Answer[6-15]Answer[6-16]Answer[6-17]Answer[6-18]Answer[6-19]Answer[6-20]Exercise6_20.java[6-21]MyTv.java[6-22]Exercise6_22.java핵심 키워드[6-23]Exercise6_23.java[6-24]Exercise6_24.java[이것이 자바다 5장 연습문제 변형]Student1.javaStudent1Practice.java결론[6-1, 6-2]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F27262b2b-65eb-4f7f-8b38-61e675f99082%252FUntitled.png%3Ftable%3Dblock%26id%3Dd5b6fa0e-88da-4381-b8ee-cad737585aab%26cache%3Dv2&w=3840&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F3fb1b814-2af4-4365-b78c-b5ea6ca0d9cc%252FUntitled.png%3Ftable%3Dblock%26id%3D209915d0-ea5f-4af4-9e3f-ce21b8178c24%26cache%3Dv2&w=3840&q=75)
SutdaCard.java
package ch06.example; public class SutdaCard { int num; boolean isKwang; public SutdaCard() { this(1, true); } public SutdaCard(int num, boolean isKwang) { this.num = num; this.isKwang = isKwang; } public String info() { return num + (isKwang ? "K" : ""); } }
[6-3, 6-4, 6-5]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Fbefaf6f8-f337-4c1e-9187-62493cb8d2b1%252FUntitled.png%3Ftable%3Dblock%26id%3D99299826-8e5d-47b8-8191-f811c4708e1a%26cache%3Dv2&w=3840&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F8e81ea57-8d04-4ead-9c39-4cc6673fe740%252FUntitled.png%3Ftable%3Dblock%26id%3D3d561522-e2d9-4bc6-a076-99df2ff04fa8%26cache%3Dv2&w=3840&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Feb973fb3-8815-40de-a65f-00f791915102%252FUntitled.png%3Ftable%3Dblock%26id%3D5f466e3e-0b96-41ab-955d-72ceebd7540f%26cache%3Dv2&w=3840&q=75)
Student.java
package ch06.example; public class Student { String name; int ban; int no; int kor; int eng; int math; public Student() { } public Student(String name, int ban, int no, int kor, int eng, int math) { this.name = name; this.ban = ban; this.no = no; this.kor = kor; this.eng = eng; this.math = math; } public int getTotal() { return kor + eng + math; } public float getAverage() { return Math.round(getTotal() / 3f * 10f) / 10f; } public String info() { return name + "," + ban + "," + no + "," + kor + "," + eng + "," + math + "," + getTotal() + "," + getAverage(); } }
[6-6]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F7ade08f1-a766-4800-9106-abffabc7a42b%252FUntitled.png%3Ftable%3Dblock%26id%3D62244c32-efab-4994-8f1b-966d2ab0a498%26cache%3Dv2&w=3840&q=75)
Exercise6_6.java
package ch06.example; public class Exercise6_6 { static double getDistance(int x, int y, int x1, int y1) { return Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)); } public static void main(String[] args) { System.out.println(getDistance(1, 1, 2, 2)); } }
[6-7]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F0e21b0ce-a114-4e65-a2aa-5eb0ed53299e%252FUntitled.png%3Ftable%3Dblock%26id%3D91e45423-5708-4db1-8204-4dc0d90d358b%26cache%3Dv2&w=3840&q=75)
MyPoint.java
package ch06.example; public class MyPoint { int x; int y; public MyPoint(int x, int y) { this.x = x; this.y = y; } public double getDistance(int x1, int y1) { return Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)); } }
[6-8]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Fe8449b75-dd82-4719-b4c6-3d0c0c42e12f%252FUntitled.png%3Ftable%3Dblock%26id%3Ded2d85fd-79cc-4d0c-b6c0-eb8c125c499c%26cache%3Dv2&w=3840&q=75)
Answer
// 클래스변수: width,height // 인스턴스변수: kind,num // 지역변수: k,n,card
[6-9]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F591c42e8-757e-4e1c-a7a0-4951b5e49606%252FUntitled.png%3Ftable%3Dblock%26id%3Db98a234c-2a9e-4b02-9053-5b028075cb50%26cache%3Dv2&w=3840&q=75)
Answer
// weapon, armor: 모든 인스턴스가 공유해야 하기 때문
[6-10]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F372e5a88-6339-41b2-ad05-51ea26cda37e%252FUntitled.png%3Ftable%3Dblock%26id%3Dabcc8514-051a-47fe-91f5-c6c212d52032%26cache%3Dv2&w=3840&q=75)
Answer
// b: 생성자는 객체 내의 필드를 초기화하기 위해 사용하는 것 // e: 생성자는 오버로딩 가능하다.
[6-11]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F1acee03d-73c1-43e8-978a-82eb02272c98%252FUntitled.png%3Ftable%3Dblock%26id%3D7ab01b41-6df7-4c44-9573-4498689f2472%26cache%3Dv2&w=3840&q=75)
Answer
// b: this는 static(클래스 메서드) 안에서는 사용 불가능하다.
[6-12]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Ff5a08e30-86ae-48d8-985d-50115e78000f%252FUntitled.png%3Ftable%3Dblock%26id%3D79229fec-76f6-4bd2-9336-5b1518cf9264%26cache%3Dv2&w=3840&q=75)
Answer
// c,d: 오버로딩은 파라미터의 개수, 타입이 다를 때 적용된다.
[6-13]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Fceee027d-7ef3-4d28-af39-36cacbae880f%252FUntitled.png%3Ftable%3Dblock%26id%3Da376ea20-610c-45d6-8f56-b974ebc08364%26cache%3Dv2&w=3840&q=75)
Answer
// b,c,d: a는 파라미터의 타입이 동일하므로 불가능하다.
[6-14]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F6025f9e1-3ddd-4c88-aa69-2116d1f529d2%252FUntitled.png%3Ftable%3Dblock%26id%3D5c8a797d-13f3-4858-bcb2-2a96fdd3a87f%26cache%3Dv2&w=3840&q=75)
Answer
// c: 인스턴스 변수는 디폴트, 명시적, 초기화블록, 생성자 순으로 초기화된다. // e: 클래스변수는 인스턴스를 만들기 이전 프로그램이 로딩되는 순간 메모리에 올라간다.
[6-15]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Ff986d8bf-db81-42cf-a1a4-029f63f9edb2%252FUntitled.png%3Ftable%3Dblock%26id%3D5936b7bb-beb6-4667-bfc0-cc6a81a879b2%26cache%3Dv2&w=3840&q=75)
Answer
// a
[6-16]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F2d309569-7231-4bd2-af90-9ec3fdbcc197%252FUntitled.png%3Ftable%3Dblock%26id%3D687a4f12-2569-4f1f-b15f-ee213b9079c0%26cache%3Dv2&w=3840&q=75)
Answer
// a: 로컬변수는 초기화가 없으면 사용이 불가능하다. // e: 힙 영역에는 인스턴스가 생성된다. 로컬변수는 스택에 생성된다.
[6-17]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Fa33b77f7-c476-4adf-b121-b65445a555ab%252FUntitled.png%3Ftable%3Dblock%26id%3Df4ea7eff-34b3-4820-addc-af33056ded5d%26cache%3Dv2&w=3840&q=75)
Answer
// b: 나머지 메서드들은 종료된 것이 아니라 대기하고 있는 상태이다.
[6-18]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Ffc5745da-b770-405b-86ba-04a9cd4a152e%252FUntitled.png%3Ftable%3Dblock%26id%3D78232c73-771e-42ce-aee6-c0dc68a706e0%26cache%3Dv2&w=3840&q=75)
Answer
// a: static 변수의 초기화에 인스턴스 변수 사용 불가능 // b: static 메서드에 인스턴스 멤버 호출 불가능 // d: static 메서드에 인스턴스 멤버 호출 불가능
[6-19]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Fe3651603-01f8-4907-9542-7f3243d10084%252FUntitled.png%3Ftable%3Dblock%26id%3D3f6fff58-f517-4e29-853f-ca70c280aad7%26cache%3Dv2&w=3840&q=75)
Answer
// ABC123 // ABC123
[6-20]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F97c80cb3-4379-48a0-bbd9-2091ca730534%252FUntitled.png%3Ftable%3Dblock%26id%3Dda2e21ad-6726-4abc-95d2-0a3f1a6ed9b3%26cache%3Dv2&w=3840&q=75)
Exercise6_20.java
package ch06.example; public class Exercise6_20 { private static int[] shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int random = (int) (Math.random() * arr.length); int tmp = arr[i]; arr[i] = arr[random]; arr[random] = tmp; } return arr; } 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 = shuffle(original); System.out.println(java.util.Arrays.toString(result)); } }
[6-21]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Fc2befefe-9987-42cf-80e1-bdeca936c74a%252FUntitled.png%3Ftable%3Dblock%26id%3Dabb8e4b1-048f-42ae-9e57-1ebe9e2bb01c%26cache%3Dv2&w=3840&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Fe68a2de0-77c1-41eb-9f5d-da865d4af8ea%252FUntitled.png%3Ftable%3Dblock%26id%3D4a73da2c-c39c-41a8-a6d6-13e09ab59b25%26cache%3Dv2&w=3840&q=75)
MyTv.java
package ch06.example; public 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() { if(this.isPowerOn=true) { this.isPowerOn = false; }else { this.isPowerOn = true; } } void volumeUp() { if(this.volume<MAX_VOLUME) { this.volume++; } } void volumnDown() { if(this.volume>MAX_VOLUME) { this.volume++; } } void channelUp() { if(this.channel>MAX_CHANNEL) { this.channel = MIN_CHANNEL; }else { this.channel++; } } void channelDown() { if(this.channel<MIN_CHANNEL) { this.channel = MAX_CHANNEL; }else { this.channel--; } } }
[6-22]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F1160beb3-6a4e-4e4a-958d-b0de48704638%252FUntitled.png%3Ftable%3Dblock%26id%3Ddbb18c8d-3313-41b9-a7bb-a4a6d49faa5b%26cache%3Dv2&w=3840&q=75)
Exercise6_22.java
package ch06.example; public class Exercise6_22 { private static boolean isNumber(String str) { return str.chars().allMatch(Character::isDigit); // for (int i = 0; i < str.length(); i++) { // if (str.charAt(i) != '0' && str.charAt(i) != '1' && // str.charAt(i) != '2' && str.charAt(i) != '3'&& // str.charAt(i) != '4' && str.charAt(i) != '5' && // str.charAt(i) != '6' && str.charAt(i) != '7'&& // str.charAt(i) != '8' && str.charAt(i) != '9') // { // return false; // } // } // return true; } public static void main(String[] args) { String str = "123"; System.out.println(str + "는 숫자입니까? " + isNumber(str)); str = "123o"; System.out.println(str + "는 숫자입니까? " + isNumber(str)); } }
핵심 키워드
- chars() 함수는 문자열을 구성하고 있는 문자들의 아스키코드 값을 stream 형태로 뽑아준다.
- allMatch() 함수는 모든 요소들이 주어진 조건을 만족하는지 조사한다.
- 이중 콜론(::) 연산자는 람다 표현식
() -> {}
을 대체할 수 있다. 이 경우 코드에서 중복되는 파라미터를 줄이고 가독성을 높일 수 있다.
- isDigit 함수는 char 값이 숫자인지 판단하여 true 또는 false 값을 리턴한다.
[6-23]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252F3621a3c0-759e-4c40-9771-8e8ac91cb188%252FUntitled.png%3Ftable%3Dblock%26id%3D5fd35e4b-6d17-4b5a-bdc2-4ff86fa5e221%26cache%3Dv2&w=3840&q=75)
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Ff4d156c7-4ba9-4307-9fbc-77bbe3f5ac0f%252FUntitled.png%3Ftable%3Dblock%26id%3D135eb1a8-4bbd-4e91-a038-833b1d1b4c06%26cache%3Dv2&w=3840&q=75)
Exercise6_23.java
package ch06.example; public class Exercise6_23 { private static int max(int[] arr) { int max = 0; if(arr == null || arr.length == 0) { return -999999; } for(int i=0; i<arr.length; i++) { if(arr[i]>max) { max = arr[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-24]
![notion image](https://inblog.ai/_next/image?url=https%3A%2F%2Fwww.notion.so%2Fimage%2Fhttps%253A%252F%252Fprod-files-secure.s3.us-west-2.amazonaws.com%252Fbf922eab-7f14-4cda-aa72-1e3c0f530cdd%252Ffe1691ca-5052-4631-a0fa-2c027a4a06c2%252FUntitled.png%3Ftable%3Dblock%26id%3D65712376-ca5d-4bf0-982a-52256f1e7e9e%26cache%3Dv2&w=3840&q=75)
Exercise6_24.java
package ch06.example; public class Exercise6_24 { private static int abs(int value) { return Math.abs(value); } public static void main(String[] args) { int value = 5; System.out.println(value + "의 절대값:" + abs(value)); value = -10; System.out.println(value + "의 절대값:" + abs(value)); } }
[이것이 자바다 5장 연습문제 변형]
Student1.java
package ch06.example; public class Student1 { String name; String height; Student1(String name, String height){ this.name = name; this.height = height; } public String getName() { return name; } public String getHeight() { return height; } }
Student1Practice.java
package ch06.example; import java.util.*; public class Student1Practice { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 학급 수 설정 System.out.printf("학급 수를 입력하세요 --> "); int classNum = scan.nextInt(); // 입력받은 수만큼 총 학급 수 설정 // 객체의 배열을 생성 시 객체 자체가 아닌 객체를 참조하는 배열이 생성됨. // 따라서 student 인스턴스는 Student1 객체의 배열에 대한 참조를 담을 수 있는 배열을 생성한 것. Student1[][] student = new Student1[classNum][]; // 학급 수 만큼 학생 키 설정 for (int i = 0; i < classNum; i++) { System.out.printf("%d반 학생 수를 입력하세요 --> ", i + 1); int studentNum = scan.nextInt(); scan.nextLine(); // 입력받은 수 만큼 각 학급의 학생 수 설정 student[i] = new Student1[studentNum]; // 열의 크기 만큼 학생의 키 설정 반복 for (int j = 0; j < studentNum; j++) { System.out.println("---------------입력---------------"); System.out.printf("%d반의 %d번 학생의 이름과 키를 공백으로 구분해서 입력하세요 --> ", i + 1, j + 1); String studentInfo = scan.nextLine(); // 입력받은 이름과 키를 공백으로 구분 String[] info = studentInfo.split(" "); String name = info[0]; String height = info[1]; // 분리한 이름과 키를 ()를 붙여서 저장 student[i][j] = new Student1(name, height); } } // 각 반 학생의 키 출력 for (int i = 0; i < student.length; i++) { System.out.printf("%d반 학생들의 키: ", i + 1); for (int j = 0; j < student[i].length; j++) { if (j == student[i].length - 1) { System.out.println(student[i][j].getName()+"("+student[i][j].getHeight()+")"); } else { System.out.print(student[i][j].getName()+"("+student[i][j].getHeight()+")" + ", "); } } } } }
결론
해당 문제들을 풀면서 allMatch() 함수, 이중 콜론 연산자, isDigit() 함수와 같이 처음 보는 문법을 익힐 수 있었다. 또한 클래스를 응용할 수 있는 문제들에 대해서도 알 수 있었다.
Share article