22.11.18_모각코 5회차

2022. 11. 30. 21:00모각코

목표

- 객체지향프로그래밍 과제 10문제 풀기(과제 9)

내용

1. Order 클래스 구현하기

 
문제 설명

아래 조건에 맞게 클래스(Menu, Food, Juice, Order)를 구현 하세요.

특별한 언급이 없다면 접근 지정자는 public으로 설정 합니다.

Menu 클래스

멤버변수

아래와 같은 멤버변수를 가지며 접근 지정자는 protected로 설정 합니다.

  • int 타입의 price

생성자

  • Menu(int price) // 입력 받은 값으로 멤버변수 price를 초기화 합니다.
  • Menu() //price를 0으로 초기화 합니다.

멤버함수

  • getPrice() //멤버변수 price의 값을 리턴 합니다.
  • setPrice(int price) //입력받은 값으로 멤버변수 price의 값을 변경 합니다.

Food 클래스

클래스 Menu를 상속 받습니다.

멤버변수

아래와 같은 멤버변수를 추가로 가지며, 접근 지정자는 private으로 설정 합니다.

  • int 타입의 cookingTime

생성자

  • Food(int price, int cookingTime) // 입력받은 값으로 멤버변수 price, cookingTime을 초기화 합니다.
  • Food() // price, cookingTime의 값을 모두 0으로 초기화 합니다.

멤버함수

  • getCookingTime() //멤버변수 cookingTime의 값을 리턴 합니다.
  • setCookingTime(int cookingTime) //입력받은 값으로 멤버변수 cookingTime의 값을 변경 합니다.

Juice 클래스

클래스 Menu를 상속 받습니다.

멤버변수

아래와 같은 멤버변수를 추가로 가지며 접근 지정자는 private으로 설정 합니다.

  • String 타입의 flavor

생성자

  • Juice(int price, String flavor) // 입력받은 값으로 멤버변수 price, flavor를 초기화 해줍니다.
  • Juice() // price는 0으로, flavor는 문자열 "오렌지" 로 초기화 해줍니다.

멤버함수

  • getFlavor() // 멤버변수 flavor를 리턴 합니다.
  • setFlavor(String flavor) // 멤버변수 flavor를 입력 받은 문자열 로 변경 합니다.

Order 클래스

멤버변수

  • Menu[] 타입의 멤버변수 orderList 를 가집니다. 접근 지정자는 private으로 설정 해줍니다.

생성자

  • Order(Menu[] orderList) // 멤버변수 orederList를 입력 받은 배열로 초기화 합니다.
  • Order() // 멤버변수 orderList를 길이가 0인 배열로 초기화 합니다.

멤버함수

  • bill() // orderList에 들어있는 모든 객체를 가지고 메소드 getPrice()를 호출 하여, 그 값을 모두 더하여 리턴 합니다.
  • waitingTime() // orderList에 있는 Food 클래스 타입의 객체들만 가지고 메소드 getCookingTime()을 호출 하여, 그 값을 모두 더하여 리턴 합니다.
//조건에 맞게 아래 클래스들을 구현 합니다.적절히 코드를 수정 해야 합니다.
class Menu{
    protected int price;
    public Menu(int price) {
        this.price = price;
    }
    public Menu() {
        price = 0;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
} 
class Food extends Menu {
    private int cookingTime;
    public Food(int price, int cookingTime) {
        this.price = price;
        this.cookingTime = cookingTime;
    }
    public Food() {
        price = 0;
        cookingTime = 0;
    }
    public int getCookingTime() {
        return cookingTime;
    }
    public void setCookingTime(int cookingTime) {
        this.cookingTime = cookingTime;
    }
}
class Juice extends Menu {
    private String flavor;
    public Juice(int price, String flavor) {
        this.price = price;
        this.flavor = flavor;
    }
    public Juice() {
        price = 0;
        flavor = "오렌지";
    }
    public String getFlavor() {
        return flavor;
    }
    public void setFlavor(String flavor) {
        this.flavor = flavor;
    }
}
class Order{
    private Menu[] orderList;
    public Order(Menu[] orderList) {
        this.orderList = orderList;
    }
    public Order() {
        orderList = new Menu[0];
    }
    public int bill() {
        int sum=0;
        for (Menu m:orderList) {
            sum += m.getPrice();
        }
        return sum;
    }
    public int waitingTime() {
        int sum=0;
        for (Menu m:orderList) {
            if (m instanceof Food) {
                sum += ((Food)m).getCookingTime();
            }
        }
        return sum;
    }
}

//아래 코드는 수정하지 마세요
class Solution {
    public int solution(int param0) {
        int answer = 0;
        return answer;
    }
}

9. 대문자는 소문자로, 소문자는 대문자로 바꾸기

 
문제 설명

함수의 인자값으로 문자열 str이 주어진다. 이때, 대문자는 소문자로, 소문자는 대문자로 바꾸어 리턴하는 함수를 구현하시오.

class Solution {
    public String solution(String str) {
        String answer = "";
        for (int i=0; i<str.length(); i++) {
            if(Character.isLowerCase(str.charAt(i))) {
        	    answer += Character.toUpperCase(str.charAt(i));
            } else {
                answer += Character.toLowerCase(str.charAt(i));
            }
        }
        return answer;
    }
}

10. 암호를 만들어라!

 

문제 설명

  • 문자열을 입력받아 각 문자를 아스키코드로 변환하여 배열에 담아 리턴한다.

입력

  • 문자열 plaintext 이 주어진다.

결과값

  • 입력받은 문자열의 각 문자를 아스키코드로 변환하고, 이를 배열에 담아 리턴한다.

입출력 예
|입력값|결과값|
|----|----|
|"hello"|[104, 101, 108, 108, 111]|

입출력 예 설명
주어진 문자열의 각자리 아스키 코드 값을 구하면 [104, 101, 108, 108, 111] 이다.
char a = 'a';
(int)a // 97

class Solution {
    public int[] solution(String plaintext) {
        int[] answer = new int[plaintext.length()];
        for (int i=0; i<plaintext.length(); i++) {
            answer[i] = plaintext.charAt(i);
        }
        return answer;
    }
}

회고

- 상속과 업캐스팅에 대해 이해할 수 있었다.

- isLowerCase 함수의 존재를 알았다.

- 문자는 때에 따라 자동으로 아스키 코드로 변환되어 숫자로 들어가나 보다.

'모각코' 카테고리의 다른 글

22.11.25_모각코 6회차  (1) 2022.11.30
22.11.11_모각코 4회차  (0) 2022.11.30
22.11.04_모각코 3회차  (0) 2022.11.07
22.10.07_모각코 2회차  (0) 2022.10.10
22.09.30_모각코 1회차  (1) 2022.09.30