HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Triangle calculator: calculate if it's a valid triangle and what type

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
whattypetrianglecalculatevalidandcalculator

Problem

This is my first program and I wanted to know if there was any way to improve the design or cut back on unnecessary coding.

import java.util.Scanner;

public class Main {

    public static void main(String args[]) {

        int side1; int side2; int side3;
        int side1s; int side2s; int side3s; 
        Boolean Triangle;
        String TriangleType;
        String Q1 = "What is the first side of the Triangle?";
        String Q2 = "What is the second side of the Triangle?";
        String Q3 = "What is the third side of the Triangle?";

        Scanner input = new Scanner(System.in);
        System.out.println(Q1);
        side1 = input.nextInt();

        Scanner input2 = new Scanner(System.in);
        System.out.println(Q2);
        side2 = input2.nextInt();

        Scanner input3 = new Scanner(System.in);
        System.out.println(Q3);
        side3 = input3.nextInt();

        if(side1 + side2 > side3) {
            Triangle = true;
            System.out.println("The triangle is true!");

        } else {
            Triangle = false;
            System.out.println("The Triangle is False!");
            return;

        }

        if(Triangle == true) {
            side1s = side1 * side1;
            side2s = side2 * side2;
            side3s = side3 * side3;

            if(side1s + side2s == side3s) {
                TriangleType = "Right";

                System.out.println("The Triangle is..." + TriangleType);

            } else if (side1s + side2s > side3s) {
                TriangleType = "Acute";

                System.out.println("The Triangle is..." + TriangleType);

            } else if(side1s + side2s < side3s) {
                TriangleType = "Obtuse";

                System.out.println("The Triangle is..." + TriangleType);

            } else {
                TriangleType = null;

                System.out.println("The Triangle is..." + TriangleType);

            }

        }
    }

}

Solution

Bug

Your code assumes that the user will enter the longest side last! This is a bug. Id the user enters the values 10, 1, and 1, it will declare that it is a triangle because 10 + 1 > 1
Functions!

Where are your functions?

One of the best ways to document your code, and make sense of it, is to have well-named functions. Your function is main, and.... well, that does not do a good job of telling us what the function does.

What you need is a function called something like boolean isValidTriangle(int sideA, int sideB, int sideC), then make that function "work".

A valid triangle is one whose shorter sides add up to more than the longest side, right? The trick is finding the longest side.... In this case, recursion is a trick that can help....

boolean isValidTriangle(int sideA, int sideB, int sideC) {

    // recursion trick, make sure sideA is the longest side!

    if (sideA < sideB) {
        // call the function with sideB first
        return isValidTriangle(sideB, sideA, sideC)
    }

    if (sideA < sideC) {
        // call the function wwith sideC first
        return isValidTriange(sideC, sideB, sideA)
    }
    
    // OK, sideA is the longest, for sure!
    return sideA <= sideB + sideC

}


Similarly, you can do the same sort of thing to classify the triangle types.

Code Snippets

boolean isValidTriangle(int sideA, int sideB, int sideC) {

    // recursion trick, make sure sideA is the longest side!

    if (sideA < sideB) {
        // call the function with sideB first
        return isValidTriangle(sideB, sideA, sideC)
    }

    if (sideA < sideC) {
        // call the function wwith sideC first
        return isValidTriange(sideC, sideB, sideA)
    }
    
    // OK, sideA is the longest, for sure!
    return sideA <= sideB + sideC

}

Context

StackExchange Code Review Q#127000, answer score: 6

Revisions (0)

No revisions yet.