Просто пытаюсь выяснить, как закодировать недопустимый тип треугольника последнему методу без изменения его необходимых предварительно написанных методов.Методы Java - вызов и определение
Создать класс с именем MyTriangle, который содержит следующие три метода:
public static boolean isValid(double sidea, double sideb, double sidec)
public static double area(double sidea, double sideb, double sidec)
public static String triangleType(double a, double b, double c)
IsValid метод возвращает истину, если сумма два коротких сторон больше, чем самая длинная сторона. Длины трех сторон треугольника отправляются на этот метод, но вы НЕ можете предположить, что они отправляются в любом конкретном порядке.
Метод области возвращает область треугольника. Учитывая длины трех сторон треугольника, площадь треугольника может быть рассчитана с использованием формулы Херона (провести исследование).
Метод triangleType возвращает одну из следующих строк: «Equilateral», «Isosceles», «Scalene», «Invalid Triangle». Вы можете определить тип треугольника, если a, b и c представляют стороны в порядке возрастания, вы должны сначала определить, действительно ли треугольник. Если это верно, треугольник равносторонний, если a == c. Isosceles, если a == b или b == c. Скален иначе.
Определите основной метод внутри вашего класса, который вводит пользователя с трех сторон для треугольника в любом порядке. Затем основной метод должен отображать тип треугольника. Если треугольник действителен, вы также должны выводить область треугольника.
public static void main(String[] args)
{
// Declare all identifiers used in the main method
int sidea, sideb, sidec;
double area;
boolean check;
String type;
Scanner input;
input = new Scanner(System.in);
System.out.println("Enter three sides for a triangle in any order: ");
sidea = input.nextInt();
sideb = input.nextInt();
sidec = input.nextInt();
check = isValid(sidea, sideb, sidec);
double a = sidea;
double b = sideb;
double c = sidec;
type = triangleType(a, b, c);
if (check == true)
{
area = area(sidea, sideb, sidec);
/*display the triangle's type and the area of the triangle. */
System.out.print(type + ". " + area + ".");
} else
{
System.out.print(type + ".");
System.exit(0);
}
}
/*Returns true if the sum of the two shorter sides is greater than the
longest side. Will sort the sides into ascending order first.*/
public static boolean isValid(double sidea, double sideb, double sidec)
{
//Will return the boolean result.
return (sidea + sideb > sidec && sideb + sidec > sidea && sidea + sidec > sideb);
}
/*Returns the area of the triangle given the lengths of the three sides of
the triangle*/
public static double area(double sidea, double sideb, double sidec)
{
double s;
double area;
s = (sidea + sideb + sidec)/2;
area = Math.sqrt(s * (s - sidea) * (s - sideb) * (s - sidec));
return area;
}
/*returns one of the following strings: "Equilateral", "Isosceles", "Scalene",
"Invalid Triangle". You can determine the triangle's type if a, b, and c
represent the sides in ascending order, you must first determine if the triangle
is valid. If it is valid, the triangle is equilateral if a == c. Isosceles
if a == b or b == c. Scalene otherwise.*/
public static String triangleType(double a, double b, double c)
{
if (c <= b)
{
double placeHolder = b;
b = c;
c = placeHolder;
}// if sidec is less than or equal to sideb, swap the numbers
if (b <= a)
{
double placeHolder = a;
a = b;
b = placeHolder;
}// if sideb is less than or equal to sidea, swap the numbers
if (c <= b)
{
double placeHolder = b;
b = c;
c = placeHolder;
}// if sidec is less than or equal to sideb, swap the numbers
//sidea is now the smallest, sideb is the middle, and sidec is the largest
String type;
if (a == c)
{
type = "Equilateral";
} else if (a == b || b == c)
{
type = "Isosceles";
} else
{
type = "Scalene";
}
return type;
}
это домашнее задание? – Sikorski
Да, это домашнее задание – Nikki
Java не JavaScript, так как ветчина не хомяк. – Jesper