java50 메서드(Method) - 메서드 : 클래스 내의 함수 ex) 믹서기 과일(입력) > 믹서기(메서드) > 과일 주스(출력, 리턴값) - 메서드 사용 이유 반복적으로 행해야하는 일을 다시 사용할 수 있도록 하기 위함이다. int sum(int a, int b) { return a+b; } 위의 메서드는 int형 a와 b를 입력받아 a+b한 값을 반환한다. 이때 return은 메서드 결과 값을 돌려주는 명령어이다. public class Sample { int sum(int a, int b) { return a + b; } public static void main(String[] args) { int a = 3; int b = 4; Sample sample = new Sample(); int c = sample.sum(a, b).. 2023. 3. 26. 클래스 - 객체 class Animal { } public class Sample { public static void main(String[] args) { Animal cat = new Animal(); } } Animal 클래스는 가장 간단한 형태의 클래스이다. 클래스의 선언만 있고 내용이 없는 껍데기뿐인 클래스이다. 클래스는 객체를 만드는 기능이 있다. Animal cat = new Animal() 으로 Animal 클래스 객체를 생성한다. 이때 new는 객체를 생성할 때 사용하는 키워드이다. 클래스에 의해서 만들어진 객체를 인스턴스라고도 한다. cat이라는 객체는 Animal의 인스턴스이다. 인스턴스는 특정 객체가 어떤 클래스의 객체인지를 관계 위주로 설명할 때 사용된다. 즉, cat은 인스턴스보다는 .. 2023. 2. 18. 객체 지향 프로그래밍 Sample 클래스에서 2대의 계산기가 필요한 상황일 때, 객체를 사용하지 않으면 똑같은 계산기 클래스 2개를 만들어 사용해야 한다. class Calculator1 { static int result = 0; static int add(int num) { result += num; return result; } } class Calculator2 { static int result = 0; static int add(int num) { result += num; return result; } } public class Sample { public static void main(String[] args) { System.out.println(Calculator1.add(3)); System.out.pri.. 2023. 2. 16. 04장 연습 문제 *연습문제는 다음 사이트에 있다. https://wikidocs.net/157996 04장 연습문제 *(연습문제 풀이 : [https://wikidocs.net/181935](https://wikidocs.net/181935))* [TOC] ## Q1. 조건문의 참과 거짓 … wikidocs.net public class Main { public static void main(String[] args) { System.out.println("Q1. 조건문의 참과 거짓 : everywhere"); System.out.println("Q2. 3의 배수의 합 : " + q2()); System.out.println("Q3. 별 표시하기 : "); q3(); System.out.println("Q4. 1부터 1.. 2023. 2. 15. for 문 - for 문 문장을 반복해서 수행해야 할 경우에 사용한다. - for 문 구조 String[] numbers = {"one", "two", "three"}; for(int i=0; i 2023. 2. 14. while문 - while 문장을 반복해서 수행할 경우 사용한다. - while 기본 구조 while (조건문) { ; ; ; ... } 조건문이 참일 때 while문 안의 문장을 반복 수행한다. int treeHit = 0; while (treeHit < 10) { treeHit++; System.out.println("나무를 " + treeHit + "번 찍었습니다."); if (treeHit == 10) { System.out.println("나무 넘어갑니다."); } } treeHit이 1에서 9까지 증가할 때는 "나무를 {treeHit}번 찍었습니다."가 출력되며, treeHit이 10이 될 경우 "나무 넘어갑니다."가 출력된다. 그 후 조건문이 treeHit=10 < 10이 참이 아니므로 while문을 종.. 2023. 2. 13. 이전 1 2 3 4 ··· 9 다음