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

Thread: Help With Putting This Math Problem Into Code

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Putting This Math Problem Into Code

    Hello,
    I need help to create the code for this math problem, I have the answer with me! Please help. I am just learning to create code. Thanks.

    Two girls agree to go on a road trip together. They travel (x + 5)km on the first day. On the second day they travel 2km more than half of the distance they travelled on the first day. On the third day they drove 3 times as far as they did on the second day. If they drove 5000km total, find the value of x.

    Answer:

    The distance they drove on the first day: (x+5)

    The distance they drove on the second day: 2 + (x+5)/2

    The distance they drove on the third day: 3 [2 + (x+5)/2]

    The total distance they travelled:

    (day 1)+(day 2)+(day 3) = 5000

    (x+5) + 2 + (x+5)/2 + 3 [2 + (x+5)/2] = 5000

    (x + 5) + 2 + (x+5)/2 + 6 + 3(x+5)/2 = 5000

    (x + 5) + 8 + 4(x+5)/2 = 5000

    (x + 5) + 8 + 2(x + 5) = 5000

    3(x+5) = 5000-8

    3(x+5) = 4992

    x + 5 = 1664

    x = 1664 - 5

    x = 1659 km


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    What do you have so far? Please post the code and ask specific question about the problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    This is what I have:

    package fridaybonusquestion;


    /**
    *
    *
    */
    public class FridayBonusQuestion {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    //Declare variables
    double x = 0;
    double day1 = (x + 5);
    double day2 = 2 + (x + 5)/2;
    double day3 = 3 *(2 + (x + 5)/2);
    double totalkm = 5000;


    //Calculate for the variable x
    totalkm = (day1) + (day2) + day3;
    totalkm = (day1) + (day2) + 6 + 3 *(day2);
    totalkm = day1 + 8 + 4 * (day1/2);
    totalkm = day1 + 8 + 2* (day1);

    That's it. Not sure where to go from here.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    The variable totalkm will only have the value from the last assignment statement. The values stored into it by the other assignment statements will be gone.

    To assign a value to the variable: x you need something like this:
    x = <the formula to compute x from the given values>;

    You will need to use algebra on a piece of paper to get that formula.
    Last edited by Norm; April 21st, 2012 at 10:44 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    I know what the do statement is but I'm not sure how I would use it in this situation. I have the math done for it but I'm not sure how I would apply this into code.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    What do statement? That was a typo. corrected now
    I have the math done
    If you have the formula to compute the value of x, have you tried coding it, compiling it and executing it? What happens?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    Would it be x = 5000 - 23/3? I am not sure how to do this.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    That looks like a very simple statement to enter in a program. Have you tried it?
    One problem will be with integer division: 23/3 = 7 no decimal places. 23/3.0 will give decimal plaxes
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    Well actually with my class, this problem right now is a little more advanced than we have done because it is a bonus. I mean it sounds simple to do but I am not sure now to do it. I understand what you mean its just I don't know how to set it up.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    Type it into the program just as you wrote it with the correction:
    double x = 5000 - 23/3.0;
    You have shown that you know how to enter an assignment statement here:
    double day3 = 3 *(2 + (x + 5)/2);
    Last edited by Norm; April 21st, 2012 at 11:54 AM. Reason: bolded statement
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    All right I declared x, now how would I input the calculations into code?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    I gave you the whole thing in my last post.

    I did not check your algebra, but it does not look like the right formula.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    Did you attach the code because I don't see it.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    Its the bold statement in post#10
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    All right I included it in x. Now where do i go from this code???

    package fridaybonusquestion;

    public class FridayBonusQuestion {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    // TODO code application logic here
    //Declare variables
    double x = 5000 - 23/3.0;
    double day1 = (x + 5);
    double day2 = 2 + (x + 5)/2;
    double day3 = 3 *(2 + (x + 5)/2);
    double totalkm = 5000;


    //Calculate for the variable x
    totalkm = (day1) + (day2) + day3;
    totalkm = (day1) + (day2) + 6 + 3 *(day2);
    totalkm = day1 + 8 + 4 * (day1/2);
    totalkm = day1 + 8 + 2* (day1);



    }





    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    You only need to assign the value to x. The rest of the code doesn't do anything.
    What more do you need besides the value of x?

    Check you algebra. The formula for calculating x doesn't look right. Print it out and compare its value to one computed by hand.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    I tried to do it but it wouldn't print it out. Um, can you think of code that might work? Please?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    I tried to do it but it wouldn't print it out
    How are you trying to print it? System.out.println("x=" + x);
    Please post the code with the problem.
    Last edited by Norm; April 21st, 2012 at 09:03 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    Well, I tried it again but I am outputting: "Therefore, x equals: 4992.333333333333." so I am close.

    Here is the problem.

    Two girls agree to go on a road trip together. They travel (x + 5)km on the first day. On the second day they travel 2km more than half of the distance they travelled on the first day. On the third day they drove 3 times as far as they did on the second day. If they drove 5000km total, find the value of x.

    Here is the code:

    package fridaybonusquestion;


    /**
    *
    *
    */
    public class FridayBonusQuestion {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    //Declare variables
    double x = 5000 - 23/3.0;
    double day1 = (x + 5);
    double day2 = 2 + (x + 5)/2;
    double day3 = 3 *(2 + (x + 5)/2);
    double totalkm = 5000;
    double newtotalkm = 4992;
    double finaltotalkm = 1664;



    //Calculate for the variable x
    totalkm = (day1) + (day2) + day3;
    totalkm = (day1) + (day2) + 6 + 3 *(day2);
    totalkm = day1 + 8 + 4 * (day1/2);
    totalkm = day1 + 8 + 2* (day1);
    newtotalkm = 3 * (day1);
    finaltotalkm = day1 - 5;

    System.out.println("Therefore, x equals: " + x + ".");

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    There is only one assignment statement that needs to be in the program: the one that sets the value of x. The rest of the assignment statements(there are about 12 of them) do nothing useful and can be removed.
    The program should set the value of x and then print it. Nothing more.

    As I said before you need to check your algebra. The first post says the answer is:
    x = 1659 km
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Putting This Math Problem Into Code

    All right, thank you for your help.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With Putting This Math Problem Into Code

    This assignment makes no sense. This program does less than you can do with a calculator.
    Are you sure you understood what the program is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help With Putting This Math Problem Into Code

    @Pettsa, please stop reporting posts that contain nothing to report. Reporting posts is to notify of someone breaking forum rules, and abusing it is in and of itself breaking forum rules. Do it again and I will have to ban you.

  24. #24
    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: Help With Putting This Math Problem Into Code

    Pettsa, do not report your own post in order to get moderator attention. That just creates more work for us and makes you seem impatient and rude, which actually decrease your chances of getting help. Consider this a warning.
    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!

Similar Threads

  1. Random math problem generator?
    By CamCompetes in forum Member Introductions
    Replies: 1
    Last Post: March 9th, 2012, 02:42 PM
  2. Math program problem.
    By dylanka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2011, 03:44 PM
  3. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  4. Math Problem
    By kidsforsale in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 14th, 2010, 04:09 PM
  5. switch and math problem issues
    By emartino in forum Java Theory & Questions
    Replies: 1
    Last Post: January 19th, 2010, 05:34 PM