메모장

[과제] Class 본문

JAVA/[실습문제]

[과제] Class

Itchild 2024. 4. 8. 17:39
728x90
반응형

 

[ 과제 1 ]

"점" 만들어주세요!~~

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(); // 입력값이 없을시 옆으로 1칸 씩 움직인다.

(11,12) -> (12,13)

point.move(3); move -> 오버로딩 x,y 둘다 3칸 움직인다.

(11,12) -> (14,15)

point.move(4,5); // x,y가 4,5 칸 움직인다.

(11,12) -> (15,17)

point.printInfo();

ex ) 현재위치는 (20,30)입니다.

 

package class01;


// ※ this() : 써보기
class Point {
   // 1.멤버변수 2.메서드 3.생성자 << 생각하기
   int x;
   int y;
   Point(){ // : 멤버변수 초기화
      this.x=0;
      this.y=0;
      // this(0,0); //이렇게 간략하게 쓸수 있음 //나 다른 생성자 들어올건데 이거 이용할거야 
   }
   Point(int x){
      this.x=x; // 입력한 값이 하나라서 둘다 같이 상승 한다 하여서 
      this.y=x;
      // this(x,x); 
   }
   Point(int x,int y){ // x,y 가 각각 입력한 값 칸수로 이동한다.
      this.x=x;
      this.y=y;
   }
   void move() {
      this.x++; // 한칸씩 이동하니까 ++; 후위형
      this.y++;
   }
   void move(int x) {
      this.x+=x; // 입력한 수만큼만 이동하니까 중첩
      this.y+=x;
   }
   void move(int x,int y) {
      this.x+=x;// 각자 x는 x대로 y는 y대로 중첩
      this.y+=y;
   }
   void printInfo() {
      System.out.println("현재위치는 ("+this.x+","+this.y+")입니다.");
   }
}

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

      Point p1 = new Point();
      Point p2 = new Point(10);
      Point p3 = new Point(1,2);

      p1.move();
      p1.printInfo();
      
      p2.move(-5);
      p2.printInfo();
      
      p3.move(3, 4);
      p3.printInfo();    
   }
}
 

[ 과제 2 ]

"학생" 만들어줘

class Student

String name

int[] score

double avg

char grade

Student student=new Student("철수");

철수는 시험을 2번 봅니다. // 시험은 기본값으로 2번 본다

Student student=new Student("영희",4);

영희는 시험을 4번 봅니다.

시험점수는 0~100 랜덤으로 저장

 

student.printInfo(); // 값을 아래처럼 출력 하도록 !

철수

1번 시험 100점

2번 시험 50점

평균 75.0점 [B]

80.0점이상 A

60.0점이상 B

나머지 C

영희

1번 시험 100점

2번 시험 50점

3번 시험 1점

4번 시험 21점

평균 43.0점 [C]

 

student.test(); // 재시험을 보는 메서드

철수가 재시험을 봅니다.

score[]배열에 저장된 시험점수(0~100)값을 랜덤으로 다시 저장합니다.

 

package class01;

import java.util.Random;

class Student {
   String name;
   int[] score;
   double avg;
   char grade;
  
   Student(String name){
      this(name,2);
   }
   Student(String name,int cnt){ // cnt = 시험 횟수
      this.name=name;
      this.score=new int[cnt];
//      this.makeScore(); // 한줄로 밑에 내용을 정리 할수 있다.
      Random rand=new Random();
      int sum=0;
      for(int i=0;i<this.score.length;i++) {
         this.score[i]=rand.nextInt(101);
         sum+=this.score[i]; // 총합
      }
      this.avg=sum*1.0/this.score.length; // 평균
      this.grade='C'; // 기본값을 C 라고 설정
      if(this.avg>=80.0) {
         this.grade='A';
      }
      else if(this.avg>=60.0) {
         this.grade='B';
      }
      
      System.out.println(this.name+"는 시험을 "+this.score.length+"번 봅니다.");
   }

   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("평균 "+this.avg+"점 ["+this.grade+"]");
   }
   
   void test() {
//      this.makeScore(); // 이렇게 한줄로 바꿀수도 있다.
	   Random rand=new Random();
	      int sum=0;
	      for(int i=0;i<this.score.length;i++) {
	         this.score[i]=rand.nextInt(101);
	         sum+=this.score[i];
	      }
	      this.avg=sum*1.0/this.score.length;
	      this.grade='C';
	      if(this.avg>=80.0) {
	         this.grade='A';
	      }
	      else if(this.avg>=60.0) {
	         this.grade='B';
	      }
      System.out.println(this.name+"가 재시험을 봅니다.");
   }

//   void makeScore() { // 이렇게 따로 만들어 놔서 부르는게 가능
//      Random rand=new Random();
//      int sum=0;
//      for(int i=0;i<this.score.length;i++) {
//         this.score[i]=rand.nextInt(101);
//         sum+=this.score[i];
//      }
//      this.avg=sum*1.0/this.score.length;
//      this.grade='C';
//      if(this.avg>=80.0) {
//         this.grade='A';
//      }
//      else if(this.avg>=60.0) {
//         this.grade='B';
//      }
//   }

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

      
      Student s1=new Student("철수");
      Student s2=new Student("영희",4);
      
      s1.printInfo();
      s2.printInfo();
      s2.test();
      s2.printInfo();  
   }
}
 

 

 

728x90
반응형

'JAVA > [실습문제]' 카테고리의 다른 글

[실습 문제] 상속 (포켓몬)  (0) 2024.04.08
[자판기 프로그램]  (0) 2024.04.08
포켓몬 문제 ( class 만들기 )  (0) 2024.04.08
[실습] 자동차 class를 코딩하기  (0) 2024.04.08
3,6,9 게임  (0) 2024.04.08