Verified Commit 855a1392 authored by Jonas Sulzer's avatar Jonas Sulzer 馃帗
Browse files

馃摝 NEW: add Geometrie.java


Signed-off-by: Jonas Sulzer's avatarJonas Sulzer <jonas@violoncello.ch>
parent cdf0c5aa
package Ex_06;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Geometrie {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Triangle triangle = new Triangle();
for (int i = 0; i < 3; i++) {
System.out.println("Construction d'un nouveau point");
System.out.print("Veuillez entrer x : ");
double x = scanner.nextDouble();
System.out.print("Veuillez entrer y : ");
double y = scanner.nextDouble();
triangle.addPoint(x, y);
}
System.out.println("P茅rim猫tre : " + triangle.getPerimeter());
if (triangle.isIsocele()) {
System.out.println("Le triangle est isoc猫le");
}
}
}
class Triangle {
private ArrayList<Point> points = new ArrayList<Point>();
public void addPoint(double x, double y) {
points.add(new Point(x, y));
}
public double getPerimeter() {
double perimeter = 0.0;
for (int i = 0; i < 3; i++) {
double x1 = points.get(i).getX();
double y1 = points.get(i).getY();
double x2 = points.get((i < 2 ? i+1 : 0)).getX();
double y2 = points.get((i < 2 ? i+1 : 0)).getY();
perimeter += Math.sqrt(Math.abs((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)));
}
return perimeter;
}
public boolean isIsocele() {
double[] distances = new double[3];
for (int i = 0; i < 3; i++) {
double x1 = points.get(i).getX();
double y1 = points.get(i).getY();
double x2 = points.get((i < 2 ? i+1 : 0)).getX();
double y2 = points.get((i < 2 ? i+1 : 0)).getY();
distances[i] = Math.sqrt(Math.abs((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)));
}
if (distances[0] == distances[1] && distances[1] == distances[2]) {
return true;
}
return false;
}
}
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment