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
66 changes: 42 additions & 24 deletions src/main/java/coordinate/CoordinateLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,57 @@ public double judgeDotCount() throws IllegalArgumentException {
if (points.size() == 2) // 두점의 길이 혹은 네점일 경우 직사각형의 넓이 값을 출력 하도록 반환한다. 그외는 예외 발생.
return findLength();
if (points.size() == 4 && isSquare())
return CalculatorSquare();
return calculatorSquare();
if (points.size() == 3)
return Math.round(calculatorTriangle());
PrintView.dotCountReWrite();
throw new IllegalArgumentException();
}

private double calculatorTriangle() {
List<Double> lengths = oneLength();
lengths.add(points.get(1).calculatorLength(points.get(2)));
double sLength = 0;
double triangleLength = 1;
for (Double length : lengths) {
sLength += length;
}
sLength = sLength / 2;
for (Double length : lengths) {
triangleLength *= sLength - length;
}
return Math.sqrt(triangleLength * sLength);
}

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


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


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

private List<Double> anotherLength() {
private List<Double> matchLengthInSquare() {
List<Double> lengthArr = new ArrayList<>();
for (int i = 2; i >= 0; i--) {
if (i == 0) {
Expand All @@ -60,26 +78,26 @@ private List<Double> anotherLength() {
return lengthArr;
}

public int CalculatorSquare() {
public int calculatorSquare() {
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);
List<Integer> lengths = isInteger(oneLength());
if (lengths.size() == 2)
cube = lengths.get(0) * lengths.get(1);
if (lengths.size() == 3) { //세변 모두 (대각선포함) 정수일 경우 정렬한뒤 두번째만 길이를 뽑아서 곱함
Collections.sort(lengths);
cube = lengths.get(0) * lengths.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());
private List<Integer> isInteger(List<Double> lengths) { //한변의 길이가 정수면 정수타입의 리스트에 담음
List<Integer> intValues = new ArrayList<>();
for (Double length : lengths) {
int IntegerNum = length.intValue();
if (IntegerNum == length)
intValues.add(IntegerNum);
}
return intList;
return intValues;
}

public List<Point> getPoints() {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/coordinate/CoordinateMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public static void run() throws IllegalArgumentException {
PrintView.drawVertical(line.getPoints());
PrintView.drawRow();
PrintView.rowNumber();
if (numbers.size() == 4)
PrintView.printLength();
if (numbers.size() == 8)
PrintView.printCube();
System.out.println((int) line.judgeDotCount());
PrintView.printWhatIs(numbers.size()/2);
System.out.print(line.judgeDotCount());
}
}
12 changes: 2 additions & 10 deletions src/main/java/coordinate/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,7 @@ 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) {
if (this.x * 3 == x)
return true;
return false;
}

public Boolean isY(int y) {
if (this.y == y)
return true;
return false;
public Boolean matchPoint(int x, int y) {
return this.x*3 == x && this.y == y;
}
}
16 changes: 14 additions & 2 deletions src/main/java/coordinate/PrintView.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static void drawDotSpace(int y, List<Point> points) {
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.matchPoint(x, y)) {
System.out.print(dot);
return false;
}
Expand Down Expand Up @@ -65,8 +65,20 @@ public static void printCube() {
System.out.print("사각형의 넓이는 ");
}

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

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

public static void printWhatIs(int count){
if(count==2)
printLength();
if(count==3)
printTriangle();
if (count==4)
printCube();
}
}
24 changes: 12 additions & 12 deletions src/test/java/coordinatetest/CoordinateLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Test;

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

public class CoordinateLineTest {
Expand All @@ -18,18 +19,17 @@ public void isIntegerTest() {

@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);

list2.add(1.0);
list2.add(2.0);
list2.add(3.0);
list2.add(4.0);
Double[] doubleValue = new Double[]{1.0,2.0,3.0,4.0};

List list1 = Arrays.asList(doubleValue); //asList : 배열을 리스트로 변환 (꺼내올순 있어도 추가는 안된다.)
List list2 = Arrays.asList(doubleValue);
List<Double> list3 = new ArrayList<>(Arrays.asList(5.0,6.0));
System.out.println(list3);
System.out.println(list1);

for (int i = 0; i < list1.size(); i++) {
System.out.println(list1.get(i));
}

for (int i = 0; i < list1.size(); i++) {
if (list1.get(i).equals(list2.get(i))) //제네릭이 Integer일때는 == 로 비교 가능하나 Double때는 equals로 비교가능함.
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/coordinatetest/PrintViewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public void tripleForBreakContinueTest() {
}
}


@Test
public void downCasting(){
double a = 2.9; //double -> int 로 강제 형변환시 소수점은 버린다.
System.out.println((int)a);
}

}