1. 변수와 자료형
2. 조건문
3. 반복문
4. 배열
5. 메서드
6. 클래스와 객체 지향 프로그래밍(OOP)
7. 상속
8. 인터페이스
9. 예외 처리
10. 컬렉션
1. 변수와 자료형
문제: 두 정수 값을 입력받아, 두 값의 합과 곱을 출력하는 프로그램을 작성하세요.
import java.util.Scanner;
public class VariableExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("첫 번째 정수를 입력하세요: ");
int num1 = scanner.nextInt();
System.out.println("두 번째 정수를 입력하세요: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
int product = num1 * num2;
System.out.println("합: " + sum);
System.out.println("곱: " + product);
}
}
풀이:
• Scanner를 이용해 사용자로부터 두 개의 정수를 입력받습니다.
• 입력받은 두 값을 더하고 곱한 값을 각각 변수에 저장한 후 출력합니다.
2. 조건문
문제: 사용자로부터 나이를 입력받아, 성인(19세 이상)인지 미성년자인지 판별하는 프로그램을 작성하세요.
import java.util.Scanner;
public class ConditionExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("나이를 입력하세요: ");
int age = scanner.nextInt();
if (age >= 19) {
System.out.println("성인입니다.");
} else {
System.out.println("미성년자입니다.");
}
}
}
풀이:
• 조건문 if를 사용해 사용자의 나이가 19세 이상인지 확인하고, 맞으면 “성인”을 출력하고 그렇지 않으면 “미성년자”를 출력합니다.
3. 반복문
문제: 1부터 10까지의 합을 출력하는 프로그램을 작성하세요.
public class LoopExample {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("1부터 10까지의 합: " + sum);
}
}
풀이:
• for 반복문을 사용하여 1부터 10까지의 숫자를 더합니다.
• 매 반복마다 sum에 현재 값을 더하고, 반복이 끝난 후 결과를 출력합니다.
4. 배열
문제: 5개의 정수를 저장할 배열을 만들고, 각 배열 요소의 합을 계산하는 프로그램을 작성하세요.
import java.util.Scanner;
public class ArrayExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = new int[5];
int sum = 0;
System.out.println("5개의 정수를 입력하세요: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
sum += numbers[i];
}
System.out.println("배열 요소의 합: " + sum);
}
}
풀이:
• 크기가 5인 배열을 선언하고, for 반복문을 사용하여 사용자가 입력한 값들을 배열에 저장하면서 합을 계산합니다.
5. 메서드
문제: 두 수를 더하는 메서드를 작성하고, 이를 이용하여 두 정수의 합을 출력하는 프로그램을 작성하세요.
import java.util.Scanner;
public class MethodExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("첫 번째 정수를 입력하세요: ");
int num1 = scanner.nextInt();
System.out.println("두 번째 정수를 입력하세요: ");
int num2 = scanner.nextInt();
int result = add(num1, num2);
System.out.println("두 수의 합: " + result);
}
public static int add(int a, int b) {
return a + b;
}
}
풀이:
• add라는 메서드를 정의하여 두 정수를 더한 결과를 반환합니다.
• main 메서드에서 add 메서드를 호출하여 두 수의 합을 계산하고 출력합니다.
6. 클래스와 객체 지향 프로그래밍(OOP)
문제: 간단한 학생 클래스를 만들어 학생의 이름과 점수를 저장하고, 해당 학생의 정보를 출력하는 프로그램을 작성하세요.
class Student {
String name;
int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public void printInfo() {
System.out.println("학생 이름: " + name);
System.out.println("점수: " + score);
}
}
public class ClassExample {
public static void main(String[] args) {
Student student = new Student("홍길동", 85);
student.printInfo();
}
}
풀이:
• Student 클래스를 만들어 이름과 점수를 저장하는 생성자와, 정보를 출력하는 메서드를 정의합니다.
• main 메서드에서 학생 객체를 생성하고, 해당 학생의 정보를 출력합니다.
7. 상속
문제: 사람(Person) 클래스를 상속받아 학생(Student) 클래스를 만들고, 해당 학생의 정보를 출력하는 프로그램을 작성하세요.
class Person {
String name;
public Person(String name) {
this.name = name;
}
public void printName() {
System.out.println("이름: " + name);
}
}
class Student extends Person {
int score;
public Student(String name, int score) {
super(name);
this.score = score;
}
public void printInfo() {
printName();
System.out.println("점수: " + score);
}
}
public class InheritanceExample {
public static void main(String[] args) {
Student student = new Student("홍길동", 90);
student.printInfo();
}
}
풀이:
• Person 클래스를 상속받아 Student 클래스를 정의하고, super()로 부모 클래스 생성자를 호출합니다.
• printInfo 메서드를 통해 이름과 점수를 출력합니다.
8. 인터페이스
문제: Flyable 인터페이스를 구현한 Bird 클래스를 작성하세요.
interface Flyable {
void fly();
}
class Bird implements Flyable {
public void fly() {
System.out.println("새가 날아갑니다.");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Bird bird = new Bird();
bird.fly();
}
}
풀이:
• Flyable 인터페이스를 정의하고, Bird 클래스에서 이를 구현하여 fly 메서드를 정의합니다.
9. 예외 처리
문제: 사용자로부터 숫자를 입력받아 나눗셈을 수행하는 프로그램을 작성하고, 0으로 나눌 때 예외 처리를 하세요.
import java.util.Scanner;
public class ExceptionExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("첫 번째 숫자를 입력하세요: ");
int num1 = scanner.nextInt();
System.out.println("두 번째 숫자를 입력하세요: ");
int num2 = scanner.nextInt();
int result = num1 / num2;
System.out.println("나눗셈 결과: " + result);
} catch (ArithmeticException e) {
System.out.println("0으로 나눌 수 없습니다.");
}
}
}
풀이:
• try-catch 블록을 사용하여 0으로 나눌 경우 발생하는 ArithmeticException을 처리합니다.
10. 컬렉션
문제: ArrayList를 사용하여 정수를 저장하고, 모든 요소를 출력하는 프로그램을 작성하세요.
import java.util.ArrayList;
import java.util.Scanner;
public class CollectionExample {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.println("정수 5개를 입력하세요: ");
for (int i = 0; i < 5; i++) {
list.add(scanner.nextInt());
}
System.out.println("입력된 정수: " + list);
}
}
풀이:
• ArrayList에 정수를 추가하고, 리스트의 모든 요소를 출력합니다.