Задание 2. Периметр и площадь треугольника
This commit is contained in:
parent
1d39b148f8
commit
3f80ab9494
@ -1,5 +1,7 @@
|
||||
package tse;
|
||||
|
||||
import tse.lr1.Triangle;
|
||||
|
||||
/**
|
||||
* @author aNNiMON
|
||||
*/
|
||||
@ -13,7 +15,15 @@ public class LR_1 implements ILabRab {
|
||||
|
||||
@Override
|
||||
public void execute(int index) {
|
||||
System.out.println(TITLES[index]);
|
||||
switch(index) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
Triangle.main();
|
||||
break;
|
||||
case 2:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -39,4 +40,12 @@ public class Util {
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
public static double readDouble(Scanner sc) {
|
||||
while (!sc.hasNextDouble()) {
|
||||
System.out.println("Ошибка! Неверный формат!");
|
||||
sc.next();
|
||||
}
|
||||
return sc.nextDouble();
|
||||
}
|
||||
}
|
||||
|
58
src/tse/lr1/Triangle.java
Normal file
58
src/tse/lr1/Triangle.java
Normal file
@ -0,0 +1,58 @@
|
||||
package tse.lr1;
|
||||
|
||||
import java.awt.geom.Point2D;
|
||||
import java.util.Scanner;
|
||||
import tse.Util;
|
||||
|
||||
/**
|
||||
* Площадь и периметр треугольника.
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public class Triangle {
|
||||
|
||||
private final Scanner scanner;
|
||||
private Point2D pointA, pointB, pointC;
|
||||
|
||||
public static void main() {
|
||||
new Triangle().work();
|
||||
}
|
||||
|
||||
public Triangle() {
|
||||
scanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
private void work() {
|
||||
pointA = readPoint('A');
|
||||
pointB = readPoint('B');
|
||||
pointC = readPoint('C');
|
||||
double sideAB = calcSideLength(pointA, pointB);
|
||||
double sideBC = calcSideLength(pointB, pointC);
|
||||
double sideAC = calcSideLength(pointA, pointC);
|
||||
|
||||
double perimeter = sideAB + sideBC + sideAC;
|
||||
System.out.println("Периметр треугольника: " + perimeter);
|
||||
|
||||
double p = perimeter / 2;
|
||||
double square = Math.sqrt(p * (p - sideAB) * (p - sideBC) * (p - sideAC));
|
||||
System.out.println("Площадь треугольника: " + square);
|
||||
}
|
||||
|
||||
private double calcSideLength(Point2D pt1, Point2D pt2) {
|
||||
//double tempX = (pt2.getX() - pt1.getX());
|
||||
//double tempY = (pt2.getY() - pt1.getY());
|
||||
//return Math.sqrt(tempX * tempX + tempY * tempY);
|
||||
return pt1.distance(pt2);
|
||||
}
|
||||
|
||||
private Point2D readPoint(char symbol) {
|
||||
System.out.println("Введите координаты точки "+ symbol +":");
|
||||
return readCoodinates();
|
||||
}
|
||||
|
||||
private Point2D readCoodinates() {
|
||||
double x = Util.readDouble(scanner);
|
||||
double y = Util.readDouble(scanner);
|
||||
return new Point2D.Double(x, y);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user