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 4 of 4

Thread: Total NOOB question

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Total NOOB question

    Hello all, I'm taking an intro to CS class based on Java. A question in the book gives the following declarations:
    init iResult, num1=25, num2=40
    double fResult, val1=17.0, val2=12.78

    and then asks: what result is stored in this assignment statement?

    fResult = (int) ((double) num1/num2);

    To me, this means that num1 is cast to a double and num2 will have to be a double as well to perform the division (25.0/40.0), answer .63. This is then cast to an integer, making it 1, and then turned into a double again since we're looking for fResult, giving a final answer of 1.0....is this correct?...if not, please point out my error.

    Thanks!

    -C


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Total NOOB question

    Did you type it in to a Java program and try running it? That's what I would do.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Total NOOB question

    So, I put it into a Java program and ran it and got 0.0, I guess all I was looking for is the "math rules" for Java. This answer tells me that it doesn't round decimals up (unless the program is set up to do so..?), so my original .63 would then be 0, then put to 0.0 since fResult is a double....is this correct?

    Thanks,
    -C

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Total NOOB question

    Absolutely right - Java, like a lot of programming languages *truncates* floating point values when it converts them to integer

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    coolidge (September 3rd, 2011)

Similar Threads

  1. Noob question: Why .class?
    By Shaybay92 in forum Java Theory & Questions
    Replies: 10
    Last Post: August 22nd, 2011, 04:56 AM
  2. I trying to calculate total price.
    By Muhanguzi in forum JDBC & Databases
    Replies: 6
    Last Post: June 15th, 2011, 03:35 PM
  3. Replies: 5
    Last Post: May 24th, 2011, 10:39 AM
  4. "The import ___ cannot be resolved" (stupid noob question)
    By RobG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2010, 03:09 PM
  5. noob question on abstract classes
    By joshred in forum Object Oriented Programming
    Replies: 1
    Last Post: September 15th, 2010, 06:27 PM