JAVA 복습하기

서화진·2022년 8월 17일
0

JAVA 학습일지

목록 보기
3/13

1) function

package ch03;

public class FunctionTest {

// 함수 구현하기

public static int addNum (int num1, int num2 ) {
		int result;
		result = num1 + num2;
		return result;
}

public static void sayHello(String greeting) {
		System.out.println(greeting);
}

public static int calcSum() {
	
		int sum = 0;
		int i;
		for(i = 0; i <= 100; i++) {
				sum += i;
		}
		return sum;
}

public static void main(String[] arge) {
	
	int n1 = 10;
	int n2 = 20;
	
	int total = addNum(n1, n2);
	
	sayHello("안녕하세요");
	int num = calcSum();
	
	System.out.println(total);
	System.out.println(num);
}

2) 멤버변수로 선언하고 메서드 구현

package ch04;

public class Student {

public int studentId;
public String studentName;
public String address;

public void showStudentInfo() {
	System.out.println( studentId + "학번 학생의 이름은 " + studentName + " 이고, 주소는 " + address + " 입니다.");
}

public String getStudentName() {
	return studentName;
}

public void setStudentName(String name) {
	studentName = name;
}
package ch04;

public class StudentTest {

public static void main(String[] args) {

	Student  studentLee = new Student();
	
	studentLee.studentId = 12345;
	studentLee.setStudentName("Lee");
	studentLee.address = "서울 강남구";
	
	studentLee.showStudentInfo();
	
	Student studentKim = new Student();
	studentKim.studentId = 54321;
	studentKim.studentName = "Kim";
	studentKim.address = "경기도 성남시";
	
	studentKim.showStudentInfo();
}

원래도 이해가 잘 되진 않았지만, 이제는 머리가 멍해지고 있다..^^..

화이팅.............

profile
초보 개발자 / 학습일지 : https://velog.io/@dinyyyyy

0개의 댓글