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

Thread: My answer comes out a +0.020 more than the answer in the book

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question My answer comes out a +0.020 more than the answer in the book

    It's a very small difference but unsettling, i checked the code and both are practically the same (intro to java (10th))

    The question is to display the values next to each other as you can see in the first s.o.p the feet to meter | meters to feet.

    public class Exercise_06_09 {
      public static void main(String[] args) {
     
        System.out.println("Feet    Meters      |      Meters    Feet");
     
        for(double i = 1.0, j = 20.0; i < 11; i++, j+=5){ //i = feet and j = meters
     
          System.out.printf("%-6.1f", i);                //feet 1.0 to 10.0
          System.out.printf("%-15.3f|", footToMeter(i));      //Conversion
          System.out.printf("     %-15.1f", j);        //Meters 20.0 to 65.0
          System.out.printf("%7.3f\n", meterToFoot(j));     //Conversion
     
        }
      }
     
      public static double footToMeter(double foot){
      return 0.305 * foot;
      }
     
      public static double meterToFoot(double meter){
      return 3.279 * meter;
      }
    }
    Last edited by UniverseCloud; July 18th, 2019 at 07:25 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My answer comes out a +0.020 more than the answer in the book

    Can you post the output that shows what you are talking about? Add some comments pointing to the problem.

    Have you done the calculation by hand? Can you show the steps you took and the results you got?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: My answer comes out a +0.020 more than the answer in the book

    Book answer:

    Feet  Meters | Meters Feet
    1.0    0.305 | 20.0 65.574
    2.0    0.610 | 25.0 81.967
    ...
    9.0    2.745 | 60.0 196.721
    10.0  3.050 | 65.0 213.115

    Actual output (The difference is in the 4th column (feet)):

    Feet    Meters      |      Meters    Feet
    1.0   0.31           |     20.0            65.580
    2.0   0.61           |     25.0            81.975
    3.0   0.92           |     30.0            98.370
    4.0   1.22           |     35.0           114.765
    5.0   1.53           |     40.0           131.160
    6.0   1.83           |     45.0           147.555
    7.0   2.14           |     50.0           163.950
    8.0   2.44           |     55.0           180.345
    9.0   2.75           |     60.0           196.740
    10.0  3.05           |     65.0           213.135

    The equations were already given.
    Meter = 0.305*foot
    Foot = 3.279*meter
    Last edited by UniverseCloud; July 18th, 2019 at 02:19 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My answer comes out a +0.020 more than the answer in the book

    What values do you get when you do the computations manually?

    Which values are different? Can you mark them? Like this: <<<<<<<<<<< .0003 too small
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: My answer comes out a +0.020 more than the answer in the book

    Sorry for late reply.

    Book output:
    Feet
     
    65.574    // << +0.006
    81.967   //  << +0.008
    ...
    196.721  // << +0.019
    213.115  // << +0.020
    It doesn't give me much to work with.

    It's true that the 4th column is rounded even though the return value is double, casting it won't do any difference.
    Last edited by UniverseCloud; July 18th, 2019 at 05:32 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My answer comes out a +0.020 more than the answer in the book

    Did you do the computation manually? Did you get the same results as the program or as the book?
    How do you know the book's results are correct?

    Is the factor (3.279) correct? What is the value of 213.115/65? Or 65.574/20?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2017
    Posts
    23
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: My answer comes out a +0.020 more than the answer in the book

    yah i did it manually and the same result from the program...

    everything seems to be in order.

    Honestly i just assumed so, when a publisher publishes his 10th book you'd automatically think its without errors.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My answer comes out a +0.020 more than the answer in the book

    Is the factor (3.279) correct? What is the value of 213.115/65? Or 65.574/20?
    Try one of those values instead of 3.279
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] need an answer
    By noura in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2017, 03:56 AM
  2. Answer me !
    By johnmecanse34 in forum Java Networking
    Replies: 2
    Last Post: November 20th, 2013, 06:12 AM
  3. "Help with answer to question from java 6 practice exam book!
    By StanTheMan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2013, 12:33 PM
  4. Can any One answer it !!!!!!!!!!!!!!!!!!!!!
    By Srinivas_Java_Developer in forum Java Servlet
    Replies: 1
    Last Post: October 23rd, 2013, 12:42 PM
  5. Replies: 3
    Last Post: March 9th, 2013, 07:22 PM