본문 바로가기

카테고리 없음

6장문제 / 8장

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package day16_test;
 
public class ch06_test08 {
 
    public static void main(String[] args) {
        String a = new String("        Oh, Happy    ");
        String b = a.trim();
        String c = b.concat(" Day.");
        
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
 
    }
 
}
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package day16_test;
 
import java.util.Calendar;
 
public class ch06_test12 {
    public static void main(String[] args) {
        
    Calendar date = Calendar.getInstance();
    date.set(2020,11,25);
    System.out.println ("약속 날짜는 "+date.get(Calendar.YEAR)+"년"
    +(date.get(Calendar.MONTH)+1)+"월"
    +date.get(Calendar.DATE) + " 일입니다.");
    }
}
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
 
public class day16Ex04 {
 
    public static void main(String[] args) {
        InputStream is = day16Ex04.class.getResourceAsStream("person.json");
        
        
        // 표占쏙옙 占쏙옙占쏙옙 占쏙옙트占쏙옙
//        InputStreamReader isr = new InputStreamReader(is, Charset.forName(UTF-8));
//        BufferedReader br= new BufferedReader(isr);
//        
//        String line = "";
//        while((line= br.readLine()) != null) {
//            System.out.println(line);
//        }
        
        // 占쏙옙트占쏙옙 > 占쏙옙크占쏙옙 > 占쏙옙체(占쏙옙占쏙옙占쏙옙트)占쏙옙 占쏙옙환
        JSONTokener toKener = new JSONTokener(is);
        JSONObject json = new JSONObject(tokener);
        System.out.println(json.get("name"));
        System.out.println(json.get("addr"));
        System.out.println(json.get("age"));
        
        
        JSONEObject json2 = new JSONObjt();
        json2.put("name","�씠�닚�떊");
        json2.put("addr","�쟾�씪�궓�룄 �븳�궛�룄");
        json2.put("age","35");
        System.out.println(json2);
        
        String jsonStr = json2.toString();
        System.out.println(jsonStr);
        
 
    }
 
}
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Day15_test;
 
//else 여야함
// p가 q와 다르다면
 
class MyPoint {
    public int x,y;
    public MyPoint(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    
    @Override
    public String toString() {    //toString 재정의
        return "Point (" +x + "," + y +")";
    }
    
    
    public boolean equals (Object obj) {
        MyPoint p = (MyPoint)obj; // 다운케스팅
        if(x == p.x && y == p.y) 
            return true;
        else
            return false;
    }
    
 
    
 
    public static void main(String[] args) {
        MyPoint p = new MyPoint(3,50);
        MyPoint q = new MyPoint(4,50);
        System.out.println(p);
        if(p.equals(q))
            System.out.println("같은 점");
        else
            System.out.println("다른 점");
 
    }
 
}
 
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Day15_test;
 
class Circle {
    public int x,y,radius;
    
    public Circle(int x, int y, int radius) { //Circle a = new Circle(2,3,5); 의 생성자
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    
    
    @Override
    public String toString() {
        return "Circle (" + x + ","+ y + ") 반지름" + radius;
    }
    
//    public boolean equals(Object obj) { // equals() 오버라이딩. 제정의
//        Circle p = (Circle)obj;    //객체 obj를 다운캐스팅
//        if(p.x == x && p.y == y) //중심이 같으면 같은 원이다.
//            return true;
//        else 
//            return false;
//    }
    
    
    public boolean equals(Object obj) { // equals() 오버라이딩. 제정의
        if(obj instanceof Circle) {
            Circle p = (Circle)obj;    //객체 obj를 다운캐스팅
            if(p.x == x && p.y == y) { //중심이 같으면 같은 원이다.
                return true;
            
            }
        }
        return false;
    }
    
    
 
    public static void main(String[] args) {
        Circle a = new Circle(2,3,5);
        Circle b = new Circle(2,3,30);
        System.out.println("원 a: " + a);
        System.out.println("원 b: " + b);
        if(a.equals(b))
            System.out.println("같은 원");
        else
            System.out.println("서로 다른 원");
        
    }
 
}
 
 
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package Day15_test;
 
import java.util.Random;
import java.util.Scanner;
 
class Person {
    String name;
    int[] numArr = new int[3];         // numArr에 난수 3개 만들기
    Random random = new Random();
    
    public Person(String name) {  //생성자
        this.name = name;
         
    }
    
    
    
    public boolean init() { // 자기꺼가 되는지 안되는지 여기서 확인
        for(int i = 0; i<numArr.length; i++) {
            numArr[i] = 1 + random.nextInt(3); // 1 ~ 3 사이
            // init이 호출될떄마다 숫자가 추가됨. 
            // 이 세 개가 동일한지 아래의 if로 물어본다
        }
        if(numArr[0] == numArr[1] && numArr[1] == numArr[2]) {
            System.out.printf("%d %d %d %s님이 이겼습니다.",numArr[0], numArr[1], numArr[2],name);
            return true;
        }else {
            System.out.printf("%d %d %d 아쉽군요!",numArr[0], numArr[1], numArr[2]);
            return false;
            
        }
    }
 
    public String toString() {
        return name;
    }
    
}
 
public class Game {
    public Scanner scanner = new Scanner(System.in);
    
    
 
    
    public void play() {
        // 2명의 정보  입력
        
        Person[] pArr = new Person[2];
        for(int i = 0; i <pArr.length; i++) { 
            System.out.println(1+i+"번쨰 선수 이름>> ");
            String name = scanner.next();
            pArr[i] = new Person(name);
        }
        
        boolean isTrue = false;
        int cnt = 0;
        scanner.nextLine();
        while(!isTrue) {
            Person person = pArr[cnt%pArr.length];
            System.out.print("[" + person + "]");
            scanner.nextLine();
            isTrue = person.init();
            //System.out.print(pArr[cnt%pArr.length]);
            scanner.nextLine();
            cnt++;
    }
}
    
    
    public static void main(String[] args) {
        new Game().play();
        
    }
 
}
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
 
public class day16Ex01 {
 
    public static void main(String[] args) throws IOException {
        InputStream in = System.in;
        OutputStream out = System.out;
        
        System.out.println("입력 (종료는 Ctrl+z) >>");
        
        int data = 0;
        while ((data = in.read())!=-1){
            out.write(data);
        }
    }
    
// 글자 츌력은 안된다.
    public static void test(String[] args) throws IOException {
        int data = 0;
        System.out.println("데이터를 입력하세요. (종료: Ctrl+z ) >> ");
        while((data = System.in.read())!= -1) { //입력받은게 -1이 아니면 계속 입력받음
            System.out.print((char)data);
        }
 
    }
 
}
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
public class day16Ex02 {
 
    // 이건 읽어오는거
    public static void main(String[] args) throws IOException, InterruptedException {
        int data =0;
        FileInputStream fis = new FileInputStream("E:\\test.data");
        OutputStream os = System.out;
        
        while((data=fis.read())!=-1) {
            os.write(data);
        }
    
    }
    
    
    // 이건 넣는거
    public static void test(String[] args) throws IOException {
        int data = 0;
        InputStream is = System.in;// is로 많이 줄인다
        FileOutputStream fos = new FileOutputStream("E:\\test.data");
        
        System.out.println("데이터 입력(종료는 Ctrl+z) >>>");
        while ((data=is.read()) !=-1){
            fos.write(data);
            
        }
        is.close();
        fos.close();
    }
}
 
 
// 읽어오는건 inputstream
// 출력하는건 outstream
cs

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Date;
 
public class day16Ex03 {
    
    public day16Ex03() throws IOException {
        // 
        String spec = "https://img.animalplanet.co.kr/news/2019/10/02/700/dmb9pj38n168l9ayvmxx.jpg";
        URL url = new URL(spec);
 
        InputStream is = url.openStream();
        FileOutputStream fos = null;
        
        File file = new File("test.jpg");
        if(!file.exists()) {
            if(file.createNewFile()) {
                fos = new FileOutputStream(file);
                int data = 0;
                while((data=is.read())!= -1){
                    fos.write(data);
                }
                fos.close();
            }
        }
        if (is != null) is.close();
        if ( fos != null) fos.close();
     }
    
    public static void main(String[] args) throws IOException {
        new day16Ex03();
        
    }
    
    
    
    
    
    
    
    
    public static void test2(String[] args) {
        File file = new File("./src"); // 검색하려는거 치면 나옴
        if(file.isDirectory()){
            System.out.println("디렉토리입니다!");
            String[] fileNames = file.list();//디렉토리 안에 뭐가 있는지 몰록
            for(String fileName : fileNames) {
                System.out.println(fileName); // 여기까지 치면 목록 나옴
            }
        }
        file = new File("."); // . 은 현재 디렉토리
        if(file.canRead()) {
            System.out.println("읽을 수 있습니다.");
            System.out.println(file.getAbsolutePath()); 
        }
        
    }
    
    public static void test(String[] args) throws IOException {
        File file = new File("test.txt");
        
        if(file.exists()) {
            System.out.println("파일이 존재 합니다.");
            if(file.delete()) { // 이게 트루가 되면 지워진거
                System.out.println("파일이 삭제되었습니다.");
            }
        } else {
            System.out.println("파일이 존재하지 않습니다.");
            if(file.createNewFile()) {
            System.out.println("파일이 생성되었습니다.");    
            }
        }
    }
}
cs