티스토리 뷰

수업/└Java

[실습문제4]클래스

onlyun 2022. 1. 28. 12:46

실습문제 1.

배열 입력하는데 중복값은 입력받지 않는다.

 

실슥문제 2.

선택 정렬, 버블 정렬, 삽입 정렬 등을 사용.

 

 

20220128

실습문제 3. 출력문 작성

-Student

package task4;
public class Student {
	private int sno;
	private String name;
	private String major;
//	private int[] scores;
	private int[] scores = new int[3]; //배열로 받을 때
	private int total;
	private double avg;
//	private int rank;
	private int rank = 1; //1로 세팅
	
	public Student(){}
	
	public Student(int sno, String name, String major, int[] scores){
		this.sno = sno;
		this.name = name;
		this.major = major;
		this.scores = scores;
	}
	
	public void scoresPro() { //c총점
		//성적 합/3 배열 scores의 합
		int total = 0;
		for(int i=0; i<scores.length; i++) {
			total += scores[i];
		}
		this.total = total;
		this.avg = (double)total/scores.length;
	}

	// rank
	public void rankPro(Student[] arr) {
		for(int i=0; i<arr.length; i++) {
			if(arr[i].avg>this.avg) //내 평균 > 평균 이면 랭크++
				this.rank++;
		}
	}
	
	//출력문
	public void display() {
//	System.out.println(sno+" "+name+" "+major+" "+scores+" "+/*총점*/" "+avg+" "+rank); //i
	System.out.print(sno+" "+name+" "+major+" ");
	for(int i=0; i<scores.length; i++) {//성적 출력하는 문장
		System.out.print(scores[i]+" ");
	}
	System.out.println(total+" "+avg+" "+rank);
}
}

-StudentTest

package task4;
import java.util.Scanner;
public class StudentTest {

	public static void main(String[] args) {
		
		//키보드 입력받아서 해보기
		Scanner sc = new Scanner(System.in);
		
		Student[] students = new Student[5];
		for(int i=0; i<students.length; i++) {
			System.out.print("학번 입력 >>");
			int sno = sc.nextInt();
			System.out.print("name 입력 >>");
			String name = sc.next();
			System.out.print("major 입력 >>");
			String major = sc.next();
			System.out.print("성적 3개 입력 >>");
			int[] scores = new int[3];
			scores[0] = sc.nextInt();
			scores[1] = sc.nextInt();
			scores[2] = sc.nextInt();
			students[i] = new Student(sno, name, major, scores);
			students[i].scoresPro(); //평균값 구하는 메서드
		}
		for(int i=0; i<students.length; i++) {
			students[i].rankPro(students); // 석차 구하기
			students[i].display();
		}
	}
}

 

end

 

- file -

실습문제4_클래스.pdf
0.06MB

 

'수업 > └Java' 카테고리의 다른 글

[CH02]변수와 자료형  (0) 2022.01.30
[CH00]Java 기초  (0) 2022.01.29
[Java]상속  (0) 2022.01.28
[Java]클래스와 생성자 복습_01.26  (0) 2022.01.26
[Java]반복문과 제어문 : for, if, continue, break  (0) 2022.01.25
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/07   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함