프로그래밍/JAVA 자바
-
throw, throws / 예외 주기 (2)프로그래밍/JAVA 자바 2021. 7. 29.
package ex0728; public class Ex01_exception { public static void main(String[] args) { // TODO Auto-generated method stub User1 ob = new User1(); try { ob.set("김자바", -5); System.out.println(ob.getName()+":"+ob.getAge()); } catch (Exception e) { System.out.println(e.toString()); } } } class User1 { private String name; private int age; public void set(String name, int age) throws Exception { try ..
-
throw / throws프로그래밍/JAVA 자바 2021. 7. 29.
package ex0727; public class Ex112_throws { public static void main(String[] args) { // TODO Auto-generated method stub User12 u = new User12(); try { u.setValue(-5); int n = u.getValue(); System.out.println(n); } catch (Exception e) { // TODO: handle exception System.out.println(e.toString()); } System.out.println("end"); } } class User12 { private int value; //throw 와 throws의 차이를 알아야함 public v..
-
예외 잡기 (try, catch, finally문)프로그래밍/JAVA 자바 2021. 7. 29.
package ex0727; import java.util.Scanner; public class Ex102_exception { public static void main(String[] args) { // 예외 처리 Scanner sc = new Scanner(System.in); int a,b,c; try { // (1)예외 발생할 가능성이 있는 코드를 작성 System.out.println("두 수 입력"); a = sc.nextInt(); b = sc.nextInt(); c = a/b; System.out.println(a+"/"+b+"="+c); } catch (Exception e) { // (2)예외가 일어났을때 실행할 코드 // Exception = 모든 예외를 catch할 수 있지만 예..
-
추상 클래스 / 인터페이스프로그래밍/JAVA 자바 2021. 7. 26.
package ex0726; public class Ex01_abstract { public static void main(String[] args) { // SortInt si = new SortInt(); // 컴파일 에러. 추상 클래스는 객체 생성 불가 // 추상 클래스는 반드시 하위 클래스가 존재하고 하위 클래스를 통해서만 객체를 생성 할 수 있다. int [] data = new int[] {40, 50, 30, 35, 25}; SortInt si = new BubbleSort(); // 업 캐스팅 // SortInt si = new SelectionSort(); print("source data : ", data); si.sort(data); print("sort data : ", data);..
-
오버라이드 / 오버로딩 / 클래스 상속프로그래밍/JAVA 자바 2021. 7. 24.
package ex0723; public class Ex01_override { public static void main(String[] args) { //오버 라이드 User1 ob1 = new User1("1001", "홍길동"); User1 ob2 = new User1("1001", "홍길동"); System.out.println(ob1 == ob2); //주소를 비교 false //User1에서 equals() 메소드를 재정의해서 학번과 이름이 동일하면 true를 반환 System.out.println(ob1.equals(ob2)); } } class User1 { private String hak; private String name; public User1() { } public User1(..
-
클래스 상속, override프로그래밍/JAVA 자바 2021. 7. 22.
package ex0722; public class Ex01_Inheritance { public static void main(String[] args) { // 클래스 상속 Sample1 s = new Sample1(); System.out.println(s.c); // 상위 클래스 칠드 접근 s.print(); s.disp(); } } //상속은 주로 검증된 것들을 상속 받기위해 사용한다. class Test1 { //상위 클래스는 Object private int a = 10; // 하위 클래스 접근 불가능 int b = 20;// 동일한 패키지 에서만 접근 가능 protected int c = 30; // 동일한 패키지 및 하위 클래스는 어디서나 접근 가능 public int d = 40; //..
-
Calendar 클래스 / 날짜 형식이 올바른지 판단하기프로그래밍/JAVA 자바 2021. 7. 22.
public class DateUtil { /** * 날짜 형식이 올바른지 판단하여 반환 * * @param date문자열의 날짜 * @return형식이 올바른지의 여부(true/false) */ public boolean isValidDate(String date) { try { if (date.length() != 8 && date.length() != 10) { //20210722 or 2021-07-22 이런식으로 8글자나 10글자인지 확인 return false; } String p = "^\\d{4}(\\.|\\-|\\/)?\\d{2}(\\.|\\-|\\/)?\\d{2}$"; //입력된 서식 체크 if (!date.matches(p)) { //매치 안되면 폴스 return false; } da..