# Help With Putting This Math Problem Into Code

• April 20th, 2012, 10:55 PM
Pettsa
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
• April 21st, 2012, 05:36 AM
Norm
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.
• April 21st, 2012, 09:45 AM
Pettsa
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.
• April 21st, 2012, 09:57 AM
Norm
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.
• April 21st, 2012, 10:39 AM
Pettsa
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.
• April 21st, 2012, 10:43 AM
Norm
Re: Help With Putting This Math Problem Into Code
What do statement? That was a typo. corrected now
Quote:

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?
• April 21st, 2012, 11:00 AM
Pettsa
Re: Help With Putting This Math Problem Into Code
Would it be x = 5000 - 23/3? I am not sure how to do this.
• April 21st, 2012, 11:10 AM
Norm
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
• April 21st, 2012, 11:22 AM
Pettsa
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.
• April 21st, 2012, 11:26 AM
Norm
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);
• April 21st, 2012, 11:31 AM
Pettsa
Re: Help With Putting This Math Problem Into Code
All right I declared x, now how would I input the calculations into code?
• April 21st, 2012, 11:53 AM
Norm
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.
• April 21st, 2012, 12:39 PM
Pettsa
Re: Help With Putting This Math Problem Into Code
Did you attach the code because I don't see it.
• April 21st, 2012, 01:34 PM
Norm
Re: Help With Putting This Math Problem Into Code
Its the bold statement in post#10
• April 21st, 2012, 01:39 PM
Pettsa
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);

}

}
• April 21st, 2012, 01:44 PM
Norm
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.
• April 21st, 2012, 08:30 PM
Pettsa
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?
• April 21st, 2012, 08:33 PM
Norm
Re: Help With Putting This Math Problem Into Code
Quote:

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.
• April 21st, 2012, 09:41 PM
Pettsa
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 + ".");
• April 22nd, 2012, 06:18 AM
Norm
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
• April 22nd, 2012, 10:20 AM
Pettsa
Re: Help With Putting This Math Problem Into Code
All right, thank you for your help.
• April 22nd, 2012, 10:26 AM
Norm
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?
• May 28th, 2012, 03:43 PM
copeg
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.
• May 28th, 2012, 06:48 PM
KevinWorkman
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.