-
Notifications
You must be signed in to change notification settings - Fork 92
[2단계 - 자동차경주] 이지현 미션 제출합니다. #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import java.util.ArrayList; | ||
|
||
public class CarWinner { | ||
private Car[] cars; //참가한 자동차들 | ||
Comment on lines
+3
to
+4
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. 컬렉션을 Collection을 감싸면서 그 외 다른 멤버 변수가 없는 상태를 일급 컬렉션이라고 합니다! 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. Cars
로 일급컬렉션을 구현했습니다. 테스트에서는 Cars를 주입한 뒤, moveAll()과 getWinnerCount() / getWinnerNames()를 호출하는 방식으로 |
||
private int winnerPosition; //우승 자동차의 위치(최대값) | ||
private int winnerCnt; //우승 자동차의 수 | ||
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. cnt 와 같이 축약된 변수명은 지양해보는게 어떨까요~? 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. 넵 수정하겠습니다!! 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. +) 이 부분 반영이 안된거같아요~! |
||
private ArrayList<String> winnerNames; //우승 자동차의 이름 | ||
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. List로 타입을 선언하는것과 ArrayList로 타입을 선언하는 것 어떤 차이가 있을까요? 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.
List는 인터페이스이고 ArrayList는 그 구현 클래스입니다. 단점으로는 List로 선언했을 때 ArrayList의 고유 메서드(ensureCapacity() 등)를 직접 사용할 수 없다는 점이 있지만, 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. 우승 자동차의 위치를 알고싶다면 어떻게 할 수 있을까요? 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. 답변일급컬렉션을 사용한다면 변수로 따로 저장하지 않고 메소드에서 뽑아내면 됩니다. 추가 질문일급컬렉션에 대해 알아보다가
이 질문에 지금 요구사항에서는 결과를 저장하지 않아서 Winner 객체가 굳이 필요없는 걸까요? 그런데 여기서 약간 고민이 되는 건, 그리고 뭔가 수정한 1급 객체인 cars가 책임이 아직도 섞여있다는 생각이 들고 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. 정말 공감되는 고민거리네요 ㅎㅎㅎㅎ 저도 이렇게 누구의 책임이지..? 하는 고민이 자주 드는 것 같아요 저는 이 경기를 보는 관중의 입장에서 자동차 경주가 어떻게 진행되고, 우승자는 누가 결정해주지? 와 같은 고민을 하게 될 것 같아요! +) 살짝 부끄럽지만 약 3년전에 제가 자판기라는 미션을 했을 때 생각했던 사고의 흐름을 참고차 남겨봅니다 |
||
|
||
public CarWinner(Car[] cars) { | ||
this.cars = cars; | ||
winnerCnt = 0; | ||
winnerPosition = Integer.MIN_VALUE; | ||
winnerNames = new ArrayList<>(); | ||
} | ||
|
||
public int getWinnerCnt() { | ||
return winnerCnt; | ||
} | ||
|
||
public int getWinnerPosition() { | ||
return winnerPosition; | ||
} | ||
|
||
public ArrayList<String> getWinnerName() { | ||
return winnerNames; | ||
} | ||
|
||
public void whichWinner(int testRounds){ | ||
//1. 모든 자동차를 주어진 횟수만큼 이동 | ||
moveAllCars(testRounds); | ||
//2. 우승자의 수와 자동차의 위치(최대값) 계산 | ||
countWinners(); | ||
//3. 우승자 이름 저장 | ||
saveWinnerName(); | ||
} | ||
Comment on lines
+28
to
+35
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. whichWinner 라는 메소드명은 어떤 의미를 나타내나요? 현재 메소드는 어떤 역할을 수행하고있나요? 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. whichWinner는 어떤 자동차가 우승하는지를 구하는 메소드명인데 너무 함축해서 메소드명을 쓴 것 같네요. |
||
|
||
//1. 모든 자동차를 주어진 횟수만큼 이동 | ||
private void moveAllCars(int testRounds){ | ||
for (Car car : cars) { | ||
for (int i = 0; i < testRounds; i++) { | ||
car.move(RandomUtil.randomGenerator()); | ||
} | ||
} | ||
} | ||
|
||
//2. 우승자의 수와 자동차의 위치(최대값) 계산 | ||
private void countWinners(){ | ||
for (Car car : cars) { | ||
updateWinnerCount(car); | ||
} | ||
} | ||
|
||
//2-1. 자동차 한 대씩 우승자의 수와 최고 위치 갱신 | ||
private void updateWinnerCount(Car car) { | ||
int position = car.getPosition(); | ||
if (position > winnerPosition) { | ||
winnerCnt = 1; | ||
winnerPosition = position; | ||
return; | ||
} | ||
if (position == winnerPosition) { | ||
winnerCnt++; | ||
} | ||
} | ||
|
||
//3. 우승자 이름 저장 | ||
private void saveWinnerName(){ | ||
for (Car car : cars) { | ||
if (car.getPosition() == winnerPosition) { | ||
winnerNames.add(car.getName()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class Cars { | ||
//자동차 경주에 참가하는 자동차들의 집합 | ||
private List<Car> cars; | ||
|
||
public Cars(List<Car> cars) { | ||
this.cars = cars; | ||
} | ||
|
||
//자동차 이동 | ||
public void moveAll(int testRounds) { | ||
for (Car car : cars) { | ||
IntStream.range(0, testRounds) | ||
.forEach(i -> car.move(RandomUtil.randomGenerator())); | ||
} | ||
} | ||
|
||
//이동된 자동차들의 최대위치 구하기 | ||
private int getMaxPosition() { | ||
return cars.stream() | ||
.mapToInt(Car::getPosition) | ||
.max() | ||
.orElse(0); | ||
} | ||
|
||
//우승 자동차 목록 | ||
public List<Car> getWinners() { | ||
int max = getMaxPosition(); | ||
return cars.stream() | ||
.filter(car -> car.getPosition() == max) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
//우승 자동차 이름 목록 | ||
public List<String> getWinnerNames() { | ||
return getWinners().stream() | ||
.map(Car::getName) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
//우승 자동차의 수 | ||
public int getWinnerCount() { | ||
return getWinners().size(); | ||
} | ||
|
||
} |
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.
java에서는 collection 사용이 권장되곤 하는데요? collection은 어떤 장점이 있을까요?
키워드 : jcf
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.
처음에 JCF가 어떤 약자인지 몰라서 헷갈렸는데, Java Collections Framework였군요!
List는 크기가 가변적인 데이터에 좋고, 인터페이스 기반의 설계가 되어있어 코드를 유연하게 작성할 수 있습니다!
배열보다는 List를 사용해야겠네요!
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.
추가적으로 list에서 제공해주는 기능이 편리하다는 부분도 하나의 큰 장점으로 다가오기도 할 것 같아요 ㅎㅎ
+) List로 반영이 되지 않은 것 같은데 확인 부탁드려요~!