ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA - 객체 지향 프로그램 연습문제
    JAVA 2024. 1. 3. 11:30
    728x90

    문제. 1 - 절차 지향 직사각형 프로그램을 객체 지향으로 변경하기

    다음은 직사각형 넓이, 둘레 길이, 정사각형 여부를 구하는 프로그램이다.

     

    • 절차 지향 프로그래밍 방식으로 되어 있는 코드를 객체 지향 프로그래밍 방식으로 변경하기
    • Retanngle 클래스를 만들기
    • RetangleOopMain에 해당 클래스를 사용하는 main() 코드 만들기

    절차 지향 코드

    package oop.ex;
    
    public class RectangleProceduralMain {
        public static void main(String[] args) {
            int width = 5;
            int height = 8;
            int area = calculateArea(width, height);
            System.out.println("넓이: " + area);
            int perimeter = calculatePerimeter(width, height);
            System.out.println("둘레 길이: " + perimeter);
            boolean square = isSquare(width, height);
            System.out.println("정사각형 여부: " + square);
        }
    
        static int calculateArea(int width, int height) {
            return width * height;
        }
    
        static int calculatePerimeter(int width, int height) {
            return 2 * (width + height);
        }
    
        static boolean isSquare(int width, int height) {
            return width == height;
        }
    }

     

    내가 푼 정답

    public class Rectangle {
        int width;
        int height;
    
        int area() {
            return width * height;
        }
    
        int perimeter() {
            return 2 * (width + height);
        }
    
        boolean isSquare() {
            return width == height;
        }
    }
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.width = 5;
        rectangle.height = 8;
    
        int area = rectangle.area();
        System.out.println("넓이 : " + area);
        int perimeter = rectangle.perimeter();
        System.out.println("둘레 길이 : " + perimeter);
        boolean square = rectangle.isSquare();
        System.out.println("정사각형 여부 : " + square);
    }

     

    문제. 2 - 객체 지향 계좌

    은행 계좌를 객체로 설계하기.

    • Account 클래스 만들기
      • int balance : 잔액
      • deposit(int amount) : 입금 메서드
        • 입금시 잔액이 증가한다.
      • withdraw(int amount) : 출금 메서드
        • 출금시 잔액이 감소한다.
        • 만약 잔액이 부족하다면 잔액 부족을 출력하기
    • AccountMain 클래스를 만들고 main() 메서드를 통해 프로그램을 시작하기
      • 계좌에 10000원 입금.
      • 계좌에 9000원 출금.
      • 계좌에 2000원 출금.(잔액 부족이 출력되는지 확인)
      • 잔고 출력.
    public class Account {
        int balance;
        int amount;
        void deposit(int amount) {
            balance += amount;
        }
    
        void withdraw(int amount) {
            if (balance >= amount) {
                balance -= amount;
            } else {
                System.out.println("잔액이 부족합니다.");
            }
        }
    }
    public static void main(String[] args) {
        Account account = new Account();
        account.deposit(10000);
        account.withdraw(9000);
        account.withdraw(2000);
        System.out.println("잔액 : " + account.balance);
    }

     

    'JAVA' 카테고리의 다른 글

    JAVA - 패키지  (0) 2024.01.04
    JAVA - 생성자 + 연습 문제  (2) 2024.01.03
    JAVA - 객체 지향 프로그램  (1) 2024.01.03
    JAVA - 기본형과 참조형 - 연습 문제  (0) 2024.01.02
    JAVA - 기본형과 참조형  (0) 2024.01.02
Designed by Tistory.