본문 바로가기
공부/과제.

JAVA 문제풀이 17

by 햄이; 2021. 7. 2.

 

Q1. 객체지향 개념 연습 문제

 

package Day13;
/*
 * Q.30
 * 자동차는 색깔과 제조사 최고속도를 가지고 있고 운전을 하는 기능이 있다.
 * 운전을 할 때 "차는 XXX색이고, 제조사는 XXX이며 최고속도는 XXX입니다. "
 * 자전거는 제조사와 어떤기능을 가지고 있고 타다라고 하는 기능이 있습니다.
 * 오토바이는 색깔,제조사,최고속도를 가지고있고 운전을 하는 기능이 있습니다.
 * 
 * 사용자 한명은 자동차를 두 대 가질 수 있고, 다른 사용자는 자전거와 오토바이를 가질 수 있습니다.
 * (단, 객체지향 개념으로 이해해서 만들어야하고 두명의 사용자는 쓸데 없는 것을 가질 수 없습니다.)
 * 자동차 1 => 은, Ford,220
 * 자동차 2 => 검정, BMW, 240
 * 자동차3 => 하얀 , 삼성, 200
 * 자동차4 => 형광 , 현대, 180
 * 
 * 자전거 1 => 삼천리, 산악용
 * 자전거 2 => 자이언트, 접이식용
 * 
 * 오토바이 => 대림, 빨간, 180
 * 
 * 사용자 1 => 자동차 2,자동차 4번을 가지고 있고 2,4를 운전합니다.
 * 사용자 2 => 자전거1, 오토바이를 가지고 있습니다. 그리고 자전거를 타고 오토바이를 운전합니다.
 * */


class Car{
	String color;
	String production;
	int maxSpeed;

	public void drive() {
		System.out.println("차는 " + color +"색이고, 제조사는 "+production+"이며,
        최고속도는 "+maxSpeed+"입니다.");
	}
}

class Bicycle{
	String production;
	String function;

	public void ride() {
		System.out.println("자전거의 제조사는 "+production+"이며,"+function+"자전거입니다.");
	}
}

class Motorcycle{
	String color;
	String production;
	int maxSpeed;
	public void drive() {
		System.out.println("오토바이는 "+color+"색이고, 제조사는 "+production+"이며,
        최고속도는 "+maxSpeed+"입니다.");
	}
}

class User1{
	public User1(Car car2, Car car4) {
		// TODO Auto-generated constructor stub
		System.out.println("사용자1");
		car2.drive();
		car4.drive();
		System.out.println();
	}
}

class User2{
	public User2(Bicycle bicycle1, Motorcycle motorcycle) {
		// TODO Auto-generated constructor stub
		System.out.println("사용자 2");
		bicycle1.ride();
		motorcycle.drive();
	}
}
public class Question1 {

	public static void main(String[] args) {

		Car car1 = new Car();
		Car car2 = new Car();
		Car car3 = new Car();
		Car car4 = new Car();

		Bicycle bicycle1 = new Bicycle();
		Bicycle bicycle2 = new Bicycle();

		Motorcycle motorcycle= new Motorcycle();


		car1.color="은";
		car1.production ="Ford";
		car1.maxSpeed =220;

		car2.color="검정";
		car2.production ="BMW";
		car2.maxSpeed =240;

		car3.color="하얀";
		car3.production ="삼성";
		car3.maxSpeed =200;

		car4.color="형광";
		car4.production ="현대";
		car4.maxSpeed =180;

		bicycle1.production ="삼천리";
		bicycle1.function ="산약용";

		bicycle2.production ="자이언트";
		bicycle2.function ="접이식용";

		motorcycle.color ="빨간";
		motorcycle.maxSpeed =180;
		motorcycle.production="대림";

		User1 user1 = new User1(car2, car4);
		User2 user2 = new User2(bicycle1, motorcycle);
	}
}

 

결과

 

 

Q2. 객체지향 개념 연습 문제 2

 

package Day13;

/*
 * Q.31
 * 4마리의 동물
 * 원숭이, 개, 닭, 돼지가 있고 각각 바나나, 뼈다귀, 모이 , 여물의 먹이를 가지고 있습니다.
 * 종류와 먹이를 말하는 기능으로 1회 동작 시키고
 * 먹이를 각각 한 칸 씩 이동을 합니다.
 * 그 이후에 종류와 먹이를 말하는 기능을 동작시켜주세요.*/

class Animal{
	String animal;
	String food;
	public String getFood() {
		return food;
	}
	public void setFood(String food) {
		this.food = food;
	}
	public void showAnimal() {
		System.out.println(animal+"의 먹이는 "+food+"입니다.");
	}
}

public class Question2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Animal animal1 = new Animal();
		Animal animal2 = new Animal();
		Animal animal3 = new Animal();
		Animal animal4 = new Animal();
		
		animal1.animal ="원숭이";
		animal1.food ="바나나";

		animal2.animal = "개";
		animal2.food ="뼈다귀";
		
		animal3.animal ="닭";
		animal3.food ="모이";
		
		animal4.animal ="돼지";
		animal4.food= "여물";
		
		animal1.showAnimal();
		animal2.showAnimal();
		animal3.showAnimal();
		animal4.showAnimal();
		System.out.println();
		
		String temp = animal4.getFood();
		animal4.setFood(animal3.getFood());
		animal3.setFood(animal2.getFood());
		animal2.setFood(animal1.getFood());
		animal1.setFood(temp);
		
		animal1.showAnimal();
		animal2.showAnimal();
		animal3.showAnimal();
		animal4.showAnimal();
	
	}
}

 

결과

 

그동안 노션 업데이트하느라

밀린 문제 싹 업데이트하는 중~

참고로 앞에 업로드한 기존 문제들과

크게 달라진 게 없을 시 건너뛰고 업로드할 예정이다.

'공부 > 과제.' 카테고리의 다른 글

JAVA 문제풀이 18  (0) 2021.07.02
JAVA 문제풀이 16  (0) 2021.05.07
JAVA 문제풀이 15  (0) 2021.05.07
JAVA 문제풀이 14  (0) 2021.05.07
JAVA 문제풀이 13 배열 낚시 게임  (0) 2021.04.28

댓글