116k views
0 votes
Write a class called Triangle that can be used to represent a triangle. It should include the following methods that return boolean values indicating if the particular property holds: a. isRight (a right triangle) b. isScalene (no two sides are the same length) c. isIsosceles (exactly two sides are the same length) d. isEquilateral (all three sides are the same length).

User Skytz
by
6.3k points

1 Answer

5 votes

Answer:

Step-by-step explanation:

import java.io.*;

class Triangle

{

private double side1, side2, side3; // the length of the sides of

// the triangle.

//---------------------------------------------------------------

// constructor

//

// input : the length of the three sides of the triangle.

//---------------------------------------------------------------

public Triangle(double side1, double side2, double side3)

{

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

}

//---------------------------------------------------------------

// isRight

//

// returns : true if and only if this triangle is a right triangle.

//---------------------------------------------------------------

boolean isRight()

(square2 == square1 + square3)

// isValid

// returns : true if and only if this triangle is a valid triangle.

boolean isValid()

(side1 + side3 < side2)

// isEquilateral

//

// returns : true if and only if all three sides of this triangle

// are of the same length.

boolean isEquilateral()

{

if (side1 == side2 && side2 == side3)

return true;

else

return false;

}

// isIsosceles

//

// returns : true if and only if exactly two sides of this triangle

// has the same length.

boolean isIsosceles()

(side2 == side3 && side1 != side3))

return true;

else

return false;

// isIsosceles

// returns : true if and only if exactly no two sides of this

// triangle has the same length.

boolean isScalene()

side2 == side3

}

//-------------------------------------------------------------------

// class Application

//

// This class is the main class of this application. It prompts

// the user for input to construct a triangle, then prints out

// the special properties of the triangle.

//-------------------------------------------------------------------

public class Application

{

//---------------------------------------------------------------

// getInput

//

// input : stdin - BufferedReader to read input from

// msg - message to prompt the user with

// returns : a double value input by user, guranteed to be

// greater than zero.

//---------------------------------------------------------------

private static double getInput(BufferedReader stdin, String msg)

throws IOException

{

System.out.print(msg);

double input = Double.valueOf(stdin.readLine()).doubleValue();

while (input <= 0) {

System.out.println("ERROR : length of the side of triangle must " +

"be a positive number.");

System.out.print(msg);

input = Double.valueOf(stdin.readLine()).doubleValue();

}

return input;

}

//---------------------------------------------------------------

// printProperties

//

// input : triangle - a Triangle object

// print out the properties of this triangle.

//---------------------------------------------------------------

private static void printProperties(Triangle triangle)

{

// We first check if this is a valid triangle. If not

// we simply returns.

if (!triangle.isValid()) {

System.out.println("This is not a valid triangle.");

return;

}

// Check for right/equilateral/isosceles/scalene triangles

// Note that a triangle can be both right triangle and isosceles

// or both right triangle and scalene.

if (triangle.isRight())

System.out.println("This is a right triangle.");

if (triangle.isEquilateral())

System.out.println("This is an equilateral triangle.");

else if (triangle.isIsosceles())

System.out.println("This is an isosceles triangle.");

else

// we do not need to call isScalene here because a triangle

// is either equilateral/isosceles or scalene.

System.out.println("This is an scalene triangle.");

}

// main

// Get the length of the sides of a triangle from user, then

// print out the properties of the triangle.

public static void main(String args[]) throws IOException

{

BufferedReader stdin = new BufferedReader

(new InputStreamReader(System.in));

double side1 = getInput(stdin,

"What is the length of the first side of your triangle? ");

double side2 = getInput(stdin,

"What is the length of the second side of your triangle? ");

double side3 = getInput(stdin,

"What is the length of the third side of your triangle? ");

System.out.print("Pondering...\\");

printProperties(new Triangle(side1, side2, side3));

}

}

User Zorina
by
6.5k points