π¨ μ§μ μμ± β Practice 2 "Develop Code"
FRQμμ "Write the method" / "Write the class"κ° λμ€λ©΄ μλ 17κ° ν¨ν΄ μ€ νλ μ΄μμ μ‘°ν©μ΄λ€. λ©μλ ν€λ(public/static/return type/λ§€κ°λ³μ) λλ½μ FRQμμ κ±°μ μλ -1μ μ΄λ€.
| μ κ·Ό μ μ΄μ | public class κ³ μ (FRQ μ μ ). νλλ μΌλ°μ μΌλ‘ private |
| μμ±μ μ΄λ¦ | ν΄λμ€λͺ κ³Ό μ νν μΌμΉ. return type μμ |
| this ν€μλ | λ§€κ°λ³μμ νλλͺ
μ΄ κ°μΌλ©΄ this.field = field; |
| νμΌλͺ | ClassName.java β μνμμλ μ±μ μ λμ§λ§ IDE/μ€μ΅μμ μΌμΉ νμ |
public class Student {
private String name;
private int grade;
// constructor β same name as class, no return type
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
}
public class+ ν΄λμ€λͺ μΌμΉ- μμ±μλ return type μμ (
voidλ μ μ) - λ§€κ°λ³μ == νλλͺ
μΌ λ
this.νμ
void λΆμ΄λ μ€μ β μ΄κ±΄ μΌλ° λ©μλκ° λ¨ (μμ±μ X). this λλ½ μ λ§€κ°λ³μκ° μκΈ° μμ μκ² λμ
λΌ νλ μ΄κΈ°ν μ€ν¨.Worked Example: λ€μ€ μμ±μ (no-arg + parameterized)
public class Book {
private String title;
private int pages;
// no-arg constructor
public Book() {
this.title = "Untitled";
this.pages = 0;
}
// parameterized constructor
public Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
}
FRQμμ "Write a constructor that..."μ΄ λμ€λ©΄ λ§€κ°λ³μ μ΄λ¦ = λͺ μΈ κ·Έλλ‘ μ¬μ©. λ°νν μ λ μ°μ§ μμ.
| ꡬ쑰 | [modifier] [static?] returnType name(paramType param, ...) |
| void λ©μλ | public void doSomething(int x) { ... } β return λ¬Έ μ ν |
| λ°ν λ©μλ | public int compute(int x, int y) { return x + y; } β λͺ¨λ κ²½λ‘μμ return νμ |
| static λ©μλ | public static int max(int a, int b) β κ°μ²΄ μμ΄ ClassName.max(...) νΈμΆ |
// instance method β needs object
public boolean isPassing() {
return grade >= 60;
}
// static method β no object needed
public static int max(int a, int b) {
if (a > b) return a;
return b;
}
publicν€μλ λΉ λ¨λ¦¬λ©΄ μ»΄νμΌ X (FRQ μλ κ°μ )- return typeκ³Ό μ€μ λ°ν νμ μΌμΉ (cast νμμ λͺ μ)
- voidκ° μλλ©΄ λͺ¨λ λΆκΈ°μμ
return
booleanλ true/false return λͺ
μ νμ.| ꡬ쑰 | for (init; condition; update) { body } |
| νμ€ νν | for (int i = 0; i < n; i++) { ... } |
| μλ°©ν₯ | for (int i = n - 1; i >= 0; i--) { ... } |
// sum 1 to n (inclusive)
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
<vs<=μ νν (off-by-one κ²μ¦)- μΈλ±μ€ λ³μ μ μΈ μμΉ (
int i) β forλ¬Έ μμμ μ μΈ κΆμ₯ - update λλ½ μ 무ν 루ν
i <= arr.length β AIOOBE. νμ i < arr.length. i++ vs ++iλ λ¨λ
μ¬μ© μ μ°¨μ΄ μμ (μ‘°μ¬: μ μμμλ λ€λ¦).| ꡬ쑰 | while (condition) { body; update; } |
| μ¬μ© μμ | λ°λ³΅ νμ λ―Έμ (μ¬μ©μ μ λ ₯, κ²μ μ’ λ£ λ±) |
| update | 루ν μμμ λ°λμ 쑰건μ λ³νμμΌμΌ ν¨ |
// halve until below 1
double x = 100.0;
int count = 0;
while (x >= 1.0) {
x = x / 2;
count++;
}
- μ‘°κ±΄μ΄ falseκ° λλ μμ λͺ νν
- update statement λλ½ = 무ν 루ν
- μ΄κΈ°ν λ³μλ while λ°κΉ₯μ μ μΈ
| ꡬ쑰 | for (Type x : collection) { ... } |
| λ°°μ΄ | for (int x : arr) { sum += x; } |
| ArrayList | for (String s : list) { ... } |
| μ μ½ | μλ³Έ 컬λ μ μμ λΆκ° (κ° λ³΅μ¬). μΈλ±μ€ μ¬μ© λΆκ° |
// sum array values β read-only iteration
int total = 0;
for (int n : nums) {
total += n;
}
- 컬λ μ μ element νμ κ³Ό λ³μ νμ μΌμΉ
- μμ μμ / μμ κ° νμνλ©΄ νμ€ for μ¬μ©
- μΈλ±μ€κ° νμνλ©΄ νμ€ for μ¬μ©
x = 0; ν΄λ μλ³Έ λ°°μ΄μ λ³νμ§ μμ. ArrayListμ .remove()λ₯Ό for-each μμμ νΈμΆνλ©΄ ConcurrentModificationException.| ꡬ쑰 | if (cond1) { ... } else if (cond2) { ... } else { ... } |
| λΉκ΅ μ°μ°μ | == != < > <= >= |
| λ Όλ¦¬ μ°μ°μ | && (AND) || (OR) ! (NOT) β λ¨μΆνκ° |
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "F";
}
- λμ
=vs λΉκ΅==κ΅¬λΆ - String/Object λΉκ΅λ
.equals()(μ λ==X) - else if μμ β μμμλΆν° νκ°
if (x = 5) μ»΄νμΌ μλ¬ (boolean μλ). if (s == "A") β μ°Έμ‘° λΉκ΅, λ¬Έμμ΄ κ°μλ false κ°λ₯.| μ μΈ + ν¬κΈ° | int[] arr = new int[10]; β λͺ¨λ μμ 0μΌλ‘ μ΄κΈ°ν |
| 리ν°λ΄ μ΄κΈ°ν | int[] arr = {1, 2, 3, 4, 5}; |
| μ κ·Ό | arr[i] β iλ 0λΆν° arr.length - 1κΉμ§ |
| κΈΈμ΄ | arr.length (μμ±, κ΄νΈ μμ) |
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = i * i; // 0, 1, 4, 9, 16
}
arr.length(μμ±) β κ΄νΈ X- μΈλ±μ€ λ²μ
0 ~ arr.length - 1 - String λ°°μ΄μ
String[] s = new String[n];β λͺ¨λ null
arr.length() β λ©μλ X, μμ±. i <= arr.length β AIOOBE. String[] newλ‘ λ§λ€λ©΄ nullμ΄λΌ .equals() νΈμΆ μ NPE.| import | import java.util.ArrayList; (FRQμμ μλ κ°μ ) |
| μμ± | ArrayList<Integer> list = new ArrayList<>(); (μ λ€λ¦μ wrapper class νμ) |
| ν΅μ¬ λ©μλ | .add(x), .add(i, x), .get(i), .set(i, x), .remove(i), .size() |
| ν¬κΈ° | .size() β λ©μλ, κ΄νΈ νμ |
ArrayList<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.set(0, 99); // nums = [99, 20]
nums.remove(0); // nums = [20]; indices shift
int n = nums.size();
- μ λ€λ¦μ wrapper (Integer/Double/Boolean) β primitive λΆκ°
.size()λ©μλ (κ΄νΈ νμ) vs λ°°μ΄.length.remove(i)ν μΈλ±μ€κ° μμΌλ‘ μννΈ
list.length (X) β λ°°μ΄ μμ±. ArrayList<int> (X) β primitive μ λ¨, Integerλ‘. 루νμμ .remove() ν μΈλ±μ€ κ·Έλλ‘ λλ©΄ element 건λλ.Worked Example: ArrayListμμ μ§μ λͺ¨λ μ κ±° (μμ ν ν¨ν΄)
// WRONG β for-each + remove β ConcurrentModificationException
// for (int x : list) if (x % 2 == 0) list.remove(...);
// RIGHT β backwards index loop
ArrayList<Integer> list = ...;
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) % 2 == 0) {
list.remove(i);
}
}
μ λ€μμλΆν°? μμμλΆν° removeνλ©΄ μΈλ±μ€κ° μννΈν΄μ λ€μ elementλ₯Ό 건λλ°κ² λλ€. λ€μμλΆν°λ μννΈκ° μν₯μ μ μ€λ€.
| μ μΈ | int[][] grid = new int[r][c]; |
| 리ν°λ΄ | int[][] g = {{1,2},{3,4}}; |
| ν/μ΄ μ | grid.length = ν, grid[0].length = μ΄ |
| μ κ·Ό | grid[row][col] β row λ¨Όμ , col λ€μ |
// row-major traversal β sum all elements
int[][] grid = new int[3][4];
int sum = 0;
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[0].length; c++) {
sum += grid[r][c];
}
}
grid.length= ν μ /grid[0].length= μ΄ μ- row-major μν: λ°κΉ₯ = ν, μμͺ½ = μ΄
- 2D for-each:
for (int[] row : grid) for (int x : row)
grid[c][r] β (X). μν μ grid.length(ν)μ grid[0].length(μ΄) μμΉ κ±°κΎΈλ‘.| ꡬ쑰 | β Base case (μ’ λ£ μ‘°κ±΄) β‘ Recursive case (μκΈ° νΈμΆ, base caseλ‘ μλ ΄) |
| μ ν | factorial, sum, gcd, binary search, mergesort |
| μ£Όμ | base caseκ° λ¨Όμ μμΌ β κ·ΈλμΌ λ¬΄ν νΈμΆ μ°¨λ¨ |
public static int factorial(int n) {
if (n <= 1) return 1; // base case
return n * factorial(n - 1); // recursive case β converges to n=1
}
- Base case λͺ
μ (
n <= 1λ±) - μ¬κ· νΈμΆμ΄ base caseλ‘ μλ ΄ (
n - 1,n / 2λ±) - return κ° μ ν (factorialμ 1, sumμ 0)
factorial(n + 1) β λ°μ° (μλ ΄ μ ν¨). μμ μ
λ ₯ μ²λ¦¬ λ―ΈλΉ μ 무ν μ¬κ·.| extends | public class Sub extends Base { ... } |
| super μμ±μ | μλΈν΄λμ€ μμ±μ 첫 μ€: super(args); |
| super λ©μλ | super.methodName(args) β λΆλͺ¨ λ©μλ νΈμΆ |
| override | κ°μ μκ·Έλμ²λ‘ μ¬μ μ (@Override annotation κΆμ₯) |
public class Vehicle {
private int wheels;
public Vehicle(int wheels) { this.wheels = wheels; }
}
public class Car extends Vehicle {
private String model;
public Car(String model) {
super(4); // must be FIRST line
this.model = model;
}
}
super(...)λ μμ±μ 첫 μ€μλ§ κ°λ₯- λΆλͺ¨μ no-arg constructor μμΌλ©΄ μμμμ
super(...)λͺ μ νμ - private νλλ μ§μ μ κ·Ό λΆκ° β getter μ¬μ©
super(...)λ₯Ό μμ±μ λ λ²μ§Έ μ€ μ΄νμ νΈμΆ β μ»΄νμΌ μλ¬. super.toString() κ°μ νΈμΆμ μμΉ μμ .| ꡬ쑰 | public class X implements MyInterface { ... } |
| μ무 | interfaceμ λͺ¨λ λ©μλ ꡬν (abstract λ©μλ) |
| Comparable | public int compareTo(T other) β μμ/0/μμ λ°ν |
public class Score implements Comparable<Score> {
private int value;
public Score(int value) { this.value = value; }
public int compareTo(Score other) {
return this.value - other.value; // neg / 0 / pos
}
}
- interfaceμ λͺ¨λ λ©μλ ꡬν (μ νλ©΄ μ»΄νμΌ X)
compareToλ°νμ μμ(less) / 0(equal) / μμ(greater)- μ λ€λ¦
Comparable<Score>λͺ μ
compareToμμ true/false return β (X) int λ°ν. this.value > other.value β (X) 그건 booleanμ΄λΌ μ»΄νμΌ μλ¨.| upcast | μλ: Animal a = new Dog(); |
| downcast | λͺ
μ: Dog d = (Dog) a; β μΊμ€νΈ μ€ν¨ μ ClassCastException |
| μμ μ²΄ν¬ | if (a instanceof Dog) { Dog d = (Dog) a; ... } |
Object obj = "hello";
if (obj instanceof String) {
String s = (String) obj; // safe downcast
System.out.println(s.length());
}
- downcast μ
instanceofλ‘ μμ μ²΄ν¬ - νΈμΆ λ©μλλ μ€μ κ°μ²΄ νμ μ override λ²μ (dynamic dispatch)
- μ κ·Ό κ°λ₯ν λ©€λ²λ μ°Έμ‘° νμ μ λ°λ¦ (μ μ )
Animal a = new Cat(); Dog d = (Dog) a; λ°νμμ νλ°..length() | λ¬Έμ κ°μ β λ©μλ, κ΄νΈ νμ |
.substring(i, j) | μΈλ±μ€ [i, j) β jλ λ―Έν¬ν¨ |
.substring(i) | iλΆν° λκΉμ§ |
.indexOf("x") | μ²μ λ°κ²¬ μμΉ, μμΌλ©΄ -1 |
.equals(t) | λ΄μ© λΉκ΅ (λμλ¬Έμ ꡬλΆ) |
.charAt(i) | iλ²μ§Έ λ¬Έμ β λ°ν νμ
char |
String s = "hello";
int n = s.length(); // 5
String t = s.substring(1, 4); // "ell"
int p = s.indexOf("l"); // 2
char c = s.charAt(0); // 'h'
boolean b = s.equals("hello"); // true
substring(i, j)λ [i, j) λ°-κ°κ΅¬κ° (j λ―Έν¬ν¨)charAtλ°νμchar(String μλ!)- String λΉκ΅λ νμ
.equals()
s.substring(0, s.length()) = μ 체. s.substring(0, s.length() - 1)λ λ§μ§λ§ λ¬Έμ μ μΈ. charAt κ²°κ³Όλ₯Ό StringμΌλ‘ μ°λ©΄ μ»΄νμΌ μλ¬.Math.abs(x) | μ λκ° β int λλ double λ°ν (μ λ ₯ νμ λ°λΌ) |
Math.pow(b, e) | b^e β double λ°ν (int νμμ cast) |
Math.sqrt(x) | μ κ³±κ·Ό β double λ°ν |
Math.random() | 0.0 β€ r < 1.0 β 1.0μ λ―Έν¬ν¨ |
int a = Math.abs(-5); // 5
double p = Math.pow(2, 10); // 1024.0
int q = (int) Math.pow(2, 10); // 1024 β cast required
double r = Math.sqrt(16); // 4.0
// random integer in [low, high]
int rand = (int)(Math.random() * (high - low + 1)) + low;
Math.powλ°ν = double (intμ λμ νλ €λ©΄ cast)Math.random()μ< 1.0(1.0 λ―Έν¬ν¨)- μ μ λ²μ [low, high] λλ€ κ³΅μ μκΈ°
int x = Math.pow(2, 3); μ»΄νμΌ μλ¬ (double β int μλ X). Math.random() * 10λ 0~9.99... β castνλ©΄ 0~9 (10 λ―Έν¬ν¨).| NullPointerException (NPE) | null.method() λλ null.field μ κ·Ό |
| ArrayIndexOutOfBoundsException | μΈλ±μ€ < 0 λλ β₯ arr.length |
| IndexOutOfBoundsException | ArrayListμ μλͺ»λ μΈλ±μ€ (list.get(size()) λ±) |
| ClassCastException | μλͺ»λ downcast (μ€μ νμ κ³Ό cast νμ λΆμΌμΉ) |
| ArithmeticException | μ μ 0 λλκΈ° (double / 0.0 = Infinity, μμΈ X) |
// NPE example β uninitialized String[]
String[] words = new String[3];
words[0].length(); // NullPointerException β words[0] is null
// AIOOBE example
int[] arr = {1, 2, 3};
int x = arr[3]; // ArrayIndexOutOfBoundsException
// ArithmeticException β int only
int y = 5 / 0; // ArithmeticException
- κ° μμΈμ λ°μ 쑰건 ν μ€ μκΈ° (MCQ λΉμΆ)
- FRQμμ try/catchλ μΆμ μ λ¨ β λ°μ μ‘°κ±΄λ§ μλ©΄ λ¨
- NPE λ°©μ§: κ°μ²΄ μ¬μ© μ null 체ν¬
5.0 / 0 = Infinity (μμΈ X). 5 / 0 = ArithmeticException. for-each μμμ list.remove β ConcurrentModificationException.== vs .equals() β MCQ 1μμ| primitive (int, double, boolean, char) | == μ¬μ© β κ° λΉκ΅ |
| κ°μ²΄ (String, Integer, μ¬μ©μ ν΄λμ€) | .equals() μ¬μ© β λ΄μ© λΉκ΅ |
== on κ°μ²΄ | μ°Έμ‘°(μ£Όμ) λΉκ΅ β κ°μ λ©λͺ¨λ¦¬λ©΄ true |
| null λΉκ΅ | obj == nullμ μμ (NPE μ λ¨) |
String a = new String("hi");
String b = new String("hi");
boolean x = (a == b); // false β different memory
boolean y = a.equals(b); // true β same content
// primitive β == is correct
int p = 5, q = 5;
boolean z = (p == q); // true
- primitive:
==/ κ°μ²΄:.equals() - String literalλΌλ¦¬
==λ μ’ μ’ true (string pool) β μ΄μ μμ‘΄νμ§ λ§ κ² - μ¬μ©μ μ μ ν΄λμ€μμ
.equals()override μ νλ©΄ Objectμ == λμ
if (s == "hello") β κ°λ true, κ°λ false (λλ²κΉ
μ§μ₯). νμ .equals() μ¬μ©. Integerλ Integer i = 200; Integer j = 200; i == j;λ false (cache λ²μ -128~127 λ°).