티스토리 뷰

반응형

✏️ 4장 실습문제 - 1번

package ex;
class TV{
    String name;
    int year, inch;
    public TV(String name, int year, int inch){
        this.name=name;
        this.year=year;
        this.inch=inch;
    }
    public void show(){
        System.out.println(name+"에서 만든 "+year+"형 "+inch+"인치 TV");
    }
}
public class SolveProblem {
    public static void main(String[] args) {
        TV myTV  = new TV("LG",2017,32);
        myTV.show();
    }
}

 

✏️ 4장 실습문제 - 2번

package ex;
import java.util.*;
class Grade{
    int math,science,english;
    public Grade(int math, int science, int english){
        this.math=math;
        this.english=english;
        this.science=science;
    }
    public int average(){
        return (math+english+science)/3;
    }
}
public class SolveProblem {
    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);

       System.out.print("수학, 과학, 영어 순으로 3개의 점수 입력>>");
       int math = scanner.nextInt();
       int science = scanner.nextInt();
       int english = scanner.nextInt();
       Grade me = new Grade(math,science,english);
       System.out.println("평균은 "+me.average());

       scanner.close();
    }
}

 

✏️ 4장 실습문제 - 3번

package ex;
import java.util.*;
class Song{
    private String title, artist, country;
    private int year;

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

 

✏️ 4장 실습문제 - 4번

package ex;
import java.util.*;
class Rectangle{
    private int x,y,width,height;
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    int square(){
        return width*height;
    }
    void show(){
        System.out.println("("+x+","+y+")에서 크기가 "+width+"x"+height+"인 사각형");
    }
    boolean contatins(Rectangle r){
        if((this.x<r.x)&&(this.y<r.y)&&(this.x+this.width)>(r.x+r.width)&&(this.y+this.height>r.y+r.height))
            return true;
        else
            return false;
    }
}
public class SolveProblem {
    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.contatins(r))
            System.out.println("t는 r을 포함합니다.");
        if(t.contatins(s))
            System.out.println("t는 s를 포함합니다.");
    }
}

 

✏️ 4장 실습문제 - 5번

package ex;
import java.util.*;
class Circle {
    private double x;
    private double 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 SolveProblem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Circle []c = new Circle[3];
        for(int i=0;i<c.length;i++){
            System.out.print("x, y, radius >>");
            double x = scanner.nextDouble();
            double y = scanner.nextDouble();
            int radius = scanner.nextInt();
            c[i] = new Circle(x,y,radius);
        }
        for(int i=0;i<c.length;i++){
            c[i].show();
        }
        scanner.close();
    }
}

 

✏️ 4장 실습문제 - 6번

package ex;
import java.util.*;
class Circle {
    private double x;
    private double y;
    private int radius;

    public Circle(double x, double y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    int R(){
        return this.radius;
    }
    public void show(){
        System.out.println("가장 면적이 큰 원은 ("+x+","+y+")"+radius);
    }
}

public class SolveProblem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Circle []c = new Circle[3];
        for(int i=0;i<c.length;i++){
            System.out.print("x, y, radius >>");
            double x = scanner.nextDouble();
            double y = scanner.nextDouble();
            int radius = scanner.nextInt();
            c[i] = new Circle(x,y,radius);
        }
        int maxR=-1;
        int resIndex=0;
        for(int i=0;i<c.length;i++){
            if(maxR<c[i].R()) {
                maxR = c[i].R();
                resIndex = i;
            }
        }
        c[resIndex].show();
        scanner.close();
    }
}

 

✏️ 4장 실습문제 - 7번

package ex;
import java.util.Scanner;
class MonthSchedule{
    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+"입니다.");
    }
}

public class SolveProblem {
    public static void main(String[] args) {
        MonthSchedule [] June = new MonthSchedule[30];
        for(int i=0;i< June.length;i++)
            June[i] = new MonthSchedule();
        Scanner scanner = new Scanner(System.in);
        System.out.println("이번달 스케쥴 관리 프로그램.");
        while (true){
            System.out.print("할일(입력:1, 보기:2, 끝내기:3) >>");
            int option = scanner.nextInt();
            if(option==3){
                System.out.println("프로그램을 종료합니다.");
                break;
            }
            System.out.print("날짜(1~30)?");
            int day = scanner.nextInt();
            if(option==1) {
                System.out.print("할일(빈칸없이입력)?");
                String work = scanner.next();
                June[day].set(work);
            }
            else if(option==2){
                String work = June[day].get();
                if(work==null)
                    June[day].show();
                else{
                    System.out.print(day+"일의 할 일은 ");
                    June[day].show();
                }
            }
        }
    }
}

 

✏️ 4장 실습문제 - 8번

package ex;
import java.util.*;
class Phone {
    private String name, phoneNumber;

    public Phone(String name, String phoneNumber) {
        this.name = name;
        this.phoneNumber = phoneNumber;
    }
    public String getName(){
        return this.name;
    }
    public void show(){
        System.out.println(this.name+"의 번호는 "+this.phoneNumber+" 입니다");
    }
}

public class SolveProblem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("인원수>>");
        int num = scanner.nextInt();
        Phone phone[] = new Phone[num];
        for(int i=0;i< phone.length;i++){
            System.out.print("이름과 전화번호(이름과 번호는 빈 칸없이 입력)>>");
            String name = scanner.next();
            String phoneNumber = scanner.next();
            phone[i] = new Phone(name,phoneNumber);
        }
        System.out.println("저장되었습니다...");
        while(true){
            boolean flag = false;
            System.out.print("검색할 이름>>");
            String searchName = scanner.next();
            if(searchName.equals("그만"))
                break;
            for(int i=0;i< phone.length;i++){
                if(searchName.equals(phone[i].getName())) {
                    phone[i].show();
                    flag=true;
                }
            }
            if(!flag)
                System.out.println(searchName+"이 없습니다.");
        }
        scanner.close();
    }
}

 

✏️ 4장 실습문제 - 9번

package ex;
import java.util.*;
class ArrayUtil{
    public static int[] concat(int[] a, int[] b){
        int len=a.length;
        int len2=b.length;
        int []c = new int[len+len2];
        for(int i=0;i<len;i++)
            c[i]=a[i];
        for(int i=0;i<len2;i++)
            c[len+i]=b[i];
        return c;
    }
    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 SolveProblem {
    public static void main(String[] args) {
        int [] array1={1,5,7,9};
        int [] array2 = {3,6,-1,100,77};
        int [] array3 = ArrayUtil.concat(array1,array2);
        ArrayUtil.print(array3);
    }
}

 

✏️ 4장 실습문제 - 10번

package ex;
import java.util.*;
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]))
               return eng[i];
       }
       return null;
   }
}

public class SolveProblem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Dictionary dictionary = new Dictionary();
        System.out.println("한영 단어 검색 프로그램입니다.");
        while(true){
            System.out.print("한글 단어?");
            String searchKor = scanner.next();
            if(searchKor.equals("그만"))
                break;
            String result = dictionary.kor2Eng(searchKor);
            if(result==null)
                System.out.println(searchKor+"는 저의 사전에 없습니다.");
            else
                System.out.println(searchKor+"은 "+result);
        }
        scanner.close();
    }
}

 

✏️ 4장 실습문제 - 11번

package ex;
import java.util.Scanner;
class Add {
    int a, b;

    void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }

    int calculate() {
        return a+b;
    }

}

class Sub {
    int a, b;

    void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }

    int calculate() {
        return a-b;
    }

}

class Mul {
    int a, b;

    void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }

    int calculate() {
        return a*b;
    }

}

class Div {
    int a, b;

    void setValue(int a, int b) {
        this.a = a;
        this.b = b;
    }

    int calculate() {
        return a/b;
    }

}

public class SolveProblem {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        Add add = new Add();
        Sub sub = new Sub();
        Mul mul = new Mul();
        Div div = new Div();

        while (true) {
            System.out.print("두 정수와 연산자를 입력하시오(종료: 숫자에 0 입력) >> ");

            int a = sc.nextInt();
            int b = sc.nextInt();
            char c = sc.next().charAt(0);

            switch (c) {
                case '+' : add.setValue(a, b); System.out.println(add.calculate()); break;
                case '-' : sub.setValue(a, b); System.out.println(sub.calculate()); break;
                case '*' : mul.setValue(a, b); System.out.println(mul.calculate()); break;
                case '/' : div.setValue(a, b); System.out.println(div.calculate()); break;

                default : System.out.println("연산자를 잘못 입력하셨습니다.");
            }

            if(a==0 && b==0) {
                System.out.println("프로그램을 종료합니다.");
                sc.close();
                break;
            }

        }
    }
}

 

✏️ 4장 실습문제 - 12번

package ex;

import java.util.Scanner;

class Reservation {
    String[] S = new String[10];
    String[] A = new String[10];
    String[] B = new String[10];

    int countS = 0, countA = 0, countB = 0;

    public Reservation() {
        for (int i = 0; i < 10; i++) {
            S[i] = "---";
            A[i] = "---";
            B[i] = "---";
        }
    }

    void show(int seat) {
        if (seat == 1) {
            System.out.print("S>> ");
            for (int i = 0; i < S.length; i++)
                System.out.print(S[i] + " ");
        }
        if (seat == 2) {
            System.out.print("A>> ");
            for (int i = 0; i < A.length; i++)
                System.out.print(A[i] + " ");
        }
        if (seat == 3) {
            System.out.print("B>> ");
            for (int i = 0; i < B.length; i++)
                System.out.print(B[i] + " ");
        }
    }

    void set(int seat, String name, int seatNumber) {
        if (seat == 1) {
            S[seatNumber-1] = name;
        } else if (seat == 2) {
            A[seatNumber-1] = name;
        } else
            B[seatNumber-1] = name;
    }
    void showAll(){
        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<A.length;i++)
            System.out.print(A[i]+" ");
        System.out.println();
        System.out.print("B>> ");
        for(int i=0;i<B.length;i++)
            System.out.print(B[i]+" ");
        System.out.println();
    }
    void delete(String deleteName, int seatNumber){
        if(seatNumber==1){
            for(int i=0;i<S.length;i++){
                if(deleteName.equals(S[i]))
                    S[i] = "---";
            }
        }
        if(seatNumber==2){
            for(int i=0;i<A.length;i++){
                if(deleteName.equals(A[i]))
                    A[i] = "---";
            }
        }
        else{
            for(int i=0;i<B.length;i++){
                if(deleteName.equals(B[i]))
                    B[i] = "---";
            }
        }
    }
}


public class SolveProblem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Reservation reservation = new Reservation();
        System.out.println("명품콘서트홀 예약 시스템입니다.");
        while (true) {
            System.out.print("예약:1, 조회:2, 취소:3, 끝내기:4>>");
            int option = scanner.nextInt();
            if (option == 1) {
                System.out.print("좌석구분 S(1), A(2), B(3)>>");
                int seat = scanner.nextInt();
                reservation.show(seat);
                System.out.println();
                System.out.print("이름>>");
                String name = scanner.next();
                System.out.print("번호>>");
                int seatNumber = scanner.nextInt();
                reservation.set(seat, name, seatNumber);
            } else if (option == 2) {
                reservation.showAll();
                System.out.println("<<<조회를 완료하였습니다.>>");
            } else if (option == 3) {
                System.out.print("좌석 S:1, A:2, B:3>>");
                int seatNumber = scanner.nextInt();
                reservation.show(seatNumber);
                System.out.println();
                System.out.print("이름>>");
                String deleteName = scanner.next();
                reservation.delete(deleteName,seatNumber);
            } else
                break;
        }
    }
}
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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
글 보관함