Program ma za zadanie obliczenia obwodu trojkata z wprowadzonych dlugosci bokow i ewentualnej obslugi wyjatku w przypadku gdy nie da sie utworzyc trojkata.
Moje pytanie brzmi, czy dalo by sie kod jakoś sensownie zoptymalizowac?
import java.util.Scanner;
public class TriangleApp {
public static void main(String[] args) throws CannotBuildTriangleException {
Scanner scanner = new Scanner(System.in);
System.out.println("Podaj pierwszy bok: ");
int first = scanner.nextInt();
System.out.println("Podaj drugi bok: ");
int second = scanner.nextInt();
System.out.println("Podaj trzeci bok: ");
int third = scanner.nextInt();
cannot(first,second,third);
TriangleCalculation calculator = new TriangleCalculation();
calculator.calculateTriangleCircuit(first, second, third);
}
private static void cannot(int first, int second, int third) throws CannotBuildTriangleException {
if (first >= second + third || second >= first + third || third >= first + second){
throw new CannotBuildTriangleException(first,second,third);
}
}
}
/////////////////////
public class TriangleCalculation {
public void calculateTriangleCircuit(int first, int second, int third) {
double wynik = first + second + third;
System.out.println("Obwod trojkata wynosi: " + wynik);
}
}
///////////////////////////////
public class CannotBuildTriangleException extends Exception {
public CannotBuildTriangleException(int first, int second, int third) {
super("Cannot make triangle with " + first + ", " + second + ", " + third + " side lengths!");
}
}