Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

5주차 과제입니다. #96

Open
wants to merge 1 commit into
base: Sinya47
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions Week05.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package For_Exam;

import java.util.Random;
import java.util.Scanner;

class Car {
protected int speed;
protected String name;
protected int move;
protected int second;

public Car() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기본생성자를 만들지 않고 super car 클래스에서 바로 super로 car의 생성자를 호출해주세요

}

public Car(int speed, String name) {
this.speed = speed;
this.name = name;
}

public String getName() {
return name;
}

public int getSpeed() {
return speed;
}

public int getMove() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용하지 않는 getter는 삭제해주세요.

return move;
}

public void setMove(int move) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용하지 않는 setter는 삭제해주세요.

this.move = move;
}

public int getSecond() {
return second;
}

public void setSecond(int second) {
this.second = second;
}

public void go() {
long seed = System.currentTimeMillis();
Random random = new Random(seed);
for (int i = 0; i < second; i++) {
if (random.nextInt(2) == 1) {
move += speed;
}
}
}

public void sayScore() {

System.out.printf("score: %d\n", move);

}

}

class SuperCar extends Car {
private long seed = System.currentTimeMillis();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seed는 random 객체를 초기화할때 단 한번만 사용되므로, 클래스의 멤버일 필요가 없습니다. 생성자 내부에서 로컬 변수로 선언한 뒤 Random을 만들때만 사용하면 됩니다.

private Random random = new Random(seed);
private Random random2 = new Random(seed);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

random의 seed가 현재시간이므로 객체를 하나만 만들어서 사용해도 됩니다. 2개를 만드는건 비효율적입니다.

private int count;

public SuperCar(int speed, String name) {
this.speed = speed;
this.name = name;
}

public void go() {
for (int i = 0; i < getSecond(); i++) {
if (random.nextInt(2) == 1) {
move += speed;
if (random2.nextInt(2) == 1) {
move += speed;
count++;
}
}
}
}

public void sayScore() {
System.out.printf("score: %d, booster: %d\n", move, count);
}

}

public class Week04 {
public static void main(String[] args) {
int speed;
String name;
int second;
int isSuper = 0;

Scanner scanner = new Scanner(System.in);
int N;
System.out.println("자동차의 갯수를 입력하세요.");
N = scanner.nextInt();

Car[] cars = new Car[N];
int count = 0;
for (int i = 0; i < cars.length; i++) {
System.out.printf("%d번 째 자동차의 스피드를 입력하세요.\n", count + 1);
speed = scanner.nextInt();
System.out.printf("%d번 째 자동차의 이름을 입력하세요.\n", count + 1);
name = scanner.next();
System.out.println("이 자동차는 슈퍼카인가요? 0 또는 1 입력");
isSuper = scanner.nextInt();
if (isSuper == 0)
cars[i] = new Car(speed, name);
else if (isSuper == 1)
cars[i] = new SuperCar(speed, name);

}

System.out.println("-----경기 참가자 소개-----");
for (int i = 0; i < cars.length; i++) {
System.out.printf("스피드는 %d이고, 이름은 %s입니다.\n", cars[i].getSpeed(), cars[i].getName());
}
System.out.println();
System.out.println("경기를 몇 초 동안 진행할까요?");
second = scanner.nextInt();

for (int i = 0; i < cars.length; i++) {
cars[i].setSecond(second);
cars[i].go();
}
System.out.println();
System.out.println("---최종 결과 발표---");
for (int i = 0; i < cars.length; i++) {
cars[i].sayScore();
}

scanner.close();
}
}