# Is it the same?

• July 2nd, 2012, 09:42 AM
unleashed-my-freedom
Is it the same?
I had tried this coding myself but I had a different answer from the solution given. May I know if it is the same?

//calculate the mass
long massOfTheBlock = length * width * height * density

System.out.println("Length of the block, in cm is " + length);
System.out.println("Width of the block, in cm is " + width);
System.out.println("Height of the block, in cm is " + height);
System.out.println("The mass of the block is" + massOfTheBlock "grams")

AND

System.out.println("Height of the block, in cm is " + height);
System.out.println("");
// Calculate and print the block's mass.
System.out.println("The mass of the block is "
+ (length * width * height * AL_DENSITY) + " grams");
• July 2nd, 2012, 09:48 AM
KevinWorkman
Re: Is it the same?
I'm really not sure what you're asking. You've posted some code snippets without any context. What are you asking us to help you with?
• July 2nd, 2012, 08:18 PM
unleashed-my-freedom
Re: Is it the same?
I'm trying to ask if it is all right for me to calculate the mass first like long massOfTheBlock = length * width * height * density after that I then print out like
System.out.println("The mass of the block is" + massOfTheBlock "grams")
OR
Do I calculate and print out at the same time like
System.out.println("The mass of the block is "
+ (length * width * height * AL_DENSITY) + " grams");
• July 2nd, 2012, 09:01 PM
copeg
Re: Is it the same?
Did you try them both? Did you get different results?
• July 8th, 2012, 07:37 PM
Re: Is it the same?
There are no real rules about how to program, we aren't counting syntax errors and the such. If you take a different approach and your program's output is correct and your program runs without errors, then your program is fine. Often, you'll learn, there are several different ways to code a program and none of them are any more "right" than the others.
Your program is fine as long as it outputs the correct density.
• July 9th, 2012, 03:32 AM
Voodoo
Re: Is it the same?
Quote:

Originally Posted by unleashed-my-freedom
I had tried this coding myself but I had a different answer from the solution given. May I know if it is the same?

//calculate the mass
long massOfTheBlock = length * width * height * density

System.out.println("Length of the block, in cm is " + length);
System.out.println("Width of the block, in cm is " + width);
System.out.println("Height of the block, in cm is " + height);
System.out.println("The mass of the block is" + massOfTheBlock "grams")

AND

System.out.println("Height of the block, in cm is " + height);
System.out.println("");
// Calculate and print the block's mass.
System.out.println("The mass of the block is "
+ (length * width * height * AL_DENSITY) + " grams");

If AL_DENSITY is equal to density then it's the same between

System.out.println("The mass of the block is" + massOfTheBlock + "grams")

and

System.out.println("The mass of the block is " + (length * width * height * AL_DENSITY) +" grams");