프로그래밍

[자바 기초] day06 : 생성자 예제 본문

자바/자바 기초

[자바 기초] day06 : 생성자 예제

시케 2023. 5. 11. 13:00
728x90
반응형

2023.05.10.수

생성자 예제

앞서 학습한 내용을 바탕으로 예제를 풀어보자

 

[ 예제1 ]


좌표 만들고 이동하기

좌표 값 x, y 값을 입력받아 좌표를 만든 후 해당 좌표들을 이동시켜라

 

좌표 값 생성

  1. 입력 값이 없으면 (0,0)으로 생성
  2. 입력 값이 하나면 (x,x)로 생성

좌표 이동

  1. 입력 값이 없으면 1씩 이동
  2. 입력 값이 하나면 x씩 이동

 

결과 예시

class Point
   int x
   int y
Point point=new Point();
   (0,0)
Point point=new Point(10);
   (10,10)
Point point=new Point(2,3);
   (2,3)
point.move();
   (11,12) -> (12,13)
point.move(3);
   (11,12) -> (14,15)
point.move(4,5);
   (11,12) -> (15,17)
point.printInfo();
   현재위치는 (20,30)입니다.

예제1 답안 코드

package class00;

class Point {
   int x;
   int y;

   Point() {
      this(0,0);
   }

   Point(int x) {
      this(x, x);
   }

   Point(int x, int y) {
      this.x = x;
      this.y = y;
   }

   void printInfo() {
      System.out.println("현재 위치는 (" + x + "," + y + ")입니다");
   }
   
   void move() {
      this.x+=1;
      this.y+=1;
   }
   
   void move(int x) {
      this.x+=x;
      this.y+=x;
   }

   void move(int x, int y) {
      this.x +=x;
      this.y +=y;

   }
}

public class Test01 {

   public static void main(String[] args) {
      Point point1 = new Point();
      Point point2 = new Point(10);
      Point point3 = new Point(2,3);
      
      point1.move();
      point2.move(3);
      point3.move(4,5);
      
      point1.printInfo();
      point2.printInfo();
      point3.printInfo();
   }

}

[ 예제2 ]

'학생'을 만든 후 이름과 몇번 시험볼지 입력받는다

각 학생의 시험 점수(0~100점)를 랜덤으로 저장한다

 

학생 정보 출력

학생의 이름과 시험, 시험점수, 평균, 등급을 출력한다

  1. 80.0점이상 등급 A
  2. 60.0점이상 B
  3. 나머지 C

재시험

학생의 시험점수(0~100)값을 랜덤으로 다시 저장한다

 

예제2 답안코드

package class01;

import java.util.Random;


class Student {
	Random rand = new Random();

	String name;
	int[] score;
	double avg;
	char grade;

	// 생성자
	Student(String name) {
		this(name, 2);
	}

	Student(String name, int N) {
		this.score = new int[N];
		this.name = name;
		
		this.grade = scoreCal(name, this.score.length);

		System.out.println(this.name + "는 시험을 " + N + "번 봅니다");

	}
	
	// 모듈화
	char scoreCal(String name, int N) {
		
		int sum = 0;
		for (int i = 0; i < N; i++) {
			this.score[i] = rand.nextInt(101);
			sum += this.score[i];
		}
		this.avg = (sum * 1.0) / this.score.length;

		if (this.avg >= 80) {
			this.grade = 'A';
		} else if (this.avg >= 60 && this.avg < 80) {
			this.grade = 'B';
		} else {
			this.grade = 'C';
		}
		
		return this.grade;
	}
	
	void printInfo() {
		System.out.println(this.name);
		for (int i = 0; i < this.score.length; i++) {
			System.out.println((i + 1) + "번 시험 " + this.score[i] + "점");
		}
		System.out.println("평균 " + avg + "점 [" + this.grade + "]");
	}

	void test() {
		this.grade = scoreCal(this.name, this.score.length);
		System.out.println(this.name + "가 재시험을 봅니다");
	}
}

public class Test01 {

	public static void main(String[] args) {

		Student student1 = new Student("럭스");
		Student student2 = new Student("유미", 4);

		student1.printInfo();
		student2.printInfo();

		student1.test();
		student1.printInfo();

	}

}

학생들의 점수의 합과 등급 산정하는 부분이 겹치기 때문에 모듈화하였다

728x90
반응형
Comments