In: Computer Science
package edu.depaul.triangle;
import java.util.Scanner;
/**
* A class to classify a set of side lengths as one of the 3 types
* of triangle: equilateral, isosceles, or scalene.
* If classification is not possible it emits an error message
*/
public class Triangle {
/**
* Define as private so that it is not a valid
* choice.
*/
private Triangle() {}
public Triangle(String[] args) {
//
// TODO: keep this simple. Constructors should not do a lot of work
//
}
// TODO: Add methods to validate input, and classify the triangle (if possible) here
private static String[] getArgs(Scanner s) {
System.out.println("press Enter by itself to quit");
System.out.println("enter 3 integers separated by space.");
String args = s.nextLine();
return args.split(" ");
}
public static void main(String[] a) {
try (Scanner scanner = new Scanner(System.in)) {
String[] args = getArgs(scanner);
// Loop until the user enters an empty line
while(args[0].length() !=0) {
//
// TODO: create a new Triangle here and call it
//
args = getArgs(scanner);
}
System.out.println("Done");
}
}
Write a Java program to determine types of triangles. The program reads 3 values from the standard input. The values represent the lengths of the sides of a triangle. The program prints a message to the standard output that indicates whether the triangle represented by the input is • an equilateral (all 3 sides are equal), or • an isosceles (exactly 2 of the 3 sides are equal), or • a scalene (all 3 sides are of different lengths) Expected behavior: a. The user enters 3 values at a prompt and presses return b. The values must be converted to integers. If they cannot be converted, the system displays an error. c. The valid values for these integers are values from 1 to and including 300. Any other integers should cause an error to be shown to the user. d. The values are delimited with spaces e. The system evaluates the results, shows either a triangle type or an error, then prompts for input again. f. When the user enters a blank line followed by return, the program ends. g. An error is shown whenever the user’s input cannot be interpreted as a triangle or when the handling of the input results in exception.
//program to classify the triangle based on entered side lengths with exception handling
import java.io.*;
import java.util.Scanner;
public class Triangle {
private Triangle() {}
public Triangle(String[] args) {
}
//function to classify the type of triangle
private static void classify(int a,int b,int c){
if(a==b && b==c)
System.out.println("Equilateral\n");
else if(a==b || b==c || a==c)
System.out.println("Isosceles\n");
else
System.out.println("Scalene\n");
}
//get the user input and split on space
private static String[] getArgs(Scanner s) {
System.out.println("press Enter by
itself to quit");
System.out.println("enter 3
integers separated by space.");
String args = s.nextLine();
return args.split(" ");
}
//main function (program control starts from here)
public static void main(String[] arg)
{
try (Scanner scanner = new
Scanner(System.in))
{
String[] args =
getArgs(scanner);
// Loop until
the user enters an empty line
int
num1=0,num2=0,num3=0;
while(args[0].length() !=0)
{
//using try catch to handle exception on parseInt
try
{
num1 =
Integer.parseInt(args[0]);
num2 =
Integer.parseInt(args[1]);
num3 =
Integer.parseInt(args[2]);
if(num1>0 &&
num1<=300 && num2>0 && num2<=300
&& num3>0 && num3<=300)
{
//
System.out.println(num1+"---"+num2+"---"+num3+"---");
classify(num1,num2,num3); //calling classification
function
}
else
{
System.out.println("Enter value between 1 and 300");
}
}
catch (NumberFormatException nfe)
{
//
nfe.printStackTrace();
System.out.println("ERROR!! Enter correct arguments(in
digits only)");
}
args = getArgs(scanner);
}
System.out.println("Done");
}
}
}