This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
5주차 과제입니다. #96
Open
Sinya47
wants to merge
1
commit into
SoongSilComputingClub:Sinya47
Choose a base branch
from
Sinya47:main
base: Sinya47
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
5주차 과제입니다. #96
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
|
||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하지 않는 getter는 삭제해주세요. |
||
return move; | ||
} | ||
|
||
public void setMove(int move) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기본생성자를 만들지 않고 super car 클래스에서 바로 super로 car의 생성자를 호출해주세요