Fields
- instance variables
- class variables
- local variables
Method(function)
내부 클래스
- bytecode파일(.class)을 토대로 new 키워드와 생성자를 이용한 선언을 통해 메모리할당을 해 메모리 영역에 생성된(올라간) 것
StarPrint starPrint = new StarPrint(); starPrint.printTriangle(3); System.out.println(); starPrint.printReverseTriangle(3); =============================== * ** *** *** ** *
public class ClassExample { public static void main(String[] args) { StarPrint starPrint = new StarPrint(); starPrint.printTriangle(3); System.out.println(); starPrint.printReverseTriangle(3); } } class StarPrint{ void printTriangle(int n) { for(int i =0;i<n;i++) { for(int j =0;j<=i;j++) System.out.print("*"); System.out.println(); } } void printReverseTriangle(int n ) { for(int i =0;i<n;i++) { for(int j =i;j<n;j++) System.out.print("*"); System.out.println(); } } }
* ** *** *** ** *
Gugudan gugudan = new Gugudan(); gugudan.printGugu(10); //1단부터 10단까지 출력 gugudan.printGugu(20); //1단부터 20단까지 출력
public class ClassExample { public static void main(String[] args) { Gugudan gugudan = new Gugudan(); gugudan.printGugu(10); gugudan.printGugu(20); } } class Gugudan{ void printGugu(int n) { for(int i =2;i<=n;i++) { for(int j =1;j<10;j++) { System.out.println(i + " x "+ j + " = " + (i*j)); } System.out.println(); } } }
생략 ... 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 2 x 1 = 2 2 x 2 = 4 ... 중략 19 x 7 = 133 19 x 8 = 152 19 x 9 = 171 20 x 1 = 20 20 x 2 = 40 20 x 3 = 60 20 x 4 = 80 20 x 5 = 100 20 x 6 = 120 20 x 7 = 140 20 x 8 = 160 20 x 9 = 180 ... 생략
- 클래스 Grade
- 데이타멤버 : 국어, 영어,수학,총점,평균
- 함수 : setGrade(......) //국어영어수학 세팅
- getAvg() // 평균
- getGrade() //평균에 따른 수우미양가
public class ClassExample { public static void main(String[] args) { Grade grade = new Grade(); grade.setGrade(80, 55, 100); double avg = grade.getAvg(); char result =grade.getGrade(); System.out.println(avg); System.out.println(result); grade.setGrade(85, 80, 80); avg=grade.getAvg(); result = grade.getGrade(); System.out.println(avg); System.out.println(result); grade.setGrade(70, 85, 40); avg = grade.getAvg(); result = grade.getGrade(); System.out.println(avg); System.out.println(result); grade.setGrade(70, 85, 100); avg = grade.getAvg(); result = grade.getGrade(); System.out.println(avg); System.out.println(result); grade.setGrade(40, 85, 40); avg = grade.getAvg(); result = grade.getGrade(); System.out.println(avg); System.out.println(result); } } class Grade{ int kor; int eng; int math; void setGrade(int kor, int eng, int math) { this.kor = kor; this.eng = eng; this.math = math; } double getAvg() { return (kor + eng + math) / 3.0; } char getGrade() { double avg = getAvg(); char grade ='가'; if(avg >= 90.0) { grade= '수'; } else if(avg >= 80.0 ) { grade= '우'; } else if(avg >= 70.0 ) { grade= '미'; } else if(avg >= 60.0 ) { grade = '양'; } else { grade= '가'; } return grade; } }
78.33333333333333 미 81.66666666666667 우 65.0 양 85.0 우 55.0 가
GetSum getsum = new GetSum(); // 1)객체 생성 int num; // 2)num 변수 선언 num = 100; getsum.setNum(num); // 3)getsum 객체의 setNum함수 호출, num값 50 저장 num = getsum.sum(); // 4)getsum객체의 sum함수 호출, 1-50까지 합 구함 System.out.println(num); getsum.setNum(1000); num = getsum.sum(); // 4)getsum객체의 sum함수 호출, 1-50까지 합 구함 System.out.println(num);
public class ClassExample { public static void main(String[] args) { GetSum getSum = new GetSum(); int num; num = 100; getSum.setNum(num); num = getSum.sum(); System.out.println(num); } } class GetSum{ int num; public void setNum(int num) { this.num = num; } public int sum() { int sum =0; for(int i =1; i<=num; i++) { sum +=i; } return sum; } }
5050