[5-5]
다음은 1과 9사이의 중복되지 않은 숫자로 이루어진 3자리 숫자를 만들어내는 프
로그램이다. (1)~(2)에 알맞은 코드를 넣어서 프로그램을 완성하시오.
public class Java {
public static void main(String[] args) {
int[] ballArr = {1,2,3,4,5,6,7,8,9};
int[] ball3 = new int[3];
for (int i = 0; i < ballArr.length; i++) {
int j = (int) (Math.random() * ballArr.length);
int tmp = 0;
tmp = ballArr[i];
ballArr[i] = ballArr[j];
ballArr[j] = tmp;
}
System.arraycopy(ballArr, 0, ball3, 0, 3);
for (int i = 0; i < ball3.length; i++) {
System.out.print(ball3[i]);
}
}
}
[5-7]
보유한 동전의 개수로 거스름돈을 지불할 수 없으면, ‘거스름 돈이 부족합니다.’라고 출력하고 종료한다. 지불할 돈이 충분히 있으면, 거스름돈을 지불 한만큼가진돈에서빼고남은동전의개수를화면에출력한다. (1)에알맞은코드를 넣어서 프로그램을 완성하시오.
public class Java {
public static void main(String[] args) {
int money = 3170;
System.out.println("money = " + money);
int[] coinUnit = {500, 100, 50, 10};
int[] coin = {5, 5, 5, 5};
for (int i = 0; i < coinUnit.length; i++) {
int coinNum = 0;
coinNum = money / coinUnit[i];
if (coinNum > coin[i]) {
coinNum = 5;
}
money = money - coinUnit[i] * coinNum;
coin[i] -= coinNum;
System.out.println(coinUnit[i]+"원: "+coinNum);
}
if (money > 0) {
System.out.println("거스름돈이 부족합니다.");
System.out.println(0);
}
System.out.println("==남은 동전의 개수==");
for (int i = 0; i < coinUnit.length; i++) {
System.out.println(coinUnit[i] + "원: " + coin[i]);
}
}
}
[5-8]
다음은 배열 answer에 담긴 데이터를 읽고 각 숫자의 개수를 세어서 개수만큼 ‘*’ 을 찍어서 그래프를 그리는 프로그램이다. (1)~(2)에 알맞은 코드를 넣어서 완성하시오.
public class Java {
public static void main(String[] args) {
int[] answer = {1,4,4,3,1,4,4,2,1,3,2};
int[] counter = new int[4];
for (int i = 0; i < answer.length; i++) {
if (answer[i] == 1) {
counter[0]++;
} else if (answer[i] == 2) {
counter[1]++;
} else if (answer[i] == 3) {
counter[2]++;
} else {
counter[3]++;
}
}
for (int i = 0; i < counter.length; i++) {
System.out.print(counter[i]);
for (int j = 0; j < counter[i]; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
[5-11]
주어진 2차원 배열의 데이터보다 가로와 세로로 1이 더 큰 배열을 생성해서 배열 의 행과 열의 마지막 요소에 각 열과 행의 총합을 저장하고 출력하는 프로그램이다.
public class Java {
public static void main(String[] args) {
int[][] score = {
{100, 100, 100},
{20, 20, 20} ,
{30, 30, 30} ,
{40, 40, 40} ,
{50, 50, 50}
};
int[][] result = new int[score.length+1][score[0].length+1];
for (int i = 0; i < score.length; i++) {
for (int j = 0; j < score[i].length; j++) {
result[i][j] = score[i][j];
result[i][3] += score[i][j];
result[5][j] += score[i][j];
result[5][3] += score[i][j];
}
}
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.printf("%4d", result[i][j]);
}
System.out.println();
}
}
}
[5-13]
단어의 글자위치를 섞어서 보여주고 원래의 단어를 맞추는 예제이다. 실행결과와 같이 동작하도록 예제의 빈 곳을 채우시오.
import java.util.Scanner;
public class Java {
public static void main(String[] args) {
String[] words = {"television", "computer", "mouse", "phone"};
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < words.length; i++) {
char[] question = words[i].toCharArray();
int a = (int) (Math.random() * question.length);
char ch = question[a];
question[a] = question[i];
question[i] = ch;
System.out.printf("Q%d. %s의 정답을 입력하세요.>", i+1, new String(question));
String answer = scanner.nextLine();
if (words[i].equals(answer.trim())) {
System.out.printf("맞았습니다.%n%n");
} else {
System.out.printf("틀렸습니다.%n%n");
}
}
}
}
[6-6]
[6-6] 두 점의 거리를 계산하는 getDistance()를 완성하시오.
public class Java {
public static void main(String[] args) {
System.out.println(getDistance(1, 1, 2, 2));
}
static double getDistance(int x, int y, int x1, int y1) {
return Math.sqrt(Math.pow((x1-x), 2) + Math.pow((y1-y), 2));
}
}
[6-20]
public class Java {
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));
}
static int[] shuffle(int[] original) {
if (original == null || original.length == 0) {
return original;
}
for (int i = 0; i < original.length; i++) {
int a = (int) (Math.random() * original.length);
int b = original[i];
original[i] = original[a];
original[a] = b;
}
return original;
}
}
[6-22]
public class Java {
public static void main(String[] args) {
String str = "123";
System.out.println(str+"는 숫자입니까? "+isNumber(str));
str = "1234o";
System.out.println(str+"는 숫자입니까? "+isNumber(str));
}
static boolean isNumber(String str) {
if (str == null || str.equals("")) {
return false;
}
for (int i = 0; i < str.length(); i++) {
int a = str.charAt(i) - '0';
if (!(0 <= a && a <= 9)) {
return false;
}
}
return true;
}
}
[6-23]
메서드명 : max
기 능 : 주어진int형배열의값중에서제일큰값을반환한다.
만일 주어진 배열이 null이거나 크기가 0인 경우, -999999를 반환한다.
반환타입 : int
매개변수 : int[] arr - 최대값을 구할 배열
public class Java {
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[]{})); // 크기가 0인 배열
}
static int max(int[] arr) {
if (arr == null || arr.length == 0) {
return -999999;
}
int max = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
[6-24]
메서드명 : abs
기 능 : 주어진 값의 절대값을 반환한다. 반환타입 : int
매개변수 : int value
public class Java {
public static void main(String[] args) {
int value = 5;
System.out.println(value+"의 절대값:"+abs(value));
value = -10;
System.out.println(value+"의 절대값:"+abs(value));
}
static int abs(int value) {
if (value < 0) {
value = -value;
}
return value;
}
}