🧤정답🧤
import java.util.Arrays;
public class Programming6 {
public static void main(String[] args) {
int junBe[] = new int[] { 1, 9, 6, 8, 4 };
String StrBe[] = new String[] { "참새", "비둘기", "독수리", "부엉이", "갈매기" };
System.out.println(Arrays.toString(reverse(junBe)));
}
// 배열값으로 반환하기 위한 메서드(참조변수 또한 배열값)
public static int[] reverse(int[] org) {
int temp[] = new int[5];
for (int i = 0; i < temp.length; i++) {
for (int j = 4 - i; j >= 0; j--) {
temp[i] = org[j];
break;
}
}
return temp;
}
}
7. 2개의 1차원 배열에서 내용이 같은지를 조사하는 메서드를 정의하고, 다음 배열을 사용해 테스트하라.
📢주어진 값📢
int a[] = {3, 2, 4, 1, 5};
int b[] = {3, 2, 4, 1};
int c[] = {3, 2, 4, 1, 5};
int d[] = {2, 7, 1, 8, 2};
🧤정답🧤
public class Ex7 {
public static void main(String[] args) {
int a[] = {3, 2, 4, 1, 5};
int b[] = {3, 2, 4, 1};
int c[] = {3, 2, 4, 1, 5};
int d[] = {2, 7, 1, 8, 2};
equal(a, b); // 배열 a와 b를 비교하기 위한 equal 메서드 호출
equal(a, c); // 배열 a와 c를 비교하기 위한 equal 메서드 호출
equal(a, d); // 배열 a와 d를 비교하기 위한 equal 메서드 호출
equal(b, c); // 배열 b와 c를 비교하기 위한 equal 메서드 호출
equal(b, d); // 배열 b와 d를 비교하기 위한 equal 메서드 호출
equal(c, d); // 배열 c와 d를 비교하기 위한 equal 메서드 호출
}
private static void equal(int[] a, int[] b) {
// 배열의 원소가 몇개가 동일한지 체크
int count = 0;
// 첫 번째 조건 배열의 길이가 같은지
if (a.length == b.length) {
for (int i = 0; i < a.length; i++) {
// 각 배열의 원소가 같은지
if (a[i] == b[i]) {
count++;
}
}
}
// 배열의 길이만큼 원소가 동일한지
if (count==a.length){
System.out.println("같습니다.");
}
else System.out.println("다릅니다.");
}
}
8. 영문 대문자로 요일('SUNDAY', 'MONDAY' 등)을 나타내는 열거 타입을 정의하고 다음과 같은 실행 결과 나타나는 테스트 프로그램을 작성하라.
📢주어진 값📢
1. switch 문으로 월요일에 대하여 '싫다', 금요일에 대하여 '좋다', 토요일과 일요일에 대하여 '최고', 나머지 요일에
대하여 '그저 그렇다'라고 출력하는 메서드를 정의한다.
2. 키보드로부터 대소문자 구분없는 영문 요일을 입력하면 '월요일은 싫다.' 등과 같이 출력되는 메인 프로그램을 작성한다.
📢출력 값📢
monday => 키보드로부터 입력한 문자열
월요일은 싫다.
🧤정답🧤
public class Ex8 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String PutDay = in.next();
Day.valueOf(PutDay.toUpperCase());
System.out.println(Day.valueOf(PutDay.toUpperCase()) + "은 싫다");
equalDay(PutDay.toUpperCase());
System.out.println(equalDay(PutDay));
}
public static String equalDay(String PutDay) {
return switch (PutDay) {
case "SUNDAY", "SATURDAY" -> "최고";
case "MONDAY" -> "싫다.";
case "FRIDAY" -> "좋다.";
default -> "그저 그렇다.";
};
}
}
enum Day {
SUNDAY("일요일"), MONDAY("월요일"), TUESDAY("화요일"), WEDNESDAY("수요일"), THURSDAY("목요일"), FRIDAY("금요일"), SATURDAY("토요일");
private String s;
Day(String s) {
this.s = s;
}
public String toString() {
return s;
}
}
9. 다음과 같은 지뢰찾기 게임 프로그램을 작성하라. 실행 결과는 '5 10 0.3'을 명령행 인수로 사용한 예이다.
📢주어진 값📢
1. 프로그램은 3개의 명령행 인수(m,n,p)를 받아들이고, m*n 크기의 배열을 생성해 지뢰를 숨긴다.
2. 숨긴 지뢰가 있는 원소는 *로 표시하고, 없는 원소는 -로 표시한다. 원소에 지뢰가 있을 확률은 세 번째 명령행 인수인 p이다.
3. 지뢰 숨김 여부를 나타내는 2차원 배열을 출력하고, 지뢰를 숨기지 않은 원소를 -대신에 이웃한 지뢰 개수로 채운 2차원 배열도 함께 출력한다.
4. 이웃한 지뢰는 상하좌우 및 대각선 원소에 숨긴 지뢰를 의미한다.
5. 지뢰 숨긴 지역을 30%로 설정하려면, 난수 발생 정적 함수 Math.random() 값이 0.3보다 적은 원소에 지뢰를 숨긴다.
🧤정답🧤
import java.util.Scanner;
public class Ex9 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("행의 값 : ");
int line = in.nextInt();
System.out.print("열의 값 : ");
int row = in.nextInt();
System.out.print("지뢰기 있을 확률의 값 : ");
double occurMinePercentage = in.nextDouble();
String HideMine = "*";
String NotHideMine = "-";
double hideoccurMinePercentage = 0;
int arroundMineCount = 0;
String Mine1[][] = new String[line][row];
System.out.println(Mine1.length);
for (int i = 0; i < Mine1.length; i++) {
for (int j = 0; j < Mine1[i].length; j++) {
hideoccurMinePercentage = Math.random();
Mine1[i][j] = "";
if (hideoccurMinePercentage < occurMinePercentage) {
Mine1[i][j] += HideMine;
} else {
Mine1[i][j] += NotHideMine;
}
System.out.printf(Mine1[i][j]);
if (j == Mine1[i].length - 1) {
System.out.println();
}
}
}
System.out.println("\n");
for (int i = 0; i < Mine1.length; i++) {
for (int j = 0; j < Mine1[i].length; j++) {
if (Mine1[i][j].equals("-")) {
arroundMineCount = 0;
Mine1[i][j] = "";
if (i == 0) {
if (j == 0) {
if (Mine1[i][j + 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j + 1].equals("*")) arroundMineCount++;
} else if (j == Mine1[i].length - 1) {
if (Mine1[i][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j].equals("*")) arroundMineCount++;
} else {
if (Mine1[i][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i][j + 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j + 1].equals("*")) arroundMineCount++;
}
} else if (i == Mine1.length - 1) {
if (j == 0) {
if (Mine1[i - 1][j].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j + 1].equals("*")) arroundMineCount++;
if (Mine1[i][j + 1].equals("*")) arroundMineCount++;
} else if (j == Mine1[i].length - 1) {
if (Mine1[i - 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j].equals("*")) arroundMineCount++;
if (Mine1[i][j - 1].equals("*")) arroundMineCount++;
}
else {
if (Mine1[i - 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j + 1].equals("*")) arroundMineCount++;
if (Mine1[i][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i][j + 1].equals("*")) arroundMineCount++;
}
} else {
if (j == 0) {
if (Mine1[i - 1][j].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j + 1].equals("*")) arroundMineCount++;
if (Mine1[i][j + 1].equals("*")) arroundMineCount++;
} else if (j == Mine1[i].length - 1) {
if (Mine1[i - 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j].equals("*")) arroundMineCount++;
if (Mine1[i][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j].equals("*")) arroundMineCount++;
} else {
if (Mine1[i - 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j].equals("*")) arroundMineCount++;
if (Mine1[i - 1][j + 1].equals("*")) arroundMineCount++;
if (Mine1[i][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i][j + 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j - 1].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j].equals("*")) arroundMineCount++;
if (Mine1[i + 1][j + 1].equals("*")) arroundMineCount++;
}
}
Mine1[i][j] = String.valueOf(arroundMineCount);
}
System.out.printf(Mine1[i][j]);
if (j == Mine1[i].length - 1) {
System.out.println();
}
}
}
}
}