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

# Thread: Help With Putting This Math Problem Into Code

1. ## 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.

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  Reply With Quote

3. ## 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.  Reply With Quote

4. ## 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.  Reply With Quote

5. ## 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.  Reply With Quote

6. ## 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.  Reply With Quote

7. ## 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?  Reply With Quote

8. ## Re: Help With Putting This Math Problem Into Code

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

9. ## 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  Reply With Quote

10. ## 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.  Reply With Quote

11. ## 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);  Reply With Quote

12. ## Re: Help With Putting This Math Problem Into Code

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

13. ## 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.  Reply With Quote

14. ## Re: Help With Putting This Math Problem Into Code

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

15. ## Re: Help With Putting This Math Problem Into Code

Its the bold statement in post#10  Reply With Quote

16. ## 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);

}

}  Reply With Quote

17. ## 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.  Reply With Quote

18. ## 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?  Reply With Quote

19. ## 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.  Reply With Quote

20. ## 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 + ".");  Reply With Quote

21. ## 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  Reply With Quote

22. ## Re: Help With Putting This Math Problem Into Code

All right, thank you for your help.  Reply With Quote

23. ## 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?  Reply With Quote

24. ## 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.  Reply With Quote

25. ## 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.  Reply With Quote