28일차 java 연산(2023-02-01)

권단비·2023년 2월 1일
0

IT

목록 보기
52/139
[계산]
import java.util.Scanner;
class Grade8 {
	int math;
	int eng;
	int kor;

	public Grade8(int math, int eng, int kor) {
		this.math = math;
		this.eng = eng;
		this.kor = kor;
	}

	public int getSum() {
		return this.math + this.eng + this.kor;
	}

	public double getAvg() {
		return (math + this.eng + this.kor) / 3.0;
	}

	public void show() {
		System.out.println("총점: " + getSum());
		System.out.println("평균: " + getAvg());
		System.out.println("계속 하시겠습니까? y / n");
	}
}
public class Test32 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		boolean run = true;
		while (run) {
			System.out.print("국어: ");
			int kor = sc.nextInt();
			System.out.print("영어: ");
			int eng = sc.nextInt();
			System.out.print("수학: ");
			int math = sc.nextInt();

			Grade8 grade = new Grade8(kor, eng, math);
			grade.show();

			String yOrn = sc.next();
			if ((yOrn.equals("y")) || (yOrn.equals("Y"))) {
				continue;
			} else {
				break;
			}
		}
		System.out.println("종료입니다.");
	}
}
[결과값]
국어: 100
영어: 76
수학: 90
총점: 266
평균: 88.66666666666667
계속 하시겠습니까? y / n
y
국어: 76
영어: 90
수학: 100
총점: 266
평균: 88.66666666666667
계속 하시겠습니까? y / n
n
종료입니다.

[계산]
//2. 로또번호를 담는 배열을 선언후
//로또 번호를 출력하시오.(단 중복 없이 출력하시오.)
import java.util.Scanner;
public class Test32 {
	public static void main(String[] args) {
		int[] lotto = new int[6];
		for (int i = 0; i < lotto.length; i++) {
			lotto[i] = (int) (Math.random() * 45) + 1;
         // Math.random()은 데이터타입이 double로 0~1 사이의 랜덤 값을 리턴한다.
		// (int)Math.random() : 0.99999 * 100 = 99가 나온다. +1을 하면 100
		// 0.00001 * 100 = 0이 나온다. +1을 하면 1
			for (int j = 0; j < i; j++) {
				if (lotto[i] == lotto[j]) {
					i--;
				}
			}
		}
		System.out.print("로또 번호:");
		for (int i = 0; i < lotto.length; i++) {
			System.out.print(lotto[i] + " ");
		}
	}
}
[결과값]
로또 번호:7 34 32 29 35 8

・lotto[i] = (int) (Math.random() * 45) + 1;
⇒1 ~ 45까지의 크기를 말함.


[값의 저장과 참조의 예]

static 키워드는 메모리의 효율보다는 공유의 목적으로 훨씬 더 많이 사용한다.

[계산]
class Box {
	private String conts;

	Box(String cont) {
		this.conts = cont;
	}

	public String toString() {
		// 해당 함수가 없을 시 (or 소문자 tostring으로 기재 시) main에서 String값이 나오지 않는다.
		// 함수 오버라이딩
		return conts;
	}
}
public class Test33 {
	public static void main(String[] args) {
		Box[] ar = new Box[3];
		// 객체 명을 직접 뿌릴 시 주소값이 나온다.
		// toString을 하게 되면 String 값이 나온다.

		// 배열에 인스턴스 저장
		ar[0] = new Box("First");
		ar[1] = new Box("Second");
		ar[2] = new Box("Third");

		// 저장된 인스턴스의 참조
		System.out.println(ar[0]);
		System.out.println(ar[1]);
		System.out.println(ar[2]);
	}
}
[결과값]
First
Second
Third

[계산]
public class ArrayRe {
	public static void main(String[] args) {
		String[] sr = new String[7];
		sr[0] = new String("Java");
		sr[1] = new String("System");
		sr[2] = new String("Compiler");
		sr[3] = new String("Park");
		sr[4] = new String("Tree");
		sr[5] = new String("Dinner");
		sr[6] = new String("Brunch Cafe");

		int cnum = 0;
		for (int i = 0; i < sr.length; i++) {
			cnum += sr[i].length();
		}
		System.out.println("총 문자의 수: " + cnum);
	}
}
// 배열 요소는 반복문을 통해 순차적 접근이 가능하며,
// 이것은 배열이 가진 큰 장점 중 하나이다.
[결과값]
총 문자의 수: 43

[계산]
class Circle8 {
	private double radius;

	public Circle8(double radius) {
		this.radius = radius;
	}

	public double getArea() {
		return radius * radius * Math.PI;
	}

	public static double getArrArea(Circle8[] c) {
		double sumArea = 0;
		for (int i = 0; i < c.length; i++) {
			sumArea += c[i].getArea();
		}
		return sumArea;
	}
}

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

		int[] a = new int[2];
		Circle8[] circleArr = new Circle8[2]; // Circle8[]은 데이터 타입이다!

		circleArr[0] = new Circle8(10);
		circleArr[1] = new Circle8(10);

// 상기 배열을 풀어서 설명
//		Circle8 b = new Circle8(10);
//		Circle8 c = new Circle8(10);
//		
//		circleArr[0] = b; // Circle8 타입의 생성된 객체
//		circleArr[1] = c; // Circle8 타입의 생성된 객체

		double areas = Circle8.getArrArea(circleArr);
		System.out.println(areas); // 해당 넓이 출력
	}
}
[결과값]
628.3185307179587

[계산]
class Rectangle8 {
	private double width;
	private double height;

	public Rectangle8(double width, double height) {
		this.width = width;
		this.height = height;
	}

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

	public static double getArrArea(Rectangle8[] r) {
		double sumArea = 0;
		for (int i = 0; i < r.length; i++) {
			sumArea += r[i].getArea();
		}
		return sumArea;
	}
}
public class Test34 {
	public static void main(String[] args) {
		Rectangle8[] recArr = new Rectangle8[2];
		recArr[0] = new Rectangle8(10, 20);
		recArr[1] = new Rectangle8(10, 30);

		double areas = Rectangle8.getArrArea(recArr);
		System.out.println(areas);
	}
}
[결과값]
500.0

[계산]
public class Test35 {
	public static void main(String[] args) {
		String[] strArr = new String[3];
		strArr[0] = "ABCDER";
		strArr[1] = "ABCDER";
		strArr[2] = "ABCDER";
		System.out.println(getStrArr(strArr)); // 18
	}

	static int getStrArr(String[] strArr) {
		int count = 0;
		for (int i = 0; i < strArr.length; i++) {
			count += strArr[i].length();
		}
		return count;
	}
}
[결과값]
18

0개의 댓글