๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] Car ์ถ”์ƒํด๋ž˜์Šค + ์‘์šฉ

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 20.
๋ฐ˜์‘ํ˜•

Q5) Car ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” Sonata, Avante, Grandeur, Genesis ํด๋ž˜์Šค๊ฐ€ ์žˆ๋‹ค. ๊ฐ ์ฐจ๋Š” ์ฃผํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ์ˆœ์„œ๋กœ ์›€์ง์ธ๋‹ค.

run( ) ๋ฉ”์„œ๋“œ๋Š” ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ๋‹ค์Œ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก Car, Sonata, Avante, Grandeur, Genesis ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ผ.

๊ฒฐ๊ณผ๋ฌธ์ด ์ด๋ ‡๊ฒŒ ๋‚˜์˜ค๊ฒŒ...

 

A)

๋”๋ณด๊ธฐ
package chapter9.q5;

public abstract class Car {
	
	public abstract void start();
	public abstract void drive();
	public abstract void stop();
	public abstract void turnOff();
	
	public void run()
	{
		start();
		drive();
		stop();
		turnOff();
	}
    
}

 

package chapter9.q5;

public class Sonata extends Car{

	@Override
	public void start() {
		System.out.println("Sonata ์‹œ๋™์„ ์ผญ๋‹ˆ๋‹ค.");
	}

	@Override
	public void drive() {
		System.out.println("Sonata ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");		
	}

	@Override
	public void stop() {
		System.out.println("Sonata ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
	}

	@Override
	public void turnOff() {
		System.out.println("Sonata ์‹œ๋™์„ ๋•๋‹ˆ๋‹ค.");
	}

}

 

package chapter9.q5;

public class Avante extends Car{
	
	@Override
	public void start() {
		System.out.println("Avante ์‹œ๋™์„ ์ผญ๋‹ˆ๋‹ค.");
	}

	@Override
	public void drive() {
		System.out.println("Avante ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");		
	}

	@Override
	public void stop() {
		System.out.println("Avante ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
	}

	@Override
	public void turnOff() {
		System.out.println("Avante ์‹œ๋™์„ ๋•๋‹ˆ๋‹ค.");
	}

}

 

package chapter9.q5;

public class Grandeur extends Car{
	
	@Override
	public void start() {
		System.out.println("Grandeur ์‹œ๋™์„ ์ผญ๋‹ˆ๋‹ค.");
	}

	@Override
	public void drive() {
		System.out.println("Grandeur ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");		
	}

	@Override
	public void stop() {
		System.out.println("Grandeur ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
	}

	@Override
	public void turnOff() {
		System.out.println("Grandeur ์‹œ๋™์„ ๋•๋‹ˆ๋‹ค.");
	}

}

 

package chapter9.q5;

public class Genesis extends Car{
	
	@Override
	public void start() {
		System.out.println("Genesis ์‹œ๋™์„ ์ผญ๋‹ˆ๋‹ค.");
	}

	@Override
	public void drive() {
		System.out.println("Genesis ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");		
	}

	@Override
	public void stop() {
		System.out.println("Genesis ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
	}

	@Override
	public void turnOff() {
		System.out.println("Genesis ์‹œ๋™์„ ๋•๋‹ˆ๋‹ค.");
	}
    
}

 

package chapter9.q5;

import java.util.ArrayList;

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

		ArrayList<Car> carList = new ArrayList<Car>();
		
		carList.add(new Sonata());
		carList.add(new Grandeur());
		carList.add(new Avante());
		carList.add(new Genesis());
		
		for(Car car : carList) {
			car.run();
			System.out.println("======================");
		}
        
	}   
}

 

 

 

Q6) Q5์—์„œ ๊ตฌํ˜„ํ•œ ์ฐจ๋Š” ๋ชจ๋‘ ๊ณตํ†ต์œผ๋กœ washCar( ) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฐจ๋ฅผ ์ฃผํ–‰ํ•œ ํ›„ ์„ธ์ฐจ๋ฅผ ํ•˜๋„๋ก ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•ด ๋ณด๋ผ. ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

โ€‹โ€‹

A)

๋”๋ณด๊ธฐ
package chapter9.q5;

public abstract class Car {
	
	public abstract void start();
	public abstract void drive();
	public abstract void stop();
	public abstract void turnOff();
	
	public void washCar() {
		System.out.println("์„ธ์ฐจ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค");
	}

	public void run() {
		start();
		drive();
		stop();
		turnOff();
		washCar();
	}
    
}

 

 

 

์‘์šฉ๋ฌธ์ œ) Car ์ด๋ฆ„์„ ์ถœ๋ ฅ๋ฌธ์— ์ง์ ‘ ์ž…๋ ฅํ•˜์ง€์•Š๊ณ  ์ด๋ฆ„๋งŒ์œผ๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ž‘์„ฑํ•ด๋ณด๋ผ.

์กฐ๊ฑด. ์ถœ๋ ฅ ํด๋ž˜์Šค๋Š” ๊ทธ๋Œ€๋กœ ๋‘˜ ๊ฒƒ.

 

A)

๋”๋ณด๊ธฐ
package chapter9.q5;

public abstract class Car {
	
	public abstract void start();
	public abstract void drive();
	public abstract void stop();
	public abstract void turnOff();

    public void start(String car) {
        System.out.println(car + " ์‹œ๋™์„ ์ผญ๋‹ˆ๋‹ค.");
    }

    public void drive(String car) {
        System.out.println(car + " ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");
    }

    public void stop(String car) {
        System.out.println(car + " ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
    }

    public void turnOff(String car) {
        System.out.println(car + " ์‹œ๋™์„ ๋•๋‹ˆ๋‹ค.");
    }
	
	public void washCar() {
        System.out.println("์„ธ์ฐจ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.");
	}

	final public void run() {
		start();
		drive();
		stop();
		turnOff();
		washCar();
	}
    
}

 

 

package chapter9.q5;

public class Sonata extends Car{

	@Override
	public void start() {
        start("Sonata");
//		System.out.println("Sonata ์‹œ๋™์„ ์ผญ๋‹ˆ๋‹ค.");
	}

	@Override
	public void drive() {
        drive("Sonata");
//		System.out.println("Sonata ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.");		
	}

	@Override
	public void stop() {
        stop("Sonata");
//		System.out.println("Sonata ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.");
	}

	@Override
	public void turnOff() {
        turnOff("Sonata");
//		System.out.println("Sonata ์‹œ๋™์„ ๋•๋‹ˆ๋‹ค.");
	}

}

-์ด๋Ÿฐ์‹์œผ๋กœ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋“ค๋„ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€