본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

Employee ArrayList 정렬 Employee ArrayList정렬 import java.util.ArrayList; import java.util.Scanner; class Employee{ private String name; private int salary; public Employee(String name, int salary) { super(); this.name = name; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSalary() { return salary; } public void setSalary(int salary) .. 더보기
Calculator 계산기 계산기 Basic Calculator public class Calculator { /** 계산 결과 저장 : result 변수 * int add(int, int){ * result = int+int; * return result; * sub * mul * div */ int result; int add(int i, int j){ result = i+j; return result; } int sub(int i, int j){ result = i-j; return result; } int mul(int i, int j){ result = i*j; return result; } int div(int i, int j) throws ArithmeticException{ result = i/j; return re.. 더보기
Scanner Scanner 예제 import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("정수 두개를 입력하면 덧셈 결과를 출력하세요."); int i = keyboard.nextInt(); int j = keyboard.nextInt(); System.out.println("덧셈 결과 : "+i+j); String a = keyboard.next(); String b = keyboard.next(); System.out.println("정수 덧셈 결과"+(a+b)); } } 정수 두개를 입력하면.. 더보기
System.in System.in 메소드 import java.io.IOException; public class SystemInTest { public static void main(String[] args) { try { int result = 0; /** * 엔터키 입력 될 때까지 키보드 입력 13 * 더이상 입력을 하지 않을 경우 x=97(아스키코드) * * 1.회원가입 - 49 * 2.회원로그인 - 65 * 3.회원로그아웃 - 48 * if(...==1) ==> 에러 * * x,q 종료 * a:97 A:65 0:48 특수문자들 ctrl+z: -1 * 다국어, 자바 데이터 타입 변환 입력 불가능 * 가: 200 200 * 1(49) 0(48) () 1(49)0(48) ==> 20 */ while((result=S.. 더보기
FileInputStream / FileOutputStream / FileReader / FileWriter FileInputStream / FileOutputStream / FileReader / FileWriter 출력 cf.객체 생성만 이름 변경해주면 똑같다. import java.io.*; // FileCopyTest.java ==> copy.txt // a.txt b.txt ==> a.txt 입력 파일 없다. 오류 발생. // src/FileCopyTest.java copy.txt => copy.txt // src/JoinTest.java copy.txt => 기존 내용은 삭제된다. // true로 소스 변경한 후에 // src/FileCopyTest.java copy.txt => 기존 내용 유지 출력. public class FileCopyTest { public static void main(Str.. 더보기
thread 예제 2 package thread; // SharedCellSync.java public class SharedCellSync { public static void main(String args[]) { holdInteger h = new holdInteger(); produceInteger p = new produceInteger(h); consumeInteger c = new consumeInteger(h); p.start(); c.start(); } } class produceInteger extends Thread { private holdInteger pHold; public produceInteger(holdInteger h) { pHold = h; } public void run() { for (i.. 더보기
wait(), notify() wait(), notify() 예제 class Account { String name; int total; public Account(String name) { this.name = name; } // 순서 스레드 접근 영역 : 동기화 영역. synchronized void deposit(int money, String loc) { if(total>7000){ try { System.out.println("최대 잔액 초과"); wait(); // wait 스레드 실행시작 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } total = 0; notify(); // notify 스레드 실행.. 더보기
synchronized 연습 thread 연습 class Account { String name; int total; public Account(String name) { this.name = name; } // 순서 스레드 접근 영역 : 동기화 영역. synchronized void deposit(int money, String loc) { total += money; try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(loc+money+total); } int getTotal() { return total; } } class Bank e.. 더보기
Join Join 예제 class Test extends Thread{ int i=1; public void run(){ try { sleep(5000); // 5초 일시중지 } catch (InterruptedException e) { e.printStackTrace(); } i=10; System.out.println("run : "+i); } } public class JoinTest { public static void main(String[] args) throws InterruptedException { Test t = new Test(); // 실행상태 ; main 생성상태 t.start(); // 대기상태 ; t 실행상태 ; main // run 수행 완료 i 값: t 수행 t.join(); // .. 더보기
Runnable Runnable 생성, 변환, 출력 // 1. 멀티 스레드 동작 클래스 정의 class SimpleThread2 implements Runnable{ String msg; // ??? public SimpleThread2(String msg) { this.msg = msg; } // 1-2. run overriding // st1 시작, st2 시작 동시에 실행, 두개 멀티스레드 객체 생성 public void run(){ for(int i=1;i 더보기