삼성SDS_멀티캠퍼스/Java
-
16일 차 Thread(쓰레드) 동기화 문제삼성SDS_멀티캠퍼스/Java 2015. 9. 30. 11:21
public class BankAccount { private int balance; public void deposit(int account) {balance += account;} public void withdraw(int account) {balance -= account;} public int getBalance() {return balance;}} public class User implements Runnable { private BankAccount account; public User(BankAccount account) {this.account = account;} public void run() {// TODO Auto-generated method stubfor (int i = 0;..
-
15일 차 도서 대여 프로그램삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 16:56
package book; public abstract class Book { private int id;private String name;private String author;private boolean rental; public boolean equlas(int id) { if (this.id == id)return true;elsereturn false; } public Book() {id = 0;name = "제목없음";author = "저자미상";rental = false;} public Book(int id, String name, String author) {this.id = id;this.name = name;this.author = author;rental = false;} public..
-
15일 차 Map삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 11:28
import java.util.HashMap;import java.util.Map; class Student {int number;String name; public Student(int number, String name) {this.number = number;this.name = name; } public String toString() {return name;}} public class Test8 { public static void main(String[] args) { Map st = new HashMap();st.put("2009001", new Student(2009001, "구준표"));st.put("2009002", new Student(2009002, "금잔디"));st.put("20..
-
15일 차 HashSet을 이용한 로또 번호 추출기삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 10:37
import java.util.HashSet;import java.util.LinkedList;import java.util.List; public class Test6 { public static void main(String[] args) { HashSet set = new HashSet(); while (set.size() < 6) {set.add((int) (Math.random() * 45) + 1);} System.out.println(set); } }
-
15일 차 제네릭메소드삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 09:28
class Store { private T data; public void setData(T data) { this.data = data;} public T getData() {return data;} } public class Test { public static void main(String[] args) { Store store = new Store();store.setData("Hello");System.out.println(store.getData()); Store intStore = new Store();intStore.setData(10);System.out.println(intStore.getData()); Integer[] arr = { 4, 3, 2, 1, 6, 7, 9 };System..
-
14일 차 정규표현식삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 09:06
# 정규표현식 : 정규표현식의 사전적인 의미로는 특정한 규칙을 가진 문자열의 집합을 표현하는데 사용하는 형식 언어이다. 주로 문자열의 검색과 치환을 위한 용도로 사용.입력한 문자열에서 특정한 조건을 표현할 경우 일반적인 조건문으로는 복잡할 수 있지만, 정규표현식을 이용하면 매우 간단하게 표현이 가능함.But, 코드가 간단한 만큼 가독성이 떨어져서 표현식을 숙지하지 않으면 이해하기 힘들다. 정규 표현식에 대한 자세한 내용이 있는 사이트 : http://www.nextree.co.kr/p4327/ ## 사용자에게 이메일과 전화번호를 입력받아 검증하는 프로그램. import java.util.Scanner; import java.util.regex.Pattern;//정규표현식 작성 public class Te..
-
14일 차 예외처리 실습예제삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 09:06
1. 0으로 나누었을때에 대한 예외처리 만들기class DivideByZeroException extends ArithmeticException { public DivideByZeroException(){ super("0으로 나눌수 없음."); } }public class Test { public static void main(String[] agrs) { double result; try{ result = quotient(1, 0); } catch (DivideByZeroException e){ System.out.println(e.toString()); } } public static double quotient (int n, int d) throws DivideByZeroException{ if (..
-
14일 차 예외의 종류삼성SDS_멀티캠퍼스/Java 2015. 9. 25. 09:05
# 예외 : 프로그램이 실행중에 종료되게 만드는 원인.크게 RuntimeException, IOException, Error 로 나뉜다.# Error : 우리의 잘못이 없는데 프로그램이 죽는것으로 어쩔수 없다. (시스템 상에서 발생하는 치명적인 에러) - 예외처리 대상이 아니다.- OutOfMemoryError- AWTError- ThreadDeath# RuntimeException : 우리(개발자)가 잘못한것. 잘못 안 할수 있던 것이다.예외처리의 필수대상은 아니다.# IOException : 필수 예외처리 대상 !!!! ※중요※ # 체크 예외와 비체크 예외자바 컴파일러는 RuntimeException 은 확인하지 않는다. 그밖의 예외는 모두 처리를 확인한다. # 다형성과 예외예외도 객체이기 때문에 n..