Results 1 to 5 of 5
  1. #1
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default Please explain how this bit of code works.

    This is a code I wrote for Fundamental Java as an assignment. I wanted to round up the quotient of d and c and I found the answer on this website. But my question is how does the code that is colored red work? Thank you in advanced,

    Allspark

    //Allspark

    public class Lab02_Ex2
    {

    final static int A = 7, B = 8;
    final static double C = 4.23, D = 5.89;

    public static void main(String [] args)
    {
    double dbl = D / C;
    int x = (int)(dbl * 100.0); // Scale it
    double dbl2 = ((double)x) / 100;

    System.out.println( A + " + " + B + " = " + (A + B));
    System.out.print( A + " * " + B + " = " + (A * B) + "\n");
    System.out.println( B + " - " + A + " = " + (B - A));
    System.out.println( D + " / " + C + " = " + dbl2);
    System.exit(0);
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    double dbl = D / C; // divides D by C
    int x = (int)(dbl * 100.0); // multiplies dbl by 100 and casts it to int (dropping fractional part
    double dbl2 = ((double)x) / 100; // casts x to double and divides result by 100

    If you print the value of dbl you'll see the results of the computations.

  3. #3
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    By "dropping fractional part" you mean that it gets rid of everything after the decimal? For example D / C = 1.394, then you multiply by 100 and it becomes 139.4. but because it is cast into integer, it does not allow decimals therefore the solution is 139.0. then you divide by 100 to revert to the original scale. Did I understand this correctly?
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Yes, you got it.

  5. #5
    Allspark is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    Thanks Norm.
    - Winners compare their achievements with their goals, while losers compare their achievements with those of other people. -

Similar Threads

  1. Please explain these 2 lines of code to me..
    By murphaph in forum New To Java
    Replies: 10
    Last Post: 01-19-2010, 02:11 PM
  2. Replies: 1
    Last Post: 08-07-2009, 09:28 AM
  3. Replies: 2
    Last Post: 11-18-2008, 10:44 PM
  4. how does this code work...explain me the execution please...
    By vital parsley in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 04:50 AM
  5. need to explain this code
    By reached in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 10:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •