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 ์๋์ ๋๋๋ค.");
}
}
-์ด๋ฐ์์ผ๋ก ๋ค๋ฅธ ํด๋์ค๋ค๋ ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
'Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ์๊ณ ๋ฆฌ์ฆ ์์ (์ ํ์ ๋ ฌ) (0) | 2021.02.20 |
---|---|
[Java] ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ (0) | 2021.02.20 |
[Java] final ์์ฝ์ด (0) | 2021.02.20 |
[Java] ํ ํ๋ฆฟ ๋ฉ์๋ ์์ฉํ๊ธฐ (0) | 2021.02.20 |
[Java] ํ ํ๋ฆฟ ๋ฉ์๋ (0) | 2021.02.20 |
๋๊ธ