29일차

김윤정·2024년 7월 26일

코딩

목록 보기
29/60
post-thumbnail

1. 프로그램을 완성 하시오.

  • 5개의 숫자를 랜덤 으로 받아 배열로 저장
  • 5개의 숫자 중 가장 큰 값을 출력
package day_2024_07_26;

import java.util.Arrays;

public class _5_Random {
	public static void main(String[] args) {

		int[] randomArr = new int[5];

		for (int i = 0; i < randomArr.length; i++) {
			randomArr[i] = (int) (Math.random() * 100 + 1);
		}

		int max = randomArr[0];

		for (int i = 0; i < randomArr.length; i++) {

			if (max < randomArr[i])
				max = randomArr[i];
		}

		System.out.println(Arrays.toString(randomArr));
		System.out.println(max);

	}
}

2. char 배열을 생성하여, 알파벳 A~Z까지 대입 후, 출력 해보자. 알파벳은 26개.

package _2024_07_26;

public class Array_Abc {
	public static void main(String[] args) {

		char[] arrAbc = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
				'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
		for (int i = 0; i < arrAbc.length; i++) {
			System.out.print(arrAbc[i]+" ");
		}

	}

}

3. 정수를 10개 저장하는 배열을 만들고 1에서 10까지 범위의 정수를 랜덤하게 생성하여 배열에 저장하라. 그리고 배열에 든 숫자들과 평균을 출력하라.

출력)
랜덤한 정수들 : 3 6 3 6 1 3 8 9 6 9
평균은 5.4

public class Array_Random {
	public static void main(String[] args) {
		int[] arrRandom = new int[10];
		double sum = 0;
		double avg;
		for (int i = 0; i < arrRandom.length; i++) {
			arrRandom[i] = (int) (Math.random() * 10 + 1);
			System.out.print(arrRandom[i] + " ");
			sum += i;
		}
		avg = sum / arrRandom.length;
		System.out.println();
		System.out.println("평균: " + avg);
	}

}

4. Rectangle 을 배열로 3개 선언

해당 객체에 인덱스 순서대로 가로 세로 설정 -
이번에는 반드시 scanner 로 입력 받을것
해당 배열에 있는 Rectangle 의 총넓이의 합을 구하시오.​
또한 아래의 함수도 만들것(static 으로 만들것)

  • 파라미터를 Rectangle 배열로 받아서 해당 배열에 들어 잇는
    Rectangle 들에 총 넓이를 리턴 하는 함수를 만
    드시오.
package _2024_07_26;

import java.util.Scanner;

class Rectangle {
	private double width, height;

	public Rectangle(double w, double h) {
		width = w;
		height = h;
	}

	public double getArea() {
		return width * height;
	}
}

public class Array_Rect {
	public static double RectAreaSum(Rectangle[] r) {
		double sum = 0;
		for (int i = 0; i < r.length; i++) {
			sum += r[i].getArea();
		}
		return sum;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Rectangle[] arrRect = new Rectangle[3];
		double areaSum = 0;

		for (int i = 0; i < arrRect.length; i++) {
			System.out.print("가로: ");
			int width = sc.nextInt();
			System.out.print("세로: ");
			int height = sc.nextInt();
			arrRect[i] = new Rectangle(width, height);
			areaSum += arrRect[i].getArea();
		}

		System.out.println("모든 사각형의 넓이의 합: " + areaSum);
		System.out.println("모든 사각형의 넓이의 합: " + RectAreaSum(arrRect));

	}

}

5. 아래가 실행되는 원리(함수)를 표현해 보세요.

String str = "age" + 17;

자료형이 다른 더하기 연산시 자료형의 범위가 넓은 쪽으로 자동 형변환이 됩니다.
따라서 정수형인 17을 문자열로 자동 형변환을 해서 연산을 하기 때문에 오류가 발생하지 않습니다.

6.StringBuilder 와 String 의 차이는?

  • String은 새로운 문자열로 값을 바꿔도 바꾸기 전의 값들이 저장공간에 그대로 남아있습니다.
  • StringBuilder은 새로운 문자열로 바꾸면 같은 저장공간에 있는 문자열을 수정합니다.

7. 아래의 메모리 그림을 그리시오. (1차원 배열)

int[] ar1 = new int[5];

8. 아래의 메모리 그림을 그리시오. (1차원 배열)

Box[] arr = new Box[5];

9. 아래를 배열을 활용하여 프로그램을 짜시오.

화폐매수 구하기

  • 1원 부터 5000 원 까지 랜덤으로 생성.
  • 500원 100 원 50 원 10원은 배열로 선언 하여 저장
    해당 랜덤생성된 화폐 매수를 아래와 같이 출력
    출력
    2860원
    500원 : 5개
    100원 : 3개
    50원 : 1개
    10원 : 1개
public class ArrayMoney {

	public static void main(String[] args) {
		int money = (int) (Math.random() * 5000 + 1);
		int[] count = new int[4];
		int moneySave = money;
		
		count[0] = moneySave / 500;
		moneySave = moneySave % 500;
		
		count[1] = moneySave / 100;
		moneySave = moneySave % 100;
		
		count[2] = moneySave / 50;
		moneySave = moneySave % 50;
		
		count[3] = moneySave / 10;
		moneySave = moneySave % 10;
		
		System.out.println("랜덤액수: " + money + "원");
		System.out.println("500원: " + count[0] + "개");
		System.out.println("100원: " + count[1] + "개");
		System.out.println("50원: " + count[2] + "개");
		System.out.println("10원: " + count[3] + "개");
		System.out.println("남은금액: " + moneySave + "원");
	}

}

10. 양의 정수 100개를 랜덤 생성하여, 배열에 저장하고, 배열에 있는 정수 중에서 3의 배수만 출력해 보자.

package _2024_07_26;

public class Array_Three {
	public static void main(String[] args) {
		int[] arrThree = new int[100];
		for (int i = 0; i < arrThree.length; i++) {
			arrThree[i] = (int) (Math.random() * 100 + 1);
			if (arrThree[i] % 3 != 0) {
				continue;
			} else {
				System.out.print(arrThree[i] + " ");
			}
		}

	}

}

0개의 댓글