• Home
  • About
    • ming photo

      ming

      studying

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Archive
    • All Posts
    • All Tags
    • All categories
  • categories
    • HTML+CSS+JavaScript
    • JAVA
    • Algorithm
    • DB
    • JSP
    • 정보처리기사
    • Spring
    • Thymeleaf
    • 기술면접
  • Projects

JAVA-수업중 과제-1(심화) : has-a 관계를 활용한 자동차 조립 공정

21 Feb 2021

심화문제1

심화문제_작성요령

심화문제_출력

1) Car 인터페이스 생성

  • 코드
      package Car_goodee;
    
      public interface Car {
            
          public void combineEngine(); // 엔진조립 메소드 생성
            
          public void combineBody(); // 차체조립 메소드 생성
    
          public void combineWheel(); // 바퀴조립 메소드 생성
    
          public void complateCar(); // 차 조립 완성 메소드 생성
    
      }
    

2) Car 인터페이스 구현 CarImpl 클래스 작성

  • 코드
      package Car_goodee;
    
      import Car_goodee.Body;
      import Car_goodee.Engine;
      import Car_goodee.Wheel;
    
      public class CarImpl implements Car {
            
          // has - a 관계(구현(implements))  : 클래스의 소속변수로 다른 클래스의 객체를 사용하는 것
          // 1) 어떤 객체가 다른 객체에 포함되는 관계
          // 2)  대부분 클래스들의 관계는 has-a 관계가 적용된다
          // - 집합 관계 : 어떤 객체가 다른 객체에 포함될 때 해당 객체가 없어도 동작에 문제가 없는 관계 => 객체 상호간의 라이프 사이클이 다른 관계
          // ex) 자동차 has a 라디오 → 자동차는 라디오가 없어도 자동차
          // 컴퓨터 has a 스피커 → 컴퓨터는 스피커가 없어도 컴퓨터
          // - 구성 관계 : 객체가 다른 객체에 포함될 때 해당 객체 없이는 동작이 불가능한 관계 => 객체 상호간의 라이프 사이클이 동일한 관계
          // ex) 자동차 has a 엔진, 컴퓨터 has a CUP
          Body body;
          Wheel wheel;
          Engine engine;
            
          public CarImpl() {} // 기본생성자 생성
            
          public CarImpl(Body body,
                          Engine engine,
                          Wheel wheel) { // 오버로딩 생성자 생성
              this.body = body;
              this.engine = engine;
              this.wheel =wheel;
                
                    
          }
          @Override
          public void combineEngine() { // engine조립 오버로딩 메소드 
              engine.combine(); // engine의 comboine 메소드 사용
          }
    
          @Override
          public void combineBody() { // body조립 오버로딩 메소드 
              body.combine(); // body의 comboine 메소드 사용
          }
    
          @Override
          public void combineWheel() { // wheel조립 오버로딩 메소드 
              wheel.combine(); // wheel의 comboine 메소드 사용
          }
    
          @Override
          public void complateCar() { // 차량완성 오버로딩 메소드 
              this.combineBody(); // 위에 정의된 메소드 가리킴
              this.combineEngine(); // 위에 정의된 메소드 가리킴
              this.combineWheel(); // 위에 정의된 메소드 가리킴
              System.out.println("차량 조립 완료!"); 
          }
    
      }
    

3) 부속품에 해당하는 Body,Wheel,Engine 클래스 작성

  • Body 코드
      package Car_goodee;
    
      public class Body {
            
          // 필드 선언
          // 필드: 객체 고유의 데이터, 객체가 가져야 할 부품, 객체의 현재 상태 데이터를 저장하는 곳
    
          String bodyName; //  문자열 타입의 bodyName 타입 필드 선언
            
          Body(String bodyName) { // 기본 생성자 생성
              this.bodyName = bodyName;
          }
    
          public void combine() { // 조립 메소드 생성
              System.out.println(bodyName + "차체를 결합합니다");
          }
      }
    
  • Wheel 코드
      package Car_goodee;
    
    
      public class Wheel {
    
          String wheelName; //  문자열 타입의 wheelName 타입 필드 선언
            
          Wheel(String wheelName){	// 기본 생성자 생성	  
              this.wheelName = wheelName; 	
          }
    
          public void combine() {	// 조립 메소드 생성
              System.out.println(wheelName + "바퀴를 결합합니다");
          }
            
      }
    
  • Engine 코드
      package Car_goodee;
    
      public class Engine {
            
          String engineName; //  문자열 타입의 wheelName 타입 필드 선언
            
          Engine (String engineName){ // 기본 생성자 생성	  
              this.engineName = engineName;
                
          }
    
          public void combine() { // 조립 메소드 생성
              System.out.println(engineName + "엔진을 결합합니다");
          }
      }
    

4) 테스트 클래스 작성 및 출력

```java
package Car_goodee;

import Car_goodee.Body;
import Car_goodee.Engine;
import Car_goodee.Wheel;
import Car_goodee.CarImpl;
import Car_goodee.Car;

public class Assembling  {

    public static void main(String[] args) {
        
        // CarImpl 이용 객체 car 생성(선언) 및 매개변수 입력
        Car car = new CarImpl(new Body("모하비 "),
                new Engine("하이브리드 "), new Wheel("스파이크 타이어 "));
                car.complateCar(); // CarImpl클래스의 complateCar메소드 호출

    }

}
``` ### ▶ 출력 결과창(Console) ![Car_과제](https://user-images.githubusercontent.com/65608960/108615517-5a840180-7448-11eb-9ba0-14a5b1e7bb62.JPG)


Share Tweet +1