Object 클래스 Object 클래스는 모든 클래스들의 조상임 상속받지 않는 모든 클래스는 자동으로 Object 클래스를 상속받음 ( extends Object { } ) Object 클래스에 다양한 메소드가 정의되어 있기 때문에, 어떤 클래스에서든지 다양한 메소드를 상속받아 사용할 수 있음 Calculator c1 = new Calculator(); System.out.println(c1); // Calculator@1dbd16a6 // Calculator 클래스가 toString이 정의되어 있는 Object 클래스를 상속받아서 자동으로, 암시적으로 toString()을 호출하기 때문에 System.out.println(c1.toString)과 같음 // "@" : 구분자 역할 // 1dbd16a6 : 인스턴스를 식별해.. onYouTube/Java 4년 전
예외 try { 예외의 발생이 예상되는 로직 } catch (예외클래스 인스턴스) { 예외가 발생했을 때 실행되는 로직 } public void divide() { try { System.out.print("계산결과는 "); System.out.print(this.left/this.right); System.out.print(" 입니다."); } catch(Exception e) { System.out.println("오류가 발생했습니다 : " + e.getMessage()); } } getMessage( ) : 에러의 원인을 간단하게 출력 toString( ) : 에러의 예외에 대한 내용과 원인을 출력 printStackTrace( ) : 에러의 발생지를 찾아서 단계별로 에러를 출력 System.out.pr.. onYouTube/Java 4년 전
[Algorithm] 백준 1085 직사각형에서 탈출 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); int w = sc.nextInt(); int h = sc.nextInt(); int w_min; // 가로 거리의 최소값 int h_min; // 세로 거리의 최소값 w_min = ((w - x) > x) ? x : w - x; h_min = ((h - y) > y) ? y : h - y; if (w_min > h_min) System.out.println(h_min); else System.out.prin.. Algorithm/백준+프로그래머스+SWEA+정올+구름 4년 전
[Algorithm] 백준 2609 최대공약수와 최소공배수 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int gcd = 1; // 최대공약수 int lcm = 1; // 최소공배수 int i; // a와 b 중 최소값 if (a == b) { gcd = a; lcm = a; } // a와 b가 같으면, 최대공약수와 최소공배수는 a(b) else { i = (a > b) ? b : a; while (i > 1) { if ((a % i == 0) && (b % i == 0)) { gcd *= i; a /= i; .. Algorithm/백준+프로그래머스+SWEA+정올+구름 4년 전
[Algorithm] 백준 10870 피보나치 수 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int x = 0; int y = 1; int result = 0; for(int i = n; i > 1; i--) { result = x + y; x = y; y = result; } if ((n == 0) || (n == 1)) System.out.println(n); else System.out.println(result); sc.close(); } } Algorithm/백준+프로그래머스+SWEA+정올+구름 4년 전
[Algorithm] 백준 1193 분수찾기 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int mom = 1; // 분모 int son; // 분자 int x1 = x; while(mom < x1) { x1 -= mom; mom++; } // mom : 대각선에 있는 분수 개수 // x1 : (mom이 홀수이면)왼쪽에서 or (mom이 짝수이면)오른쪽에서 몇번째에 위치하는지 if(mom % 2 != 0) { // 대각선에 있는 분수 개수가 홀수일 때 son = mom - x1 + 1; mom = x1; } else { // 대각선에 .. Algorithm/백준+프로그래머스+SWEA+정올+구름 4년 전
[Algorithm] 백준 10250 ACM 호텔 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int x, y; // x : 엘리베이터로부터의 거리, y : 층 while(t > 0) { int h = sc.nextInt(); int w = sc.nextInt(); int n = sc.nextInt(); if(n % h == 0) { // h층에 배정될 경우 y = h; x = n / h; } else { y = n % h; x = n / h + 1; } System.out.println(y * 100 + x); // yyxx호 or yxx.. Algorithm/백준+프로그래머스+SWEA+정올+구름 4년 전
[Algorithm] 백준 2577 숫자의 개수 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int abc = a * b * c; int[] arr = new int[10]; while (abc != 0) { int rest = abc % 10; arr[rest]++; abc /= 10; } for (int i = 0; i < 10; i++) System.out.println(arr[i]); sc.close(); } } Algorithm/백준+프로그래머스+SWEA+정올+구름 4년 전