Skip to content
Merged
Show file tree
Hide file tree
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
68 changes: 67 additions & 1 deletion src/main/java/coordinate/CoordinateLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,76 @@ public CoordinateLine(List<Integer> numbers) {
}
}

public double findLineLength() {
public double judgeDotCount() throws IllegalArgumentException {
if (points.size() == 2) // 두점의 길이 혹은 네점일 경우 직사각형의 넓이 값을 출력 하도록 반환한다. 그외는 예외 발생.
return findLength();
if (points.size() == 4 && isSquare())
return CalculatorSquare();
PrintView.dotCountReWrite();
throw new IllegalArgumentException();
}

public double findLength() {
return points.get(0).calculatorLength(points.get(1));
}


public Boolean isSquare() {
List<Double> lengthArr = oneLength(); //직사각형의 한 길이를 구해서 리스트에 담음
List<Double> anotherArr = anotherLength(); //그와 동일한 길이의 직사각형의 한 길이를 구해서 리스트에 담음
int count = 0;
for (int i = 0; i < lengthArr.size(); i++) {
if (lengthArr.get(i).equals(anotherArr.get(i)))
count++;
}
if (count == 3) // 세쌍의 길이가 서로 같으면 직삭각형이라고 판별한다.
return true;
return false;
}


private List<Double> oneLength() {
List<Double> lengthArr = new ArrayList<>();
for (int i = 1; i < points.size(); ++i) {
lengthArr.add(points.get(0).calculatorLength(points.get(i)));
}
return lengthArr;
}

private List<Double> anotherLength() {
List<Double> lengthArr = new ArrayList<>();
for (int i = 2; i >= 0; i--) {
if (i == 0) {
lengthArr.add(points.get(i + 1).calculatorLength(points.get(i + 2)));
continue;
}
lengthArr.add(points.get(points.size() - 1).calculatorLength(points.get(i)));
}
return lengthArr;
}

public int CalculatorSquare() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

메소드 이름 소문자???

int cube = 0;
List<Integer> intList = isInteger(oneLength());
if (intList.size() == 2)
cube = intList.get(0) * intList.get(1);
if (intList.size() == 3) { //세변 모두 (대각선포함) 정수일 경우 정렬한뒤 두번째만 길이를 뽑아서 곱함
Collections.sort(intList);
cube = intList.get(0) * intList.get(1);
}
return cube;
}

private List<Integer> isInteger(List<Double> lengthArr) { //한변의 길이가 정수면 정수타입의 리스트에 담음
List<Integer> intList = new ArrayList<>();
for (int i = 0; i < lengthArr.size(); i++) {
int IntegerNum = lengthArr.get(i).intValue();
if (IntegerNum == lengthArr.get(i))
intList.add(lengthArr.get(i).intValue());
}
return intList;
}

public List<Point> getPoints() {
return points;
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/coordinate/CoordinateMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

public class CoordinateMain {
public static void main(String[] args) {
try {
run();
} catch (Exception e) {
run();
while (true) {
try {
run();
break;
} catch (Exception e) {
continue;
}
}
}

Expand All @@ -19,6 +22,10 @@ public static void run() throws IllegalArgumentException {
PrintView.drawVertical(line.getPoints());
PrintView.drawRow();
PrintView.rowNumber();
PrintView.printLength(line.findLineLength());
if (numbers.size() == 4)
PrintView.printLength();
if (numbers.size() == 8)
PrintView.printCube();
System.out.println((int) line.judgeDotCount());
}
}
14 changes: 9 additions & 5 deletions src/main/java/coordinate/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ public class Point {
private int x;
private int y;

public Point(int x, int y)throws IllegalArgumentException {
public Point(int x, int y) throws IllegalArgumentException {
if (x < 0 || x > 24 || y < 0 || y > 24)
throw new IllegalArgumentException();
this.x = x;
this.y = y;
}

public double calculatorLength(Point point) {
return Math.sqrt(Math.pow(this.x - point.x, 2) + Math.pow(this.y - point.y, 2));
public double calculatorLength(Point points) {
return Math.sqrt(Math.pow(this.x - points.x, 2) + Math.pow(this.y - points.y, 2));
}

public Boolean isX(int x) {
return this.x * 3 == x;
if (this.x * 3 == x)
return true;
return false;
}

public Boolean isY(int y) {
return this.y == y;
if (this.y == y)
return true;
return false;
}
}
17 changes: 12 additions & 5 deletions src/main/java/coordinate/PrintView.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ public static void drawVertical(List<Point> points) {
System.out.printf(THREE_STRING, VERTICAL);
drawDotSpace(y, points);
System.out.println();

}
}

private static void drawDotSpace(int y, List<Point> points) {
for (int x = 1; x <= AXIS_MAX * ROW.length(); x++) {
if(drawDot(y, points, x))
if (drawDot(y, points, x))
System.out.print(BLANK);
}
}

private static Boolean drawDot(int y, List<Point> points, int x) {
String dot = "●";
for (Point point : points) {
if(point.isY(y) && point.isX(x)) {
if (point.isY(y) && point.isX(x)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

point.matchPoint(x, y);로 구현하는 것은 어떨까?

System.out.print(dot);
return false;
}
Expand All @@ -58,8 +57,16 @@ public static void rowNumber() {
System.out.println();
}

public static void printLength(double lineLength) {
public static void printLength() {
System.out.print("두 점 사이 거리는 ");
System.out.println(lineLength);
}

public static void printCube() {
System.out.print("사각형의 넓이는 ");
}

public static void dotCountReWrite() {
System.out.println("직사각형이 아니거나 점의 개수는 2개혹은 4개 이어야 합니다.");
}

}
15 changes: 0 additions & 15 deletions src/test/java/CoordinateTest/CoordinateLineTest.java

This file was deleted.

41 changes: 41 additions & 0 deletions src/test/java/coordinatetest/CoordinateLineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package coordinatetest;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class CoordinateLineTest {
@Test
public void isIntegerTest() {
int a = 4;
double b = Math.sqrt(16);
if (a == (int) b)
System.out.println("트루");
System.out.println("거짓");
System.out.println((int) Math.sqrt(16));
}

@Test
public void twoArrayListCompare() {
List<Double> list1 = new ArrayList<>();
List<Double> list2 = new ArrayList<>();

list1.add(1.0);
list1.add(2.0);
list1.add(3.0);
list1.add(4.0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Arrays.asList()를 사용하면 좀 더 깔끔하게 구현할 수 있음.


list2.add(1.0);
list2.add(2.0);
list2.add(3.0);
list2.add(4.0);

for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).equals(list2.get(i))) //제네릭이 Integer일때는 == 로 비교 가능하나 Double때는 equals로 비교가능함.
System.out.println(i + "번째 요소가 각각 같습니다.");
}
}


}