Java/chapter2

ch02 Student 메서드

낭구리 2021. 8. 23. 17:54
package ch02;

import java.util.Random;

public class Student {

	//객체의 속성은 멤버 변수로, 객체의 기능은 메서드로 구현한다.
	int studentId;
	String studentName;
	String address;
	
	//메서드 정의 
	public void study() {
		System.out.println(studentName + "학생이 공부를 합니다.");
		
	}
	
	public void breakTime() {
		System.out.println(studentName + "학생이 휴식을 합니다.");
	}
	
	public void showInfo() {
		System.out.println(studentId + " , " + studentName + ", " + address);
	}
	
	public static void main(String[] args) {
		Student studentKim = new Student();
		studentKim.studentName = "김길동";
		studentKim.studentId = 1;
		studentKim.address = "부산시 해운대구";
		//메서드 호출
		studentKim.study();
		studentKim.breakTime();
		studentKim.showInfo();
		System.out.println("======");
		
		Student studentLee = new Student();
		studentLee.studentName = "이순신";
		studentLee.studentId = 2;
		studentLee.address = "부산시 남포동";
		
		studentLee.study();
		studentLee.breakTime();
		studentLee.showInfo();
		
		
	    System.out.println("----------");
		System.out.println(randomNumber());
		System.out.println(randomNumber());
		System.out.println(randomNumber());
		System.out.println(randomNumber());
		System.out.println(randomNumber());
		System.out.println(randomNumber());
	    
	}//main
	
	//함수 만들기 독립적인기능이라 함수라 불린다
	public static int randomNumber() {
		int value;
		Random random = new Random();
		value = random.nextInt(45) + 1; //0~44까지 랜덤숫자를 반환 --> 1~45숫자중에 하나를 반환
		return value;
		
		
		
		
		
		
	}
	
	
	
	
	
}

'Java > chapter2' 카테고리의 다른 글

ch03 Bus  (0) 2021.08.25
ch02 FormainTest1  (0) 2021.08.25
ch01 FunctionTest  (0) 2021.08.23
ch01 Warrior  (0) 2021.08.23
ch01 Order  (0) 2021.08.23