Skip to content
Rey edited this page Mar 3, 2025 · 1 revision

Answer

아래코드와 반드시 동일해야하만 정답은 아닙니다. 문제를 만들면서 생각했던 방향성일뿐 해결하는 방식은 다양할 수 있으니, 참고만 해주시면 좋겠습니다.

  • Object

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (this == obj)
            return true;
        if (obj instanceof Person) {
            return ((Person) obj).getId().equals(this.getId());
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
  • Collection

    • Comparable

      @Override
      public int compareTo(Car o) {
          // TODO Auto-generated method stub
          int modelCompare = this.getModel().compareTo(o.getModel());
          if (modelCompare != 0) {
              return modelCompare;
          }
      
          int priceCompare = o.getPrice()- this.getPrice();
          if (priceCompare != 0) {
              return priceCompare;
          }
      
          return this.getSpeed()- o.getSpeed();
      }
    • Comparator

      PriorityQueue<Car> cars = new PriorityQueue<>((c1, c2) -> {
          int priceCompare = c2.getPrice() - c1.getPrice();
          if (priceCompare != 0) {
              return priceCompare;
          }
      
          int modelCompare = c2.getModel().compareTo(c1.getModel());
          if (modelCompare != 0) {
              return modelCompare;
          }
      
          return c1.getSpeed() - c2.getSpeed();
      });
  • Exception

    • checkAge

      public void checkAge(int age) throws Exception {
          if (age < 18) {
              throw new InvalidAgeException("나이는 18세 이상이어야 합니다.");
          }
          System.out.println("나이 검증 통과!");
      }
    • checkAgeResult

      public void checkAgeResult(int age) {
          try {
              checkAge(age);
          } catch (Exception e) {
              // Code
          }
      }
    • unchecked

      public void divide(int n) {
          try {
              int i = 10 / n;
          } catch (Exception e) {
      
          }
      }
  • File IO

    • Book

      public class Book implements Serializable {
          private String title;
          private String author;
          private String isbn;
          private int price;
          private transient String ssn;
      }

Clone this wiki locally