Beginner`s Boolean question

ZKR

Honorable
Mar 12, 2012
35
0
10,580
Hello.
I have to write a program to read 3 values to check if they can form a triangle (any side is not larger the the other 2 sides combined).
So I wote:

import java.util.Scanner;
public class Triangle
{
//-----------------------------------------------------------------------------------------------------------
// Calculates total length of sides of a triangle and determines triangle type. Prints an error if a triangle
// cannot be formed.
//-----------------------------------------------------------------------------------------------------------
public static void main (String []args)
{
Scanner scan = new Scanner(System.in);
double side1, side2, side3;

System.out.println ("Please enter 3 numbers representing the length of each side of the triangle to be formed");
side1 = scan.nextDouble();
side2 = scan.nextDouble();
side3 = scan.nextDouble();

if (side1 > side2 + side3) //
if (side2 > side1 + side3) // Determines whether a triangle can be formed by the 3 values
if (side3 > side1 + side2)

System.out.println ("temp messgae ...error");

else
System.out.println ("ok");


But it won`t print, how can I combine the IFs. I have to use those also to define the triangle type (quilateral, isosceles or scalene).

Thanks for any help.
 

randomizer

Distinguished
Well I think the logic of your program is obscured by your lack of formatting. I'm not going to give you a solution, but I will show you more clearly what your if-else blocks are doing.

Code:
if (side1 > side2 + side3)
{
    if (side2 > side1 + side3)
    {
        if (side3 > side1 + side2)
        {
            System.out.println ("temp messgae ...error" );
        }
        else
        {
             System.out.println ("ok" );
        }
    }
}

At the moment, if any of the first two if statements fail, nothing will ever print.
 

ZKR

Honorable
Mar 12, 2012
35
0
10,580
I have eventually got to use that:

import java.util.Scanner;
public class Triangle
{
//-----------------------------------------------------------------------------------------------------------
// Calculates total length of sides of a triangle and determines triangle type. Prints an error if a triangle
// cannot be formed.
//-----------------------------------------------------------------------------------------------------------
public static void main (String []args)
{
Scanner scan = new Scanner(System.in);
double side1, side2, side3, perimeter;

// Reads input data from the user to get side values, and based on those calculates potential perimeter.
System.out.println ("Please enter 3 numbers representing the length of each side of the triangle to be formed");
side1 = scan.nextDouble();
side2 = scan.nextDouble();
side3 = scan.nextDouble();
perimeter = side1 + side2 + side3;

// Determines whether a triangle can be formed by the 3 sides and prints an error message if it cannot.

if (side1 > side2 + side3 || side2 > side1 + side3 || side3 > side1 + side2)
System.out.println ("The given values would not form a triangle.");
else
{ //Then determines type of triangle and prints the perimeter and triangle type.
System.out.println ("Triangle`s perimeter is: " + perimeter);

if (side1 == side2 && side2 == side3)
System.out.println ("Sides form an equilateral triangle.");
else
if (side1 == side2 || side1 == side3 || side2 == side3)
System.out.println ("Sides form an isosceles triangle.");
else System.out.println ("Sides form a scalene triangle.");
}
}
}

I have finally included the first IFs in one statement with || and the else in { } to let it continue calculating only if a triangle can be formed.
It works perfectly. Do you think it`s fine in terms of code (it`s a university home assignment, already submitted it). [the tabs were lost when posting].
 

randomizer

Distinguished
EDIT: Oops, this became a wall of text...

FYI, the [ code ] and [ /code ] (without spaces inside the brackets) are what I used above to keep formatting. It works better with spaces than tabs for indentation.

As for the code, I would have combined the double variable declarations with the initialisations. You should declare variables as close as possible to where you use them (and at the same time if possible). ie.

Code:
System.out.println ("Please enter 3 numbers representing the length of each side of the triangle to be formed" );

double side1 = scan.nextDouble();
double side2 = scan.nextDouble();
double side3 = scan.nextDouble();
double perimeter = side1 + side2 + side3;

It's not important for assignments because you write them and then throw them away, but it's good programming practice nonetheless. The main reason for doing this is that it helps prevent "dead" code as the program goes through changes. If I declare a variable on line 323 and don't use it until, say, line 335, someone may later change the code around line 335 and remove the need for the variable but not remove the declaration. In that instance there's a small amount of wasted memory set aside for something that is never used.

Secondly, I'd have removed the comments. I'm guessing that your professor wants them there as it seems to be a professor thing to do, but they really serve little purpose in most cases as long as the code is good. For example, let's look at your first comment (excluding the class description), which covers the code above:

"Reads input data from the user to get side values, and based on those calculates potential perimeter."

The first line that prints to the console and the 4 variable assignments make it pretty obvious that it does exactly that (which is a good thing!), so the comment is redundant. I've seen worse though, such as something similar to this in production code related to insurance:

Code:
double excess; // the excess
Really?

Now the real problem with comments is threefold:

1. They can be flat out wrong from the start. I have seen comments which describe the opposite of what the code does. Not helpful at all...
2. They may have been correct for the original code, but often get left unchanged when the code evolves. Therefore they become a point of confusion rather than helpful.
3. If you need comments to describe what your code does, then you've written it badly and should do it again. A programmer with even a small amount of experience should be able to work it out within a reasonable amount of time. Don't write magic code that somehow works but which everybody is afraid to touch for fear of breaking it.

The only time comments are really helpful is if it's difficult to express in code why you've written it the way you have, even to an experienced programmer. This might happen if your program interacts with unreliable third party services that do quirky things which require special error handling in unexpected places. Even in that case, how error handling works should still be clear.
 

ZKR

Honorable
Mar 12, 2012
35
0
10,580
Thanks, I`ll use the variable declaration advice.
They constantly scare you that you should write comments, so although I know it`s belaboring the obvious, I did that because of those threats, so they won`t cut the points off the grade.
 

randomizer

Distinguished
As I said, it's a professor thing. I'm sure that many of them demand it because they can't read the code themselves :lol: Obviously you don't want to sacrifice marks, but keep in mind that you should still be aiming for code that explains itself (unless you're not doing programming for a career, then it really doesn't matter) :)
 

Ijack

Distinguished
Just as an aside, what you say about wasted memory inside a loop is incorrect. If the variable is declared within the loop it will go out of scope for each iteration of the loop and its memory will be released. Otherwise it would cause a memory leak whether it was used or not.

I think there are good arguments for declaring variables in a block at the start of their scope. Use of a code analyzer, for example in Eclipse, will catch any unsed variables.
 

randomizer

Distinguished


You're right, I knew I'd brain fart on something when writing that much.



Some developers like to stick to simple text editors though :)

Come to think of it, a bigger issue with declaring variables in a wider scope is that it's just annoying for someone wanting to use the same identifier in that wider scope later on. There probably are good arguments for declaring variables early, but there are plenty of good arguments against it as well. It's more of a style issue than anything else, and if you happen to work for a firm they will have standards that you have to comply with anyway. I, for example, have to do pushups if I don't. :lol: