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

1. ## New to programming

hi, I am very new to programming and i need help on on this question
"a table have 4 legs, a stool have 3 leg, given 300 legs, find number of tables and stools."

it is to be done with public static void main(String[] args){
I started recently with the class so we only know so much to methods,Conditionals and recursion.

I have done quadratic equations with java. so i may know a thing or two on variables and math methods.
the thing is this question has an equation with two variables. I am to list down the number of stools and tables with System.out.print

so, can anyone tell me what i need to know or just how to start out on this question?

I apologize for my english edit:i posted on the wrong section , you can still help me out here  Reply With Quote

3. ## Re: New to programming

Well you don't have a quadratic equation there, what you have is 4t + 3s = 300 - it's the same kind of equation (y = mx + c) that's used to specify a straight line on a graph. You can't draw a line because you have to find an integer solution (half a table is no good to anyone). You would expect a set of pairs of t and s which would each satisfy the equation. If you had to solve this equation by hand, how would you start? I think once you use a pencil and paper on this problem, you will quickly think of something to code.  Reply With Quote

4. ## Re: New to programming

Seems to me that there are multiple solutions to this.

The pen and paper version:

1) 96 stools and 3 tables( (96*3)+(3*4) = 300)
2) 92 stools and 6 tables( (92*3)+(6*4) = 300)
3) 88 stools and 9 tables
...ect.

I find it interesting that the number of tables with 4 legs will be multiple of 3, and the number of stools with 3 legs will be a multiple of 4.

Start here:
```public class TableStool
{
public static void main(String[] args)
{
//continue writing code
}
}```

I never got as far as quadratics in math, I just looked it up on wiki looks like Greek to me.

However I can think of a way to do it without any complex math.

Start with 3 tables and less than 4 stools will not satisfy the equation:

```int legsLeft, stools, tables = 3;
do
{
//more code goes here
}while (stools > 3);```

inside this loop, starting with the 3 tables, subtract that number of legs from 300:
then divide the result by the number of legs in stools:

```legsLeft = 300 - (4 * tables);
stools = legsLeft / 3;

//OR skip the legsLeft variable:

stools = (300 - (4 * tables))/3;```

Then setup a println to display the number of tables and stools.

After the println, add 3 more tables, it has to be a multiple of 3:

`tables += 3;`

And then let the do...while do the rest of the work. This might be too simple to get you that A+ however.  Reply With Quote

5. ## Re: New to programming

I did that at first. I know it is a linear equation,the last exercise my teacher gave me was a quadratic equation.
anyway,I would turn the equation in terms of t or s.
but I am not sure how to assign 2 variables as they work in pairs.  Reply With Quote

6. ## Re: New to programming

I am asked to list all the possible solutions.  Reply With Quote

7. ## Re: New to programming

```public class tablestools {

public static void main(String[] args)
{
int s,t;
//100 = 3s + 4t;

s = (100-4t)/3;
t = (100-3s)/4;

}

}```
This is all I could think of right now.I know its garbage and super basic.
-I am thinking of using a Math.random on one of the variables to generate the other but it may cause a infinite loop.(not sure how to counter that)
-or by creating a class similar to "countdown" but i am not really familiar with that.

fyi,I am using eclipse.
I am sorry for the lack of info  Reply With Quote

8. ## Re: New to programming

You have two variables and only one equation. The way to solve this problem is to 'fix' one of the variables and find a value that works for the other. I guess you can derive a minimum and a maximum value for either stools or tables? Then you have to try each value in that range to see if there's a value for the other variable that satisfies the equation (and the integer constraint). To work through a range of values, you need a loop...

I think you should post some code now. There are enough clues on this page for you to make a start.

public class tableStool
Class name with a lower case initial letter?
Java Code Conventions  Reply With Quote

9. ## Re: New to programming Originally Posted by Sean4u Class name with a lower case initial letter?
Fixed, lol, I was trying not to spoon feed too much. My book has errors in the examples, a headache, but it makes you figure it out on your own. Originally Posted by Cranky This is all I could think of right now.I know its garbage and super basic.
Put together the example I showed you. Put the do..while inside of the main, and put the rest of the code inside the do..while.

Sorry if you saw it before I finished editing it. There is a complete example there now.  Reply With Quote

10. ## The Following User Says Thank You to Spidey1980 For This Useful Post:

Cranky (August 22nd, 2011)