본문 바로가기
It Study/POWER JAVA(기본서)

POWER JAVA 5장 프로그래밍 1번 문제 ~ 8번 문제

by prlkt5200 2022. 10. 14.
반응형

아직까지는 진도가 많이 느리네요

그나마 다행인 것은 이번주와 다음주 근무 스케줄이나 근무시간이 제가 진도를 빼기 수월하게 바뀌었다는 거네요 ㅎㅎㅎ

이번주에 노력해서 일요일까지 최대한 진도를 많이 뽑고싶네요~~

 

(카카오가 터지는 바람에 정상적인 포스팅을 하기가 많이 어려워졌습니다ㅠㅠ...  그래도 전화위복이라고, 이것을 기회로 당분간 포스팅은 멈추고 개인 공부에 힘쓸 예정입니다.  )

 

1번 문제

 

(다른 여러 기능도 추가하고싶었습니다. 개인적으로 저는 은행앱에 관심이 있었기에 이런 문제가 재미있게 다가왔습니다. 하지만 시간상 간단하게만 구현했습니다. 참고하실 분들은 이것을 응용시켜 보세요!)

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
package test;
 
import java.util.Scanner;
 
public class BankAccount {
 
    private int balance;
    private int amount;
    Scanner sc = new Scanner(System.in);
 
    BankAccount(int balance) {
        this.balance = balance;
    }
 
    public void setBalance() {
        System.out.println("충전 금액을 입력하세요: ");
        int data = sc.nextInt();
        balance = data;
    }
 
    public int getBalance() {
        System.out.println("잔액: ");
        return balance;
    }
 
    public void transfer(BankAccount other) {
        System.out.println("송금할 금액을 입력하세요: ");
        amount = sc.nextInt();
        this.balance -= amount;
        other.balance += amount;
    }
 
    public static void main(String[] args) {
 
        BankAccount b = new BankAccount(0);
        BankAccount other = new BankAccount(0);
        b.setBalance();
        b.transfer(other);
        System.out.println("나의 계좌 잔액: " + b.getBalance());
        System.out.println("상대의 계좌 잔액 : " + other.getBalance());
    }
 
}
 
cs

 

2번 문제

 

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
package test;
 
public class Circle {
 
    static private int x, y;
    int radius;
 
    Circle(int x, int y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
 
    public static void move(int dx, int dy) {
        x += dx;
        y += dy;
 
    }
 
    @Override
    public String toString() {
        return "move() 호출 " + "[" + x + ", " + y + ", " + radius + "]";
 
    }
 
    public static void main(String[] args) {
        Circle c = new Circle(10105);
        System.out.println(c + "\n");
        c.move(1020);
        System.out.println(c);
    }
 
}
 
cs

3번 문제

 

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
package test;
 
public class MyMetricTest {
    public static void main(String[] args) {
        MyMetric.kiloToMile(1);
        MyMetric.mileToKilo(1);
    }
}
 
class MyMetric {
 
    static void kiloToMile(double kilo) {
        System.out.println("km =" + kilo * 0.62137);
    }
 
    static void mileToKilo(double mile) {
        System.out.println("km =" + mile * 1.60934);
    }
 
    public static void main(String[] args) {
 
    }
 
}
 
cs

 

4번 문제

(훨씬 더 간단한 풀이가 있겠지만 최대한 완성도 있게, 그러면서도 실험하면서 하다보니, 1개를 푸는데도 오래 걸리네요;;)

 

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
package test;
 
import java.util.Scanner;
 
public class Car {
    private String model;
    private String make;
    static Scanner sc = new Scanner(System.in);
    private static int accumulateCount;
    private static int count;
 
    Car(String model, String make) {
        this.model = model;
        this.make = make;
 
        this.accumulateCount++;
        this.count++;
        if (count > 1) {
            count--;
        }
    }
 
    public static Car makeCar() {
 
        System.out.println("차종과, 제작자를 입력하세요: ");
        String a = sc.next();
        String b = sc.next();
        return new Car(a, b);
    }
 
    private static void numberOfCars() {
        count = count;
        accumulateCount = accumulateCount;
    }
 
    public static void getNumberOfCars() {
        System.out.println("자동차 대수= " + count + ", 누적자동차= " + accumulateCount);
    }
 
    public static void main(String[] args) {
 
        Car c1 = Car.makeCar();
        Car c2 = Car.makeCar();
 
        Car.getNumberOfCars();
    }
}
cs

 

5번 문제

(클래스를 나눠서 작업해봤습니다. 그래서 두 개 다 봐야합니다~

그리고 이 문제도 매우 간단한 문제이나, 제가 초반에 헤맸었는데... 그 이유가 배열을 만들 때 객체 배열, 다시 말해 객체를넣을 수 있는 배열을 만들어 줘야 했음에도, 정수 배열을 만들었습니다...

즉 클래스 타입 [] 참조변수 = new 클래스타입[길이]; 를 해서 배열을 만들어줘야 이 배열의 각 인덱스에 객체를 집어넣을 수 있고 이것은 길이가 고정되어 있으므로 정적 배열입니다. 여기서 arrayList를 활용해준다면 동적배열이 됩니다.)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package test;
 
public class Circle {
    private int radius;
 
    public int get() {
        return radius;
    }
 
    public Circle() {
        this.radius = (int) ((Math.random() * 100+ 1);
    }
 
    public static void main(String[] args) {
 
    }
 
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package test;
 
import java.util.Arrays;
 
public class CircleTest {
 
    public static void main(String[] args) {
        Circle[] list = new Circle[3];
        for (int i = 0; i < list.length; i++) {
            list[i] = new Circle();
 
            System.out.println(list[i].get());
        }
    }
 
}
 
cs

 

6번 문제

(어느덧 밤을 새가니, 슬슬 정신이 몽롱하고 

그냥 모든 게 귀찮네요 ㅋㅋㅋ 그래서 아래 문제는 핵심기능만 살려서 문제를 바로 풀었습니다.

어쨌거나 문제를 해결했다는 점에서 만족스럽네요 ㅎㅎ!

 

ps: 이 문제는 제 블로그에 power java 5장 lab 책 정보 저장-https://forfire700.tistory.com/40 를 보고 오시는 것을 추천합니다.)

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
package test;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
 
public class Contact {
 
    private String name;
    private String tel;
    private String email;
    private static int count;
    static ArrayList<Contact> arr = new ArrayList<Contact>();
    static Scanner sc = new Scanner(System.in);
 
    public Contact() {
 
        System.out.println("연락처 정보를 입력하세요 띄어쓰기는 허용이 안됩니다.");
        System.out.println("이름: ");
        this.name = sc.next();
        System.out.println("번호: ");
        this.tel = sc.next();
        System.out.println("이메일: ");
        this.email = sc.next();
        count++;
    }
 
    public static void addContact() {
        do {
 
            arr.add(new Contact());
            System.out.println("종료를 원하면 -1을 입력해주세요 아니면 아무 숫자를 입력해주세요");
            int input = sc.nextInt();
            if (input == -1) {
                break;
            }
        } while (true);
 
    }
 
    public static void main(String[] args) {
        System.out.println("시작합니다.");
 
        Contact.addContact();
 
        for (Contact search : arr) {
            System.out.println("검색하려는 이름을 입력하세요: ");
            String name = sc.next();
 
            if (search.name.equals(name) != true) {
                System.out.println("검색중입니다.");
            }
            System.out.println(search.name + ", " + search.tel + ", " + search.email);
        }
    }
 
}
 
cs

 

7번 문제
(10.15 다시 문제를 풀기 시작했습니다. 노트를 정리할 때는 이런 식을 하면 되겠다 구상을 하니 더 편하네요. 왜 메모를 습관화 하라는지 알겠습니다. 다만 지저준하게 메모한 덕분에 생각한 것을 구현하는 데 시간이 조금 걸렸습니다.)

 

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
package test;
 
import java.util.ArrayList;
import java.util.Scanner;
 
public class Word {
 
    private String kor;
    private String eng;
    private int count;
    Scanner sc = new Scanner(System.in);
    private boolean b = true;
    private ArrayList<Word> list = new ArrayList<Word>();
 
    private Word() {
 
    }
 
    private Word(String kor, String eng) {
        this.kor = kor;
        this.eng = eng;
 
    }
 
    public void saveWord() {
 
        while (b) {
            System.out.println("한국어 단어를 입력하세요:");
            String kor = sc.next();
            System.out.println("영어 단어를 입력하세요:");
            String eng = sc.next();
            list.add(new Word(kor, eng));
            System.out.println("계속은 1. 종료 0.");
            int re = sc.nextInt();
            if (re == 0) {
                b = false;
            } else if (re == 1) {
                b = true;
            }
        }
    }
 
    public void searchWord() {
 
        System.out.println("검색 시작은 start, 종료는 quit");
        String answer = sc.next();
        System.out.println("검색할 단어를 입력해주세요");
        String search = sc.next();
 
        if (answer.equals("start")) {
            for (Word w : list) {
                if (w.kor.equals(search) || w.eng.equals(search)) {
                    System.out.println("사전에 있는 단어입니다.");
                    System.out.println("(" + w.kor + ", " + w.eng + ")");
                    count++;
                    System.out.println("시스템을 종료하겠습니다.");
                    break;
                } else {
                    if (count == 1)
                        break;
                    System.out.println("검색중");
                }
            }
            if (count == 0)
                System.out.println("죄송합니다 사전에 없는 단어입니다.");
            count = 0;
        }
    }
 
    public static void main(String[] args) {
        Word test = new Word();
        test.saveWord();
        test.searchWord();
    }
 
}
 
cs

 

8번 문제

이 문제는 간단하게만 구현했습니다!~~

 

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
package test;
 
import java.util.Scanner;
 
public class Account {
 
    public static Scanner sc = new Scanner(System.in);
    private int balance;
    private String account;
    private String accountBank;
 
    public Account(String account, String Bank) {
        this.account = account;
        this.accountBank = accountBank;
    }
 
    public static Account makeAccount() {
 
        System.out.println("생성할 계좌의 번호를 입력하세요: ");
        String account = sc.next();
        System.out.println("생성할 계좌의 은행을 입력하세요: ");
        String name = sc.next();
        return new Account(account, name);
 
    }
 
    public int getBalance() {
        return balance;
    }
 
    public void setBalance(int balance) {
        this.balance += balance;
    }
    public void setBalance1(int balance) {
        this.balance -= balance;
    }
 
    public String getAccount() {
        return account;
    }
 
    public void setAccount(String account) {
        this.account = account;
    }
}
 
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
package test;
 
import java.util.Scanner;
 
public class Atm {
    public static Scanner sc = new Scanner(System.in);
    private static boolean b = true;
 
    public static int inputMoney() {
        System.out.println("얼마를 입금하시겠습니까: ");
        int inputMoney = sc.nextInt();
        return inputMoney;
    }
 
    public static int outputMoney() {
        System.out.println("얼마를 입금하시겠습니까: ");
        int outputMoney = sc.nextInt();
        return outputMoney;
    }
 
    public static void main(String[] args) {
        System.out.println("계좌를 생성합니다");
        Account me = Account.makeAccount();
 
        while (b) {
            System.out.println("1.현금 입금\n2.현금 인출\n3.계좌이체\n4.종료\n");
            int input = sc.nextInt();
            switch (input) {
            case 1:
 
                me.setBalance(Atm.inputMoney());
                System.out.println(" 현재 잔액은 " + me.getBalance() + "입니다.");
                System.out.println("계속하시겠습니까?: ");
                String answer1 = sc.next();
                if (answer1 == "no")
                    b = false;
 
                break;
            case 2:
                me.setBalance1(Atm.inputMoney());
                System.out.println(" 현재 잔액은 " + me.getBalance() + "입니다.");
                System.out.println("계속하시겠습니까?: ");
                String answer2 = sc.next();
                if (answer2 == "no")
                    b = false;
 
                break;
            case 3:
                System.out.println("이체할 계좌번호를 입력하세요: ");
                String accountBank = sc.next();
                System.out.println("이체할 금액을 입력하세요: ");
                int money = sc.nextInt();
                me.setBalance1(money);
 
                System.out.println(accountBank + "로 이체가 완료되었습니다. 잔액은 " + me.getBalance() + "입니다.");
                System.out.println("계속하시겠습니까?: ");
                String answer3 = sc.next();
                if (answer3 == "no")
                    b = false;
                break;
            case 4:
                System.out.println("시스템을 종료합니다. ");
                b = false;
            }
        }
 
    }
 
}
cs

 

반응형