프로그래밍 언어 복습/JAVA
#36 JAVA 자동차 오토바이 자전거 문제
사재원 교수
2022. 5. 25. 21:40
정답1 공개
package hihihi;
class Car{
String color;
String kind;
int maxspeed;
Car(String color,String kind,int maxspeed){
this.color = color;
this.kind = kind;
this.maxspeed = maxspeed;
}
void print() {
System.out.println(kind + color + " 자동차가 "+maxspeed + " 만큼 달립니다.");
}
}
class Bycle{
String kind;
String power;
Bycle(String kind,String power) {
this.kind = kind;
this.power = power;
}
void print() {
System.out.println(kind + " 자전거의 기능은 "+ power +" 입니다.");
}
}
class Ohto{
String kind;
String color;
int maxspeed;
Ohto(String kind,String color,int maxspeed){
this.kind = kind;
this.color = color;
this.maxspeed = maxspeed;
}
void pirnt(){
System.out.println(kind + color + " 오토바이가 "+maxspeed + " 만큼 달립니다.");
}
}
class Human1{
String name;
Car c1;
Car c2;
Human1(String name){
this.name = name;
}
}
class Human2{
String name;
Bycle b1;
Ohto o1;
Human2(String name){
this.name = name;
}
}
public class newproject {
public static void main(String[] args) {
//자동차 생성
Car c1 = new Car("은","Ford",220);
Car c2 = new Car("검정","BMW",240);
Car c3 = new Car("하얀","삼성",200);
Car c4 = new Car("형광","현대",180);
//자전거 생성
Bycle b1 = new Bycle("삼천리", "산악용");
Bycle b2 = new Bycle("자이언트", "접이식용");
//오토바이 생성
Ohto o1 = new Ohto("대림","빨간",180);
//사람1 생성
Human1 h1 = new Human1("사람1");
h1.c1 = c2;
h1.c2 = c4;
System.out.println(h1.name + " 소유");
h1.c1.print();
h1.c2.print();
//사람2 생성
Human2 h2 = new Human2("사람2");
h2.b1 = b1;
h2.o1 = o1;
System.out.println(h2.name + " 소유");
h2.b1.print();
h2.o1.pirnt();
}
}
정답2 공개 (여기서는 Scanner로 자신이 구매할 자동차 자전거 오토바이를 각각 대수를 입력할수있고 구매할때 색깔 제조사 등은 랜덤으로 정해진다)
package hihihi;
import java.util.Random;
import java.util.Scanner;
class Human{
int a;
int b;
int c;
Human(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
Car[] cr = new Car[a];
for(int i=0;i<cr.length;i++) {
cr[i] = new Car();
System.out.println((i+1) + "번 차 : " + cr[i].color + "," + cr[i].kind + "," + cr[i].speed);
}
By[] by = new By[b];
for(int i=0;i<by.length;i++) {
by[i] = new By();
System.out.println((i+1) + "번 자전거 : " + by[i].byhow + "," + by[i].bykind);
}
Ohto[] oh = new Ohto[c];
for(int i=0;i<oh.length;i++) {
oh[i] = new Ohto();
System.out.println((i+1) + "번 오토바이 : " + oh[i].ohcolor + "," + oh[i].ohhow +","+oh[i].ohspeed);
}
}
}
class Car{
Random r = new Random();
String color;
String kind;
int speed;
Car(){
String[] carcolor = {"은","검정","하얀","형광"};
String[] carkind = {"Ford","BMW","삼성","현대"};
int[] carspeed = {220,240,200,180};
this.color = carcolor[r.nextInt(carcolor.length)];
this.kind = carkind[r.nextInt(carkind.length)];
this.speed = carspeed[r.nextInt(carspeed.length)];
}
}
class By{
Random r = new Random();
String bykind;
String byhow;
By(){
String[] bykind = {"삼천리","자이언트"};
String[] bywhat = {"산악용","접이식용"};
this.bykind = bykind[r.nextInt(bykind.length)];
this.byhow = bywhat[r.nextInt(bywhat.length)];
}
}
class Ohto{
Random r = new Random();
String ohhow;
String ohcolor;
int ohspeed;
Ohto(){
String[] ohtocolor = {"빨간"};
String[] ohtokind = {"대림"};
int[] ohtospeed = {180};
this.ohcolor = ohtocolor[0];
this.ohhow = ohtokind[0];
this.ohspeed = ohtospeed[0];
}
}
public class newproject {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
//두사람 생성하기
Human[] h = new Human[2];
System.out.println("----첫번쨰 사람----");
int a1 = sc.nextInt();
int b1 = sc.nextInt();
int c1 = sc.nextInt();
h[0] = new Human(a1,b1,c1);
System.out.println("----두번쨰 사람----");
int a2 = sc.nextInt();
int b2 = sc.nextInt();
int c2 = sc.nextInt();
h[1] = new Human(a2,b2,c2);
}
}
감사합니다.