Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Is it the same?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default 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");

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default 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");

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Is it the same?

    Did you try them both? Did you get different results?

  5. #5
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default 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.

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Is it the same?

    Quote Originally Posted by unleashed-my-freedom View Post
    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");