대학교 수업/Java 프로그래밍

[JAVA] 명품 자바 프로그래밍 4장 실습문제

Jongung 2022. 4. 17. 03:04

1번

import java.util.Scanner;
class TV{
    private String mf;
    private int year;
    private int inch;
    TV(String mf, int year, int inch){
        this.mf = mf;
        this.year = year;
        this.inch = inch;
    }
    public void show(){
        System.out.println(mf + "에서 만든 " + year + "년형 " + inch + "인치 TV");
    }
}
public class main {
    public static void main(String[] args){
        TV myTV = new TV("LG", 2017, 32);
        myTV.show();
    }
}

1번

2번

import java.util.Scanner;
class Grade{
    int math, science, english;
    Grade(int a, int b, int c){
        math = a;
        science = b;
        english = c;
    }
    public int average(){
        return (math + science + english) / 3;
    }
}
public class main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        System.out.print("수학, 과학, 영어 순으로 3개의 정수 입력 >> ");
        int math = sc.nextInt();
        int science = sc.nextInt();
        int english = sc.nextInt();
        Grade me = new Grade(math, science, english);
        System.out.println("평균은 "+me.average()); // average()는 평균을 계산하여 리턴

        sc.close();
    }
}

2번

 

3번

import java.util.Scanner;
class Song{
    String title, artist, country;
    int year;
    Song(){
        this("title", "artist", 1000, "country");
    }
    Song(String title, String artist, int year, String country){
        this.title = title;
        this.artist = artist;
        this.year = year;
        this.country = country;
    }
    public void show(){
        System.out.print(year + "년 " + country + "국적의 " + artist + "가 부른 " + title);
    }
}
public class main {
    public static void main(String[] args){
        Song song = new Song("Dancing Queen", "ABBA", 1978, "스웨덴");
        song.show();
    }
}

3번

 

4번

import java.util.Scanner;
class Rectangle{
    int x,y,width,height;
    Rectangle(int x, int y, int width, int height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    public int square(){
        return width * height;
    }
    public void show(){
        System.out.print("(" + x + "," + y + ")에서 크기가 " + width + "*" + height + "\n");
    }
    public boolean contains(Rectangle r){
        if(x < r.x && y < r.y){
            if((width + x) > (r.x + r.width) && (height + y) > (r.y + r.height)){
                return true;
            }
        }
        return false;
    }
}
public class main {
    public static void main(String[] args){
        Rectangle r = new Rectangle(2, 2, 8, 7);
        Rectangle s = new Rectangle(5, 5, 6, 6);
        Rectangle t = new Rectangle(1, 1, 10, 10);

        r.show();
        System.out.println("s의 면적은 "+s.square());
        if(t.contains(r)) System.out.println("t는 r을 포함합니다.");
        if(t.contains(s)) System.out.println("t는 s를 포함합니다.");
    }
}

4번

 

5번

import java.util.Scanner;

class Circle {
    private double x, y;
    private int radius;
    public Circle(double x, double y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    public void show() {
        System.out.println("(" + x + "," + y + ")"+ radius);
    }
}

public class main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Circle c[] = new Circle[3]; // 3개의 Circle 배열 선언
        for(int i=0; i < c.length; i ++) {
            System.out.print("x, y, radius >>");
            double a = sc.nextDouble(); // x값 읽기.
            double b = sc.nextDouble(); // y값 읽기.
            int d = sc.nextInt(); // radius값 읽기.
            c[i] = new Circle(a,b,d); // Circle 객체 생성
        }
        for(int i=0; i<c.length; i++)
            c[i].show(); // 모든 Circle 객체 출력
        sc.close();
    }

}

5번

 

6번

import java.util.Scanner;

class Circle {
    private double x, y;
    private int radius;
    public Circle(double x, double y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    public void show() {
        System.out.println("(" + x + "," + y + ")"+ radius);
    }
    public int getRadius(){
        return radius;
    }
}

public class main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Circle c[] = new Circle[3]; // 3개의 Circle 배열 선언
        for(int i=0; i < c.length; i ++) {
            System.out.print("x, y, radius >>");
            double a = sc.nextDouble(); // x값 읽기.
            double b = sc.nextDouble(); // y값 읽기.
            int d = sc.nextInt(); // radius값 읽기.
            c[i] = new Circle(a,b,d); // Circle 객체 생성
        }
        int max = 0, index = -1;
        for(int i=0; i<c.length; i++){
            c[i].show();
            if(max < c[i].getRadius()){
                index = i;
                max = c[i].getRadius();
            }
        }

        System.out.print("가장 면적이 큰 원은? ");
        c[index].show();
        sc.close();
    }

}

6번

 

7번

import java.util.Scanner;

class Day {
    private String work;
    public void set(String work){
        this.work = work;
    }
    public String get(){
        return work;
    }
    public void show(){
        if(work == null) System.out.println("없습니다.");
        else System.out.println(work + "입니다.");
    }
}

class MonthSchedule{
    private Scanner sc;
    private Day days[];

    MonthSchedule(int a){
        this.days = new Day[a];
        for(int i = 0; i < days.length; i++){
            days[i] = new Day();
        }
        sc = new Scanner(System.in);
    }
    private void input(){
        System.out.print("날짜(1~30)? ");
        int arrCnt = sc.nextInt();
        System.out.print("할일(빈칸없이입력)");
        String work = sc.next();
        days[arrCnt].set(work);
    }
    private void view(){
        System.out.print("날짜(1~30)? ");
        int arrCnt = sc.nextInt();
        System.out.print(arrCnt + "일의 할일은 ");
        days[arrCnt].show();
    }
    private void finish(){
        System.out.println("프로그램을 종료합니다.");
        sc.close();
    }
    public void run(){
        System.out.println("이번달 스케쥴 관리 프로그램.");
        while(true){
            System.out.print("할일(입력:1, 보기:2, 끝내기:3) >>");
            int select = sc.nextInt();
            if(select == 1){
                input();
            }
            else if(select == 2){
                view();
            }
            else if(select == 3){
                finish(); return;
            }
        }
    }
}

public class main {

    public static void main(String[] args) {
        MonthSchedule m = new MonthSchedule(30);
        m.run();
    }

}

7번

 

8번

import java.util.Scanner;

class Phone {
    private String name;
    private String tel;
    Phone(String name, String tel) {
        this.name = name;
        this.tel = tel;
    }
    public String getName() {
        return name;
    }
    public String getTel() {
        return tel;
    }
}

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        Phone phone[];
        int i;
        System.out.print("인원수 >> ");
        int num = sc.nextInt();
        phone = new Phone[num];
        for(i=0; i<phone.length; i++) {
            System.out.print("이름과 전화번호(이름과 번호는 빈 칸없이 입력)>>");
            String name = sc.next();
            String tel = sc.next();
            phone[i] = new Phone(name, tel);
        }
        System.out.println("저장되었습니다...");
        while(true) {
            System.out.print("검색할 이름 >>");
            String name = sc.next();
            for(i=0; i<num; i++) {
                if(name.equals(phone[i].getName())) {
                    System.out.println(name+"의 번호는 "+phone[i].getTel()+" 입니다.");
                    i--;
                    break;
                }
            }
            if(name.equals("그만")) break;
            if (i == num) System.out.println(name+"이 없습니다.");
        }

        sc.close();
    }

}​

8번

 

9번

class ArrayUtil {
    public static int[] concat(int[] a, int[] b) {
        int temp[] = new int[a.length + b.length];
        int i = 0;
        for(; i < a.length; i++){
            temp[i] = a[i];
        }
        for(; i < a.length + b.length; i++){
            temp[i] = b[i-a.length];
        }
        return temp;
    }
    public static void print(int[] a) {
        System.out.print("[");
        for(int i = 0; i < a.length; i++){
            System.out.print(" " + a[i] + " ");
        }
        System.out.print("]");
    }
}

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array1 = {1, 5, 7, 9};
        int[] array2 = {3, 6, -1, 100, 77};
        int[] array3 = ArrayUtil.concat(array1, array2);
        ArrayUtil.print(array3);
    }

}

9번

 

10번

import java.util.Scanner;

class Dictionary {
    private static String[] kor = {"사랑", "아기", "돈", "미래", "희망"};
    private static String[] eng = {"love", "baby", "money", "future","hope"};
    public static String kor2Eng(String word) {
        for(int i=0; i<kor.length;i++) {
            if(word.equals(kor[i])) {
                if(i%2==0)
                    System.out.print(word+"은 ");
                else
                    System.out.print(word+"는 ");
                return eng[i];
            }
        }
        return "false";
    }
}

public class main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("한영 단어 검색 프로그램입니다.");
        while(true) {
            System.out.print("한글 단어?");
            String search = sc.next();
            if(search.equals("그만")) break;
            String eng = Dictionary.kor2Eng(search);
            if(eng.equals("false"))
                System.out.println(search+"는 저의 사전에 없습니다.");
            else
                System.out.println(eng);
        }
        sc.close();
    }

}

10번

 

11번

import java.util.Scanner;
class Add {
    private int a;
    private int b;
    public void setValue(int a, int b){
        this.a = a;
        this.b = b;
    }
    public int calculate(){
        return a + b;
    }
}

class Sub{
    private int a;
    private int b;
    public void setValue(int a, int b){
        this.a = a;
        this.b = b;
    }
    public int calculate(){
        return a - b;
    }
}

class Mul{
    private int a;
    private int b;
    public void setValue(int a, int b){
        this.a = a;
        this.b = b;
    }
    public int calculate(){
        return a * b;
    }
}

class Div{
    private int a;
    private int b;
    public void setValue(int a, int b){
        this.a = a;
        this.b = b;
    }
    public int calculate(){
        return a / b;
    }
}

public class main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("두 정수와 연산자를 입력하시오>> ");
        int a = in.nextInt();
        int b = in.nextInt();
        char c = in.next().charAt(0);
        switch (c){
            case '+':
                Add adds = new Add();
                adds.setValue(a, b);
                System.out.println(adds.calculate());
                break;
            case '-':
                Sub subs = new Sub();
                subs.setValue(a, b);
                System.out.println(subs.calculate());
                break;
            case '*':
                Mul muls = new Mul();
                muls.setValue(a, b);
                System.out.println(muls.calculate());
                break;
            case '/':
                Div divs = new Div();
                divs.setValue(a, b);
                System.out.println(divs.calculate());
                break;
            default:
                break;

        }
    }

}

11번

 

12번

import java.util.Scanner;
class Reservation{
    private String s[];
    private String a[];
    private String b[];
    private Scanner in;
    Reservation(){
        in = new Scanner(System.in);
        s = new String[10];
        a = new String[10];
        b = new String[10];
        for(int i = 0; i < s.length; i++){
            s[i] = "___";
            a[i] = "___";
            b[i] = "___";
        }
    }
    public void printSeat(){
        System.out.print("S >>");
        for(int i = 0; i < s.length; i++){
            System.out.print(s[i] + " ");
        }
        System.out.println();
        System.out.print("A >>");
        for(int i = 0; i < s.length; i++){
            System.out.print(a[i] + " ");
        }
        System.out.println();
        System.out.print("B >>");
        for(int i = 0; i < s.length; i++){
            System.out.print(b[i] + " ");
        }
        System.out.println();
        System.out.println("<< 조회를 완료하였습니다 >>");

    }
    public void printSeat(String seat[]){
        for(int i = 0; i < seat.length; i++){
            System.out.print(seat[i] + " ");
        }
        System.out.println();
    }
    public void chooseSeat(){
        while(true){
            System.out.print("좌석 구분 S(1), A(2), B(3)");
            int num = in.nextInt();
            switch (num){
                case 1:
                    System.out.print("S >>");
                    printSeat(s);
                    newSeat((s));
                    return;
                case 2:
                    System.out.print("A >>");
                    printSeat(a);
                    newSeat(a);
                    return;
                case 3:
                    System.out.print("B >>");
                    printSeat(b);
                    newSeat(b);
                    return;
                default:
                    System.out.print("다시 입력해 주세요.");
            }
        }
    }
    public void newSeat(String seat[]){
        System.out.print("이름 >> ");
        String name = in.next();
        System.out.print("번호 >>");
        int num = in.nextInt();
        seat[num] = name;
    }
    public void deleteSeat(){
        while(true){
            System.out.print("좌석 구분 S(1), A(2), B(3)");
            int num = in.nextInt();
            switch (num){
                case 1:
                    System.out.print("S >>");
                    printSeat(s);
                    delete((s));
                    return;
                case 2:
                    System.out.print("A >>");
                    printSeat(a);
                    delete(a);
                    return;
                case 3:
                    System.out.print("B >>");
                    printSeat(b);
                    delete(b);
                    return;
                default:
                    System.out.print("다시 입력해 주세요.");
            }
        }
    }
    public void delete(String seat[]){
        System.out.print("이름 >> ");
        String name = in.next();
        for(int i = 0; i < seat.length; i ++){
            if(seat[i].equals(name)){
                seat[i] = "___";
                break;
            }
        }
    }
}

class main{
    public static void main(String[] args){
        Reservation r = new Reservation();
        Scanner in = new Scanner(System.in);
        System.out.println("명품 콘서트홀 예약 시스템입니다.");
        while (true){
            System.out.print("예약:1, 조회:2, 취소:3, 끝내기:4 >> ");
            int num = in.nextInt();
            switch (num){
                case 1:
                    r.chooseSeat();
                    break;
                case 2:
                    r.printSeat();
                    break;
                case 3:
                    r.deleteSeat();
                    break;
                case 4:
                    in.close();
                    return;
            }
        }
    }
}

12번